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

Commit

Permalink
🔥 Polyfill PatternPluginManager - to allow work with Zend SMv2 and v3
Browse files Browse the repository at this point in the history
Fixes issue with PHP 7.2 signature compatibility.
  • Loading branch information
michalbundyra committed Nov 2, 2017
1 parent c6b995d commit d19e3ac
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 68 deletions.
17 changes: 17 additions & 0 deletions autoload/patternPluginManagerPolyfill.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) 2017 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\PatternPluginManager;
use Zend\ServiceManager\ServiceManager;

call_user_func(function () {
$target = method_exists(ServiceManager::class, 'configure')
? PatternPluginManager\PatternPluginManagerV3Polyfill::class
: PatternPluginManager\PatternPluginManagerV2Polyfill::class;

class_alias($target, PatternPluginManager::class);
});
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"require": {
"php": "^5.6 || ^7.0",
"psr/cache": "^1.0",
"zendframework/zend-eventmanager": "^2.6.3 || ^3.2",
"zendframework/zend-servicemanager": "^2.7.8 || ^3.3",
"zendframework/zend-stdlib": "^2.7.7 || ^3.1",
"zendframework/zend-eventmanager": "^2.6.3 || ^3.2"
"zendframework/zend-stdlib": "^2.7.7 || ^3.1"
},
"require-dev": {
"cache/integration-tests": "^0.16",
Expand All @@ -46,6 +46,9 @@
"mongofill/mongofill": "Alternative to ext-mongo - a pure PHP implementation designed as a drop in replacement"
},
"autoload": {
"files": [
"autoload/patternPluginManagerPolyfill.php"
],
"psr-4": {
"Zend\\Cache\\": "src/"
}
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions src/PatternPluginManager/PatternPluginManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Cache\PatternPluginManager;

use Zend\Cache\Exception;
use Zend\Cache\Pattern;
use Zend\ServiceManager\Exception\InvalidServiceException;

/**
* Trait providing common logic between FormElementManager 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 PatternPluginManagerTrait
{
/**
* Override build to inject options as PatternOptions instance.
*
* {@inheritDoc}
*/
public function build($plugin, array $options = null)
{
if (empty($options)) {
return parent::build($plugin);
}

$plugin = parent::build($plugin);
$plugin->setOptions(new Pattern\PatternOptions($options));
return $plugin;
}

/**
* 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Cache;
namespace Zend\Cache\PatternPluginManager;

use Zend\Cache\Pattern;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\Exception\InvalidServiceException;

/**
* Plugin manager implementation for cache pattern adapters
* zend-servicemanager v2-compatible plugin manager implementation for cache pattern adapters.
*
* Enforces that retrieved adapters are instances of
* Pattern\PatternInterface. Additionally, it registers a number of default
* patterns available.
*/
class PatternPluginManager extends AbstractPluginManager
class PatternPluginManagerV2Polyfill extends AbstractPluginManager
{
use PatternPluginManagerTrait;

protected $aliases = [
'callback' => Pattern\CallbackCache::class,
'Callback' => Pattern\CallbackCache::class,
Expand Down Expand Up @@ -53,14 +53,14 @@ class PatternPluginManager extends AbstractPluginManager
/**
* Don't share by default
*
* @var boolean
* @var bool
*/
protected $shareByDefault = false;

/**
* Don't share by default
*
* @var boolean
* @var bool
*/
protected $sharedByDefault = false;

Expand All @@ -74,7 +74,7 @@ class PatternPluginManager extends AbstractPluginManager
*
* {@inheritDoc}
*/
public function get($plugin, array $options = [], $usePeeringServiceManagers = true)
public function get($plugin, $options = [], $usePeeringServiceManagers = true)
{
if (empty($options)) {
return parent::get($plugin, [], $usePeeringServiceManagers);
Expand All @@ -84,57 +84,4 @@ public function get($plugin, array $options = [], $usePeeringServiceManagers = t
$plugin->setOptions(new Pattern\PatternOptions($options));
return $plugin;
}

/**
* Override build to inject options as PatternOptions instance.
*
* {@inheritDoc}
*/
public function build($plugin, array $options = null)
{
if (empty($options)) {
return parent::build($plugin);
}

$plugin = parent::build($plugin);
$plugin->setOptions(new Pattern\PatternOptions($options));
return $plugin;
}

/**
* 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);
}
}
}
87 changes: 87 additions & 0 deletions src/PatternPluginManager/PatternPluginManagerV3Polyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Cache\PatternPluginManager;

use Zend\Cache\Pattern;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Factory\InvokableFactory;

/**
* zend-servicemanager v3-compatible plugin manager implementation for cache pattern adapters.
*
* Enforces that retrieved adapters are instances of
* Pattern\PatternInterface. Additionally, it registers a number of default
* patterns available.
*/
class PatternPluginManagerV3Polyfill extends AbstractPluginManager
{
use PatternPluginManagerTrait;

protected $aliases = [
'callback' => Pattern\CallbackCache::class,
'Callback' => Pattern\CallbackCache::class,
'capture' => Pattern\CaptureCache::class,
'Capture' => Pattern\CaptureCache::class,
'class' => Pattern\ClassCache::class,
'Class' => Pattern\ClassCache::class,
'object' => Pattern\ObjectCache::class,
'Object' => Pattern\ObjectCache::class,
'output' => Pattern\OutputCache::class,
'Output' => Pattern\OutputCache::class,
];

protected $factories = [
Pattern\CallbackCache::class => InvokableFactory::class,
Pattern\CaptureCache::class => InvokableFactory::class,
Pattern\ClassCache::class => InvokableFactory::class,
Pattern\ObjectCache::class => InvokableFactory::class,
Pattern\OutputCache::class => InvokableFactory::class,

// v2 normalized FQCNs
'zendcachepatterncallbackcache' => InvokableFactory::class,
'zendcachepatterncapturecache' => InvokableFactory::class,
'zendcachepatternclasscache' => InvokableFactory::class,
'zendcachepatternobjectcache' => InvokableFactory::class,
'zendcachepatternoutputcache' => InvokableFactory::class,
];

/**
* Don't share by default
*
* @var bool
*/
protected $shareByDefault = false;

/**
* Don't share by default
*
* @var bool
*/
protected $sharedByDefault = false;

/**
* @var string
*/
protected $instanceOf = Pattern\PatternInterface::class;

/**
* Override get to inject options as PatternOptions instance.
*
* {@inheritDoc}
*/
public function get($plugin, array $options = null, $usePeeringServiceManagers = true)
{
if (empty($options)) {
return parent::get($plugin, null, $usePeeringServiceManagers);
}

$plugin = parent::get($plugin, null, $usePeeringServiceManagers);
$plugin->setOptions(new Pattern\PatternOptions($options));
return $plugin;
}
}

0 comments on commit d19e3ac

Please sign in to comment.