Skip to content

Commit 58366f5

Browse files
Make code compatible with Symfony 3.0
1 parent e73cd00 commit 58366f5

11 files changed

+223
-111
lines changed

.travis.yml

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@ matrix:
99
- php: 5.5
1010
- php: 5.6
1111
- php: 5.6
12-
env: DEPENDENCY_INJECTION_VERSION=2.2.*
12+
env: SYMFONY_VERSION=2.2.*
1313
- php: 5.6
14-
env: DEPENDENCY_INJECTION_VERSION=2.3.*
14+
env: SYMFONY_VERSION=2.3.*
1515
- php: 5.6
16-
env: DEPENDENCY_INJECTION_VERSION=2.4.* EXPRESSION_LANGUAGE_VERSION=2.4.*
16+
env: SYMFONY_VERSION=2.4.* EXPRESSION_LANGUAGE_VERSION=2.4.*
1717
- php: 5.6
18-
env: DEPENDENCY_INJECTION_VERSION=2.5.* EXPRESSION_LANGUAGE_VERSION=2.5.*
18+
env: SYMFONY_VERSION=2.5.* EXPRESSION_LANGUAGE_VERSION=2.5.*
1919
- php: 5.6
20-
env: DEPENDENCY_INJECTION_VERSION=2.6.* EXPRESSION_LANGUAGE_VERSION=2.6.*
20+
env: SYMFONY_VERSION=2.6.* EXPRESSION_LANGUAGE_VERSION=2.6.*
2121
- php: 5.6
22-
env: DEPENDENCY_INJECTION_VERSION=2.7.* EXPRESSION_LANGUAGE_VERSION=2.7.*
22+
env: SYMFONY_VERSION=2.7.* EXPRESSION_LANGUAGE_VERSION=2.7.*
2323
- php: 5.6
24-
env: DEPENDENCY_INJECTION_VERSION=3.0.* EXPRESSION_LANGUAGE_VERSION=3.0.*
24+
env: SYMFONY_VERSION=3.0.* EXPRESSION_LANGUAGE_VERSION=3.0.*
2525

2626
env:
2727
global:
28-
- DEPENDENCY_INJECTION_VERSION=""
28+
- SYMFONY_VERSION=""
2929
- EXPRESSION_LANGUAGE_VERSION=""
3030

3131
before_install:
32-
- if [ "$DEPENDENCY_INJECTION_VERSION" != "" ]; then composer require --dev --no-update symfony/dependency-injection "$DEPENDENCY_INJECTION_VERSION"; fi
32+
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/dependency-injection "$SYMFONY_VERSION"; fi
33+
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/config "$SYMFONY_VERSION"; fi
34+
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/yaml "$SYMFONY_VERSION"; fi
3335
- if [ "$EXPRESSION_LANGUAGE_VERSION" != "" ]; then composer require --dev --no-update symfony/expression-language "$EXPRESSION_LANGUAGE_VERSION"; fi
3436

3537
install:

ConstructorResolver.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,15 @@ private function resolveFactory(Definition $definition)
110110
return $definition->getFactory();
111111
}
112112

113-
if ($definition->getFactoryClass() && $definition->getFactoryMethod()) {
113+
if (method_exists($definition, 'getFactoryClass')
114+
&& $definition->getFactoryClass()
115+
&& $definition->getFactoryMethod()) {
114116
return array($definition->getFactoryClass(), $definition->getFactoryMethod());
115117
}
116118

117-
if ($definition->getFactoryService() && $definition->getFactoryMethod()) {
119+
if (method_exists($definition, 'getFactoryService')
120+
&& $definition->getFactoryService()
121+
&& $definition->getFactoryMethod()) {
118122
return array(new Reference($definition->getFactoryService()), $definition->getFactoryMethod());
119123
}
120124

ServiceDefinitionValidator.php

+45-34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Matthias\SymfonyServiceDefinitionValidator\Exception\ServiceNotFoundException;
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
1111
use Symfony\Component\DependencyInjection\Definition;
12+
use Symfony\Component\DependencyInjection\Reference;
1213

1314
class ServiceDefinitionValidator implements ServiceDefinitionValidatorInterface
1415
{
@@ -39,9 +40,7 @@ public function validateAttributes(Definition $definition)
3940
{
4041
$this->validateClass($definition);
4142

42-
$this->validateFactoryClass($definition);
43-
44-
$this->validateFactoryService($definition);
43+
$this->validateFactory($definition);
4544
}
4645

4746
public function validateArguments(Definition $definition)
@@ -70,50 +69,46 @@ private function validateClass(Definition $definition)
7069
}
7170
}
7271

73-
private function validateFactoryClass(Definition $definition)
72+
private function validateFactory(Definition $definition)
7473
{
75-
$factoryClass = $definition->getFactoryClass();
76-
77-
if (!$factoryClass) {
78-
return;
74+
if (method_exists($definition, 'getFactoryClass')) {
75+
// Symfony <= 3.0.0
76+
$factoryClass = $definition->getFactoryClass();
77+
if ($factoryClass) {
78+
$this->validateFactoryClassAndMethod($factoryClass, $definition->getFactoryMethod());
79+
}
7980
}
8081

81-
$factoryMethod = $definition->getFactoryMethod();
82-
83-
if ($factoryClass && !$factoryMethod) {
84-
throw new MissingFactoryMethodException();
82+
if (method_exists($definition, 'getFactoryService')) {
83+
$factoryService = $definition->getFactoryService();
84+
if ($factoryService) {
85+
$this->validateFactoryServiceAndMethod($factoryService, $definition->getFactoryMethod());
86+
}
8587
}
8688

87-
$factoryClass = $this->resolveValue($factoryClass);
89+
if (method_exists($definition, 'getFactory')) {
90+
$factory = $definition->getFactory();
91+
if (!is_array($factory) || count($factory) !== 2) {
92+
return;
93+
}
8894

89-
$this->validateFactoryClassAndMethod($factoryClass, $factoryMethod);
95+
list($factoryClassOrService, $method) = $factory;
96+
if (is_string($factoryClassOrService)) {
97+
$this->validateFactoryClassAndMethod($factoryClassOrService, $method);
98+
} else {
99+
$this->validateFactoryServiceAndMethod((string) $factoryClassOrService, $method);
100+
}
101+
}
90102
}
91103

92-
private function validateFactoryService(Definition $definition)
104+
private function validateFactoryClassAndMethod($factoryClass, $factoryMethod)
93105
{
94-
$factoryServiceId = $definition->getFactoryService();
95-
96-
if (!$factoryServiceId) {
97-
return;
98-
}
99-
100-
$factoryMethod = $definition->getFactoryMethod();
101-
if (!$factoryMethod) {
106+
if ($factoryClass && !$factoryMethod) {
102107
throw new MissingFactoryMethodException();
103108
}
104109

105-
if (!$this->containerBuilder->has($factoryServiceId)) {
106-
throw new ServiceNotFoundException($factoryServiceId);
107-
}
108-
109-
$factoryServiceDefinition = $this->containerBuilder->findDefinition($factoryServiceId);
110-
$factoryClass = $factoryServiceDefinition->getClass();
111-
112-
$this->validateFactoryClassAndMethod($factoryClass, $factoryMethod);
113-
}
110+
$factoryClass = $this->resolveValue($factoryClass);
114111

115-
private function validateFactoryClassAndMethod($factoryClass, $factoryMethod)
116-
{
117112
if (!class_exists($factoryClass) && !interface_exists($factoryClass)) {
118113
throw new ClassNotFoundException($factoryClass);
119114
}
@@ -153,4 +148,20 @@ private function resolveValue($value)
153148
{
154149
return $this->containerBuilder->getParameterBag()->resolveValue($value);
155150
}
151+
152+
private function validateFactoryServiceAndMethod($factoryServiceId, $factoryMethod)
153+
{
154+
if (!$factoryMethod) {
155+
throw new MissingFactoryMethodException();
156+
}
157+
158+
if (!$this->containerBuilder->has($factoryServiceId)) {
159+
throw new ServiceNotFoundException($factoryServiceId);
160+
}
161+
162+
$factoryServiceDefinition = $this->containerBuilder->findDefinition($factoryServiceId);
163+
$factoryClass = $factoryServiceDefinition->getClass();
164+
165+
$this->validateFactoryClassAndMethod($factoryClass, $factoryMethod);
166+
}
156167
}

Tests/ConstructorResolverTest.php

+40-20
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,20 @@ public function getFactoryServiceAndFactoryMethodAreDefinedData()
7474
$factoryMethod = 'create';
7575
$expectedConstructor = new \ReflectionMethod($factoryClass, $factoryMethod);
7676

77-
$definition = new Definition();
78-
$definition->setFactoryService($factoryService);
79-
$definition->setFactoryMethod($factoryMethod);
77+
$data = array();
78+
79+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactoryService')) {
80+
$definition = new Definition();
81+
$definition->setFactoryService($factoryService);
82+
$definition->setFactoryMethod($factoryMethod);
8083

81-
$data = array(array($factoryClass, $definition, $expectedConstructor));
84+
$data[] = array($factoryClass, $definition, $expectedConstructor);
85+
}
8286

83-
if (method_exists($definition, 'setFactory')) {
87+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactory')) {
8488
$definition = new Definition();
8589
$definition->setFactory(array(new Reference($factoryService), $factoryMethod));
90+
8691
$data[] = array($factoryClass, $definition, $expectedConstructor);
8792
}
8893

@@ -107,15 +112,20 @@ public function getFactoryClassAndFactoryMethodAreDefinedData()
107112
$factoryMethod = 'create';
108113
$expectedConstructor = new \ReflectionMethod($factoryClass, $factoryMethod);
109114

110-
$definition = new Definition();
111-
$definition->setFactoryClass($factoryClass);
112-
$definition->setFactoryMethod($factoryMethod);
115+
$data = array();
116+
117+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactoryClass')) {
118+
$definition = new Definition();
119+
$definition->setFactoryClass($factoryClass);
120+
$definition->setFactoryMethod($factoryMethod);
113121

114-
$data = array(array($definition, $expectedConstructor));
122+
$data[] = array($definition, $expectedConstructor);
123+
}
115124

116-
if (method_exists($definition, 'setFactory')) {
125+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactory')) {
117126
$definition = new Definition();
118127
$definition->setFactory(array($factoryClass, $factoryMethod));
128+
119129
$data[] = array($definition, $expectedConstructor);
120130
}
121131

@@ -140,15 +150,20 @@ public function getFactoryClassDoesNotExistData()
140150
$factoryClass = 'NonExistingClass';
141151
$factoryMethod = 'create';
142152

143-
$definition = new Definition();
144-
$definition->setFactoryClass($factoryClass);
145-
$definition->setFactoryMethod($factoryMethod);
153+
$data = array();
154+
155+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactoryClass')) {
156+
$definition = new Definition();
157+
$definition->setFactoryClass($factoryClass);
158+
$definition->setFactoryMethod($factoryMethod);
146159

147-
$data = array(array($definition));
160+
$data[] = array($definition);
161+
}
148162

149-
if (method_exists($definition, 'setFactory')) {
163+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactory')) {
150164
$definition = new Definition();
151165
$definition->setFactory(array($factoryClass, $factoryMethod));
166+
152167
$data[] = array($definition);
153168
}
154169

@@ -173,15 +188,20 @@ public function getFactoryMethodIsNotStaticData()
173188
$factoryClass = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\FactoryClass';
174189
$factoryMethod = 'createNonStatic';
175190

176-
$definition = new Definition();
177-
$definition->setFactoryClass($factoryClass);
178-
$definition->setFactoryMethod($factoryMethod);
191+
$data = array();
192+
193+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactoryClass')) {
194+
$definition = new Definition();
195+
$definition->setFactoryClass($factoryClass);
196+
$definition->setFactoryMethod($factoryMethod);
179197

180-
$data = array(array($definition));
198+
$data[] = array($definition);
199+
}
181200

182-
if (method_exists($definition, 'setFactory')) {
201+
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactory')) {
183202
$definition = new Definition();
184203
$definition->setFactory(array($factoryClass, $factoryMethod));
204+
185205
$data[] = array($definition);
186206
}
187207

Tests/Functional/Fixtures/correct_service_definitions.xml

-15
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@
1515
class="%factory.class%">
1616
</service>
1717

18-
<service id="mailer_from_factory"
19-
class="Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Mailer"
20-
factory-service="factory"
21-
factory-method="create">
22-
<argument>string argument</argument>
23-
</service>
24-
25-
<!-- factory class should be resolved -->
26-
<service id="mailer_from_variable_factory"
27-
factory-class="%factory.class%"
28-
factory-method="create"
29-
class="Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Mailer">
30-
<argument>string argument</argument>
31-
</service>
32-
3318
<service id="mailer"
3419
class="Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Mailer">
3520
<argument type="service" id="transport"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<parameters>
6+
<parameter key="factory.class">Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Factory</parameter>
7+
</parameters>
8+
<services>
9+
<service id="mailer_from_factory"
10+
class="Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Mailer">
11+
<factory service="factory" method="create"/>
12+
<argument>string argument</argument>
13+
</service>
14+
15+
<!-- factory class should be resolved -->
16+
<service id="mailer_from_variable_factory"
17+
class="Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Mailer">
18+
<factory class="%factory.class%" method="create"/>
19+
<argument>string argument</argument>
20+
</service>
21+
</services>
22+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<parameters>
6+
<parameter key="factory.class">Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Factory</parameter>
7+
</parameters>
8+
<services>
9+
<service id="mailer_from_factory"
10+
class="Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Mailer"
11+
factory-service="factory"
12+
factory-method="create">
13+
<argument>string argument</argument>
14+
</service>
15+
16+
<!-- factory class should be resolved -->
17+
<service id="mailer_from_variable_factory"
18+
factory-class="%factory.class%"
19+
factory-method="create"
20+
class="Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Mailer">
21+
<argument>string argument</argument>
22+
</service>
23+
</services>
24+
</container>

Tests/Functional/Fixtures/reported_problems.yml

-22
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,3 @@ services:
77
password: "password"
88
calls:
99
- [setAttribute, [3, 2]]
10-
11-
# Issue 12: https://github.com/matthiasnoback/symfony-service-definition-validator/issues/12
12-
# A service created by a factory might itself be created by another factory, in which case it should be possible to
13-
# supply an interface, not a class
14-
validator_builder:
15-
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\ValidatorBuilderInterface
16-
validator:
17-
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Validator
18-
factory_service: validator_builder
19-
factory_method: build
20-
21-
# Issue 11: https://github.com/matthiasnoback/symfony-service-definition-validator/issues/11
22-
# It is alright for the factory service to be private
23-
service_created_by_private_factory:
24-
class: stdClass
25-
factory_service: private_factory
26-
factory_method: create
27-
arguments:
28-
- required_argument
29-
private_factory:
30-
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Factory
31-
private: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
# Issue 12: https://github.com/matthiasnoback/symfony-service-definition-validator/issues/12
3+
# A service created by a factory might itself be created by another factory, in which case it should be possible to
4+
# supply an interface, not a class
5+
validator_builder:
6+
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\ValidatorBuilderInterface
7+
validator:
8+
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Validator
9+
factory_service: validator_builder
10+
factory_method: build
11+
12+
# Issue 11: https://github.com/matthiasnoback/symfony-service-definition-validator/issues/11
13+
# It is alright for the factory service to be private
14+
service_created_by_private_factory:
15+
class: stdClass
16+
factory_service: private_factory
17+
factory_method: create
18+
arguments:
19+
- required_argument
20+
private_factory:
21+
class: Matthias\SymfonyServiceDefinitionValidator\Tests\Functional\Fixtures\Factory
22+
private: true

0 commit comments

Comments
 (0)