From af56e49ca7c97985b73d53ca62e18b23a22a3886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Thu, 13 Sep 2018 01:04:30 +0200 Subject: [PATCH 1/5] Added `PluginAwareInterface` and `PluginCapableInterface` to provide a proper interface for that logic --- src/Storage/Adapter/AbstractAdapter.php | 11 ++++++----- src/Storage/PluginAwareInterface.php | 12 ++++++++++++ src/Storage/PluginCapableInterface.php | 15 +++++++++++++++ src/StorageFactory.php | 5 +++-- 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/Storage/PluginAwareInterface.php create mode 100644 src/Storage/PluginCapableInterface.php diff --git a/src/Storage/Adapter/AbstractAdapter.php b/src/Storage/Adapter/AbstractAdapter.php index e54841843..9ccc381b4 100644 --- a/src/Storage/Adapter/AbstractAdapter.php +++ b/src/Storage/Adapter/AbstractAdapter.php @@ -18,13 +18,14 @@ use Zend\Cache\Storage\Event; use Zend\Cache\Storage\ExceptionEvent; use Zend\Cache\Storage\Plugin; +use Zend\Cache\Storage\PluginAwareInterface; use Zend\Cache\Storage\PostEvent; use Zend\Cache\Storage\StorageInterface; use Zend\EventManager\EventManager; use Zend\EventManager\EventManagerInterface; use Zend\EventManager\EventsCapableInterface; -abstract class AbstractAdapter implements StorageInterface, EventsCapableInterface +abstract class AbstractAdapter implements StorageInterface, PluginAwareInterface { /** * The used EventManager if any @@ -258,7 +259,7 @@ protected function triggerException($eventName, ArrayObject $args, & $result, \E * @param Plugin\PluginInterface $plugin * @return bool */ - public function hasPlugin(Plugin\PluginInterface $plugin) + public function hasPlugin(Plugin\PluginInterface $plugin): bool { $registry = $this->getPluginRegistry(); return $registry->contains($plugin); @@ -272,7 +273,7 @@ public function hasPlugin(Plugin\PluginInterface $plugin) * @return AbstractAdapter Provides a fluent interface * @throws Exception\LogicException */ - public function addPlugin(Plugin\PluginInterface $plugin, $priority = 1) + public function addPlugin(Plugin\PluginInterface $plugin, $priority = 1): void { $registry = $this->getPluginRegistry(); if ($registry->contains($plugin)) { @@ -295,7 +296,7 @@ public function addPlugin(Plugin\PluginInterface $plugin, $priority = 1) * @return AbstractAdapter Provides a fluent interface * @throws Exception\LogicException */ - public function removePlugin(Plugin\PluginInterface $plugin) + public function removePlugin(Plugin\PluginInterface $plugin): void { $registry = $this->getPluginRegistry(); if ($registry->contains($plugin)) { @@ -310,7 +311,7 @@ public function removePlugin(Plugin\PluginInterface $plugin) * * @return SplObjectStorage */ - public function getPluginRegistry() + public function getPluginRegistry(): SplObjectStorage { if (! $this->pluginRegistry instanceof SplObjectStorage) { $this->pluginRegistry = new SplObjectStorage(); diff --git a/src/Storage/PluginAwareInterface.php b/src/Storage/PluginAwareInterface.php new file mode 100644 index 000000000..c2f638150 --- /dev/null +++ b/src/Storage/PluginAwareInterface.php @@ -0,0 +1,12 @@ + Date: Thu, 13 Sep 2018 01:11:12 +0200 Subject: [PATCH 2/5] Mark `EventsCapableInterface` to provide plugin capabilities as deprecated --- src/StorageFactory.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/StorageFactory.php b/src/StorageFactory.php index 688c32a1d..85c7ad3a2 100644 --- a/src/StorageFactory.php +++ b/src/StorageFactory.php @@ -12,8 +12,9 @@ use Traversable; use Zend\Cache\Storage\PluginAwareInterface; use Zend\EventManager\EventsCapableInterface; -use Zend\Stdlib\ArrayUtils; use Zend\ServiceManager\ServiceManager; +use Zend\Stdlib\ArrayUtils; +use const E_USER_DEPRECATED; abstract class StorageFactory { @@ -75,11 +76,20 @@ public static function factory($cfg) // add plugins if (isset($cfg['plugins'])) { if (! $adapter instanceof PluginAwareInterface) { - throw new Exception\RuntimeException(sprintf( - "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", - get_class($adapter), + if (! $adapter instanceof EventsCapableInterface) { + throw new Exception\RuntimeException(sprintf( + "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", + get_class($adapter), + EventsCapableInterface::class + )); + } + + trigger_error(sprintf( + 'Using "%s" to provide plugin capabilities to storage adapters is deprecated as of ' + . 'zendframework 2.9; please use "%s" instead', + EventsCapableInterface::class, PluginAwareInterface::class - )); + ), E_USER_DEPRECATED); } if (! is_array($cfg['plugins'])) { From b255a0fee21498bc923aa5086c3e211633118047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Thu, 13 Sep 2018 01:29:47 +0200 Subject: [PATCH 3/5] Added unit test for deprecation warning --- ...erWithStorageAndEventsCapableInterface.php | 351 ++++++++++++++++++ test/StorageFactoryTest.php | 21 ++ 2 files changed, 372 insertions(+) create mode 100644 test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php diff --git a/test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php b/test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php new file mode 100644 index 000000000..42b106c43 --- /dev/null +++ b/test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php @@ -0,0 +1,351 @@ +prophesize(Cache\Storage\AdapterPluginManager::class); + + $adapters->get('Foo')->willReturn(new AdapterWithStorageAndEventsCapableInterface()); + + Cache\StorageFactory::setAdapterPluginManager($adapters->reveal()); + ErrorHandler::start(E_USER_DEPRECATED); + + Cache\StorageFactory::factory(['adapter' => 'Foo', 'plugins' => ['IgnoreUserAbort']]); + + $stack = ErrorHandler::stop(); + $this->assertInstanceOf(ErrorException::class, $stack); + } } From 6cdf71bfb80d6e59f6b53925939271ee5bdb071d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Thu, 13 Sep 2018 09:32:34 +0200 Subject: [PATCH 4/5] Removed return types and added docblocks --- src/Storage/Adapter/AbstractAdapter.php | 30 +++++++------------------ src/Storage/PluginAwareInterface.php | 30 +++++++++++++++++++++---- src/Storage/PluginCapableInterface.php | 24 ++++++++++++++++---- 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/src/Storage/Adapter/AbstractAdapter.php b/src/Storage/Adapter/AbstractAdapter.php index 9ccc381b4..90978eff7 100644 --- a/src/Storage/Adapter/AbstractAdapter.php +++ b/src/Storage/Adapter/AbstractAdapter.php @@ -254,26 +254,18 @@ protected function triggerException($eventName, ArrayObject $args, & $result, \E } /** - * Check if a plugin is registered - * - * @param Plugin\PluginInterface $plugin - * @return bool + * {@inheritdoc} */ - public function hasPlugin(Plugin\PluginInterface $plugin): bool + public function hasPlugin(Plugin\PluginInterface $plugin) { $registry = $this->getPluginRegistry(); return $registry->contains($plugin); } /** - * Register a plugin - * - * @param Plugin\PluginInterface $plugin - * @param int $priority - * @return AbstractAdapter Provides a fluent interface - * @throws Exception\LogicException + * {@inheritdoc} */ - public function addPlugin(Plugin\PluginInterface $plugin, $priority = 1): void + public function addPlugin(Plugin\PluginInterface $plugin, $priority = 1) { $registry = $this->getPluginRegistry(); if ($registry->contains($plugin)) { @@ -290,13 +282,9 @@ public function addPlugin(Plugin\PluginInterface $plugin, $priority = 1): void } /** - * Unregister an already registered plugin - * - * @param Plugin\PluginInterface $plugin - * @return AbstractAdapter Provides a fluent interface - * @throws Exception\LogicException + * {@inheritdoc} */ - public function removePlugin(Plugin\PluginInterface $plugin): void + public function removePlugin(Plugin\PluginInterface $plugin) { $registry = $this->getPluginRegistry(); if ($registry->contains($plugin)) { @@ -307,11 +295,9 @@ public function removePlugin(Plugin\PluginInterface $plugin): void } /** - * Return registry of plugins - * - * @return SplObjectStorage + * {@inheritdoc} */ - public function getPluginRegistry(): SplObjectStorage + public function getPluginRegistry() { if (! $this->pluginRegistry instanceof SplObjectStorage) { $this->pluginRegistry = new SplObjectStorage(); diff --git a/src/Storage/PluginAwareInterface.php b/src/Storage/PluginAwareInterface.php index c2f638150..fc77ff6d7 100644 --- a/src/Storage/PluginAwareInterface.php +++ b/src/Storage/PluginAwareInterface.php @@ -1,12 +1,34 @@ Date: Thu, 13 Sep 2018 10:00:54 +0200 Subject: [PATCH 5/5] Fixed codestyle in `AdapterWithStorageAndEventsCapableInterface` test asset --- ...erWithStorageAndEventsCapableInterface.php | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php b/test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php index 42b106c43..ab10f856b 100644 --- a/test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php +++ b/test/Storage/Adapter/TestAsset/AdapterWithStorageAndEventsCapableInterface.php @@ -22,7 +22,6 @@ class AdapterWithStorageAndEventsCapableInterface implements StorageInterface, E */ public function getEventManager() { - } /** @@ -34,7 +33,6 @@ public function getEventManager() */ public function setOptions($options) { - } /** @@ -44,7 +42,6 @@ public function setOptions($options) */ public function getOptions() { - } /** @@ -59,7 +56,6 @@ public function getOptions() */ public function getItem($key, & $success = null, & $casToken = null) { - } /** @@ -72,7 +68,6 @@ public function getItem($key, & $success = null, & $casToken = null) */ public function getItems(array $keys) { - } /** @@ -85,7 +80,6 @@ public function getItems(array $keys) */ public function hasItem($key) { - } /** @@ -98,7 +92,6 @@ public function hasItem($key) */ public function hasItems(array $keys) { - } /** @@ -111,7 +104,6 @@ public function hasItems(array $keys) */ public function getMetadata($key) { - } /** @@ -124,7 +116,6 @@ public function getMetadata($key) */ public function getMetadatas(array $keys) { - } /** @@ -138,7 +129,6 @@ public function getMetadatas(array $keys) */ public function setItem($key, $value) { - } /** @@ -151,7 +141,6 @@ public function setItem($key, $value) */ public function setItems(array $keyValuePairs) { - } /** @@ -165,7 +154,6 @@ public function setItems(array $keyValuePairs) */ public function addItem($key, $value) { - } /** @@ -178,7 +166,6 @@ public function addItem($key, $value) */ public function addItems(array $keyValuePairs) { - } /** @@ -192,7 +179,6 @@ public function addItems(array $keyValuePairs) */ public function replaceItem($key, $value) { - } /** @@ -205,7 +191,6 @@ public function replaceItem($key, $value) */ public function replaceItems(array $keyValuePairs) { - } /** @@ -225,7 +210,6 @@ public function replaceItems(array $keyValuePairs) */ public function checkAndSetItem($token, $key, $value) { - } /** @@ -238,7 +222,6 @@ public function checkAndSetItem($token, $key, $value) */ public function touchItem($key) { - } /** @@ -251,7 +234,6 @@ public function touchItem($key) */ public function touchItems(array $keys) { - } /** @@ -264,7 +246,6 @@ public function touchItems(array $keys) */ public function removeItem($key) { - } /** @@ -277,7 +258,6 @@ public function removeItem($key) */ public function removeItems(array $keys) { - } /** @@ -291,7 +271,6 @@ public function removeItems(array $keys) */ public function incrementItem($key, $value) { - } /** @@ -304,7 +283,6 @@ public function incrementItem($key, $value) */ public function incrementItems(array $keyValuePairs) { - } /** @@ -318,7 +296,6 @@ public function incrementItems(array $keyValuePairs) */ public function decrementItem($key, $value) { - } /** @@ -331,7 +308,6 @@ public function decrementItem($key, $value) */ public function decrementItems(array $keyValuePairs) { - } /** @@ -341,7 +317,6 @@ public function decrementItems(array $keyValuePairs) */ public function getCapabilities() { - } public function hasPlugin(PluginInterface $plugin)