Skip to content

Commit

Permalink
configuration backend moved to configuration bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Jul 28, 2012
1 parent ec2e6a2 commit a51da3f
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 339 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
vendor/*
.DS_Store
.idea
.project
.buildpath
.eclipse
.settings
coverage
phpunit.xml
vendor
composer.lock
53 changes: 0 additions & 53 deletions CacheWarmer/MenuCacheWarmer.php

This file was deleted.

120 changes: 66 additions & 54 deletions Config/OptionMerger.php → Config/OptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @package MenuBundle
* @subpackage Config
*/

namespace Millwright\MenuBundle\Config;

use Symfony\Component\Routing\RouterInterface;
Expand All @@ -16,15 +15,18 @@
use JMS\SecurityExtraBundle\Annotation\Secure;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

use Millwright\ConfigurationBundle\Config\OptionBuilderBase;

use Millwright\MenuBundle\Annotation\Menu;
use Millwright\MenuBundle\Annotation\MenuDefault;

/**
* @author Stefan Zerkalica <zerkalica@gmail.com>
* @category Millwright
* @package MenuBundle
* @subpackage Config
*/
class OptionMerger implements OptionMergerInterface
class OptionBuilder extends OptionBuilderBase
{
/**
* @var RouterInterface
Expand All @@ -39,42 +41,42 @@ class OptionMerger implements OptionMergerInterface
public function __construct(
RouterInterface $router,
Reader $reader
)
{
$this->router = $router;
$this->reader = $reader;
) {
$this->router = $router;
$this->reader = $reader;
}

protected function getDefaultParams()
{
return array(
'uri' => null,
'label' => null,
'name' => null,
'attributes' => array(),
'linkAttributes' => array(),
'childrenAttributes' => array(),
'labelAttributes' => array(),
'display' => true,
'displayChildren' => true,
'secureParams' => array(),
'roles' => array(),
'route' => null,
'routeAbsolute' => false,
'uri' => null,
'label' => null,
'name' => null,
'attributes' => array(),
'linkAttributes' => array(),
'childrenAttributes' => array(),
'labelAttributes' => array(),
'display' => true,
'displayChildren' => true,
'secureParams' => array(),
'roles' => array(),
'route' => null,
'routeAbsolute' => false,
'routeAcceptedParameters' => array(),
'routeRequiredParameters' => array(),
'showNonAuthorized' => false,
'showAsText' => false,
'translateDomain' => null,
'translateParameters' => array(),
'type' => null,
'showNonAuthorized' => false,
'showAsText' => false,
'translateDomain' => null,
'translateParameters' => array(),
'type' => null,
);
}

/**
* Get action method by route name
*
* @param string $name route name
*
* @return array array(\ReflectionMethod, array())
*/
protected function getRouteInfo($name)
Expand All @@ -99,10 +101,10 @@ protected function getRouteInfo($name)
unset($defaults['_controller']);

$compiledRoute = $route->compile();
$tokens = $compiledRoute->getVariables();
$tokens = $compiledRoute->getVariables();

return array(
'method' => $method,
'method' => $method,
'routeAcceptedParameters' => array_flip(array_merge(array_keys($defaults), $tokens)),
'routeRequiredParameters' => array_merge(array_keys($route->getRequirements()), $tokens)
);
Expand All @@ -113,31 +115,36 @@ protected function getRouteInfo($name)
*
* @param array $options
* @param array $annotations
* @param array[\ReflectionParameter] $arguments
* @param array[\ReflectionParameter] $arguments
*
* @return array
*/
protected function getAnnotations(array $annotations,
array $arguments = array())
protected function getAnnotations(
array $annotations,
array $arguments = array()
)
{
$options = array();
foreach($annotations as $param) {
foreach ($annotations as $param) {
if ($param instanceof SecureParam) {
/** @var $param SecureParam */
$options['secureParams'][$param->name] = $this->annotationToArray($param);
/* @var $argument \ReflectionParameter */
$argument = $arguments[$param->name];
$class = $argument->getClass();
$argument = $arguments[$param->name];
$class = $argument->getClass();
if (!$class) {
throw new \InvalidArgumentException(sprintf('Secured action parameter has no class definition'));
}

$options['secureParams'][$param->name]['class'] = $class->getName();
} else if ($param instanceof Secure || $param instanceof Menu || $param instanceof MenuDefault) {
$options += $this->annotationToArray($param);
} else {
if ($param instanceof Secure || $param instanceof Menu || $param instanceof MenuDefault) {
$options += $this->annotationToArray($param);
}
}
}

foreach($annotations as $param) {
foreach ($annotations as $param) {
if ($param instanceof ParamConverter && isset($options['secureParams'][$param->getName()])) {
/** @var $param ParamConverter*/
$options['secureParams'][$param->getName()]['class'] = $param->getClass();
Expand All @@ -151,12 +158,13 @@ protected function getAnnotations(array $annotations,
* Convert annotation object to array, remove empty values
*
* @param Object $annotation
*
* @return array
*/
protected function annotationToArray($annotation)
{
$options = array();
foreach((array) $annotation as $key => $value) {
foreach ((array) $annotation as $key => $value) {
if ($value !== null) {
$options[$key] = $value;
}
Expand All @@ -174,27 +182,28 @@ protected function annotationToArray($annotation)
* 3. From @Menu, @Secure, @SecureParam annotations of class
* 4. Normalize empty params from array @see OptionMerger::getDefaultParams()
*
* @param array $options
* @param array $parameters
* @param array $options
* @param array $parameters
* @param string $name
*
* @return void
*/
protected function merge(array & $options, array & $parameters, $name)
{
foreach($options as $key => $value) {
if(empty($value)) {
foreach ($options as $key => $value) {
if (empty($value)) {
unset($options[$key]);
}
}

if(isset($parameters[$name])) {
if (isset($parameters[$name])) {
$options += $parameters[$name];
}

if($name) {
if ($name) {
$annotationsOptions = array();
$classAnnotations = array();
$arguments = array();
$classAnnotations = array();
$arguments = array();

if (empty($options['uri'])) {
$route = isset($options['route']) ? $options['route'] : $name;
Expand All @@ -211,14 +220,16 @@ protected function merge(array & $options, array & $parameters, $name)
$arguments[$argument->getName()] = $argument;
}

$annotations = $this->reader->getMethodAnnotations($method);
$annotations = $this->reader->getMethodAnnotations($method);
$annotationsOptions = $this->getAnnotations($annotations, $arguments);

$class = $method->getDeclaringClass();
$class = $method->getDeclaringClass();
$classAnnotations = $this->reader->getClassAnnotations($class);
}
}
$annotationsOptions = array_merge($annotationsOptions, $this->getAnnotations($classAnnotations, $arguments));
$annotationsOptions = array_merge(
$annotationsOptions, $this->getAnnotations($classAnnotations, $arguments)
);

$options += $annotationsOptions;
$options += $this->getDefaultParams();
Expand All @@ -230,26 +241,27 @@ protected function merge(array & $options, array & $parameters, $name)
$options += array(
'children' => array(),
);
foreach($options['children'] as $name => & $child) {
foreach ($options['children'] as $name => & $child) {
$this->merge($child, $parameters, $name);
}
}

/**
* {@inheritdoc}
* @see Millwright\MenuBundle\Config.OptionMergerInterface::normalize()
*/
public function normalize(array $menuOptions)
public function build()
{
foreach($menuOptions['items'] as & $item) {
foreach($item as $key => $value) {
if(empty($value)) {
$menuOptions = parent::build();

foreach ($menuOptions['items'] as & $item) {
foreach ($item as $key => $value) {
if (empty($value)) {
unset($item[$key]);
}
}
}

foreach($menuOptions['tree'] as $name => & $menu) {
foreach ($menuOptions['tree'] as $name => & $menu) {
$this->merge($menu, $menuOptions['items'], $name);
$menuOptions['items'][$name] = null;
}
Expand Down
15 changes: 0 additions & 15 deletions Config/OptionMergerInterface.php

This file was deleted.

16 changes: 5 additions & 11 deletions DependencyInjection/Compiler/MenuBuilderOptionsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Config\Definition\Processor;

use Millwright\ConfigurationBundle\ContainerUtil as Util;

use Millwright\MenuBundle\DependencyInjection\MenuConfiguration;
use Millwright\RadBundle\Util;

class MenuBuilderOptionsPass implements CompilerPassInterface
{
Expand All @@ -18,14 +20,7 @@ public function process(ContainerBuilder $container)
return;
}

$menuContainers = Util::getDefinitionsByTag('millwright_menu.menu_options', $container);

$config = array();
/** @var $definition Definition */
foreach ($menuContainers as $definition) {
$bundleConfig = $definition->getArgument(0);
$config = Util::merge($config, $bundleConfig);
}
$config = Util::collectConfiguration('millwright_menu.menu_options', $container);

//@todo place normalization here and remove from menu builder and ConfigCache
// why getRouteCollection falls here ?
Expand Down Expand Up @@ -60,7 +55,6 @@ public function process(ContainerBuilder $container)
unset($config['renderers']);
}

$container->getDefinition('millwright_menu.builder')
->replaceArgument(5, $config);
$container->getDefinition('millwright_menu.option.builder')->addMethodCall('setDefaults', array($config));
}
}
14 changes: 0 additions & 14 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,4 @@
*/
class Configuration extends MenuConfiguration
{
protected function setChildren($node)
{
$node
->scalarNode('generator_cache_class')
->defaultValue('%kernel.name%%kernel.environment%MenuTree')
->end()
->scalarNode('cache_dir')->defaultValue('%kernel.cache_dir%')->end()
->scalarNode('debug')->defaultValue('%kernel.debug%')->end()
;

parent::setChildren($node);

return $this;
}
}
Loading

0 comments on commit a51da3f

Please sign in to comment.