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

Commit 5967e0b

Browse files
committed
Merge pull request #22 from ezimuel/feature/refactor-service-event-manager
[WIP] Refactor with new service and event manager
2 parents 35181fc + cec6ebb commit 5967e0b

21 files changed

+342
-187
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
"require": {
1616
"php": ">=5.5",
1717
"zendframework/zend-stdlib": "~2.5",
18-
"zendframework/zend-servicemanager": "~2.5",
19-
"zendframework/zend-eventmanager": "~2.5"
18+
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
19+
"zendframework/zend-eventmanager": "dev-develop as 2.7.0"
2020
},
2121
"require-dev": {
22-
"zendframework/zend-serializer": "~2.5",
22+
"zendframework/zend-serializer": "dev-develop as 2.6.0",
2323
"zendframework/zend-session": "~2.5",
2424
"fabpot/php-cs-fixer": "1.7.*",
2525
"phpunit/PHPUnit": "~4.0"

src/PatternFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Traversable;
1313
use Zend\Stdlib\ArrayUtils;
14+
use Zend\ServiceManager\ServiceManager;
1415

1516
abstract class PatternFactory
1617
{
@@ -63,7 +64,7 @@ public static function factory($patternName, $options = [])
6364
public static function getPluginManager()
6465
{
6566
if (static::$plugins === null) {
66-
static::$plugins = new PatternPluginManager();
67+
static::$plugins = new PatternPluginManager(new ServiceManager);
6768
}
6869

6970
return static::$plugins;

src/PatternPluginManager.php

+19-32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Zend\Cache;
1111

1212
use Zend\ServiceManager\AbstractPluginManager;
13+
use Zend\ServiceManager\Factory\InvokableFactory;
1314

1415
/**
1516
* Plugin manager implementation for cache pattern adapters
@@ -20,18 +21,22 @@
2021
*/
2122
class PatternPluginManager extends AbstractPluginManager
2223
{
23-
/**
24-
* Default set of adapters
25-
*
26-
* @var array
27-
*/
28-
protected $invokableClasses = [
29-
'callback' => 'Zend\Cache\Pattern\CallbackCache',
30-
'capture' => 'Zend\Cache\Pattern\CaptureCache',
31-
'class' => 'Zend\Cache\Pattern\ClassCache',
32-
'object' => 'Zend\Cache\Pattern\ObjectCache',
33-
'output' => 'Zend\Cache\Pattern\OutputCache',
34-
'page' => 'Zend\Cache\Pattern\PageCache',
24+
protected $aliases = [
25+
'callback' => Pattern\CallbackCache::class,
26+
'capture' => Pattern\CaptureCache::class,
27+
'class' => Pattern\ClassCache::class,
28+
'object' => Pattern\ObjectCache::class,
29+
'output' => Pattern\OutputCache::class,
30+
'page' => Pattern\PageCache::class,
31+
];
32+
33+
protected $factories = [
34+
Pattern\CallbackCache::class => InvokableFactory::class,
35+
Pattern\CaptureCache::class => InvokableFactory::class,
36+
Pattern\ClassCache::class => InvokableFactory::class,
37+
Pattern\ObjectCache::class => InvokableFactory::class,
38+
Pattern\OutputCache::class => InvokableFactory::class,
39+
Pattern\PageCache::class => InvokableFactory::class,
3540
];
3641

3742
/**
@@ -42,25 +47,7 @@ class PatternPluginManager extends AbstractPluginManager
4247
protected $shareByDefault = false;
4348

4449
/**
45-
* Validate the plugin
46-
*
47-
* Checks that the pattern adapter loaded is an instance of Pattern\PatternInterface.
48-
*
49-
* @param mixed $plugin
50-
* @return void
51-
* @throws Exception\RuntimeException if invalid
50+
* @var string
5251
*/
53-
public function validatePlugin($plugin)
54-
{
55-
if ($plugin instanceof Pattern\PatternInterface) {
56-
// we're okay
57-
return;
58-
}
59-
60-
throw new Exception\RuntimeException(sprintf(
61-
'Plugin of type %s is invalid; must implement %s\Pattern\PatternInterface',
62-
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
63-
__NAMESPACE__
64-
));
65-
}
52+
protected $instanceOf = Pattern\PatternInterface::class;
6653
}

src/Service/StorageCacheAbstractServiceFactory.php

+20-21
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
namespace Zend\Cache\Service;
1111

1212
use Zend\Cache\StorageFactory;
13-
use Zend\ServiceManager\AbstractFactoryInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
13+
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
14+
use Interop\Container\ContainerInterface;
1515

1616
/**
1717
* Storage cache factory for multiple caches.
@@ -31,52 +31,51 @@ class StorageCacheAbstractServiceFactory implements AbstractFactoryInterface
3131
protected $configKey = 'caches';
3232

3333
/**
34-
* @param ServiceLocatorInterface $services
35-
* @param string $name
36-
* @param string $requestedName
37-
* @return bool
34+
* @param ContainerInterface $container
35+
* @param string $requestedName
36+
* @return boolean
3837
*/
39-
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
38+
public function canCreateServiceWithName(ContainerInterface $container, $requestedName)
4039
{
41-
$config = $this->getConfig($services);
40+
$config = $this->getConfig($container);
4241
if (empty($config)) {
4342
return false;
4443
}
45-
4644
return (isset($config[$requestedName]) && is_array($config[$requestedName]));
4745
}
4846

4947
/**
50-
* @param ServiceLocatorInterface $services
51-
* @param string $name
52-
* @param string $requestedName
53-
* @return \Zend\Cache\Storage\StorageInterface
48+
* Create an object
49+
*
50+
* @param ContainerInterface $container
51+
* @param string $requestedName
52+
* @param null|array $options
53+
* @return object
5454
*/
55-
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
55+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
5656
{
57-
$config = $this->getConfig($services);
58-
$config = $config[$requestedName];
59-
return StorageFactory::factory($config);
57+
$config = $this->getConfig($container);
58+
return StorageFactory::factory($config[$requestedName]);
6059
}
6160

6261
/**
6362
* Retrieve cache configuration, if any
6463
*
65-
* @param ServiceLocatorInterface $services
64+
* @param ContainerInterface $container
6665
* @return array
6766
*/
68-
protected function getConfig(ServiceLocatorInterface $services)
67+
protected function getConfig(ContainerInterface $container)
6968
{
7069
if ($this->config !== null) {
7170
return $this->config;
7271
}
7372

74-
if (!$services->has('Config')) {
73+
if (!$container->has('Config')) {
7574
$this->config = [];
7675
return $this->config;
7776
}
7877

79-
$config = $services->get('Config');
78+
$config = $container->get('Config');
8079
if (!isset($config[$this->configKey])) {
8180
$this->config = [];
8281
return $this->config;

src/Service/StorageCacheFactory.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@
1010
namespace Zend\Cache\Service;
1111

1212
use Zend\Cache\StorageFactory;
13-
use Zend\ServiceManager\FactoryInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
13+
use Zend\ServiceManager\Factory\FactoryInterface;
14+
use Interop\Container\ContainerInterface;
1515

1616
/**
1717
* Storage cache factory.
1818
*/
1919
class StorageCacheFactory implements FactoryInterface
2020
{
21-
public function createService(ServiceLocatorInterface $serviceLocator)
21+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
2222
{
23-
// Configure the cache
24-
$config = $serviceLocator->get('Config');
23+
$config = $container->get('Config');
2524
$cacheConfig = isset($config['cache']) ? $config['cache'] : [];
26-
$cache = StorageFactory::factory($cacheConfig);
27-
28-
return $cache;
25+
return StorageFactory::factory($cacheConfig);
2926
}
3027
}

src/Storage/Adapter/AbstractAdapter.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Zend\EventManager\EventManager;
2424
use Zend\EventManager\EventManagerInterface;
2525
use Zend\EventManager\EventsCapableInterface;
26+
use Zend\EventManager\SharedEventManager;
2627

2728
abstract class AbstractAdapter implements StorageInterface, EventsCapableInterface
2829
{
@@ -125,7 +126,8 @@ public function setOptions($options)
125126
$this->options = $options;
126127

127128
$event = new Event('option', $this, new ArrayObject($options->toArray()));
128-
$this->getEventManager()->trigger($event);
129+
130+
$this->getEventManager()->triggerEvent($event);
129131
}
130132
return $this;
131133
}
@@ -188,7 +190,7 @@ public function getCaching()
188190
public function getEventManager()
189191
{
190192
if ($this->events === null) {
191-
$this->events = new EventManager([__CLASS__, get_class($this)]);
193+
$this->events = new EventManager(new SharedEventManager(), [__CLASS__, get_class($this)]);
192194
}
193195
return $this->events;
194196
}
@@ -202,7 +204,7 @@ public function getEventManager()
202204
*/
203205
protected function triggerPre($eventName, ArrayObject $args)
204206
{
205-
return $this->getEventManager()->trigger(new Event($eventName . '.pre', $this, $args));
207+
return $this->getEventManager()->triggerEvent(new Event($eventName . '.pre', $this, $args));
206208
}
207209

208210
/**
@@ -216,7 +218,7 @@ protected function triggerPre($eventName, ArrayObject $args)
216218
protected function triggerPost($eventName, ArrayObject $args, & $result)
217219
{
218220
$postEvent = new PostEvent($eventName . '.post', $this, $args, $result);
219-
$eventRs = $this->getEventManager()->trigger($postEvent);
221+
$eventRs = $this->getEventManager()->triggerEvent($postEvent);
220222

221223
return $eventRs->stopped()
222224
? $eventRs->last()
@@ -239,7 +241,7 @@ protected function triggerPost($eventName, ArrayObject $args, & $result)
239241
protected function triggerException($eventName, ArrayObject $args, & $result, \Exception $exception)
240242
{
241243
$exceptionEvent = new ExceptionEvent($eventName . '.exception', $this, $args, $result, $exception);
242-
$eventRs = $this->getEventManager()->trigger($exceptionEvent);
244+
$eventRs = $this->getEventManager()->triggerEvent($exceptionEvent);
243245

244246
if ($exceptionEvent->getThrowException()) {
245247
throw $exceptionEvent->getException();

src/Storage/Adapter/AdapterOptions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected function triggerOptionEvent($optionName, $optionValue)
235235
{
236236
if ($this->adapter instanceof EventsCapableInterface) {
237237
$event = new Event('option', $this->adapter, new ArrayObject([$optionName => $optionValue]));
238-
$this->adapter->getEventManager()->trigger($event);
238+
$this->adapter->getEventManager()->triggerEvent($event);
239239
}
240240
}
241241

src/Storage/AdapterPluginManager.php

+49-40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Zend\Cache\Exception;
1313
use Zend\ServiceManager\AbstractPluginManager;
14+
use Zend\ServiceManager\Factory\InvokableFactory;
1415

1516
/**
1617
* Plugin manager implementation for cache storage adapters
@@ -21,26 +22,52 @@
2122
*/
2223
class AdapterPluginManager extends AbstractPluginManager
2324
{
24-
/**
25-
* Default set of adapters
26-
*
27-
* @var array
28-
*/
29-
protected $invokableClasses = [
30-
'apc' => 'Zend\Cache\Storage\Adapter\Apc',
31-
'blackhole' => 'Zend\Cache\Storage\Adapter\BlackHole',
32-
'dba' => 'Zend\Cache\Storage\Adapter\Dba',
33-
'filesystem' => 'Zend\Cache\Storage\Adapter\Filesystem',
34-
'memcache' => 'Zend\Cache\Storage\Adapter\Memcache',
35-
'memcached' => 'Zend\Cache\Storage\Adapter\Memcached',
36-
'memory' => 'Zend\Cache\Storage\Adapter\Memory',
37-
'mongodb' => 'Zend\Cache\Storage\Adapter\MongoDb',
38-
'redis' => 'Zend\Cache\Storage\Adapter\Redis',
39-
'session' => 'Zend\Cache\Storage\Adapter\Session',
40-
'xcache' => 'Zend\Cache\Storage\Adapter\XCache',
41-
'wincache' => 'Zend\Cache\Storage\Adapter\WinCache',
42-
'zendserverdisk' => 'Zend\Cache\Storage\Adapter\ZendServerDisk',
43-
'zendservershm' => 'Zend\Cache\Storage\Adapter\ZendServerShm',
25+
protected $aliases = [
26+
'apc' => Adapter\Apc::class,
27+
'Apc' => Adapter\Apc::class,
28+
'blackhole' => Adapter\BlackHole::class,
29+
'BlackHole' => Adapter\BlackHole::class,
30+
'dba' => Adapter\Dba::class,
31+
'Dba' => Adapter\Dba::class,
32+
'filesystem' => Adapter\Filesystem::class,
33+
'Filesystem' => Adapter\Filesystem::class,
34+
'memcache' => Adapter\Memcache::class,
35+
'Memcache' => Adapter\Memcache::class,
36+
'memcached' => Adapter\Memcached::class,
37+
'Memcached' => Adapter\Memcached::class,
38+
'memory' => Adapter\Memory::class,
39+
'Memory' => Adapter\Memory::class,
40+
'mongodb' => Adapter\MongoDb::class,
41+
'MongoDb' => Adapter\MongoDb::class,
42+
'redis' => Adapter\Redis::class,
43+
'Redis' => Adapter\Redis::class,
44+
'session' => Adapter\Session::class,
45+
'Session' => Adapter\Session::class,
46+
'xcache' => Adapter\XCache::class,
47+
'XCache' => Adapter\XCache::class,
48+
'wincache' => Adapter\WinCache::class,
49+
'WinCache' => Adapter\WinCache::class,
50+
'zendserverdisk' => Adapter\ZendServerDisk::class,
51+
'ZendServerDisk' => Adapter\ZendServerDisk::class,
52+
'zendservershm' => Adapter\ZendServerShm::class,
53+
'ZendServerShm' => Adapter\ZendServerShm::class
54+
];
55+
56+
protected $factories = [
57+
Adapter\Apc::class => InvokableFactory::class,
58+
Adapter\BlackHole::class => InvokableFactory::class,
59+
Adapter\Dba::class => InvokableFactory::class,
60+
Adapter\Filesystem::class => InvokableFactory::class,
61+
Adapter\Memcache::class => InvokableFactory::class,
62+
Adapter\Memcached::class => InvokableFactory::class,
63+
Adapter\Memory::class => InvokableFactory::class,
64+
Adapter\MongoDb::class => InvokableFactory::class,
65+
Adapter\Redis::class => InvokableFactory::class,
66+
Adapter\Session::class => InvokableFactory::class,
67+
Adapter\XCache::class => InvokableFactory::class,
68+
Adapter\WinCache::class => InvokableFactory::class,
69+
Adapter\ZendServerDisk::class => InvokableFactory::class,
70+
Adapter\ZendServerShm::class => InvokableFactory::class
4471
];
4572

4673
/**
@@ -51,25 +78,7 @@ class AdapterPluginManager extends AbstractPluginManager
5178
protected $shareByDefault = false;
5279

5380
/**
54-
* Validate the plugin
55-
*
56-
* Checks that the adapter loaded is an instance of StorageInterface.
57-
*
58-
* @param mixed $plugin
59-
* @return void
60-
* @throws Exception\RuntimeException if invalid
81+
* @var string
6182
*/
62-
public function validatePlugin($plugin)
63-
{
64-
if ($plugin instanceof StorageInterface) {
65-
// we're okay
66-
return;
67-
}
68-
69-
throw new Exception\RuntimeException(sprintf(
70-
'Plugin of type %s is invalid; must implement %s\StorageInterface',
71-
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
72-
__NAMESPACE__
73-
));
74-
}
83+
protected $instanceOf = StorageInterface::class;
7584
}

0 commit comments

Comments
 (0)