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

[3.0] Mark static factories as deprecated #176

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
/phpcs.xml export-ignore
/phpunit.xml.dist export-ignore
/test/ export-ignore
/stubs/
17 changes: 17 additions & 0 deletions autoload/adapterPluginManagerPolyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

use Zend\Cache\Storage\AdapterPluginManager;
use Zend\ServiceManager\ServiceManager;

call_user_func(function () {
$target = method_exists(ServiceManager::class, 'configure')
? AdapterPluginManager\AdapterPluginManagerV3Polyfill::class
: AdapterPluginManager\AdapterPluginManagerV2Polyfill::class;

class_alias($target, AdapterPluginManager::class);
});
3 changes: 3 additions & 0 deletions benchmark/FilesystemStorageAdapterBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ZendBench\Cache;

use Zend\Cache\StorageFactory;
use Zend\Stdlib\ErrorHandler;

/**
* @Revs(100)
Expand All @@ -27,9 +28,11 @@ public function __construct()
$this->fail("Can't create temporary cache directory: {$err['message']}");
}

ErrorHandler::start(E_USER_DEPRECATED);
$this->storage = StorageFactory::adapterFactory('filesystem', [
'cache_dir' => $this->tmpCacheDir,
]);
ErrorHandler::clean();

parent::__construct();
}
Expand Down
3 changes: 3 additions & 0 deletions benchmark/MemoryStorageAdapterBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ZendBench\Cache;

use Zend\Cache\StorageFactory;
use Zend\Stdlib\ErrorHandler;

/**
* @Revs(100)
Expand All @@ -13,8 +14,10 @@ class MemoryStorageAdapterBench extends CommonStorageAdapterBench
{
public function __construct()
{
ErrorHandler::start(E_USER_DEPRECATED);
// instantiate the storage adapter
$this->storage = StorageFactory::adapterFactory('memory');
ErrorHandler::clean();

parent::__construct();
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
},
"autoload": {
"files": [
"autoload/patternPluginManagerPolyfill.php"
"autoload/patternPluginManagerPolyfill.php",
"autoload/adapterPluginManagerPolyfill.php"
],
"psr-4": {
"Zend\\Cache\\": "src/"
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@
<env name="TESTS_ZEND_CACHE_REDIS_PORT" value="6379" />
<env name="TESTS_ZEND_CACHE_REDIS_PASSWORD" value="" />
<env name="TESTS_ZEND_CACHE_REDIS_DATABASE" value="false" />

<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
34 changes: 33 additions & 1 deletion src/PatternFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
use Zend\Stdlib\ArrayUtils;
use Zend\ServiceManager\ServiceManager;

/**
* @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions.
*/
abstract class PatternFactory
{

/**
* The pattern manager
*
Expand All @@ -25,13 +29,21 @@ abstract class PatternFactory
/**
* Instantiate a cache pattern
*
* @param string|Pattern\PatternInterface $patternName
* @param string|Pattern\PatternInterface $patternName
* @param array|Traversable|Pattern\PatternOptions $options
*
* @return Pattern\PatternInterface
* @throws Exception\InvalidArgumentException
* @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions.
*/
public static function factory($patternName, $options = [])
{
trigger_error(sprintf(
'%s is deprecated; please use %s::get instead',
__METHOD__,
PatternPluginManager::class
), E_USER_DEPRECATED);

if ($options instanceof Pattern\PatternOptions) {
$options = $options->toArray();
}
Expand All @@ -51,6 +63,7 @@ public static function factory($patternName, $options = [])

if ($patternName instanceof Pattern\PatternInterface) {
$patternName->setOptions(new Pattern\PatternOptions($options));

return $patternName;
}

Expand All @@ -61,9 +74,15 @@ public static function factory($patternName, $options = [])
* Get the pattern plugin manager
*
* @return PatternPluginManager
* @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions.
*/
public static function getPluginManager()
{
trigger_error(sprintf(
'%s is deprecated',
__METHOD__
), E_USER_DEPRECATED);

if (static::$plugins === null) {
static::$plugins = new PatternPluginManager(new ServiceManager);
}
Expand All @@ -75,20 +94,33 @@ public static function getPluginManager()
* Set the pattern plugin manager
*
* @param PatternPluginManager $plugins
*
* @return void
* @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions.
*/
public static function setPluginManager(PatternPluginManager $plugins)
{
trigger_error(sprintf(
'%s is deprecated',
__METHOD__
), E_USER_DEPRECATED);

static::$plugins = $plugins;
}

/**
* Reset pattern plugin manager to default
*
* @return void
* @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions.
*/
public static function resetPluginManager()
{
trigger_error(sprintf(
'%s is deprecated',
__METHOD__
), E_USER_DEPRECATED);

static::$plugins = null;
}
}
2 changes: 1 addition & 1 deletion src/PatternPluginManager/PatternPluginManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Zend\ServiceManager\Exception\InvalidServiceException;

/**
* Trait providing common logic between FormElementManager implementations.
* Trait providing common logic between PatternPluginManager implementations.
*
* Trait does not define properties, as the properties common between the
* two versions are originally defined in their parent class, causing a
Expand Down
145 changes: 145 additions & 0 deletions src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Cache\Storage\AdapterPluginManager;

use Zend\Cache\Exception;
use Zend\Cache\Storage\PluginManager;
use Zend\Cache\Storage\StorageInterface;
use Zend\EventManager\EventsCapableInterface;
use Zend\ServiceManager\Exception\InvalidServiceException;
use Zend\Stdlib\ArrayUtils;

/**
* Trait providing common logic between AdapterPluginManager implementations.
*
* Trait does not define properties, as the properties common between the
* two versions are originally defined in their parent class, causing a
* resolution conflict.
*/
trait AdapterPluginManagerTrait
{
/**
* 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);
}
}

private function parseOptions(array $options)
{
$plugins = [];
if (array_key_exists('plugins', $options)) {
$plugins = $options['plugins'] ?: [];
unset($options['plugins']);
}

$adapter = $options;

if (isset($options['options'])) {
$adapter = $options['options'];
}

if (isset($options['adapter']['options']) && is_array($options['adapter']['options'])) {
$adapter = ArrayUtils::merge($adapter, $options['adapter']['options']);
}

return [$adapter, $plugins];
}

/**
* Attaches plugins by using the provided plugin manager.
*
* @param StorageInterface $adapter
* @param PluginManager $pluginManager
* @param array $plugins
*
* @return void
* @throws Exception\RuntimeException if adapter does not implement `EventsCapableInterface`
* @throws Exception\InvalidArgumentException if the plugin configuration does not fit specification.
*/
private function attachPlugins(StorageInterface $adapter, PluginManager $pluginManager, array $plugins)
{
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
));
}

foreach ($plugins as $k => $v) {
$pluginPrio = 1; // default priority

if (is_string($k)) {
if (! is_array($v)) {
throw new Exception\InvalidArgumentException(
"'plugins.{$k}' needs to be an array"
);
}
$pluginName = $k;
$pluginOptions = $v;
} elseif (is_array($v)) {
if (! isset($v['name'])) {
throw new Exception\InvalidArgumentException(
"Invalid plugins[{$k}] or missing plugins[{$k}].name"
);
}
$pluginName = (string) $v['name'];

if (isset($v['options'])) {
$pluginOptions = $v['options'];
} else {
$pluginOptions = [];
}

if (isset($v['priority'])) {
$pluginPrio = $v['priority'];
}
} else {
$pluginName = $v;
$pluginOptions = [];
}

$plugin = $pluginManager->get($pluginName, $pluginOptions);
if (! $adapter->hasPlugin($plugin)) {
$adapter->addPlugin($plugin, $pluginPrio);
}
}
}
}
Loading