Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[DI] resolves sf services in config #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/DependencyInjection/ExerciseHTMLPurifierExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$configs = array_map([$this, 'resolveServices'], $configs);
$configs = $container->resolveServices($configs);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @HeahDude, the test no longer passes with this solution, @App\MyService is not replaced by Reference instance

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright then we need to be more careful with possible conflict and allow escaping @ when it's expected as legit character.
I propose to use a regex instead and allow escaping by doubling @@ as it is done in yaml config in Symfony.
We should also add a note about it in the docs (in the README).


// Set default serializer cache path, while ensuring a default profile is defined
$configs['html_profiles']['default']['config']['Cache.SerializerPath'] = $configs['default_cache_serializer_path'];
Expand Down Expand Up @@ -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;
}
}
44 changes: 44 additions & 0 deletions tests/DependencyInjection/ExerciseHTMLPurifierExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -465,3 +505,7 @@ public function __construct(\HTMLPurifier $advancedPurifier)
{
}
}

class MyCustomInjectorService extends \HTMLPurifier_Injector
{
}