fix travis build
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Router / RoutePluginManager.php
CommitLineData
44d399bc
S
1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Mvc\Router;
11
12use Interop\Container\ContainerInterface;
13use Zend\ServiceManager\AbstractPluginManager;
14use Zend\ServiceManager\Exception\InvalidServiceException;
15
16/**
17 * Plugin manager implementation for routes
18 *
19 * Enforces that routes retrieved are instances of RouteInterface. It overrides
20 * configure() to map invokables to the component-specific
21 * RouteInvokableFactory.
22 *
23 * The manager is marked to not share by default, in order to allow multiple
24 * route instances of the same type.
25 */
26class RoutePluginManager extends AbstractPluginManager
27{
28 /**
29 * Only RouteInterface instances are valid
30 *
31 * @var string
32 */
33 protected $instanceOf = RouteInterface::class;
34
35 /**
36 * Do not share instances. (v3)
37 *
38 * @var bool
39 */
40 protected $shareByDefault = false;
41
42 /**
43 * Do not share instances. (v2)
44 *
45 * @var bool
46 */
47 protected $sharedByDefault = false;
48
49 /**
50 * Constructor
51 *
52 * Ensure that the instance is seeded with the RouteInvokableFactory as an
53 * abstract factory.
54 *
55 * @param ContainerInterface|\Zend\ServiceManager\ConfigInterface $configOrContainerInstance
56 * @param array $v3config
57 */
58 public function __construct($configOrContainerInstance, array $v3config = [])
59 {
60 $this->addAbstractFactory(RouteInvokableFactory::class);
61 parent::__construct($configOrContainerInstance, $v3config);
62 }
63
64 /**
65 * Validate a route plugin. (v2)
66 *
67 * @param object $plugin
68 * @throws InvalidServiceException
69 */
70 public function validate($plugin)
71 {
72 if (! $plugin instanceof $this->instanceOf) {
73 throw new InvalidServiceException(sprintf(
74 'Plugin of type %s is invalid; must implement %s',
75 (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
76 RouteInterface::class
77 ));
78 }
79 }
80
81 /**
82 * Validate a route plugin. (v2)
83 *
84 * @param object $plugin
85 * @throws Exception\RuntimeException
86 */
87 public function validatePlugin($plugin)
88 {
89 try {
90 $this->validate($plugin);
91 } catch (InvalidServiceException $e) {
92 throw new Exception\RuntimeException(
93 $e->getMessage(),
94 $e->getCode(),
95 $e
96 );
97 }
98 }
99
100 /**
101 * Pre-process configuration. (v3)
102 *
103 * Checks for invokables, and, if found, maps them to the
104 * component-specific RouteInvokableFactory; removes the invokables entry
105 * before passing to the parent.
106 *
107 * @param array $config
108 * @return void
109 */
110 public function configure(array $config)
111 {
112 if (isset($config['invokables']) && ! empty($config['invokables'])) {
113 $aliases = $this->createAliasesForInvokables($config['invokables']);
114 $factories = $this->createFactoriesForInvokables($config['invokables']);
115
116 if (! empty($aliases)) {
117 $config['aliases'] = isset($config['aliases'])
118 ? array_merge($config['aliases'], $aliases)
119 : $aliases;
120 }
121
122 $config['factories'] = isset($config['factories'])
123 ? array_merge($config['factories'], $factories)
124 : $factories;
125
126 unset($config['invokables']);
127 }
128
129 parent::configure($config);
130 }
131
132 /**
133 * Create aliases for invokable classes.
134 *
135 * If an invokable service name does not match the class it maps to, this
136 * creates an alias to the class (which will later be mapped as an
137 * invokable factory).
138 *
139 * @param array $invokables
140 * @return array
141 */
142 protected function createAliasesForInvokables(array $invokables)
143 {
144 $aliases = [];
145 foreach ($invokables as $name => $class) {
146 if ($name === $class) {
147 continue;
148 }
149 $aliases[$name] = $class;
150 }
151 return $aliases;
152 }
153
154 /**
155 * Create invokable factories for invokable classes.
156 *
157 * If an invokable service name does not match the class it maps to, this
158 * creates an invokable factory entry for the class name; otherwise, it
159 * creates an invokable factory for the entry name.
160 *
161 * @param array $invokables
162 * @return array
163 */
164 protected function createFactoriesForInvokables(array $invokables)
165 {
166 $factories = [];
167 foreach ($invokables as $name => $class) {
168 if ($name === $class) {
169 $factories[$name] = RouteInvokableFactory::class;
170 continue;
171 }
172
173 $factories[$class] = RouteInvokableFactory::class;
174 }
175 return $factories;
176 }
177}