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

[WIP] Refactor with new service and event manager #22

Merged
Changes from 1 commit
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
Prev Previous commit
First code refactor for new zend service and event manager
  • Loading branch information
ezimuel committed Oct 23, 2015

Verified

This commit was signed with the committer’s verified signature. The key has expired.
kylekurz Kyle Kurz
commit cec6ebbd420e0455fc0aeaacf91935a96a5f351b
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
"zendframework/zend-eventmanager": "dev-develop as 2.7.0"
},
"require-dev": {
"zendframework/zend-serializer": "~2.5",
"zendframework/zend-serializer": "dev-develop as 2.6.0",
"zendframework/zend-session": "~2.5",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
3 changes: 2 additions & 1 deletion src/PatternFactory.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\ServiceManager\ServiceManager;

abstract class PatternFactory
{
@@ -63,7 +64,7 @@ public static function factory($patternName, $options = [])
public static function getPluginManager()
{
if (static::$plugins === null) {
static::$plugins = new PatternPluginManager();
static::$plugins = new PatternPluginManager(new ServiceManager);
}

return static::$plugins;
22 changes: 2 additions & 20 deletions src/PatternPluginManager.php
Original file line number Diff line number Diff line change
@@ -47,25 +47,7 @@ class PatternPluginManager extends AbstractPluginManager
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.

Ugh - found another slight BC break in SM v3: this becomes $sharedByDefault (note shared vs share). That change fixes a few errors.


/**
* Validate the plugin
*
* Checks that the pattern adapter loaded is an instance of Pattern\PatternInterface.
*
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException if invalid
* @var string
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof Pattern\PatternInterface) {
// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s\Pattern\PatternInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
}
protected $instanceOf = Pattern\PatternInterface::class;
}
12 changes: 9 additions & 3 deletions src/Service/StorageCacheAbstractServiceFactory.php
Original file line number Diff line number Diff line change
@@ -41,15 +41,21 @@ public function canCreateServiceWithName(ContainerInterface $container, $request
if (empty($config)) {
return false;
}

return (isset($config[$requestedName]) && is_array($config[$requestedName]));
}

/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $this->getConfig($container);
$config = $config[$requestedName];
return StorageFactory::factory($config);
return StorageFactory::factory($config[$requestedName]);
}

/**
4 changes: 2 additions & 2 deletions src/Storage/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
@@ -204,7 +204,7 @@ public function getEventManager()
*/
protected function triggerPre($eventName, ArrayObject $args)
{
return $this->getEventManager()->trigger(new Event($eventName . '.pre', $this, $args));
return $this->getEventManager()->triggerEvent(new Event($eventName . '.pre', $this, $args));
}

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

if ($exceptionEvent->getThrowException()) {
throw $exceptionEvent->getException();
38 changes: 17 additions & 21 deletions src/Storage/AdapterPluginManager.php
Original file line number Diff line number Diff line change
@@ -24,19 +24,33 @@ class AdapterPluginManager extends AbstractPluginManager
{
protected $aliases = [
'apc' => Adapter\Apc::class,
'Apc' => Adapter\Apc::class,
'blackhole' => Adapter\BlackHole::class,
'BlackHole' => Adapter\BlackHole::class,
'dba' => Adapter\Dba::class,
'Dba' => Adapter\Dba::class,
'filesystem' => Adapter\Filesystem::class,
'Filesystem' => Adapter\Filesystem::class,
'memcache' => Adapter\Memcache::class,
'Memcache' => Adapter\Memcache::class,
'memcached' => Adapter\Memcached::class,
'Memcached' => Adapter\Memcached::class,
'memory' => Adapter\Memory::class,
'Memory' => Adapter\Memory::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,
'wincache' => Adapter\WinCache::class,
'WinCache' => Adapter\WinCache::class,
'zendserverdisk' => Adapter\ZendServerDisk::class,
'zendservershm' => Adapter\ZendServerShm::class
'ZendServerDisk' => Adapter\ZendServerDisk::class,
'zendservershm' => Adapter\ZendServerShm::class,
'ZendServerShm' => Adapter\ZendServerShm::class
];

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

/**
* Validate the plugin
*
* Checks that the adapter loaded is an instance of StorageInterface.
*
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException if invalid
* @var string
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof StorageInterface) {
// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s\StorageInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
}
protected $instanceOf = StorageInterface::class;
}
47 changes: 17 additions & 30 deletions src/Storage/PluginManager.php
Original file line number Diff line number Diff line change
@@ -23,19 +23,24 @@
class PluginManager extends AbstractPluginManager
{
protected $aliases = [
'clearexpiredbyfactor' => Storage\Plugin\ClearExpiredByFactor::class,
'exceptionhandler' => Storage\Plugin\ExceptionHandler::class,
'ignoreuserabort' => Storage\Plugin\IgnoreUserAbort::class,
'optimizebyfactor' => Storage\Plugin\OptimizeByFactor::class,
'serializer' => Storage\Plugin\Serializer::class,
'clearexpiredbyfactor' => Plugin\ClearExpiredByFactor::class,
'ClearExpiredByFactor' => Plugin\ClearExpiredByFactor::class,
'exceptionhandler' => Plugin\ExceptionHandler::class,
'ExceptionHandler' => Plugin\ExceptionHandler::class,
'ignoreuserabort' => Plugin\IgnoreUserAbort::class,
'IgnoreUserAbort' => Plugin\IgnoreUserAbort::class,
'optimizebyfactor' => Plugin\OptimizeByFactor::class,
'OptimizeByFactor' => Plugin\OptimizeByFactor::class,
'serializer' => Plugin\Serializer::class,
'Serializer' => Plugin\Serializer::class
];

protected $factories = [
Storage\Plugin\ClearExpiredByFactor::class => InvokableFactory::class,
Storage\Plugin\ExceptionHandler::class => InvokableFactory::class,
Storage\Plugin\IgnoreUserAbort::class => InvokableFactory::class,
Storage\Plugin\OptimizeByFactor::class => InvokableFactory::class,
Storage\Plugin\Serializer::class => InvokableFactory::class
Plugin\ClearExpiredByFactor::class => InvokableFactory::class,
Plugin\ExceptionHandler::class => InvokableFactory::class,
Plugin\IgnoreUserAbort::class => InvokableFactory::class,
Plugin\OptimizeByFactor::class => InvokableFactory::class,
Plugin\Serializer::class => InvokableFactory::class
];

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

/**
* Validate the plugin
*
* Checks that the plugin loaded is an instance of Plugin\PluginInterface.
*
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException if invalid
* @var string
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof Plugin\PluginInterface) {
// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s\Plugin\PluginInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
}
protected $instanceOf = Plugin\PluginInterface::class;
}
2 changes: 1 addition & 1 deletion src/StorageFactory.php
Original file line number Diff line number Diff line change
@@ -220,7 +220,7 @@ public static function pluginFactory($pluginName, $options = [])
public static function getPluginManager()
{
if (static::$plugins === null) {
static::$plugins = new Storage\PluginManager();
static::$plugins = new Storage\PluginManager(new ServiceManager);
}
return static::$plugins;
}
136 changes: 136 additions & 0 deletions test/EventManagerIntrospectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Cache;

use ReflectionProperty;
use Zend\EventManager\EventManager;

/**
* Offer methods for introspecting event manager events and listeners.
*/
trait EventManagerIntrospectionTrait
{
/**
* Retrieve a list of event names from an event manager.
*
* @param EventManager $events
* @return string[]
*/
private function getEventsFromEventManager(EventManager $events)
{
$r = new ReflectionProperty($events, 'events');
$r->setAccessible(true);
$listeners = $r->getValue($events);
return array_keys($listeners);
}

/**
* Retrieve an interable list of listeners for an event.
*
* Given an event and an event manager, returns an iterator with the
* listeners for that event, in priority order.
*
* If $withPriority is true, the key values will be the priority at which
* the given listener is attached.
*
* Do not pass $withPriority if you want to cast the iterator to an array,
* as many listeners will likely have the same priority, and thus casting
* will collapse to the last added.
*
* @param string $event
* @param EventManager $events
* @param bool $withPriority
* @return \Traversable
*/
private function getListenersForEvent($event, EventManager $events, $withPriority = false)
{
$r = new ReflectionProperty($events, 'events');
$r->setAccessible(true);
$listeners = $r->getValue($events);

if (! isset($listeners[$event])) {
return $this->traverseListeners([]);
}

return $this->traverseListeners($listeners[$event], $withPriority);
}

/**
* Assert that a given listener exists at the specified priority.
*
* @param callable $expectedListener
* @param int $expectedPriority
* @param string $event
* @param EventManager $events
* @param string $message Failure message to use, if any.
*/
private function assertListenerAtPriority(
callable $expectedListener,
$expectedPriority,
$event,
EventManager $events,
$message = ''
) {
$message = $message ?: sprintf(
'Listener not found for event "%s" and priority %d',
$event,
$expectedPriority
);
$listeners = $this->getListenersForEvent($event, $events, true);
$found = false;
foreach ($listeners as $priority => $listener) {
if ($listener === $expectedListener
&& $priority === $expectedPriority
) {
$found = true;
break;
}
}
$this->assertTrue($found, $message);
}

/**
* Returns an indexed array of listeners for an event.
*
* Returns an indexed array of listeners for an event, in priority order.
* Priority values will not be included; use this only for testing if
* specific listeners are present, or for a count of listeners.
*
* @param string $event
* @param EventManager $events
* @return callable[]
*/
private function getArrayOfListenersForEvent($event, EventManager $events)
{
return iterator_to_array($this->getListenersForEvent($event, $events));
}

/**
* Generator for traversing listeners in priority order.
*
* @param array $listeners
* @param bool $withPriority When true, yields priority as key.
*/
public function traverseListeners(array $queue, $withPriority = false)
{
krsort($queue, SORT_NUMERIC);

foreach ($queue as $priority => $listeners) {
$priority = (int) $priority;
foreach ($listeners as $listener) {
if ($withPriority) {
yield $priority => $listener;
} else {
yield $listener;
}
}
}
}
}
4 changes: 3 additions & 1 deletion test/PatternFactoryTest.php
Original file line number Diff line number Diff line change
@@ -34,7 +34,9 @@ public function testDefaultPluginManager()

public function testChangePluginManager()
{
$plugins = new Cache\PatternPluginManager();
$plugins = new Cache\PatternPluginManager(
$this->getMockBuilder('Interop\Container\ContainerInterface')->getMock()
);
Cache\PatternFactory::setPluginManager($plugins);
$this->assertSame($plugins, Cache\PatternFactory::getPluginManager());
}
Loading