Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #7 from arnaud-lb/resolve-services

Resolve services in config
  • Loading branch information...
commit b0ae9d2fdd58151d59b0b85800acca305f401cd6 2 parents 0ab58a3 + c94aac0
@jmikola jmikola authored
View
28 DependencyInjection/ExerciseHTMLPurifierExtension.php
@@ -8,6 +8,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
+use Symfony\Component\DependencyInjection\ContainerInterface;
class ExerciseHTMLPurifierExtension extends Extension
{
@@ -31,6 +32,7 @@ public function load(array $configs, ContainerBuilder $container)
));
$configs = $this->processConfiguration(new Configuration(), $configs);
+ $configs = array_map(array($this, 'resolveServices'), $configs);
$paths = array();
foreach ($configs as $name => $config) {
@@ -68,4 +70,30 @@ public function getAlias()
{
return 'exercise_html_purifier';
}
+
+ private function resolveServices($value)
+ {
+ if (is_array($value)) {
+ $value = array_map(array($this, 'resolveServices'), $value);
+ } else if (is_string($value) && 0 === strpos($value, '@')) {
+ if (0 === strpos($value, '@?')) {
+ $value = substr($value, 2);
+ $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
+ } else {
+ $value = substr($value, 1);
+ $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
+ }
+
+ if ('=' === substr($value, -1)) {
+ $value = substr($value, 0, -1);
+ $strict = false;
+ } else {
+ $strict = true;
+ }
+
+ $value = new Reference($value, $invalidBehavior, $strict);
+ }
+
+ return $value;
+ }
}
View
24 Tests/DependencyInjection/ExerciseHTMLPurifierExtensionTest.php
@@ -106,6 +106,30 @@ public function testShouldLoadCustomConfiguration()
));
}
+ public function testShouldResolveServices()
+ {
+ $container = new ContainerBuilder;
+ $extension = new ExerciseHTMLPurifierExtension();
+
+ $config = array(
+ 'simple' => array(
+ 'AutoFormat.Custom' => array('@service_container'),
+ ),
+ );
+
+ $this->extension->load(array($config), $this->container);
+
+ $definition = $this->container->getDefinition('exercise_html_purifier.config.simple');
+ $calls = $definition->getMethodCalls();
+
+ $call = $calls[0];
+ $this->assertSame('loadArray', $call[0]);
+
+ $args = $call[1];
+
+ $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $args[0]['AutoFormat.Custom'][0]);
+ }
+
/**
* Assert that the named config definition extends the default profile and
* loads the given options.
Please sign in to comment.
Something went wrong with that request. Please try again.