Skip to content

Commit df042f0

Browse files
authored
Merge pull request #28 from yokai-php/auto-registration-deprecation
Auto registration deprecation
2 parents 2ce47e1 + 622eec8 commit df042f0

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed

DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPass.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/**
99
* @author Yann Eugoné <[email protected]>
10+
*
11+
* @deprecated
1012
*/
1113
class ConventionedEnumCollectorCompilerPass implements CompilerPassInterface
1214
{
@@ -21,6 +23,11 @@ public function process(ContainerBuilder $container)
2123
return;
2224
}
2325

26+
@trigger_error(
27+
'"' . __CLASS__ . '" is deprecated since v2.2. Please use Symfony\'s PSR4 Service discovery instead.',
28+
E_USER_DEPRECATED
29+
);
30+
2431
if (true === $bundles) {
2532
$bundles = $container->getParameter('kernel.bundles');
2633
} else {

DependencyInjection/CompilerPass/DeclarativeEnumCollectorCompilerPass.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
/**
1919
* @author Yann Eugoné <[email protected]>
20+
*
21+
* @deprecated
2022
*/
2123
class DeclarativeEnumCollectorCompilerPass implements CompilerPassInterface
2224
{
@@ -59,6 +61,11 @@ public function __construct($bundle, $transDomain = null)
5961
*/
6062
public function process(ContainerBuilder $container)
6163
{
64+
@trigger_error(
65+
'"' . __CLASS__ . '" is deprecated since v2.2. Please use Symfony\'s PSR4 Service discovery instead.',
66+
E_USER_DEPRECATED
67+
);
68+
6269
if (!class_exists('Symfony\Component\Finder\Finder')) {
6370
throw new RuntimeException('You need the symfony/finder component to register enums.');
6471
}

DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ public function getConfigTreeBuilder()
2121
$rootNode
2222
->children()
2323
->variableNode('register_bundles')
24+
->info('[DEPRECATED] bundles for which to auto-register enums.')
2425
->defaultFalse()
2526
->end()
27+
->booleanNode('enum_autoconfiguration')
28+
->info('If set to true, all services that implements EnumInterface, will obtain the "enum" tag automatically.')
29+
->defaultTrue()
30+
->end()
31+
->booleanNode('enum_registry_autoconfigurable')
32+
->info('If set to true, add an alias for the enum registry so your service can ask for it via autoconfiguration.')
33+
->defaultTrue()
34+
->end()
2635
->end()
2736
;
2837

DependencyInjection/EnumExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
88
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
99
use Yokai\EnumBundle\Enum\EnumInterface;
10+
use Yokai\EnumBundle\Registry\EnumRegistryInterface;
1011

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

32-
if (method_exists($container, 'registerForAutoconfiguration')) {
33+
if ($config['enum_autoconfiguration'] && method_exists($container, 'registerForAutoconfiguration')) {
3334
$container->registerForAutoconfiguration(EnumInterface::class)
3435
->addTag('enum');
3536
}
37+
38+
if ($config['enum_registry_autoconfigurable']) {
39+
$container->setAlias(EnumRegistryInterface::class, 'enum.registry');
40+
}
3641
}
3742
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Tests\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Yokai\EnumBundle\DependencyInjection\EnumExtension;
7+
use Yokai\EnumBundle\Enum\EnumInterface;
8+
use Yokai\EnumBundle\Registry\EnumRegistryInterface;
9+
10+
/**
11+
* @author Yann Eugoné <[email protected]>
12+
*/
13+
class EnumExtensionTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @return EnumExtension
17+
*/
18+
public function extension()
19+
{
20+
return new EnumExtension();
21+
}
22+
23+
/**
24+
* @test
25+
*/
26+
public function it_register_enum_for_autoconfiguration_by_default()
27+
{
28+
$container = new ContainerBuilder();
29+
$this->extension()->load([[]], $container);
30+
31+
if (method_exists($container, 'getAutoconfiguredInstanceof')) {
32+
$autoconfigure = $container->getAutoconfiguredInstanceof();
33+
$this->assertArrayHasKey(EnumInterface::class, $autoconfigure);
34+
$this->assertEquals(['enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags());
35+
} else {
36+
$this->assertTrue(true); // not in this version
37+
}
38+
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function it_do_not_register_enum_for_autoconfiguration_if_asked_to()
44+
{
45+
$container = new ContainerBuilder();
46+
$this->extension()->load([['enum_autoconfiguration' => false]], $container);
47+
48+
if (method_exists($container, 'getAutoconfiguredInstanceof')) {
49+
$autoconfigure = $container->getAutoconfiguredInstanceof();
50+
$this->assertArrayNotHasKey(EnumInterface::class, $autoconfigure);
51+
} else {
52+
$this->assertTrue(true); // not in this version
53+
}
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function it_register_enum_registry_alias_for_autowire_by_default()
60+
{
61+
$container = new ContainerBuilder();
62+
$this->extension()->load([[]], $container);
63+
$this->assertTrue($container->hasAlias(EnumRegistryInterface::class));
64+
}
65+
66+
/**
67+
* @test
68+
*/
69+
public function it_do_not_register_enum_registry_alias_for_autowire_if_asked_to()
70+
{
71+
$container = new ContainerBuilder();
72+
$this->extension()->load([['enum_registry_autoconfigurable' => false]], $container);
73+
$this->assertFalse($container->hasAlias(EnumRegistryInterface::class));
74+
}
75+
}

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@
2323
"autoload": {
2424
"psr-4": { "Yokai\\EnumBundle\\": "" }
2525
},
26-
"minimum-stability": "stable"
26+
"minimum-stability": "stable",
27+
"extra": {
28+
"branch-alias": {
29+
"dev-master": "2.x-dev"
30+
}
31+
}
2732
}

0 commit comments

Comments
 (0)