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

Commit 687b886

Browse files
committed
Merge branch 'feature/psr-container' into develop
Close #180
2 parents 939f297 + c4fffd9 commit 687b886

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#180](https://github.com/zendframework/zend-servicemanager/pull/180) adds
10+
explicit support for PSR-11 (ContainerInterface) by requiring
11+
container-interop at a minimum version of 1.2.0, and adding a requirement on
12+
psr/container 1.0. `Zend\ServiceManager\ServiceLocatorInterface` now
13+
explicitly extends the `ContainerInterface` from both projects.
14+
15+
Factory interfaces still typehint against the container-interop variant, as
16+
changing the typehint would break backwards compatibility. Users can
17+
duck-type most of these interfaces, however, by creating callables or
18+
invokables that typehint against psr/container instead.
1019

1120
### Deprecated
1221

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"require": {
1717
"php": "^5.6 || ^7.0",
18-
"container-interop/container-interop": "~1.0",
18+
"container-interop/container-interop": "^1.2",
19+
"psr/container": "^1.0",
1920
"zendframework/zend-stdlib": "^3.1"
2021
},
2122
"require-dev": {
@@ -44,7 +45,8 @@
4445
}
4546
},
4647
"provide": {
47-
"container-interop/container-interop-implementation": "^1.1"
48+
"container-interop/container-interop-implementation": "^1.2",
49+
"psr/container-implementation": "^1.0"
4850
},
4951
"bin": [
5052
"bin/generate-deps-for-config-factory",

doc/book/psr-11.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# PSR-11 Support
2+
3+
[container-interop/container-interop 1.2.0](https://github.com/container-interop/container-interop/releases/tag/1.2.0)
4+
modifies its codebase to extend interfaces from [psr/container](https://github.com/php-fig/container)
5+
(the official interfaces for [PSR-11](http://www.php-fig.org/psr/psr-11/)). If
6+
you are on a pre-3.3.0 version of zend-servicemanager, update your project, and
7+
receive container-interop 1.2, then zend-servicemanager can already act as a
8+
PSR-11 provider!
9+
10+
zend-servicemanager 3.3.0 requires at least version 1.2 of container-interop,
11+
and _also_ requires psr/container 1.0 to explicitly signal that it is a PSR-11
12+
provider, and to allow removal of the container-interop dependency later.
13+
14+
Version 4.0 will require only psr/container, and will update the various factory
15+
interfaces and exception implementations to typehint against the PSR-11
16+
interfaces, which will require changes to any implementations you have. In the
17+
meantime, you can [duck-type](https://en.wikipedia.org/wiki/Duck_typing) the
18+
following factory types:
19+
20+
- `Zend\ServiceManager\Factory\FactoryInterface`: use a callable with the
21+
following signature:
22+
23+
```php
24+
function (
25+
\Psr\Container\ContainerInterface $container,
26+
string $requestedName,
27+
array $options = null
28+
)
29+
```
30+
31+
- `Zend\ServiceManager\Factory\DelegatorFactoryInterface`: use a callable with
32+
the following signature:
33+
34+
```php
35+
function (
36+
\Psr\Container\ContainerInterface $container,
37+
string $name,
38+
callable $callback,
39+
array $options = null
40+
)
41+
```
42+
43+
- `Zend\ServiceManager\Initializer\InitializerInterface`: use a callable with
44+
the following signature:
45+
46+
```php
47+
function (
48+
\Psr\Container\ContainerInterface $container,
49+
$instance
50+
)
51+
```
52+
53+
Abstract factories _can not_ be duck typed, due to the additional `canCreate()`
54+
method.
55+
56+
You can also leave your factories as-is for now, and update them once
57+
zend-servicemanager v4.0 is released, at which time we will be providing tooling
58+
to help migrate your factories to PSR-11.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pages:
44
- index.md
55
- 'Quick Start': quick-start.md
66
- Reference:
7+
- 'PSR-11 Support': psr-11.md
78
- 'Configuring the service manager': configuring-the-service-manager.md
89
- Delegators: delegators.md
910
- 'Lazy services': lazy-services.md

src/ServiceLocatorInterface.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
namespace Zend\ServiceManager;
99

10-
use Interop\Container\ContainerInterface;
11-
use Interop\Container\Exception\ContainerException;
10+
use Psr\Container\ContainerInterface as PsrContainerInterface;
11+
use Psr\Container\ContainerExceptionInterface;
12+
use Interop\Container\ContainerInterface as InteropContainerInterface;
1213

1314
/**
1415
* Interface for service locator
1516
*/
16-
interface ServiceLocatorInterface extends ContainerInterface
17+
interface ServiceLocatorInterface extends
18+
PsrContainerInterface,
19+
InteropContainerInterface
1720
{
1821
/**
1922
* Build a service by its name, using optional options (such services are NEVER cached).
@@ -25,7 +28,7 @@ interface ServiceLocatorInterface extends ContainerInterface
2528
* factory could be found to create the instance.
2629
* @throws Exception\ServiceNotCreatedException If factory/delegator fails
2730
* to create the instance.
28-
* @throws ContainerException if any other error occurs
31+
* @throws ContainerExceptionInterface if any other error occurs
2932
*/
3033
public function build($name, array $options = null);
3134
}

test/ServiceManagerTest.php

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

1010
use DateTime;
1111
use PHPUnit\Framework\TestCase;
12+
use Psr\Container\ContainerInterface;
1213
use stdClass;
1314
use Zend\ServiceManager\Factory\FactoryInterface;
1415
use Zend\ServiceManager\Factory\InvokableFactory;
@@ -29,6 +30,12 @@ public function createContainer(array $config = [])
2930
return $this->creationContext;
3031
}
3132

33+
public function testServiceManagerIsAPsr11Container()
34+
{
35+
$container = $this->createContainer();
36+
$this->assertInstanceOf(ContainerInterface::class, $container);
37+
}
38+
3239
public function testConfigurationCanBeMerged()
3340
{
3441
$serviceManager = new SimpleServiceManager([

0 commit comments

Comments
 (0)