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

Commit e325179

Browse files
committed
Merge branch 'refactor-sm' into develop
Close #9 V3, here we come!
2 parents a4914dd + 224a028 commit e325179

File tree

87 files changed

+3818
-4958
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+3818
-4958
lines changed

.php_cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ install:
4141
script:
4242
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
4343
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then ./vendor/bin/phpunit ; fi
44-
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run ; fi
44+
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/phpcs ; fi
4545

4646
after_script:
4747
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi

CHANGELOG.md

Lines changed: 216 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,233 @@
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+
'MyClassC' => 'MyFactory' // This is equivalent as using ::class
22+
],
23+
]);
24+
25+
$sm->get(MyClassA::class); // MyFactory will receive MyClassA::class as second parameter
26+
```
27+
28+
- Writing a plugin manager has been simplified. If you have simple needs, you no
29+
longer need to implement the complete `validate` method.
30+
31+
In versions 2.x, if your plugin manager only allows creating instances that
32+
implement `Zend\Validator\ValidatorInterface`, you needed to write the
33+
following code:
34+
35+
```php
36+
class MyPluginManager extends AbstractPluginManager
37+
{
38+
public function validate($instance)
39+
{
40+
if ($instance instanceof \Zend\Validator\ValidatorInterface) {
41+
return;
42+
}
43+
44+
throw new InvalidServiceException(sprintf(
45+
'Plugin manager "%s" expected an instance of type "%s", but "%s" was received',
46+
__CLASS__,
47+
\Zend\Validator\ValidatorInterface::class,
48+
is_object($instance) ? get_class($instance) : gettype($instance)
49+
));
50+
}
51+
}
52+
```
53+
54+
In version 3, this becomes:
55+
56+
```php
57+
use Zend\ServiceManager\AbstractPluginManager;
58+
use Zend\Validator\ValidatorInterface;
59+
60+
class MyPluginManager extends AbstractPluginManager
61+
{
62+
protected $instanceOf = ValidatorInterface::class;
63+
}
64+
```
65+
66+
Of course, you can still override the `validate` method if your logic is more
67+
complex.
1068

1169
### Deprecated
1270

13-
- Nothing.
71+
- Nothing
1472

1573
### Removed
1674

17-
- Nothing.
75+
- Peering has been removed. It was a complex and rarely used feature that was
76+
misunderstood most of the time.
1877

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

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

23233
## 2.6.1 - TBD
24234

CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,24 @@ To do so:
7777

7878
## Running Coding Standards Checks
7979

80-
This component uses [php-cs-fixer](http://cs.sensiolabs.org/) for coding
80+
This component uses [phpcs](https://github.com/squizlabs/PHP_CodeSniffer) for coding
8181
standards checks, and provides configuration for our selected checks.
82-
`php-cs-fixer` is installed by default via Composer.
82+
`phpcs` is installed by default via Composer.
8383

8484
To run checks only:
8585

8686
```console
87-
$ ./vendor/bin/php-cs-fixer fix . -v --diff --dry-run --config-file=.php_cs
87+
$ ./vendor/bin/phpcs
8888
```
8989

90-
To have `php-cs-fixer` attempt to fix problems for you, omit the `--dry-run`
91-
flag:
90+
`phpcs` provides another tool, `phpcbf`, that can automatically fix many common
91+
problems:
9292

9393
```console
94-
$ ./vendor/bin/php-cs-fixer fix . -v --diff --config-file=.php_cs
94+
$ ./vendor/bin/phpcbf
9595
```
9696

97-
If you allow php-cs-fixer to fix CS issues, please re-run the tests to ensure
97+
If you allow `phpcbf` to fix CS issues, please re-run the tests to ensure
9898
they pass, and make sure you add and commit the changes after verification.
9999

100100
## Recommended Workflow for Contributions

composer.json

Lines changed: 8 additions & 9 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",
22-
"fabpot/php-cs-fixer": "1.7.*",
23-
"phpunit/PHPUnit": "~4.0"
21+
"phpunit/phpunit": "~4.6",
22+
"ocramius/proxy-manager": "~1.0",
23+
"squizlabs/php_codesniffer": "^2.0@dev"
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)