Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 6ea9695

Browse files
bakura10weierophinney
authored andcommitted
Refactor SM
1 parent f28540c commit 6ea9695

29 files changed

+1158
-2835
lines changed

CHANGELOG.md

Lines changed: 212 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,229 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.7.0 - TBD
5+
## v3.0.0 - TBD
66

77
### Added
88

9-
- Nothing.
9+
- You can now map multiple key names to the same factory. It was previously
10+
possible in ZF2 but it was not enforced by the `FactoryInterface` interface.
11+
Now the interface receives the `$requestedName` as the *second* parameter
12+
(previously, it was the third).
13+
14+
Example:
15+
16+
```php
17+
$sm = new \Zend\ServiceManager\ServiceManager([
18+
'factories' => [
19+
MyClassA::class => MyFactory::class,
20+
MyClassB::class => MyFactory::class
21+
]
22+
]);
23+
24+
$sm->get(MyClassA::class); // MyFactory will receive MyClassA::class as second parameter
25+
```
26+
27+
- Writing a plugin manager has been simplified. If you have simple needs, you no
28+
longer need to implement the complete `validate` method.
29+
30+
In versions 2.x, if your plugin manager only allows creating instances that
31+
implement `Zend\Validator\ValidatorInterface`, you needed to write the
32+
following code:
33+
34+
```php
35+
class MyPluginManager extends AbstractPluginManager
36+
{
37+
public function validate($instance)
38+
{
39+
if ($instance instanceof \Zend\Validator\ValidatorInterface) {
40+
return;
41+
}
42+
43+
throw new InvalidServiceException(sprintf(
44+
'Plugin manager "%s" expected an instance of type "%s", but "%s" was received',
45+
__CLASS__,
46+
\Zend\Validator\ValidatorInterface::class,
47+
is_object($instance) ? get_class($instance) : gettype($instance)
48+
));
49+
}
50+
}
51+
```
52+
53+
In version 3, this becomes:
54+
55+
```php
56+
class MyPluginManager extends AbstractPluginManager
57+
{
58+
protected $instanceOf = \Zend\Validator\ValidatorInterface::class;
59+
}
60+
```
61+
62+
Of course, you can still override the `validate` method if your logic is more
63+
complex.
1064

1165
### Deprecated
1266

13-
- Nothing.
67+
- Nothing
1468

1569
### Removed
1670

17-
- Nothing.
71+
- Peering has been removed. It was a complex and rarely used feature that was
72+
misunderstood most of the time.
1873

19-
### Fixed
74+
- Integration with `Zend\Di` has been removed. It may be re-integrated later.
2075

21-
- Nothing.
76+
- The `invokables` configuration key no longer exists. It has been replaced by a
77+
built-in factory.
78+
79+
In versions 2.x:
80+
81+
```php
82+
return [
83+
'service_manager' => [
84+
'invokables' => [
85+
MyClass::class => MyClass:class,
86+
],
87+
],
88+
];
89+
```
90+
91+
In ZF 3.x:
92+
93+
```php
94+
return [
95+
'service_manager' => [
96+
'factories' => [
97+
MyClass::class => 'Zend\ServiceManager\Factory\InvokableFactory',
98+
],
99+
],
100+
];
101+
```
102+
103+
- `MutableCreationOptionsInterface` has been removed, as options can now be
104+
passed directly through factories.
105+
106+
- `ServiceLocatorAwareInterface` and its associated trait has been removed. It
107+
was an anti-pattern, and you are encouraged to inject your dependencies in
108+
factories instead of injecting the whole service locator.
109+
110+
### Changed/Fixed
111+
112+
v3 of the ServiceManager component is a completely rewritten, more efficient
113+
implementation of the service locator pattern. It includes a number of breaking
114+
changes, outlined in this section.
115+
116+
- You no longer need a `Zend\ServiceManager\Config` object to configure the
117+
service manager; you can pass the configuration array directly instead.
118+
119+
In version 2.x:
120+
121+
```php
122+
$config = new \Zend\ServiceManager\Config([
123+
'factories' => [...]
124+
]);
125+
126+
$sm = new \Zend\ServiceManager\ServiceManager($config);
127+
```
128+
129+
In ZF 3.x:
130+
131+
```php
132+
$sm = new \Zend\ServiceManager\ServiceManager([
133+
'factories' => [...]
134+
]);
135+
```
136+
137+
- The ServiceManager is now immutable. Once configured, it cannot be altered.
138+
You need to create a new service manager if you need to change the
139+
configuration. This ensures safer and more aggressive caching. A new method,
140+
`withConfig()` allows you to create a new instance that merges the provided
141+
configuration.
142+
143+
- Interfaces for `FactoryInterface`, `DelegatorFactoryInterface` and
144+
`AbstractFactoryInterface` have changed. All are now directly invokable. This
145+
allows a number of performance optimization internally.
146+
147+
Additionally, all signatures that accepted a "canonical name" argument now
148+
remove it.
149+
150+
Most of the time, rewriting a factory to match the new interface implies
151+
replacing the method name by `__invoke`, and removing the canonical name
152+
argument if present.
153+
154+
For instance, here is a simple version 2.x factory:
155+
156+
```php
157+
class MyFactory implements FactoryInterface
158+
{
159+
function createService(ServiceLocatorInterface $sl)
160+
{
161+
// ...
162+
}
163+
}
164+
```
165+
166+
The equivalent version 3 factory:
167+
168+
```php
169+
class MyFactory implements FactoryInterface
170+
{
171+
function __invoke(ServiceLocatorInterface $sl, $requestedName)
172+
{
173+
// ...
174+
}
175+
}
176+
```
177+
178+
Note another change in the above: factories also receive a second parameter,
179+
enforced through the interface, that allows you to easily map multiple service
180+
names to the same factory.
181+
182+
- Plugin managers will now receive the parent service locator instead of itself
183+
in factories. In version 2.x, you needed to call the method
184+
`getServiceLocator()` to retrieve the parent (application) service locator.
185+
This was confusing, and not IDE friendly as this method was not enforced
186+
through the interface.
187+
188+
In version 2.x, if a factory was set to a service name defined in a plugin manager:
189+
190+
```php
191+
class MyFactory implements FactoryInterface
192+
{
193+
function createService(ServiceLocatorInterface $sl)
194+
{
195+
// $sl is actually a plugin manager
196+
197+
$parentLocator = $sl->getServiceLocator();
198+
199+
// ...
200+
}
201+
}
202+
```
203+
204+
In version 3:
205+
206+
```php
207+
class MyFactory implements FactoryInterface
208+
{
209+
function __invoke(ServiceLocatorInterface $sl, $requestedName)
210+
{
211+
// $sl is already the main, parent service locator. If you need to
212+
// retrieve the plugin manager again, you can retrieve it through the
213+
// servicelocator:
214+
$pluginManager = $sl->get(MyPluginManager::class);
215+
// ...
216+
}
217+
}
218+
```
219+
220+
In practice, this should reduce code, as dependencies often come from the main
221+
service locator, and not the plugin manager itself.
222+
223+
- `PluginManager` now enforces the need for the main service locator in its
224+
constructor. In v2.x, people often forgot to set the parent locator, which led
225+
to bugs in factories trying to fetch dependencies from the parent locator.
226+
227+
- It's so fast now that your app will fly!
22228

23229
## 2.6.1 - TBD
24230

composer.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"description": " ",
44
"license": "BSD-3-Clause",
55
"keywords": [
6-
"zf2",
7-
"servicemanager"
6+
"zf",
7+
"servicemanager",
8+
"service-manager"
89
],
910
"homepage": "https://github.com/zendframework/zend-servicemanager",
1011
"autoload": {
@@ -17,21 +18,19 @@
1718
"container-interop/container-interop": "~1.0"
1819
},
1920
"require-dev": {
20-
"zendframework/zend-di": "~2.5",
21-
"zendframework/zend-mvc": "~2.5",
21+
"phpunit/phpunit": "~4.6",
2222
"fabpot/php-cs-fixer": "1.7.*",
23-
"phpunit/PHPUnit": "~4.0"
23+
"ocramius/proxy-manager": "~1.0"
2424
},
2525
"suggest": {
26-
"ocramius/proxy-manager": "ProxyManager 0.5.* to handle lazy initialization of services",
27-
"zendframework/zend-di": "Zend\\Di component"
26+
"ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services"
2827
},
2928
"minimum-stability": "dev",
3029
"prefer-stable": true,
3130
"extra": {
3231
"branch-alias": {
3332
"dev-master": "2.6-dev",
34-
"dev-develop": "2.7-dev"
33+
"dev-develop": "3.0-dev"
3534
}
3635
},
3736
"autoload-dev": {

0 commit comments

Comments
 (0)