Skip to content

Auto registration deprecation #28

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* @author Yann Eugoné <[email protected]>
*
* @deprecated
*/
class ConventionedEnumCollectorCompilerPass implements CompilerPassInterface
{
Expand All @@ -21,6 +23,11 @@ public function process(ContainerBuilder $container)
return;
}

@trigger_error(
'"' . __CLASS__ . '" is deprecated since v2.2. Please use Symfony\'s PSR4 Service discovery instead.',
E_USER_DEPRECATED
);

if (true === $bundles) {
$bundles = $container->getParameter('kernel.bundles');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @author Yann Eugoné <[email protected]>
*
* @deprecated
*/
class DeclarativeEnumCollectorCompilerPass implements CompilerPassInterface
{
Expand Down Expand Up @@ -59,6 +61,11 @@ public function __construct($bundle, $transDomain = null)
*/
public function process(ContainerBuilder $container)
{
@trigger_error(
'"' . __CLASS__ . '" is deprecated since v2.2. Please use Symfony\'s PSR4 Service discovery instead.',
E_USER_DEPRECATED
);

if (!class_exists('Symfony\Component\Finder\Finder')) {
throw new RuntimeException('You need the symfony/finder component to register enums.');
}
Expand Down
9 changes: 9 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->variableNode('register_bundles')
->info('[DEPRECATED] bundles for which to auto-register enums.')
->defaultFalse()
->end()
->booleanNode('enum_autoconfiguration')
->info('If set to true, all services that implements EnumInterface, will obtain the "enum" tag automatically.')
->defaultTrue()
->end()
->booleanNode('enum_registry_autoconfigurable')
->info('If set to true, add an alias for the enum registry so your service can ask for it via autoconfiguration.')
->defaultTrue()
->end()
->end()
;

Expand Down
7 changes: 6 additions & 1 deletion DependencyInjection/EnumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Yokai\EnumBundle\Enum\EnumInterface;
use Yokai\EnumBundle\Registry\EnumRegistryInterface;

/**
* @author Yann Eugoné <[email protected]>
Expand All @@ -29,9 +30,13 @@ public function load(array $configs, ContainerBuilder $container)
$xmlLoader->load('validators.xml');
$xmlLoader->load('twig.xml');

if (method_exists($container, 'registerForAutoconfiguration')) {
if ($config['enum_autoconfiguration'] && method_exists($container, 'registerForAutoconfiguration')) {
$container->registerForAutoconfiguration(EnumInterface::class)
->addTag('enum');
}

if ($config['enum_registry_autoconfigurable']) {
$container->setAlias(EnumRegistryInterface::class, 'enum.registry');
}
}
}
75 changes: 75 additions & 0 deletions Tests/DependencyInjection/EnumExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Yokai\EnumBundle\Tests\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Yokai\EnumBundle\DependencyInjection\EnumExtension;
use Yokai\EnumBundle\Enum\EnumInterface;
use Yokai\EnumBundle\Registry\EnumRegistryInterface;

/**
* @author Yann Eugoné <[email protected]>
*/
class EnumExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @return EnumExtension
*/
public function extension()
{
return new EnumExtension();
}

/**
* @test
*/
public function it_register_enum_for_autoconfiguration_by_default()
{
$container = new ContainerBuilder();
$this->extension()->load([[]], $container);

if (method_exists($container, 'getAutoconfiguredInstanceof')) {
$autoconfigure = $container->getAutoconfiguredInstanceof();
$this->assertArrayHasKey(EnumInterface::class, $autoconfigure);
$this->assertEquals(['enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags());
} else {
$this->assertTrue(true); // not in this version
}
}

/**
* @test
*/
public function it_do_not_register_enum_for_autoconfiguration_if_asked_to()
{
$container = new ContainerBuilder();
$this->extension()->load([['enum_autoconfiguration' => false]], $container);

if (method_exists($container, 'getAutoconfiguredInstanceof')) {
$autoconfigure = $container->getAutoconfiguredInstanceof();
$this->assertArrayNotHasKey(EnumInterface::class, $autoconfigure);
} else {
$this->assertTrue(true); // not in this version
}
}

/**
* @test
*/
public function it_register_enum_registry_alias_for_autowire_by_default()
{
$container = new ContainerBuilder();
$this->extension()->load([[]], $container);
$this->assertTrue($container->hasAlias(EnumRegistryInterface::class));
}

/**
* @test
*/
public function it_do_not_register_enum_registry_alias_for_autowire_if_asked_to()
{
$container = new ContainerBuilder();
$this->extension()->load([['enum_registry_autoconfigurable' => false]], $container);
$this->assertFalse($container->hasAlias(EnumRegistryInterface::class));
}
}
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
"autoload": {
"psr-4": { "Yokai\\EnumBundle\\": "" }
},
"minimum-stability": "stable"
"minimum-stability": "stable",
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
}
}