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

Commit cec6ebb

Browse files
committed
First code refactor for new zend service and event manager
1 parent 16fe199 commit cec6ebb

18 files changed

+249
-129
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"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

+2-20
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,7 @@ class PatternPluginManager extends AbstractPluginManager
4747
protected $shareByDefault = false;
4848

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

src/Service/StorageCacheAbstractServiceFactory.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ public function canCreateServiceWithName(ContainerInterface $container, $request
4141
if (empty($config)) {
4242
return false;
4343
}
44-
4544
return (isset($config[$requestedName]) && is_array($config[$requestedName]));
4645
}
4746

47+
/**
48+
* Create an object
49+
*
50+
* @param ContainerInterface $container
51+
* @param string $requestedName
52+
* @param null|array $options
53+
* @return object
54+
*/
4855
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
4956
{
5057
$config = $this->getConfig($container);
51-
$config = $config[$requestedName];
52-
return StorageFactory::factory($config);
58+
return StorageFactory::factory($config[$requestedName]);
5359
}
5460

5561
/**

src/Storage/Adapter/AbstractAdapter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function getEventManager()
204204
*/
205205
protected function triggerPre($eventName, ArrayObject $args)
206206
{
207-
return $this->getEventManager()->trigger(new Event($eventName . '.pre', $this, $args));
207+
return $this->getEventManager()->triggerEvent(new Event($eventName . '.pre', $this, $args));
208208
}
209209

210210
/**
@@ -241,7 +241,7 @@ protected function triggerPost($eventName, ArrayObject $args, & $result)
241241
protected function triggerException($eventName, ArrayObject $args, & $result, \Exception $exception)
242242
{
243243
$exceptionEvent = new ExceptionEvent($eventName . '.exception', $this, $args, $result, $exception);
244-
$eventRs = $this->getEventManager()->trigger($exceptionEvent);
244+
$eventRs = $this->getEventManager()->triggerEvent($exceptionEvent);
245245

246246
if ($exceptionEvent->getThrowException()) {
247247
throw $exceptionEvent->getException();

src/Storage/AdapterPluginManager.php

+17-21
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,33 @@ class AdapterPluginManager extends AbstractPluginManager
2424
{
2525
protected $aliases = [
2626
'apc' => Adapter\Apc::class,
27+
'Apc' => Adapter\Apc::class,
2728
'blackhole' => Adapter\BlackHole::class,
29+
'BlackHole' => Adapter\BlackHole::class,
2830
'dba' => Adapter\Dba::class,
31+
'Dba' => Adapter\Dba::class,
2932
'filesystem' => Adapter\Filesystem::class,
33+
'Filesystem' => Adapter\Filesystem::class,
3034
'memcache' => Adapter\Memcache::class,
35+
'Memcache' => Adapter\Memcache::class,
3136
'memcached' => Adapter\Memcached::class,
37+
'Memcached' => Adapter\Memcached::class,
3238
'memory' => Adapter\Memory::class,
39+
'Memory' => Adapter\Memory::class,
3340
'mongodb' => Adapter\MongoDb::class,
41+
'MongoDb' => Adapter\MongoDb::class,
3442
'redis' => Adapter\Redis::class,
43+
'Redis' => Adapter\Redis::class,
3544
'session' => Adapter\Session::class,
45+
'Session' => Adapter\Session::class,
3646
'xcache' => Adapter\XCache::class,
47+
'XCache' => Adapter\XCache::class,
3748
'wincache' => Adapter\WinCache::class,
49+
'WinCache' => Adapter\WinCache::class,
3850
'zendserverdisk' => Adapter\ZendServerDisk::class,
39-
'zendservershm' => Adapter\ZendServerShm::class
51+
'ZendServerDisk' => Adapter\ZendServerDisk::class,
52+
'zendservershm' => Adapter\ZendServerShm::class,
53+
'ZendServerShm' => Adapter\ZendServerShm::class
4054
];
4155

4256
protected $factories = [
@@ -64,25 +78,7 @@ class AdapterPluginManager extends AbstractPluginManager
6478
protected $shareByDefault = false;
6579

6680
/**
67-
* Validate the plugin
68-
*
69-
* Checks that the adapter loaded is an instance of StorageInterface.
70-
*
71-
* @param mixed $plugin
72-
* @return void
73-
* @throws Exception\RuntimeException if invalid
81+
* @var string
7482
*/
75-
public function validatePlugin($plugin)
76-
{
77-
if ($plugin instanceof StorageInterface) {
78-
// we're okay
79-
return;
80-
}
81-
82-
throw new Exception\RuntimeException(sprintf(
83-
'Plugin of type %s is invalid; must implement %s\StorageInterface',
84-
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
85-
__NAMESPACE__
86-
));
87-
}
83+
protected $instanceOf = StorageInterface::class;
8884
}

src/Storage/PluginManager.php

+17-30
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@
2323
class PluginManager extends AbstractPluginManager
2424
{
2525
protected $aliases = [
26-
'clearexpiredbyfactor' => Storage\Plugin\ClearExpiredByFactor::class,
27-
'exceptionhandler' => Storage\Plugin\ExceptionHandler::class,
28-
'ignoreuserabort' => Storage\Plugin\IgnoreUserAbort::class,
29-
'optimizebyfactor' => Storage\Plugin\OptimizeByFactor::class,
30-
'serializer' => Storage\Plugin\Serializer::class,
26+
'clearexpiredbyfactor' => Plugin\ClearExpiredByFactor::class,
27+
'ClearExpiredByFactor' => Plugin\ClearExpiredByFactor::class,
28+
'exceptionhandler' => Plugin\ExceptionHandler::class,
29+
'ExceptionHandler' => Plugin\ExceptionHandler::class,
30+
'ignoreuserabort' => Plugin\IgnoreUserAbort::class,
31+
'IgnoreUserAbort' => Plugin\IgnoreUserAbort::class,
32+
'optimizebyfactor' => Plugin\OptimizeByFactor::class,
33+
'OptimizeByFactor' => Plugin\OptimizeByFactor::class,
34+
'serializer' => Plugin\Serializer::class,
35+
'Serializer' => Plugin\Serializer::class
3136
];
3237

3338
protected $factories = [
34-
Storage\Plugin\ClearExpiredByFactor::class => InvokableFactory::class,
35-
Storage\Plugin\ExceptionHandler::class => InvokableFactory::class,
36-
Storage\Plugin\IgnoreUserAbort::class => InvokableFactory::class,
37-
Storage\Plugin\OptimizeByFactor::class => InvokableFactory::class,
38-
Storage\Plugin\Serializer::class => InvokableFactory::class
39+
Plugin\ClearExpiredByFactor::class => InvokableFactory::class,
40+
Plugin\ExceptionHandler::class => InvokableFactory::class,
41+
Plugin\IgnoreUserAbort::class => InvokableFactory::class,
42+
Plugin\OptimizeByFactor::class => InvokableFactory::class,
43+
Plugin\Serializer::class => InvokableFactory::class
3944
];
4045

4146
/**
@@ -46,25 +51,7 @@ class PluginManager extends AbstractPluginManager
4651
protected $shareByDefault = false;
4752

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

src/StorageFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static function pluginFactory($pluginName, $options = [])
220220
public static function getPluginManager()
221221
{
222222
if (static::$plugins === null) {
223-
static::$plugins = new Storage\PluginManager();
223+
static::$plugins = new Storage\PluginManager(new ServiceManager);
224224
}
225225
return static::$plugins;
226226
}
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Cache;
11+
12+
use ReflectionProperty;
13+
use Zend\EventManager\EventManager;
14+
15+
/**
16+
* Offer methods for introspecting event manager events and listeners.
17+
*/
18+
trait EventManagerIntrospectionTrait
19+
{
20+
/**
21+
* Retrieve a list of event names from an event manager.
22+
*
23+
* @param EventManager $events
24+
* @return string[]
25+
*/
26+
private function getEventsFromEventManager(EventManager $events)
27+
{
28+
$r = new ReflectionProperty($events, 'events');
29+
$r->setAccessible(true);
30+
$listeners = $r->getValue($events);
31+
return array_keys($listeners);
32+
}
33+
34+
/**
35+
* Retrieve an interable list of listeners for an event.
36+
*
37+
* Given an event and an event manager, returns an iterator with the
38+
* listeners for that event, in priority order.
39+
*
40+
* If $withPriority is true, the key values will be the priority at which
41+
* the given listener is attached.
42+
*
43+
* Do not pass $withPriority if you want to cast the iterator to an array,
44+
* as many listeners will likely have the same priority, and thus casting
45+
* will collapse to the last added.
46+
*
47+
* @param string $event
48+
* @param EventManager $events
49+
* @param bool $withPriority
50+
* @return \Traversable
51+
*/
52+
private function getListenersForEvent($event, EventManager $events, $withPriority = false)
53+
{
54+
$r = new ReflectionProperty($events, 'events');
55+
$r->setAccessible(true);
56+
$listeners = $r->getValue($events);
57+
58+
if (! isset($listeners[$event])) {
59+
return $this->traverseListeners([]);
60+
}
61+
62+
return $this->traverseListeners($listeners[$event], $withPriority);
63+
}
64+
65+
/**
66+
* Assert that a given listener exists at the specified priority.
67+
*
68+
* @param callable $expectedListener
69+
* @param int $expectedPriority
70+
* @param string $event
71+
* @param EventManager $events
72+
* @param string $message Failure message to use, if any.
73+
*/
74+
private function assertListenerAtPriority(
75+
callable $expectedListener,
76+
$expectedPriority,
77+
$event,
78+
EventManager $events,
79+
$message = ''
80+
) {
81+
$message = $message ?: sprintf(
82+
'Listener not found for event "%s" and priority %d',
83+
$event,
84+
$expectedPriority
85+
);
86+
$listeners = $this->getListenersForEvent($event, $events, true);
87+
$found = false;
88+
foreach ($listeners as $priority => $listener) {
89+
if ($listener === $expectedListener
90+
&& $priority === $expectedPriority
91+
) {
92+
$found = true;
93+
break;
94+
}
95+
}
96+
$this->assertTrue($found, $message);
97+
}
98+
99+
/**
100+
* Returns an indexed array of listeners for an event.
101+
*
102+
* Returns an indexed array of listeners for an event, in priority order.
103+
* Priority values will not be included; use this only for testing if
104+
* specific listeners are present, or for a count of listeners.
105+
*
106+
* @param string $event
107+
* @param EventManager $events
108+
* @return callable[]
109+
*/
110+
private function getArrayOfListenersForEvent($event, EventManager $events)
111+
{
112+
return iterator_to_array($this->getListenersForEvent($event, $events));
113+
}
114+
115+
/**
116+
* Generator for traversing listeners in priority order.
117+
*
118+
* @param array $listeners
119+
* @param bool $withPriority When true, yields priority as key.
120+
*/
121+
public function traverseListeners(array $queue, $withPriority = false)
122+
{
123+
krsort($queue, SORT_NUMERIC);
124+
125+
foreach ($queue as $priority => $listeners) {
126+
$priority = (int) $priority;
127+
foreach ($listeners as $listener) {
128+
if ($withPriority) {
129+
yield $priority => $listener;
130+
} else {
131+
yield $listener;
132+
}
133+
}
134+
}
135+
}
136+
}

test/PatternFactoryTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public function testDefaultPluginManager()
3434

3535
public function testChangePluginManager()
3636
{
37-
$plugins = new Cache\PatternPluginManager();
37+
$plugins = new Cache\PatternPluginManager(
38+
$this->getMockBuilder('Interop\Container\ContainerInterface')->getMock()
39+
);
3840
Cache\PatternFactory::setPluginManager($plugins);
3941
$this->assertSame($plugins, Cache\PatternFactory::getPluginManager());
4042
}

0 commit comments

Comments
 (0)