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

[WIP] ServiceManager v2 + v3 compatibility #64

Closed
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
19 changes: 14 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,23 @@ matrix:
- php: 5.5
env:
- EXECUTE_CS_CHECK=true
- PECL_INSTALL_APCU='apcu-4.0.8'
- php: 5.5
env:
- SERVICE_MANAGER_V2=true
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
- PECL_INSTALL_APCU='apcu-4.0.8'
- php: 5.6
env:
- SERVICE_MANAGER_V2=true
- php: 7
- php: 7
env:
- PECL_INSTALL_APCU='apcu'
- PECL_INSTALL_APCU_BC='apcu_bc-beta'
- SERVICE_MANAGER_V2=true
- php: hhvm
- php: hhvm
env:
- SERVICE_MANAGER_V2=true
allow_failures:
- php: 7
- php: hhvm
Expand All @@ -60,11 +67,13 @@ before_install:
- if [[ $EXECUTE_TEST_COVERALLS != '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
- if [[ $SERVICE_MANAGER_V2 == 'true' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^2.7.2" ; fi
- if [[ $SERVICE_MANAGER_V2 != 'true' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^3.0" ; fi

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
- if [[ $PECL_INSTALL_APCU != '' ]]; then echo "yes\nno\n" | pecl install $PECL_INSTALL_APCU || return 0 ; fi

# see https://pear.php.net/bugs/bug.php?id=21007
# pecl install adds the "extension=*.so" directive on top of php.ini which results in wrong extension order
# -> Attach another ini file loading the extension kind of solves the issue.
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
},
"require": {
"php": ">=5.5",
"zendframework/zend-stdlib": "~2.5",
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
"zendframework/zend-eventmanager": "dev-develop as 2.7.0"
"zendframework/zend-stdlib": "~2.7",
"zendframework/zend-servicemanager": "^2.7.3 || ^3.0",
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w00t! we can use tagged versions!

},
"require-dev": {
"zendframework/zend-serializer": "dev-develop as 2.6.0",
"zendframework/zend-session": "~2.5",
"zendframework/zend-session": "dev-develop as 2.6.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to future selves: we'll want to update this to a tagged version once we've tagged a version containing the SM/EM updates.

"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
Expand Down
43 changes: 42 additions & 1 deletion src/PatternPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\Exception\InvalidServiceException;

/**
* Plugin manager implementation for cache pattern adapters
Expand Down Expand Up @@ -42,12 +43,52 @@ class PatternPluginManager extends AbstractPluginManager
/**
* Don't share by default
*
* @var array
* @var boolean
*/
protected $shareByDefault = false;

/**
* Don't share by default
*
* @var boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezimuel can you please write in the doc-block which property belongs to which service manager version as for me this looks like a mistake.

*/
protected $sharedByDefault = false;

/**
* @var string
*/
protected $instanceOf = Pattern\PatternInterface::class;

/**
* Validate the plugin is of the expected type (v3).
*
* Validates against `$instanceOf`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validate($instance)
{
if (! $instance instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'%s can only create instances of %s; %s is invalid',
get_class($this),
$this->instanceOf,
(is_object($instance) ? get_class($instance) : gettype($instance))
));
}
}

/**
* Validate the plugin is of the expected type (v2).
*
* Proxies to `validate()`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validatePlugin($instance)
{
$this->validate($instance);
}
}
18 changes: 17 additions & 1 deletion src/Service/StorageCacheAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StorageCacheAbstractServiceFactory implements AbstractFactoryInterface
* @param string $requestedName
* @return boolean
*/
public function canCreateServiceWithName(ContainerInterface $container, $requestedName)
public function canCreate(ContainerInterface $container, $requestedName)
{
$config = $this->getConfig($container);
if (empty($config)) {
Expand All @@ -44,6 +44,17 @@ public function canCreateServiceWithName(ContainerInterface $container, $request
return (isset($config[$requestedName]) && is_array($config[$requestedName]));
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return boolean
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this->canCreate($serviceLocator, $requestedName);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty line following this new method. (CS)


/**
* Create an object
*
Expand All @@ -58,6 +69,11 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
return StorageFactory::factory($config[$requestedName]);
}

public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this($serviceLocator, $requestedName);
}

/**
* Retrieve cache configuration, if any
*
Expand Down
40 changes: 40 additions & 0 deletions src/Storage/AdapterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,48 @@ class AdapterPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

/**
* Don't share by default
*
* @var boolean
*/
protected $shareByDefault = false;

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

* @var string
*/
protected $instanceOf = StorageInterface::class;

/**
* Validate the plugin is of the expected type (v3).
*
* Validates against `$instanceOf`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validate($instance)
{
if (! $instance instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'%s can only create instances of %s; %s is invalid',
get_class($this),
$this->instanceOf,
(is_object($instance) ? get_class($instance) : gettype($instance))
));
}
}

/**
* Validate the plugin is of the expected type (v2).
*
* Proxies to `validate()`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validatePlugin($instance)
{
$this->validate($instance);
}
}