Skip to content

Commit

Permalink
replace old AdminApi::getLinks system with new service definition and…
Browse files Browse the repository at this point in the history
… DI. refs #2500 and refs #2494

will require documentation.

see companion commit in Pages module (FC-features branch) for implementation.

/cc @cmfcmf
  • Loading branch information
craigh committed Jul 19, 2015
1 parent 9e9822a commit 24c50a8
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 10 deletions.
19 changes: 19 additions & 0 deletions src/lib/EventHandlers/SystemListeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* information regarding copyright and licensing.
*/

use Zikula\Core\Event\GenericEvent;

/**
* Event handler to override templates.
*/
Expand All @@ -35,6 +37,7 @@ protected function setupHandlerDefinitions()
$this->addHandlerDefinition('core.preinit', 'initDB');
$this->addHandlerDefinition('core.init', 'setupCsfrProtection');
$this->addHandlerDefinition('theme.init', 'clickJackProtection');
$this->addHandlerDefinition('zikula.link_collector', 'processHookListeners');
}

/**
Expand Down Expand Up @@ -291,4 +294,20 @@ public function clickJackProtection(Zikula_Event $event)
//header("X-Content-Security-Policy: frame-ancestors 'self'");
header('X-XSS-Protection: 1');
}

/**
* Respond to zikula.link_collector events.
*
* Create a BC Layer for the zikula.link_collector event to gather Hook-related links.
*
* @param GenericEvent $event
*/
public function processHookListeners(GenericEvent $event)
{
$event->setArgument('modname', $event->getSubject());
$event->setArgument('modfunc', array(1 => 'getLinks'));
$event->setArgument('api', true);
$this->addHooksLink($event);
$this->addServiceLink($event);
}
}
3 changes: 3 additions & 0 deletions src/lib/Zikula/Bundle/CoreBundle/CoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Zikula\Bundle\CoreBundle\DependencyInjection\Compiler\DoctrinePass;
use Zikula\Bundle\CoreBundle\DependencyInjection\Compiler\RegisterCoreListenersPass;
use Zikula\Bundle\CoreBundle\DependencyInjection\Compiler\LinkContainerPass;

class CoreBundle extends Bundle
{
Expand All @@ -18,6 +19,8 @@ public function build(ContainerBuilder $container)

$container->addCompilerPass(new RegisterCoreListenersPass(), PassConfig::TYPE_AFTER_REMOVING);

$container->addCompilerPass(new LinkContainerPass());

// todo - see if we can do this only on module install/upgrade - drak
$container->addCompilerPass(new ValidateServiceDefinitionsPass(), PassConfig::TYPE_AFTER_REMOVING);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Zikula\Bundle\CoreBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class LinkContainerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('zikula.link_container_collector')) {
return;
}

$definition = $container->getDefinition('zikula.link_container_collector');

foreach ($container->findTaggedServiceIds('zikula.link_container') as $id => $linkContainer) {
$definition->addMethodCall('addContainer', array(new Reference($id)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<!-- Make controller resolving working with Zikula base classes. -->
<parameter key="controller_resolver.class">Zikula\Bundle\CoreBundle\Controller\ControllerResolver</parameter>
<parameter key="zikula.cache_clearer.class">Zikula\Bundle\CoreBundle\CacheClearer</parameter>
<parameter key="zikula.link_container_collector.class">Zikula\Core\LinkContainer\LinkContainerCollector</parameter>
</parameters>

<services>
Expand All @@ -25,5 +26,8 @@
<argument type="service" id="fos_js_routing.extractor" />
<argument>%jms_i18n_routing.locales%</argument>
</service>
<service id="zikula.link_container_collector" class="%zikula.link_container_collector.class%">
<argument type="service" id="event_dispatcher" />
</service>
</services>
</container>
54 changes: 54 additions & 0 deletions src/lib/Zikula/Core/LinkContainer/LinkContainerCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright Zikula Foundation 2015 - Zikula Application Framework
*
* This work is contributed to the Zikula Foundation under one or more
* Contributor Agreements and licensed to You under the following license:
*
* @license GNU/LGPLv3 (or at your option, any later version).
* @package Zikula
* @subpackage Response
*
* Please see the NOTICE file distributed with this source code for further
* information regarding copyright and licensing.
*/

namespace Zikula\Core\LinkContainer;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Zikula\Core\Event\GenericEvent;

class LinkContainerCollector
{
private $linkContainers;
private $eventDispatcher;

public function __construct(EventDispatcherInterface $dispatcher)
{
$this->eventDispatcher = $dispatcher;
$this->linkContainers = array();
}

public function addContainer(LinkContainerInterface $linkContainer)
{
$this->linkContainers[$linkContainer->getBundleName()] = $linkContainer;
}

public function getLinks($containerName, $type = LinkContainerInterface::TYPE_ADMIN)
{
$links = array();
if ($this->hasContainer($containerName)) {
$links = $this->linkContainers[$containerName]->getLinks($type);
}
// fire event here to add more links like hooks, moduleServices, etc
$event = new GenericEvent($containerName, array('type' => $type), $links);
$links = $this->eventDispatcher->dispatch(LinkContainerInterface::EVENT_NAME, $event)->getData();

return $links;
}

public function hasContainer($containerName)
{
return isset($this->linkContainers[$containerName]);
}
}
34 changes: 34 additions & 0 deletions src/lib/Zikula/Core/LinkContainer/LinkContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright Zikula Foundation 2015 - Zikula Application Framework
*
* This work is contributed to the Zikula Foundation under one or more
* Contributor Agreements and licensed to You under the following license:
*
* @license GNU/LGPLv3 (or at your option, any later version).
* @package Zikula
* @subpackage Response
*
* Please see the NOTICE file distributed with this source code for further
* information regarding copyright and licensing.
*/

namespace Zikula\Core\LinkContainer;

interface LinkContainerInterface
{
const EVENT_NAME = 'zikula.link_collector';
const TYPE_ADMIN = 'admin';
const TYPE_USER = 'user';

/**
* @return string
*/
public function getBundleName();

/**
* @param string $type
* @return array
*/
public function getLinks($type = self::TYPE_ADMIN);
}
7 changes: 4 additions & 3 deletions src/lib/legacy/viewplugins/function.adminpanelmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ function smarty_function_adminpanelmenu($params, Zikula_View $view)
: $router->generate($module['capabilities']['admin']['route']);
$moduleSelected = empty($moduleSelected) && strpos($view->getRequest()->getUri(), $module['url']) ? " class='Selected'" : "";
$htmlContent .= "<li{$moduleSelected}><a href=\"" . DataUtil::formatForDisplay($url) . "\"><img src=\"$img\" alt=\"\" style=\"height: 18px\" /> " . $module['displayname'] . "</a>";
try {

$links = $view->getContainer()->get('zikula.link_container_collector')->getLinks($module['name'], 'admin');
if (empty($links)) {
$links = (array)ModUtil::apiFunc($module['name'], 'admin', 'getLinks');
} catch (\Exception $e) {
$links = array();
}

if ((count($links) > 0) && ($links[0] != false)) {
// create second-level list from module adminLinks
$htmlContent .= '<ul class="text-left">';
Expand Down
17 changes: 11 additions & 6 deletions src/lib/legacy/viewplugins/function.modulelinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* last Class for the last element (optional)
* seperator Link seperator (optional)
* class CSS class (optional).
* returnAsArray return results as array, not as formatted html - MUST set assign
*
* @param array $params All attributes passed to this function from the template.
* @param Zikula_View $view Reference to the Zikula_View object.
Expand All @@ -44,6 +45,7 @@ function smarty_function_modulelinks($params, Zikula_View $view)
$menuItemClass = isset($params['itemclass']) ? $params['itemclass'] : '';
$menuItemFirst = isset($params['first']) ? $params['first'] : '';
$menuItemLast = isset($params['last']) ? $params['last'] : '';
$returnAsArray = isset($params['returnAsArray']) ? (boolean) $params['returnAsArray'] : false;

if (empty($menuLinks)) {
if (!isset($params['modname']) || !ModUtil::available($params['modname'])) {
Expand All @@ -59,17 +61,20 @@ function smarty_function_modulelinks($params, Zikula_View $view)

$params['type'] = isset($params['type']) ? $params['type'] : 'admin';

// get the links from the module API
$menuLinks = ModUtil::apiFunc($params['modname'], $params['type'], 'getLinks', $params);
// get the menu links
// try the Core-2.0 way first, then try the legacy way.
$menuLinks = $view->getContainer()->get('zikula.link_container_collector')->getLinks($params['modname'], $params['type']);
if (empty($menuLinks)) {
$menuLinks = ModUtil::apiFunc($params['modname'], $params['type'], 'getLinks', $params);
}
}

// return if there are no links to print
if (!$menuLinks) {
// return if there are no links to print or template has requested to returnAsArray
if ((!$menuLinks) || ($returnAsArray && isset($params['assign']))) {
if (isset($params['assign'])) {
$view->assign($params['assign'], $menuLinks);
} else {
return '';
}
return '';
}

$html = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
<a title="{$adminlink.menutexttitle|safetext}" href="{$adminlink.menutexturl|safetext}">{$adminlink.menutext|safetext}</a>

{assign var="modlinks" value=false}
{modapifunc modname=$adminlink.modname type='admin' func='getlinks' assign='modlinks'}
{*{modapifunc modname=$adminlink.modname type='admin' func='getlinks' assign='modlinks'}*}
{modulelinks modname=$adminlink.modname type='admin' assign='modlinks' returnAsArray=true}
{if $modlinks}
<div class="dropdown" style="display: inline">
<a class="caret" data-toggle="dropdown" href="#" title="{gt text='Functions'}"></a>
Expand Down

0 comments on commit 24c50a8

Please sign in to comment.