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

ServiceManager v2-v3 compatibility #68

Closed
wants to merge 8 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
20 changes: 19 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,31 @@ matrix:
env:
- EXECUTE_CS_CHECK=true
- PECL_INSTALL_APCU='apcu-4.0.8'
- php: 5.5
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
- PECL_INSTALL_APCU='apcu-4.0.8'
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
- PECL_INSTALL_APCU='apcu-4.0.8'
- php: 5.6
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
- PECL_INSTALL_APCU='apcu-4.0.8'
- php: 7
env:
- PECL_INSTALL_APCU='apcu'
- PECL_INSTALL_APCU_BC='apcu_bc-beta'
- php: 7
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
- PECL_INSTALL_APCU='apcu'
- PECL_INSTALL_APCU_BC='apcu_bc-beta'
- php: hhvm
- php: hhvm
env:
- SERVICE_MANAGER_VERSION="^2.7.5"
allow_failures:
- php: 7
- php: hhvm
Expand All @@ -60,11 +76,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_VERSION != '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:$SERVICE_MANAGER_VERSION" ; fi
- if [[ $SERVICE_MANAGER_VERSION == '' ]]; 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
10 changes: 5 additions & 5 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.5 || ^3.0",
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0"
},
"require-dev": {
"zendframework/zend-serializer": "dev-develop as 2.6.0",
"zendframework/zend-session": "~2.5",
"zendframework/zend-serializer": "^2.6.0",
"zendframework/zend-session": "dev-develop as 2.6.0",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
Expand Down
71 changes: 63 additions & 8 deletions 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 All @@ -23,31 +24,85 @@ class PatternPluginManager extends AbstractPluginManager
{
protected $aliases = [
'callback' => Pattern\CallbackCache::class,
'Callback' => Pattern\CallbackCache::class,
'capture' => Pattern\CaptureCache::class,
'Capture' => Pattern\CaptureCache::class,
'class' => Pattern\ClassCache::class,
'Class' => Pattern\ClassCache::class,
'object' => Pattern\ObjectCache::class,
'Object' => Pattern\ObjectCache::class,
'output' => Pattern\OutputCache::class,
'page' => Pattern\PageCache::class,
'Output' => Pattern\OutputCache::class,
];

protected $factories = [
Pattern\CallbackCache::class => InvokableFactory::class,
Pattern\CaptureCache::class => InvokableFactory::class,
Pattern\ClassCache::class => InvokableFactory::class,
Pattern\ObjectCache::class => InvokableFactory::class,
Pattern\OutputCache::class => InvokableFactory::class,
Pattern\PageCache::class => InvokableFactory::class,
Pattern\CallbackCache::class => InvokableFactory::class,
Pattern\CaptureCache::class => InvokableFactory::class,
Pattern\ClassCache::class => InvokableFactory::class,
Pattern\ObjectCache::class => InvokableFactory::class,
Pattern\OutputCache::class => InvokableFactory::class,

// v2 normalized FQCNs
'zendcachepatterncallbackcache' => InvokableFactory::class,
'zendcachepatterncapturecache' => InvokableFactory::class,
'zendcachepatternclasscache' => InvokableFactory::class,
'zendcachepatternobjectcache' => InvokableFactory::class,
'zendcachepatternoutputcache' => InvokableFactory::class,
];

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

/**
* Don't share by default
*
* @var boolean
*/
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 $plugin
* @throws Exception\RuntimeException if invalid
*/
public function validatePlugin($plugin)
{
try {
$this->validate($plugin);
} catch (InvalidServiceException $e) {
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
}
21 changes: 19 additions & 2 deletions src/Service/StorageCacheAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
namespace Zend\Cache\Service;

use Zend\Cache\StorageFactory;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
use Zend\ServiceManager\AbstractFactoryInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Storage cache factory for multiple caches.
Expand All @@ -35,7 +36,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 +45,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);
}

/**
* Create an object
*
Expand All @@ -58,6 +70,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
9 changes: 8 additions & 1 deletion src/Service/StorageCacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

namespace Zend\Cache\Service;

use Zend\Cache\Storage\StorageInterface;
use Zend\Cache\StorageFactory;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\FactoryInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Storage cache factory.
Expand All @@ -24,4 +26,9 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$cacheConfig = isset($config['cache']) ? $config['cache'] : [];
return StorageFactory::factory($cacheConfig);
}

public function createService(ServiceLocatorInterface $serviceLocator)
{
return $this($serviceLocator, StorageInterface::class);
}
}
100 changes: 84 additions & 16 deletions src/Storage/AdapterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace Zend\Cache\Storage;

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

/**
Expand All @@ -25,6 +27,7 @@ class AdapterPluginManager extends AbstractPluginManager
'apc' => Adapter\Apc::class,
'Apc' => Adapter\Apc::class,
'blackhole' => Adapter\BlackHole::class,
'blackHole' => Adapter\BlackHole::class,
'BlackHole' => Adapter\BlackHole::class,
'dba' => Adapter\Dba::class,
'Dba' => Adapter\Dba::class,
Expand All @@ -37,47 +40,112 @@ class AdapterPluginManager extends AbstractPluginManager
'memory' => Adapter\Memory::class,
'Memory' => Adapter\Memory::class,
'mongodb' => Adapter\MongoDb::class,
'mongoDb' => Adapter\MongoDb::class,
'MongoDb' => Adapter\MongoDb::class,
'redis' => Adapter\Redis::class,
'Redis' => Adapter\Redis::class,
'session' => Adapter\Session::class,
'Session' => Adapter\Session::class,
'xcache' => Adapter\XCache::class,
'xCache' => Adapter\XCache::class,
'XCache' => Adapter\XCache::class,
'wincache' => Adapter\WinCache::class,
'winCache' => Adapter\WinCache::class,
'WinCache' => Adapter\WinCache::class,
'zendserverdisk' => Adapter\ZendServerDisk::class,
'zendServerDisk' => Adapter\ZendServerDisk::class,
'ZendServerDisk' => Adapter\ZendServerDisk::class,
'zendservershm' => Adapter\ZendServerShm::class,
'ZendServerShm' => Adapter\ZendServerShm::class
'zendServerShm' => Adapter\ZendServerShm::class,
'ZendServerShm' => Adapter\ZendServerShm::class,
];

protected $factories = [
Adapter\Apc::class => InvokableFactory::class,
Adapter\BlackHole::class => InvokableFactory::class,
Adapter\Dba::class => InvokableFactory::class,
Adapter\Filesystem::class => InvokableFactory::class,
Adapter\Memcache::class => InvokableFactory::class,
Adapter\Memcached::class => InvokableFactory::class,
Adapter\Memory::class => InvokableFactory::class,
Adapter\MongoDb::class => InvokableFactory::class,
Adapter\Redis::class => InvokableFactory::class,
Adapter\Session::class => InvokableFactory::class,
Adapter\XCache::class => InvokableFactory::class,
Adapter\WinCache::class => InvokableFactory::class,
Adapter\ZendServerDisk::class => InvokableFactory::class,
Adapter\ZendServerShm::class => InvokableFactory::class
Adapter\Apc::class => InvokableFactory::class,
Adapter\BlackHole::class => InvokableFactory::class,
Adapter\Dba::class => InvokableFactory::class,
Adapter\Filesystem::class => InvokableFactory::class,
Adapter\Memcache::class => InvokableFactory::class,
Adapter\Memcached::class => InvokableFactory::class,
Adapter\Memory::class => InvokableFactory::class,
Adapter\MongoDb::class => InvokableFactory::class,
Adapter\Redis::class => InvokableFactory::class,
Adapter\Session::class => InvokableFactory::class,
Adapter\WinCache::class => InvokableFactory::class,
Adapter\XCache::class => InvokableFactory::class,
Adapter\ZendServerDisk::class => InvokableFactory::class,
Adapter\ZendServerShm::class => InvokableFactory::class,

// v2 normalized FQCNs
'zendcachestorageadapterapc' => InvokableFactory::class,
'zendcachestorageadapterblackhole' => InvokableFactory::class,
'zendcachestorageadapterdba' => InvokableFactory::class,
'zendcachestorageadapterfilesystem' => InvokableFactory::class,
'zendcachestorageadaptermemcache' => InvokableFactory::class,
'zendcachestorageadaptermemcached' => InvokableFactory::class,
'zendcachestorageadaptermemory' => InvokableFactory::class,
'zendcachestorageadaptermongodb' => InvokableFactory::class,
'zendcachestorageadapterredis' => InvokableFactory::class,
'zendcachestorageadaptersession' => InvokableFactory::class,
'zendcachestorageadapterwincache' => InvokableFactory::class,
'zendcachestorageadapterxcache' => InvokableFactory::class,
'zendcachestorageadapterzendserverdisk' => InvokableFactory::class,
'zendcachestorageadapterzendservershm' => InvokableFactory::class,
];

/**
* Do not share by default
* Do not share by default (v3)
*
* @var array
*/
protected $sharedByDefault = false;

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

/**
* @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)
{
try {
$this->validate($instance);
} catch (InvalidServiceException $e) {
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
}
Loading