3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license.
29 * Accepted HTTP methods for this route.
32 private $methods = array('GET', 'POST', 'PUT', 'DELETE');
35 * Target for this route, can be anything.
41 * The name of this route, used for reversed routing
47 * Custom parameter filters for this route
50 private $filters = array();
53 * Array containing parameters passed through request URL
56 private $parameters = array();
59 * Set named parameters to target method
60 * @example [ [0] => [ ["link_id"] => "12312" ] ]
63 private $parametersByName;
72 * @param array $config
74 public function __construct($resource, array $config)
76 $this->url = $resource;
77 $this->config = $config;
78 $this->methods = isset($config['methods']) ? (array) $config['methods'] : [];
79 $this->target = isset($config['target']) ? $config['target'] : null;
80 $this->name = isset($config['name']) ? $config['name'] : null;
83 public function getUrl()
88 public function setUrl($url)
92 // make sure that the URL is suffixed with a forward slash
93 if (substr($url, -1) !== '/') {
100 public function getTarget()
102 return $this->target;
105 public function setTarget($target)
107 $this->target = $target;
110 public function getMethods()
112 return $this->methods;
115 public function setMethods(array $methods)
117 $this->methods = $methods;
120 public function getName()
125 public function setName($name)
127 $this->name = (string)$name;
130 public function setFilters(array $filters, $parametersByName = false)
132 $this->filters = $filters;
134 if ($parametersByName) {
135 $this->parametersByName = true;
139 public function getRegex()
141 return preg_replace_callback("/(:\w+)/", array(&$this, 'substituteFilter'), $this->url);
144 private function substituteFilter($matches)
146 if (isset($matches[1]) && isset($this->filters[$matches[1]])) {
147 return $this->filters[$matches[1]];
153 public function getParameters()
155 return $this->parameters;
158 public function setParameters(array $parameters)
160 $this->parameters = $parameters;
163 public function dispatch()
165 $action = explode('::', $this->config['_controller']);
166 $instance = new $action[0];
168 if ($this->parametersByName) {
169 $this->parameters = array($this->parameters);
172 call_user_func_array(array($instance, $action[1]), $this->parameters);