3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Mvc\Router
;
12 use Interop\Container\ContainerInterface
;
13 use Zend\ServiceManager\Exception\ServiceNotCreatedException
;
14 use Zend\ServiceManager\AbstractFactoryInterface
;
15 use Zend\ServiceManager\FactoryInterface
;
16 use Zend\ServiceManager\ServiceLocatorInterface
;
19 * Specialized invokable/abstract factory for use with RoutePluginManager.
21 * Can be mapped directly to specific route plugin names, or used as an
22 * abstract factory to map FQCN services to invokables.
24 class RouteInvokableFactory
implements
25 AbstractFactoryInterface
,
29 * Options used to create instance (used with zend-servicemanager v2)
33 protected $creationOptions = [];
36 * Can we create a route instance with the given name? (v3)
38 * Only works for FQCN $routeName values, for classes that implement RouteInterface.
40 * @param ContainerInterface $container
41 * @param string $routeName
44 public function canCreate(ContainerInterface
$container, $routeName)
46 if (! class_exists($routeName)) {
50 if (! is_subclass_of($routeName, RouteInterface
::class)) {
58 * Can we create a route instance with the given name? (v2)
60 * Proxies to canCreate().
62 * @param ServiceLocatorInterface $container
63 * @param string $normalizedName
64 * @param string $routeName
67 public function canCreateServiceWithName(ServiceLocatorInterface
$container, $normalizedName, $routeName)
69 return $this->canCreate($container, $routeName);
73 * Create and return a RouteInterface instance.
75 * If the specified $routeName class does not exist or does not implement
76 * RouteInterface, this method will raise an exception.
78 * Otherwise, it uses the class' `factory()` method with the provided
79 * $options to produce an instance.
81 * @param ContainerInterface $container
82 * @param string $routeName
83 * @param null|array $options
84 * @return RouteInterface
86 public function __invoke(ContainerInterface
$container, $routeName, array $options = null)
88 $options = $options ?
: [];
90 if (! class_exists($routeName)) {
91 throw new ServiceNotCreatedException(sprintf(
92 '%s: failed retrieving invokable class "%s"; class does not exist',
98 if (! is_subclass_of($routeName, RouteInterface
::class)) {
99 throw new ServiceNotCreatedException(sprintf(
100 '%s: failed retrieving invokable class "%s"; class does not implement %s',
103 RouteInterface
::class
107 return $routeName::factory($options);
111 * Create a route instance with the given name. (v2)
113 * Proxies to __invoke().
115 * @param ServiceLocatorInterface $container
116 * @param string $normalizedName
117 * @param string $routeName
118 * @return RouteInterface
120 public function createServiceWithName(ServiceLocatorInterface
$container, $normalizedName, $routeName)
122 return $this($container, $routeName, $this->creationOptions
);
126 * Create and return RouteInterface instance
128 * For use with zend-servicemanager v2; proxies to __invoke().
130 * @param ServiceLocatorInterface $container
131 * @return RouteInterface
133 public function createService(ServiceLocatorInterface
$container, $normalizedName = null, $routeName = null)
135 $routeName = $routeName ?
: RouteInterface
::class;
136 return $this($container, $routeName, $this->creationOptions
);
140 * Set options to use when creating a service (v2)
142 * @param array $creationOptions
144 public function setCreationOptions(array $creationOptions)
146 $this->creationOptions
= $creationOptions;