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

Refactor v2 #6

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/test export-ignore
/vendor export-ignore
.coveralls.yml export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
.php_cs export-ignore
phpunit.xml.dist export-ignore
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ nbproject
tmp/

clover.xml
composer.lock
coveralls-upload.json
phpunit.xml
vendor
4 changes: 1 addition & 3 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
$finder = Symfony\CS\Finder\DefaultFinder::create()
->in('src')
->in('test')
->notPath('TestAsset')
->notPath('_files')
->filter(function (SplFileInfo $file) {
Expand Down Expand Up @@ -31,8 +29,8 @@ $config->fixers(
'method_argument_space',
'object_operator',
'php_closing_tag',
'psr0',
'remove_lines_between_uses',
'short_array_syntax',
'short_tag',
'standardize_not_equal',
'trailing_spaces',
Expand Down
17 changes: 3 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,30 @@ sudo: false

language: php

branches:
except:
- /^release-.*$/
- /^ghgfk-.*$/

cache:
directories:
- $HOME/.composer/cache

matrix:
fast_finish: true
include:
- php: 5.5
env:
- EXECUTE_CS_CHECK=true
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
- EXECUTE_CS_CHECK=true
- php: 7
- php: hhvm
allow_failures:
- php: 7
- php: hhvm

notifications:
irc: "irc.freenode.org#zftalk.dev"
email: false

before_install:
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- if [[ $EXECUTE_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- composer self-update
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
- composer install --no-interaction --prefer-source

script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
Expand Down
206 changes: 202 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,206 @@
# Changelog
# CHANGELOG

All notable changes to this project will be documented in this file, in reverse chronological order by release.
## v3.O.0 - TBD

## 2.5.2 - TBD
### Added

* You can now map multiple key names to the same factory. It was previously possible in ZF2 but it was not enforced
by the `FactoryInterface` interface. Now, this interface receives the `$requestedName` as second parameter.

Example:

```php
$sm = new \Zend\ServiceManager\ServiceManager([
'factories' => [
MyClassA::class => MyFactory::class,
MyClassB::class => MyFactory::class
]
]);

$sm->get(MyClassA::class); // MyFactory will receive MyClassA::class as second parameter
```

* Writing a plugin manager has been simplified. If you have simple needs, you no longer need to implement the complete
`validate` method.

In ZF 2.x, if your plugin manager only accepts to create instances that implement `Zend\Validator\ValidatorInterface`,
you needed to write this code:

```php
class MyPluginManager extends AbstractPluginManager
{
public function validate($instance)
{
if ($instance instanceof \Zend\Validator\ValidatorInterface) {
return;
}

throw new InvalidServiceException(sprintf(
'Plugin manager "%s" expected an instance of type "%s", but "%s" was received',
__CLASS__,
\Zend\Validator\ValidatorInterface::class,
is_object($instance) ? get_class($instance) : gettype($instance)
));
}
}
```

In ZF 3.x:

```php
class MyPluginManager extends AbstractPluginManager
{
protected $instanceOf = \Zend\Validator\ValidatorInterface::class;
}
```

Of course, you can still override `validate` method if your logic is more complex.

### Deprecated

* Nothing

### Removed

* Peering has been removed. It was a complex and rarely used feature that was misunderstood most of the time.

* Integration with `Zend\Di` has been removed. It may be re-integrated later as part of another component.

* `invokables` key no longer exists. It has been replaced by a built-in factory.

In ZF 2.x:

```php
return [
'service_manager' => [
'invokables' => [
MyClass::class => MyClass:class
]
]
];
```

In ZF 3.x:

```php
return [
'service_manager' => [
'factories' => [
MyClass::class => \Zend\ServiceManager\Factory\InvokableFactory:class
]
]
];
```

* `MutableCreationOptionsInterface` has been removed, as options can now be passed directly through factories. (??)

* `ServiceLocatorAwareInterface` and its associated trait has been removed. It was an anti-pattern, and you are encouraged
to inject your dependencies in factories instead of injecting the whole service locator.

### Changed/Fixed

v3 of the ServiceManager component is a completely rewritten, more efficient implementation of the service locator
pattern. It includes a number of breaking changes, that are outlined in this section.

* You no longer need a `Zend\ServiceManager\Config` object to configure the service manager, but you instead need to
simply pass an array.

In ZF 2.x:

```php
$config = new \Zend\ServiceManager\Config([
'factories' => [...]
]);

$sm = new \Zend\ServiceManager\ServiceManager($config);
```

In ZF 3.x:

```php
$sm = new \Zend\ServiceManager\ServiceManager([
'factories' => [...]
]);
```

* Service manager is now immutable. Once configured, it cannot be altered. You need to create a new service manager
if you need to change the configuration. This allow to ensure safer and more aggressive caching.

* Interfaces for `FactoryInterface`, `DelegatorFactoryInterface` and `AbstractFactoryInterface` have changed. Now,
they are all callable. This allow to optimize performance. Most of the time, rewriting a factory to match new interface
implies replacing the method name by `__invoke`.

For instance, here is a simple ZF 2.x factory:

```php
class MyFactory implements FactoryInterface
{
function createService(ServiceLocatorInterface $sl)
{
// ...
}
}
```

The equivalent ZF 3.x factory:

```php
class MyFactory implements FactoryInterface
{
function __invoke(ServiceLocatorInterface $sl, $requestedName)
{
// ...
}
}
```

As you can see, factories also receive a second parameter enforce through interface, that allows to easily map multiple
service names to the same factory.

* Plugin managers will now receive the parent service locator instead of itself in factories. In ZF 2.x, you needed
to call the method `getServiceLocator` to retrieve the main service locator. This was confusing, and was not IDE friendly
as this method was not enforced through interface.

In ZF 2.x, if a factory was set to a service name defined in a plugin manager:

```php
class MyFactory implements FactoryInterface
{
function createService(ServiceLocatorInterface $sl)
{
// $sl is actually a plugin manager

$parentLocator = $sl->getServiceLocator();

// ...
}
}
```

In ZF 3.x:

```php
class MyFactory implements FactoryInterface
{
function __invoke(ServiceLocatorInterface $sl, $requestedName)
{
// $sl is already the main, parent service locator. If you need to retrieve the plugin manager again, you
// can retrieve it through the SL
$pluginManager = $sl->get(MyPluginManager::class);
// ...
}
}
```

In practice, this should reduce code as dependencies often come from the main service locator, and not the plugin
manager itself.

* `PluginManager` now enforce the need for the main service locator in their constructor. In ZF2, people often forgot
to set the parent locator, which leads to bugs in factories trying to fetch dependencies from the parent locator.

* It's so fast now that your app will fly.

## 2.5.2

### Added

Expand All @@ -21,4 +219,4 @@ All notable changes to this project will be documented in this file, in reverse
- [#3](https://github.com/zendframework/zend-servicemanager/pull/3) properly updates the
codebase to PHP 5.5, by taking advantage of the default closure binding
(`$this` in a closure is the invoking object when created within a method). It
also removes several `@requires PHP 5.4.0` annotations.
also removes several `@requires PHP 5.4.0` annotations.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# zend-servicemanager

[![Build Status](https://secure.travis-ci.org/zendframework/zend-servicemanager.svg?branch=master)](https://secure.travis-ci.org/zendframework/zend-servicemanager)
[![Coverage Status](https://coveralls.io/repos/zendframework/zend-servicemanager/badge.svg?branch=master)](https://coveralls.io/r/zendframework/zend-servicemanager?branch=master)

The Service Locator design pattern is implemented by the `Zend\ServiceManager`
component. The Service Locator is a service/object locator, tasked with
retrieving other objects.
Expand Down
73 changes: 35 additions & 38 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
{
"name": "zendframework/zend-servicemanager",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
"servicemanager"
],
"homepage": "https://github.com/zendframework/zend-servicemanager",
"autoload": {
"psr-4": {
"Zend\\ServiceManager\\": "src/"
}
},
"require": {
"php": ">=5.5"
},
"require-dev": {
"zendframework/zend-di": "~2.5",
"zendframework/zend-mvc": "~2.5",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
"suggest": {
"ocramius/proxy-manager": "ProxyManager 0.5.* to handle lazy initialization of services",
"zendframework/zend-di": "Zend\\Di component"
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "2.5-dev",
"dev-develop": "2.6-dev"
}
},
"autoload-dev": {
"psr-4": {
"ZendTest\\ServiceManager\\": "test/"
}
"name": "zendframework/zend-servicemanager",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"zf",
"servicemanager",
"service-manager"
],
"homepage": "https://github.com/zendframework/zend-servicemanager",
"autoload": {
"psr-4": {
"Zend\\ServiceManager\\": "src/"
}
},
"require": {
"php": ">=5.5",
"container-interop/container-interop": "1.*"
},
"require-dev": {
"phpunit/phpunit": "~4.6",
"ocramius/proxy-manager": "~1.0"
},
"suggest": {
"ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services"
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev",
"dev-develop": "3.1-dev"
}
},
"autoload-dev": {
"psr-4": {
"ZendTest\\ServiceManager\\": "test/"
}
}
}
Loading