OSDN Git Service

バッファーの追加
[php-libraries/Router.git] / src / Route.php
1 <?php
2 /**
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
4  * CONTRIBUTORS
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
16  * DAMAGE.
17  *
18  * This software consists of voluntary contributions made by many
19  * individuals and is licensed under the MIT license.
20  */
21
22 namespace PHPRouter;
23
24 class Route
25 {
26     /**
27      * URL of this Route
28      * @var string
29      */
30     private $url;
31
32     /**
33      * Accepted HTTP methods for this route.
34      *
35      * @var string[]
36      */
37     private $methods;
38
39     /**
40      * Target for this route, can be anything.
41      * @var mixed
42      */
43     private $target;
44
45     /**
46      * The name of this route, used for reversed routing
47      * @var string
48      */
49     private $name;
50
51     /**
52      * Custom parameter filters for this route
53      * @var array
54      */
55     private $filters = array();
56
57     /**
58      * Array containing parameters passed through request URL
59      * @var array
60      */
61     private $parameters = array();
62
63     /**
64      * Set named parameters to target method
65      * @example [ [0] => [ ["link_id"] => "12312" ] ]
66      * @var bool
67      */
68     private $parametersByName;
69
70     /**
71      * @var string
72      */
73     private $action;
74
75     /**
76      * @var array
77      */
78     private $config;
79
80     /**
81      * @param       $resource
82      * @param array $config
83      */
84     public function __construct($resource, array $config)
85     {
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;
94     }
95
96     public function getUrl()
97     {
98         return $this->url;
99     }
100
101     public function setUrl($url)
102     {
103         $url = (string)$url;
104
105         // make sure that the URL is suffixed with a forward slash
106         if (substr($url, -1) !== '/') {
107             $url .= '/';
108         }
109
110         $this->url = $url;
111     }
112
113     public function getTarget()
114     {
115         return $this->target;
116     }
117
118     public function setTarget($target)
119     {
120         $this->target = $target;
121     }
122
123     public function getMethods()
124     {
125         return $this->methods;
126     }
127
128     public function setMethods(array $methods)
129     {
130         $this->methods = $methods;
131     }
132
133     public function getName()
134     {
135         return $this->name;
136     }
137
138     public function setName($name)
139     {
140         $this->name = (string)$name;
141     }
142
143     public function setFilters(array $filters, $parametersByName = false)
144     {
145         $this->filters          = $filters;
146         $this->parametersByName = $parametersByName;
147     }
148
149     public function getRegex()
150     {
151         return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url);
152     }
153
154     private function substituteFilter($matches)
155     {
156         if (isset($matches[1], $this->filters[$matches[1]])) {
157             return $this->filters[$matches[1]];
158         }
159
160         return '([\w-%]+)';
161     }
162
163     public function getParameters()
164     {
165         return $this->parameters;
166     }
167
168     public function setParameters(array $parameters)
169     {
170         $this->parameters = $parameters;
171     }
172
173     public function getValidRouteAction()
174     {
175         $action = explode('::', $this->config['_controller']);
176         $class  = @$action[0];
177         $method = @$action[1];
178
179         if ( !class_exists($class)) {
180             return null;
181         }
182
183         $instance = new $class();
184
185         if (empty($action[1]) || trim($action[1]) === '') {
186             $method = "__invoke";
187         }
188
189         if ( !method_exists($instance, $method)) {
190             return null;
191         }
192
193         return $this->config['_controller'];
194     }
195
196     public function dispatch()
197     {
198         $action   = explode('::', $this->config['_controller']);
199         $instance = new $action[0];
200
201         if ($this->parametersByName) {
202             $this->parameters = array($this->parameters);
203         }
204
205         ob_start();
206
207         if (empty($action[1]) || trim($action[1]) === '') {
208             // __invoke on a class
209             call_user_func_array($instance, $this->parameters);
210         } else {
211             call_user_func_array(array($instance, $action[1]), $this->parameters);
212         }
213
214         $result = ob_get_clean();
215
216         return $result;
217     }
218
219     public function getAction()
220     {
221         return $this->action;
222     }
223 }