3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
7 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
8 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
9 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
10 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
11 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
12 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
13 * CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
15 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
18 * This software consists of voluntary contributions made by many
19 * individuals and is licensed under the MIT license.
33 * Accepted HTTP methods for this route.
40 * Target for this route, can be anything.
46 * The name of this route, used for reversed routing
52 * Custom parameter filters for this route
55 private $filters = array();
58 * Array containing parameters passed through request URL
61 private $parameters = array();
64 * Set named parameters to target method
65 * @example [ [0] => [ ["link_id"] => "12312" ] ]
68 private $parametersByName;
82 * @param array $config
84 public function __construct($resource, array $config)
86 $this->url = $resource;
87 $this->config = $config;
88 $this->methods = isset($config['methods']) ? (array)$config['methods'] : array();
89 $this->target = isset($config['target']) ? $config['target'] : null;
90 $this->name = isset($config['name']) ? $config['name'] : null;
91 $this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
92 $action = explode('::', $this->config['_controller']);
93 $this->action = isset($action[1]) ? $action[1] : null;
96 public function getUrl()
101 public function setUrl($url)
105 // make sure that the URL is suffixed with a forward slash
106 if (substr($url, -1) !== '/') {
113 public function getTarget()
115 return $this->target;
118 public function setTarget($target)
120 $this->target = $target;
123 public function getMethods()
125 return $this->methods;
128 public function setMethods(array $methods)
130 $this->methods = $methods;
133 public function getName()
138 public function setName($name)
140 $this->name = (string)$name;
143 public function setFilters(array $filters, $parametersByName = false)
145 $this->filters = $filters;
146 $this->parametersByName = $parametersByName;
149 public function getRegex()
151 return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url);
154 private function substituteFilter($matches)
156 if (isset($matches[1], $this->filters[$matches[1]])) {
157 return $this->filters[$matches[1]];
163 public function getParameters()
165 return $this->parameters;
168 public function setParameters(array $parameters)
170 $this->parameters = $parameters;
173 public function getValidRouteAction()
175 $action = explode('::', $this->config['_controller']);
176 $class = @$action[0];
177 $method = @$action[1];
179 if ( !class_exists($class)) {
183 $instance = new $class();
185 if (empty($action[1]) || trim($action[1]) === '') {
186 $method = "__invoke";
189 if ( !method_exists($instance, $method)) {
193 return $this->config['_controller'];
196 public function dispatch()
198 $action = explode('::', $this->config['_controller']);
199 $instance = new $action[0];
201 if ($this->parametersByName) {
202 $this->parameters = array($this->parameters);
207 if (empty($action[1]) || trim($action[1]) === '') {
208 // __invoke on a class
209 call_user_func_array($instance, $this->parameters);
211 call_user_func_array(array($instance, $action[1]), $this->parameters);
214 $result = ob_get_clean();
219 public function getAction()
221 return $this->action;