|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the PPI Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2011-2016 Paul Dragoonis <[email protected]> |
|
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
|
7
|
|
|
* |
|
8
|
|
|
* @link http://www.ppi.io |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PPI\Framework\Router; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
|
14
|
|
|
use Zend\ServiceManager\Exception\RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Plugin manager implementation for routes. |
|
18
|
|
|
* |
|
19
|
|
|
* Enforces that routes retrieved are instances of RouteInterface. It overrides |
|
20
|
|
|
* createFromInvokable() to call the route's factory method in order to get an |
|
21
|
|
|
* instance. The manager is marked to not share by default, in order to allow |
|
22
|
|
|
* multiple route instances of the same type. |
|
23
|
|
|
*/ |
|
24
|
|
|
class RoutePluginManager extends AbstractPluginManager |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Do not share instances. |
|
28
|
|
|
* |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $shareByDefault = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Override setInvokableClass(). |
|
35
|
|
|
* |
|
36
|
|
|
* Performs normal operation, but also auto-aliases the class name to the |
|
37
|
|
|
* service name. This ensures that providing the FQCN does not trigger an |
|
38
|
|
|
* abstract factory later. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
* @param string $invokableClass |
|
42
|
|
|
* @param null|bool $shared |
|
43
|
|
|
* |
|
44
|
|
|
* @return RoutePluginManager |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setInvokableClass($name, $invokableClass, $shared = null) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::setInvokableClass($name, $invokableClass, $shared); |
|
49
|
|
|
if ($name != $invokableClass) { |
|
50
|
|
|
$this->setAlias($invokableClass, $name); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Validate the plugin. |
|
58
|
|
|
* |
|
59
|
|
|
* Checks that the filter loaded is either a valid callback or an instance |
|
60
|
|
|
* of FilterInterface. |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed $plugin |
|
63
|
|
|
* |
|
64
|
|
|
* @throws RuntimeException if invalid |
|
65
|
|
|
*/ |
|
66
|
|
|
public function validatePlugin($plugin) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($plugin instanceof RouteInterface) { |
|
|
|
|
|
|
69
|
|
|
// we're okay |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
throw new RuntimeException(sprintf( |
|
74
|
|
|
'Plugin of type %s is invalid; must implement %s\RouteInterface', |
|
75
|
|
|
(is_object($plugin) ? get_class($plugin) : gettype($plugin)), |
|
76
|
|
|
__NAMESPACE__ |
|
77
|
|
|
)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Attempt to create an instance via an invokable class. |
|
82
|
|
|
* |
|
83
|
|
|
* Overrides parent implementation by invoking the route factory, |
|
84
|
|
|
* passing $creationOptions as the argument. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $canonicalName |
|
87
|
|
|
* @param string $requestedName |
|
88
|
|
|
* |
|
89
|
|
|
* @throws Exception\RuntimeException If resolved class does not exist, or does not implement RouteInterface |
|
90
|
|
|
* |
|
91
|
|
|
* @return null|\stdClass |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function createFromInvokable($canonicalName, $requestedName) |
|
94
|
|
|
{ |
|
95
|
|
|
$invokable = $this->invokableClasses[$canonicalName]; |
|
96
|
|
|
if (!class_exists($invokable)) { |
|
97
|
|
|
throw new RuntimeException(sprintf( |
|
98
|
|
|
'%s: failed retrieving "%s%s" via invokable class "%s"; class does not exist', |
|
99
|
|
|
__METHOD__, |
|
100
|
|
|
$canonicalName, |
|
101
|
|
|
($requestedName ? '(alias: ' . $requestedName . ')' : ''), |
|
102
|
|
|
$invokable |
|
103
|
|
|
)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (!static::isSubclassOf($invokable, __NAMESPACE__ . '\RouteInterface')) { |
|
|
|
|
|
|
107
|
|
|
throw new RuntimeException(sprintf( |
|
108
|
|
|
'%s: failed retrieving "%s%s" via invokable class "%s"; class does not implement %s\RouteInterface', |
|
109
|
|
|
__METHOD__, |
|
110
|
|
|
$canonicalName, |
|
111
|
|
|
($requestedName ? '(alias: ' . $requestedName . ')' : ''), |
|
112
|
|
|
$invokable, |
|
113
|
|
|
__NAMESPACE__ |
|
114
|
|
|
)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $invokable::factory($this->creationOptions); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.