Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merged branch vicb/fmwk/config (PR #6852)

This PR was squashed before being merged into the master branch (closes #6852).

Commits
-------

fde7585 [DIC] Better handling of enableable configurations

Discussion
----------

[DIC] Better handling of enableable configurations

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no, this feature has not been released yet
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

My definition of bug fix might be discussable. The thing which I think is not discussable is that this PR fixes the semantic - and I think it is important for a "semantic configuration": before this PR, some nodes had `->canBeDisabled` for nodes that were actually disabled by default. Those nodes now have `->canBeEnabled` which sounds right.

**Edit: Jan 28, 2013** - history:

See [the related comments](symfony/symfony#6829 (comment)).

I think Symfony **must** get the configuration right as we can expect of lot of devs to use this as a template when writting their own configuration.

@schmittjoh could you please give me your feedback on [this change](https://github.com/symfony/symfony/pull/6852/files#L4R224) considering [the rationale](https://github.com/symfony/symfony/pull/6852/files#L3R7).

---------------------------------------------------------------------------

by stof at 2013-01-23T16:10:33Z

@vicb your links are broken as they are pointing to the PR creation page

---------------------------------------------------------------------------

by stof at 2013-01-23T16:10:55Z

and to create a TODO list, it has to be a list first

---------------------------------------------------------------------------

by vicb at 2013-01-23T16:31:10Z

@stof thanks for reporting the broken links, they are fixed /cc @schmittjoh

---------------------------------------------------------------------------

by vicb at 2013-01-23T16:31:50Z

@Tobion please submit a PR to my repo, I don't have much time to work on this. Thanks !

---------------------------------------------------------------------------

by vicb at 2013-01-25T15:14:47Z

@fabpot @schmittjoh I'd like your feedback on the latest commit, rationale is in the method phpDoc. It better matches what we do now and seem the most sensible thing to do.

edit: with this you can no more disable the node explicitly, I have to find a better solution

---------------------------------------------------------------------------

by schmittjoh at 2013-01-25T15:20:13Z

Looks good.

On Fri, Jan 25, 2013 at 4:15 PM, Victor Berchet <notifications@github.com>wrote:

> @fabpot <https://github.com/fabpot> @schmittjoh<https://github.com/schmittjoh>I'd like your feedback on the latest commit, rationale is in the method
> phpDoc. It better matches what we do now and seem the most sensible thing
> to do.
>
> —
> Reply to this email directly or view it on GitHub<symfony/symfony#6852 (comment)>.
>
>

---------------------------------------------------------------------------

by vicb at 2013-01-28T14:37:57Z

@fabpot I know I keep insisting on this one and I am sorry for that but I think this should be considered as a bug fix (see the PR header for details) and should be merged in 2.2. I think the Symfony core should be exemplary as it is used by many developers as a template when creating their own bundle. *This PR is no more a WIP and can be merged right now*.

In addition to fixing the enableable nodes, this PR contain new UTs and some fixes to the code / tests.

---------------------------------------------------------------------------

by fabpot at 2013-01-28T16:43:42Z

@vicb As explained in a comment, this is not a BC break as this feature does not exist in 2.1. So, I can make the change to the CHANGELOG if you want after merging, or I can let you make the change.

---------------------------------------------------------------------------

by vicb at 2013-01-28T16:46:33Z

I am going to change it right now !

---------------------------------------------------------------------------

by vicb at 2013-01-28T16:46:56Z

(and thanks for having checked this)

---------------------------------------------------------------------------

by vicb at 2013-01-28T16:54:37Z

@fabpot I have updated the changelog and the PR header.

I am not sure if the commits should be squashed or not. On one side the multiple commits can help understand the changes but on the other side that's a lot of small commits which could pollute history. I let you choose what to do.
  • Loading branch information...
commit d032758b8387a3415b6d8a9f4e64463db01a9827 2 parents 7a5d134 + ca5a57d
@fabpot fabpot authored
View
1  CHANGELOG.md
@@ -4,6 +4,7 @@ CHANGELOG
2.2.0
-----
+ * added Extension::isConfigEnabled() to ease working with enableable configurations
* added an Extension base class with sensible defaults to be used in conjunction
with the Config component.
* added PrependExtensionInterface (to be able to allow extensions to prepend
View
37 Extension/Extension.php
@@ -12,6 +12,8 @@
namespace Symfony\Component\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Container;
+use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
+use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Definition\Processor;
@@ -62,26 +64,19 @@ public function getNamespace()
*
* @return string The alias
*
- * @throws \BadMethodCallException When the extension name does not follow conventions
+ * @throws BadMethodCallException When the extension name does not follow conventions
*/
public function getAlias()
{
$className = get_class($this);
if (substr($className, -9) != 'Extension') {
- throw new \BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
+ throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
}
$classBaseName = substr(strrchr($className, '\\'), 1, -9);
return Container::underscore($classBaseName);
}
- final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
- {
- $processor = new Processor();
-
- return $processor->processConfiguration($configuration, $configs);
- }
-
/**
* {@inheritDoc}
*/
@@ -104,4 +99,28 @@ public function getConfiguration(array $config, ContainerBuilder $container)
return null;
}
+
+ final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
+ {
+ $processor = new Processor();
+
+ return $processor->processConfiguration($configuration, $configs);
+ }
+
+ /**
+ * @param ContainerBuilder $container
+ * @param array $config
+ *
+ * @return Boolean Whether the configuration is enabled
+ *
+ * @throws InvalidArgumentException When the config is not enableable
+ */
+ protected function isConfigEnabled(ContainerBuilder $container, array $config)
+ {
+ if (!array_key_exists('enabled', $config)) {
+ throw new InvalidArgumentException("The config array has no 'enabled' key.");
+ }
+
+ return (Boolean) $container->getParameterBag()->resolveValue($config['enabled']);
+ }
}
View
81 Tests/Extension/ExtensionTest.php
@@ -0,0 +1,81 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\DependencyInjection\Tests\Extension;
+
+class ExtensionTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @dataProvider getResolvedEnabledFixtures
+ */
+ public function testIsConfigEnabledReturnsTheResolvedValue($enabled)
+ {
+ $pb = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBag')
+ ->setMethods(array('resolveValue'))
+ ->getMock()
+ ;
+
+ $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
+ ->setMethods(array('getParameterBag'))
+ ->getMock()
+ ;
+
+ $pb->expects($this->once())
+ ->method('resolveValue')
+ ->with($this->equalTo($enabled))
+ ->will($this->returnValue($enabled))
+ ;
+
+ $container->expects($this->once())
+ ->method('getParameterBag')
+ ->will($this->returnValue($pb))
+ ;
+
+ $extension = $this->getMockBuilder('Symfony\Component\DependencyInjection\Extension\Extension')
+ ->setMethods(array())
+ ->getMockForAbstractClass()
+ ;
+
+ $r = new \ReflectionMethod('Symfony\Component\DependencyInjection\Extension\Extension', 'isConfigEnabled');
+ $r->setAccessible(true);
+
+ $r->invoke($extension, $container, array('enabled' => $enabled));
+ }
+
+ public function getResolvedEnabledFixtures()
+ {
+ return array(
+ array(true),
+ array(false)
+ );
+ }
+
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
+ * @expectedExceptionMessage The config array has no 'enabled' key.
+ */
+ public function testIsConfigEnabledOnNonEnableableConfig()
+ {
+ $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
+ ->getMock()
+ ;
+
+ $extension = $this->getMockBuilder('Symfony\Component\DependencyInjection\Extension\Extension')
+ ->setMethods(array())
+ ->getMockForAbstractClass()
+ ;
+
+ $r = new \ReflectionMethod('Symfony\Component\DependencyInjection\Extension\Extension', 'isConfigEnabled');
+ $r->setAccessible(true);
+
+ $r->invoke($extension, $container, array());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.