fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Mvc / Router / SimpleRouteStack.php
blob3613860436bea533d93494dcd6a9cbe876a23aab
1 <?php
2 /**
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
8 */
10 namespace Zend\Mvc\Router;
12 use Traversable;
13 use Zend\Stdlib\ArrayUtils;
14 use Zend\Stdlib\RequestInterface as Request;
16 /**
17 * Simple route stack implementation.
19 class SimpleRouteStack implements RouteStackInterface
21 /**
22 * Stack containing all routes.
24 * @var PriorityList
26 protected $routes;
28 /**
29 * Route plugin manager
31 * @var RoutePluginManager
33 protected $routePluginManager;
35 /**
36 * Default parameters.
38 * @var array
40 protected $defaultParams = array();
42 /**
43 * Create a new simple route stack.
45 * @param RoutePluginManager $routePluginManager
47 public function __construct(RoutePluginManager $routePluginManager = null)
49 $this->routes = new PriorityList();
51 if (null === $routePluginManager) {
52 $routePluginManager = new RoutePluginManager();
55 $this->routePluginManager = $routePluginManager;
57 $this->init();
60 /**
61 * factory(): defined by RouteInterface interface.
63 * @see \Zend\Mvc\Router\RouteInterface::factory()
64 * @param array|Traversable $options
65 * @return SimpleRouteStack
66 * @throws Exception\InvalidArgumentException
68 public static function factory($options = array())
70 if ($options instanceof Traversable) {
71 $options = ArrayUtils::iteratorToArray($options);
72 } elseif (!is_array($options)) {
73 throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
76 $routePluginManager = null;
77 if (isset($options['route_plugins'])) {
78 $routePluginManager = $options['route_plugins'];
81 $instance = new static($routePluginManager);
83 if (isset($options['routes'])) {
84 $instance->addRoutes($options['routes']);
87 if (isset($options['default_params'])) {
88 $instance->setDefaultParams($options['default_params']);
91 return $instance;
94 /**
95 * Init method for extending classes.
97 * @return void
99 protected function init()
104 * Set the route plugin manager.
106 * @param RoutePluginManager $routePlugins
107 * @return SimpleRouteStack
109 public function setRoutePluginManager(RoutePluginManager $routePlugins)
111 $this->routePluginManager = $routePlugins;
112 return $this;
116 * Get the route plugin manager.
118 * @return RoutePluginManager
120 public function getRoutePluginManager()
122 return $this->routePluginManager;
126 * addRoutes(): defined by RouteStackInterface interface.
128 * @see RouteStackInterface::addRoutes()
129 * @param array|Traversable $routes
130 * @return SimpleRouteStack
131 * @throws Exception\InvalidArgumentException
133 public function addRoutes($routes)
135 if (!is_array($routes) && !$routes instanceof Traversable) {
136 throw new Exception\InvalidArgumentException('addRoutes expects an array or Traversable set of routes');
139 foreach ($routes as $name => $route) {
140 $this->addRoute($name, $route);
143 return $this;
147 * addRoute(): defined by RouteStackInterface interface.
149 * @see RouteStackInterface::addRoute()
150 * @param string $name
151 * @param mixed $route
152 * @param int $priority
153 * @return SimpleRouteStack
155 public function addRoute($name, $route, $priority = null)
157 if (!$route instanceof RouteInterface) {
158 $route = $this->routeFromArray($route);
161 if ($priority === null && isset($route->priority)) {
162 $priority = $route->priority;
165 $this->routes->insert($name, $route, $priority);
167 return $this;
171 * removeRoute(): defined by RouteStackInterface interface.
173 * @see RouteStackInterface::removeRoute()
174 * @param string $name
175 * @return SimpleRouteStack
177 public function removeRoute($name)
179 $this->routes->remove($name);
180 return $this;
184 * setRoutes(): defined by RouteStackInterface interface.
186 * @param array|Traversable $routes
187 * @return SimpleRouteStack
189 public function setRoutes($routes)
191 $this->routes->clear();
192 $this->addRoutes($routes);
193 return $this;
197 * Get the added routes
199 * @return Traversable list of all routes
201 public function getRoutes()
203 return $this->routes;
207 * Check if a route with a specific name exists
209 * @param string $name
210 * @return bool true if route exists
212 public function hasRoute($name)
214 return $this->routes->get($name) !== null;
218 * Get a route by name
220 * @param string $name
221 * @return RouteInterface the route
223 public function getRoute($name)
225 return $this->routes->get($name);
229 * Set a default parameters.
231 * @param array $params
232 * @return SimpleRouteStack
234 public function setDefaultParams(array $params)
236 $this->defaultParams = $params;
237 return $this;
241 * Set a default parameter.
243 * @param string $name
244 * @param mixed $value
245 * @return SimpleRouteStack
247 public function setDefaultParam($name, $value)
249 $this->defaultParams[$name] = $value;
250 return $this;
254 * Create a route from array specifications.
256 * @param array|Traversable $specs
257 * @return RouteInterface
258 * @throws Exception\InvalidArgumentException
260 protected function routeFromArray($specs)
262 if ($specs instanceof Traversable) {
263 $specs = ArrayUtils::iteratorToArray($specs);
264 } elseif (!is_array($specs)) {
265 throw new Exception\InvalidArgumentException('Route definition must be an array or Traversable object');
268 if (!isset($specs['type'])) {
269 throw new Exception\InvalidArgumentException('Missing "type" option');
270 } elseif (!isset($specs['options'])) {
271 $specs['options'] = array();
274 $route = $this->getRoutePluginManager()->get($specs['type'], $specs['options']);
276 if (isset($specs['priority'])) {
277 $route->priority = $specs['priority'];
280 return $route;
284 * match(): defined by RouteInterface interface.
286 * @see \Zend\Mvc\Router\RouteInterface::match()
287 * @param Request $request
288 * @return RouteMatch|null
290 public function match(Request $request)
292 foreach ($this->routes as $name => $route) {
293 if (($match = $route->match($request)) instanceof RouteMatch) {
294 $match->setMatchedRouteName($name);
296 foreach ($this->defaultParams as $paramName => $value) {
297 if ($match->getParam($paramName) === null) {
298 $match->setParam($paramName, $value);
302 return $match;
306 return;
310 * assemble(): defined by RouteInterface interface.
312 * @see \Zend\Mvc\Router\RouteInterface::assemble()
313 * @param array $params
314 * @param array $options
315 * @return mixed
316 * @throws Exception\InvalidArgumentException
317 * @throws Exception\RuntimeException
319 public function assemble(array $params = array(), array $options = array())
321 if (!isset($options['name'])) {
322 throw new Exception\InvalidArgumentException('Missing "name" option');
325 $route = $this->routes->get($options['name']);
327 if (!$route) {
328 throw new Exception\RuntimeException(sprintf('Route with name "%s" not found', $options['name']));
331 unset($options['name']);
333 return $route->assemble(array_merge($this->defaultParams, $params), $options);