1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc\Router;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Specialized invokable/abstract factory for use with RoutePluginManager.
*
* Can be mapped directly to specific route plugin names, or used as an
* abstract factory to map FQCN services to invokables.
*/
class RouteInvokableFactory implements
AbstractFactoryInterface,
FactoryInterface
{
/**
* Options used to create instance (used with zend-servicemanager v2)
*
* @var array
*/
protected $creationOptions = [];
/**
* Can we create a route instance with the given name? (v3)
*
* Only works for FQCN $routeName values, for classes that implement RouteInterface.
*
* @param ContainerInterface $container
* @param string $routeName
* @return bool
*/
public function canCreate(ContainerInterface $container, $routeName)
{
if (! class_exists($routeName)) {
return false;
}
if (! is_subclass_of($routeName, RouteInterface::class)) {
return false;
}
return true;
}
/**
* Can we create a route instance with the given name? (v2)
*
* Proxies to canCreate().
*
* @param ServiceLocatorInterface $container
* @param string $normalizedName
* @param string $routeName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $container, $normalizedName, $routeName)
{
return $this->canCreate($container, $routeName);
}
/**
* Create and return a RouteInterface instance.
*
* If the specified $routeName class does not exist or does not implement
* RouteInterface, this method will raise an exception.
*
* Otherwise, it uses the class' `factory()` method with the provided
* $options to produce an instance.
*
* @param ContainerInterface $container
* @param string $routeName
* @param null|array $options
* @return RouteInterface
*/
public function __invoke(ContainerInterface $container, $routeName, array $options = null)
{
$options = $options ?: [];
if (! class_exists($routeName)) {
throw new ServiceNotCreatedException(sprintf(
'%s: failed retrieving invokable class "%s"; class does not exist',
__CLASS__,
$routeName
));
}
if (! is_subclass_of($routeName, RouteInterface::class)) {
throw new ServiceNotCreatedException(sprintf(
'%s: failed retrieving invokable class "%s"; class does not implement %s',
__CLASS__,
$routeName,
RouteInterface::class
));
}
return $routeName::factory($options);
}
/**
* Create a route instance with the given name. (v2)
*
* Proxies to __invoke().
*
* @param ServiceLocatorInterface $container
* @param string $normalizedName
* @param string $routeName
* @return RouteInterface
*/
public function createServiceWithName(ServiceLocatorInterface $container, $normalizedName, $routeName)
{
return $this($container, $routeName, $this->creationOptions);
}
/**
* Create and return RouteInterface instance
*
* For use with zend-servicemanager v2; proxies to __invoke().
*
* @param ServiceLocatorInterface $container
* @return RouteInterface
*/
public function createService(ServiceLocatorInterface $container, $normalizedName = null, $routeName = null)
{
$routeName = $routeName ?: RouteInterface::class;
return $this($container, $routeName, $this->creationOptions);
}
/**
* Set options to use when creating a service (v2)
*
* @param array $creationOptions
*/
public function setCreationOptions(array $creationOptions)
{
$this->creationOptions = $creationOptions;
}
}