use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Router / Console / Simple.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-2010 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 /**
11 * @namespace
12 */
13 namespace Zend\Mvc\Router\Console;
14
15 use Traversable;
16 use Zend\Console\RouteMatcher\DefaultRouteMatcher;
17 use Zend\Console\Request as ConsoleRequest;
18 use Zend\Console\RouteMatcher\RouteMatcherInterface;
19 use Zend\Filter\FilterChain;
20 use Zend\Mvc\Router\Exception;
21 use Zend\Stdlib\ArrayUtils;
22 use Zend\Stdlib\RequestInterface as Request;
23 use Zend\Validator\ValidatorChain;
24
25 /**
26 * Segment route.
27 *
28 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
29 * @license http://framework.zend.com/license/new-bsd New BSD License
30 * @see http://guides.rubyonrails.org/routing.html
31 */
32 class Simple implements RouteInterface
33 {
34 /**
35 * List of assembled parameters.
36 *
37 * @var array
38 */
39 protected $assembledParams = [];
40
41 /**
42 * @var RouteMatcherInterface
43 */
44 protected $matcher;
45
46 /**
47 * Create a new simple console route.
48 *
49 * @param string|RouteMatcherInterface $routeOrRouteMatcher
50 * @param array $constraints
51 * @param array $defaults
52 * @param array $aliases
53 * @param null|array|Traversable|FilterChain $filters
54 * @param null|array|Traversable|ValidatorChain $validators
55 * @throws Exception\InvalidArgumentException
56 */
57 public function __construct(
58 $routeOrRouteMatcher,
59 array $constraints = [],
60 array $defaults = [],
61 array $aliases = [],
62 $filters = null,
63 $validators = null
64 ) {
65 if (is_string($routeOrRouteMatcher)) {
66 $this->matcher = new DefaultRouteMatcher($routeOrRouteMatcher, $constraints, $defaults, $aliases);
67 } elseif ($routeOrRouteMatcher instanceof RouteMatcherInterface) {
68 $this->matcher = $routeOrRouteMatcher;
69 } else {
70 throw new Exception\InvalidArgumentException(
71 "routeOrRouteMatcher should either be string, or class implementing RouteMatcherInterface. "
72 . gettype($routeOrRouteMatcher) . " was given."
73 );
74 }
75 }
76
77 /**
78 * factory(): defined by Route interface.
79 *
80 * @see \Zend\Mvc\Router\RouteInterface::factory()
81 * @param array|Traversable $options
82 * @throws Exception\InvalidArgumentException
83 * @return self
84 */
85 public static function factory($options = [])
86 {
87 if ($options instanceof Traversable) {
88 $options = ArrayUtils::iteratorToArray($options);
89 } elseif (!is_array($options)) {
90 throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
91 }
92
93 if (!isset($options['route'])) {
94 throw new Exception\InvalidArgumentException('Missing "route" in options array');
95 }
96
97 foreach ([
98 'constraints',
99 'defaults',
100 'aliases',
101 ] as $opt) {
102 if (!isset($options[$opt])) {
103 $options[$opt] = [];
104 }
105 }
106
107 if (!isset($options['validators'])) {
108 $options['validators'] = null;
109 }
110
111 if (!isset($options['filters'])) {
112 $options['filters'] = null;
113 }
114
115 return new static(
116 $options['route'],
117 $options['constraints'],
118 $options['defaults'],
119 $options['aliases'],
120 $options['filters'],
121 $options['validators']
122 );
123 }
124
125 /**
126 * match(): defined by Route interface.
127 *
128 * @see Route::match()
129 * @param Request $request
130 * @param null|int $pathOffset
131 * @return RouteMatch
132 */
133 public function match(Request $request, $pathOffset = null)
134 {
135 if (!$request instanceof ConsoleRequest) {
136 return;
137 }
138
139 $params = $request->getParams()->toArray();
140 $matches = $this->matcher->match($params);
141
142 if (null !== $matches) {
143 return new RouteMatch($matches);
144 }
145 return;
146 }
147
148 /**
149 * assemble(): Defined by Route interface.
150 *
151 * @see \Zend\Mvc\Router\RouteInterface::assemble()
152 * @param array $params
153 * @param array $options
154 * @return mixed
155 */
156 public function assemble(array $params = [], array $options = [])
157 {
158 $this->assembledParams = [];
159 }
160
161 /**
162 * getAssembledParams(): defined by Route interface.
163 *
164 * @see RouteInterface::getAssembledParams
165 * @return array
166 */
167 public function getAssembledParams()
168 {
169 return $this->assembledParams;
170 }
171 }