3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
12 use Zend\ServiceManager\AbstractPluginManager
;
13 use Zend\ServiceManager\Factory\InvokableFactory
;
14 use Zend\ServiceManager\Exception\InvalidServiceException
;
17 * Plugin manager implementation for cache pattern adapters
19 * Enforces that retrieved adapters are instances of
20 * Pattern\PatternInterface. Additionally, it registers a number of default
23 class PatternPluginManager
extends AbstractPluginManager
25 protected $aliases = [
26 'callback' => Pattern\CallbackCache
::class,
27 'Callback' => Pattern\CallbackCache
::class,
28 'capture' => Pattern\CaptureCache
::class,
29 'Capture' => Pattern\CaptureCache
::class,
30 'class' => Pattern\ClassCache
::class,
31 'Class' => Pattern\ClassCache
::class,
32 'object' => Pattern\ObjectCache
::class,
33 'Object' => Pattern\ObjectCache
::class,
34 'output' => Pattern\OutputCache
::class,
35 'Output' => Pattern\OutputCache
::class,
38 protected $factories = [
39 Pattern\CallbackCache
::class => InvokableFactory
::class,
40 Pattern\CaptureCache
::class => InvokableFactory
::class,
41 Pattern\ClassCache
::class => InvokableFactory
::class,
42 Pattern\ObjectCache
::class => InvokableFactory
::class,
43 Pattern\OutputCache
::class => InvokableFactory
::class,
45 // v2 normalized FQCNs
46 'zendcachepatterncallbackcache' => InvokableFactory
::class,
47 'zendcachepatterncapturecache' => InvokableFactory
::class,
48 'zendcachepatternclasscache' => InvokableFactory
::class,
49 'zendcachepatternobjectcache' => InvokableFactory
::class,
50 'zendcachepatternoutputcache' => InvokableFactory
::class,
54 * Don't share by default
58 protected $shareByDefault = false;
61 * Don't share by default
65 protected $sharedByDefault = false;
70 protected $instanceOf = Pattern\PatternInterface
::class;
73 * Override get to inject options as PatternOptions instance.
77 public function get($plugin, array $options = [], $usePeeringServiceManagers = true)
79 if (empty($options)) {
80 return parent
::get($plugin, [], $usePeeringServiceManagers);
83 $plugin = parent
::get($plugin, [], $usePeeringServiceManagers);
84 $plugin->setOptions(new Pattern\
PatternOptions($options));
89 * Override build to inject options as PatternOptions instance.
93 public function build($plugin, array $options = null)
95 if (empty($options)) {
96 return parent
::build($plugin);
99 $plugin = parent
::build($plugin);
100 $plugin->setOptions(new Pattern\
PatternOptions($options));
105 * Validate the plugin is of the expected type (v3).
107 * Validates against `$instanceOf`.
109 * @param mixed $instance
110 * @throws InvalidServiceException
112 public function validate($instance)
114 if (! $instance instanceof $this->instanceOf) {
115 throw new InvalidServiceException(sprintf(
116 '%s can only create instances of %s; %s is invalid',
119 (is_object($instance) ?
get_class($instance) : gettype($instance))
125 * Validate the plugin is of the expected type (v2).
127 * Proxies to `validate()`.
129 * @param mixed $plugin
130 * @throws Exception\RuntimeException if invalid
132 public function validatePlugin($plugin)
135 $this->validate($plugin);
136 } catch (InvalidServiceException
$e) {
137 throw new Exception\
RuntimeException($e->getMessage(), $e->getCode(), $e);