diff --git a/src/DependencyInjection/ExerciseHTMLPurifierExtension.php b/src/DependencyInjection/ExerciseHTMLPurifierExtension.php index 76965aaf..6babc052 100644 --- a/src/DependencyInjection/ExerciseHTMLPurifierExtension.php +++ b/src/DependencyInjection/ExerciseHTMLPurifierExtension.php @@ -8,6 +8,7 @@ use Exercise\HTMLPurifierBundle\HTMLPurifiersRegistryInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -21,6 +22,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('html_purifier.xml'); $configs = $this->processConfiguration(new Configuration(), $configs); + $configs = array_map([$this, 'resolveServices'], $configs); // Set default serializer cache path, while ensuring a default profile is defined $configs['html_profiles']['default']['config']['Cache.SerializerPath'] = $configs['default_cache_serializer_path']; @@ -120,4 +122,25 @@ private static function getResolvedConfig(string $parameter, array $parents, arr return array_filter(array_column($parents, $parameter)); } + + private function resolveServices($value) + { + if (is_array($value)) { + return array_map([$this, 'resolveServices'], $value); + } + + 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; + } + + return new Reference($value, $invalidBehavior); + } + + return $value; + } } diff --git a/tests/DependencyInjection/ExerciseHTMLPurifierExtensionTest.php b/tests/DependencyInjection/ExerciseHTMLPurifierExtensionTest.php index 274e5431..d4c61ebf 100644 --- a/tests/DependencyInjection/ExerciseHTMLPurifierExtensionTest.php +++ b/tests/DependencyInjection/ExerciseHTMLPurifierExtensionTest.php @@ -370,6 +370,46 @@ public function testShouldRegisterAliases() ); } + public function testShouldResolveServices() + { + $config = [ + 'html_profiles' => [ + 'default' => [ + 'config' => [ + 'AutoFormat.Custom' => [ + '@'.MyCustomInjectorService::class, + ], + ], + ], + ], + ]; + + $this->extension->load([$config], $this->container); + $this->container->register(MyCustomInjectorService::class); + + $this->container->register(ServiceWithDefaultConfig::class) + ->setAutowired(true) + ->setPublic(true) + ; + + $this->container->compile(); + + $defaultConfigArgument1 = $this->container + ->findDefinition(ServiceWithDefaultConfig::class) + ->getArgument(0) + ; + + $this->assertInstanceOf(Definition::class, $defaultConfigArgument1); + + /** @var Definition $htmlPurifierConfigDefinition */ + $htmlPurifierConfigDefinition = $defaultConfigArgument1->getArgument(0); + + $customInjectors = $htmlPurifierConfigDefinition->getArgument(1); + self::assertArrayHasKey('AutoFormat.Custom', $customInjectors); + + $this->assertInstanceOf(Definition::class, $customInjectors['AutoFormat.Custom'][0]); + } + /** * Asserts that the named config definition extends the default profile and * loads the given options. @@ -465,3 +505,7 @@ public function __construct(\HTMLPurifier $advancedPurifier) { } } + +class MyCustomInjectorService extends \HTMLPurifier_Injector +{ +}