use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Controller / Plugin / Url.php
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
10 namespace Zend\Mvc\Controller\Plugin;
11
12 use Traversable;
13 use Zend\EventManager\EventInterface;
14 use Zend\Mvc\Exception;
15 use Zend\Mvc\InjectApplicationEventInterface;
16 use Zend\Mvc\ModuleRouteListener;
17 use Zend\Mvc\MvcEvent;
18 use Zend\Mvc\Router\RouteStackInterface;
19
20 class Url extends AbstractPlugin
21 {
22 /**
23 * Generates a URL based on a route
24 *
25 * @param string $route RouteInterface name
26 * @param array|Traversable $params Parameters to use in url generation, if any
27 * @param array|bool $options RouteInterface-specific options to use in url generation, if any.
28 * If boolean, and no fourth argument, used as $reuseMatchedParams.
29 * @param bool $reuseMatchedParams Whether to reuse matched parameters
30 *
31 * @throws \Zend\Mvc\Exception\RuntimeException
32 * @throws \Zend\Mvc\Exception\InvalidArgumentException
33 * @throws \Zend\Mvc\Exception\DomainException
34 * @return string
35 */
36 public function fromRoute($route = null, $params = [], $options = [], $reuseMatchedParams = false)
37 {
38 $controller = $this->getController();
39 if (!$controller instanceof InjectApplicationEventInterface) {
40 throw new Exception\DomainException('Url plugin requires a controller that implements InjectApplicationEventInterface');
41 }
42
43 if (!is_array($params)) {
44 if (!$params instanceof Traversable) {
45 throw new Exception\InvalidArgumentException(
46 'Params is expected to be an array or a Traversable object'
47 );
48 }
49 $params = iterator_to_array($params);
50 }
51
52 $event = $controller->getEvent();
53 $router = null;
54 $matches = null;
55 if ($event instanceof MvcEvent) {
56 $router = $event->getRouter();
57 $matches = $event->getRouteMatch();
58 } elseif ($event instanceof EventInterface) {
59 $router = $event->getParam('router', false);
60 $matches = $event->getParam('route-match', false);
61 }
62 if (!$router instanceof RouteStackInterface) {
63 throw new Exception\DomainException('Url plugin requires that controller event compose a router; none found');
64 }
65
66 if (3 == func_num_args() && is_bool($options)) {
67 $reuseMatchedParams = $options;
68 $options = [];
69 }
70
71 if ($route === null) {
72 if (!$matches) {
73 throw new Exception\RuntimeException('No RouteMatch instance present');
74 }
75
76 $route = $matches->getMatchedRouteName();
77
78 if ($route === null) {
79 throw new Exception\RuntimeException('RouteMatch does not contain a matched route name');
80 }
81 }
82
83 if ($reuseMatchedParams && $matches) {
84 $routeMatchParams = $matches->getParams();
85
86 if (isset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
87 $routeMatchParams['controller'] = $routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER];
88 unset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER]);
89 }
90
91 if (isset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE])) {
92 unset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE]);
93 }
94
95 $params = array_merge($routeMatchParams, $params);
96 }
97
98 $options['name'] = $route;
99 return $router->assemble($params, $options);
100 }
101 }