@@ -42,12 +42,12 @@ |
||
42 | 42 | { |
43 | 43 | $delimiter = ViewFinder::HINT_PATH_DELIMITER; |
44 | 44 | |
45 | - if (strpos($name, $delimiter) === false) { |
|
46 | - return str_replace('/', '.', $name); |
|
47 | - } |
|
45 | + if (strpos($name, $delimiter) === false) { |
|
46 | + return str_replace('/', '.', $name); |
|
47 | + } |
|
48 | 48 | |
49 | - [$namespace, $name] = explode($delimiter, $name); |
|
49 | + [$namespace, $name] = explode($delimiter, $name); |
|
50 | 50 | |
51 | - return $namespace.$delimiter.str_replace('/', '.', $name); |
|
51 | + return $namespace.$delimiter.str_replace('/', '.', $name); |
|
52 | 52 | } |
53 | 53 | } |
54 | 54 | \ No newline at end of file |
@@ -38,313 +38,313 @@ |
||
38 | 38 | */ |
39 | 39 | class Factory implements FactoryContract |
40 | 40 | { |
41 | - use Extensions, |
|
42 | - Concerns\ManagesLayouts, |
|
43 | - Concerns\ManagesComponents, |
|
44 | - Concerns\ManagesTranslations; |
|
41 | + use Extensions, |
|
42 | + Concerns\ManagesLayouts, |
|
43 | + Concerns\ManagesComponents, |
|
44 | + Concerns\ManagesTranslations; |
|
45 | 45 | |
46 | - /** |
|
47 | - * The IoC container instance. |
|
48 | - * |
|
49 | - * @var \Syscodes\Contracts\Container\Container $container |
|
50 | - */ |
|
51 | - protected $container; |
|
46 | + /** |
|
47 | + * The IoC container instance. |
|
48 | + * |
|
49 | + * @var \Syscodes\Contracts\Container\Container $container |
|
50 | + */ |
|
51 | + protected $container; |
|
52 | 52 | |
53 | - /** |
|
54 | - * The engine implementation. |
|
55 | - * |
|
56 | - * @var \Syscodes\View\Engines\EngineResolver $engines |
|
57 | - */ |
|
58 | - protected $engines; |
|
53 | + /** |
|
54 | + * The engine implementation. |
|
55 | + * |
|
56 | + * @var \Syscodes\View\Engines\EngineResolver $engines |
|
57 | + */ |
|
58 | + protected $engines; |
|
59 | 59 | |
60 | - /** |
|
61 | - * The event dispatcher instance. |
|
62 | - * |
|
63 | - * @var \Syscodes\Contracts\Events\Dispatcher $events |
|
64 | - */ |
|
65 | - protected $events; |
|
60 | + /** |
|
61 | + * The event dispatcher instance. |
|
62 | + * |
|
63 | + * @var \Syscodes\Contracts\Events\Dispatcher $events |
|
64 | + */ |
|
65 | + protected $events; |
|
66 | 66 | |
67 | - /** |
|
68 | - * The view finder implementation. |
|
69 | - * |
|
70 | - * @var \Syscodes\View\FileViewFinder $finder |
|
71 | - */ |
|
72 | - protected $finder; |
|
67 | + /** |
|
68 | + * The view finder implementation. |
|
69 | + * |
|
70 | + * @var \Syscodes\View\FileViewFinder $finder |
|
71 | + */ |
|
72 | + protected $finder; |
|
73 | 73 | |
74 | - /** |
|
75 | - * The number of active rendering operations. |
|
76 | - * |
|
77 | - * @var int $renderCount |
|
78 | - */ |
|
79 | - protected $renderCount = 0; |
|
74 | + /** |
|
75 | + * The number of active rendering operations. |
|
76 | + * |
|
77 | + * @var int $renderCount |
|
78 | + */ |
|
79 | + protected $renderCount = 0; |
|
80 | 80 | |
81 | - /** |
|
82 | - * Array of shared data. |
|
83 | - * |
|
84 | - * @var array $shared |
|
85 | - */ |
|
86 | - protected $shared = []; |
|
81 | + /** |
|
82 | + * Array of shared data. |
|
83 | + * |
|
84 | + * @var array $shared |
|
85 | + */ |
|
86 | + protected $shared = []; |
|
87 | 87 | |
88 | - /** |
|
89 | - * Constructor: Create a new Parser class instance. |
|
90 | - * |
|
91 | - * @param \Syscodes\View\Engines\EngineResolver $engine |
|
92 | - * @param \Syscodes\Contracts\View\ViewFinder $finder |
|
93 | - * @param \Syscodes\Contracts\Events\Dispatcher $events |
|
94 | - * |
|
95 | - * @return void |
|
96 | - */ |
|
97 | - public function __construct(EngineResolver $engines, ViewFinder $finder, Dispatcher $events) |
|
98 | - { |
|
99 | - $this->finder = $finder; |
|
100 | - $this->engines = $engines; |
|
101 | - $this->events = $events; |
|
88 | + /** |
|
89 | + * Constructor: Create a new Parser class instance. |
|
90 | + * |
|
91 | + * @param \Syscodes\View\Engines\EngineResolver $engine |
|
92 | + * @param \Syscodes\Contracts\View\ViewFinder $finder |
|
93 | + * @param \Syscodes\Contracts\Events\Dispatcher $events |
|
94 | + * |
|
95 | + * @return void |
|
96 | + */ |
|
97 | + public function __construct(EngineResolver $engines, ViewFinder $finder, Dispatcher $events) |
|
98 | + { |
|
99 | + $this->finder = $finder; |
|
100 | + $this->engines = $engines; |
|
101 | + $this->events = $events; |
|
102 | 102 | |
103 | - $this->share('__env', $this); |
|
104 | - } |
|
103 | + $this->share('__env', $this); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Check existance view file. |
|
108 | - * |
|
109 | - * @param string $view |
|
110 | - * |
|
111 | - * @return bool |
|
112 | - */ |
|
113 | - public function viewExists($view) |
|
114 | - { |
|
115 | - try { |
|
116 | - $this->finder->find($view); |
|
117 | - } catch(InvalidArgumentException $e) { |
|
118 | - return false; |
|
119 | - } |
|
106 | + /** |
|
107 | + * Check existance view file. |
|
108 | + * |
|
109 | + * @param string $view |
|
110 | + * |
|
111 | + * @return bool |
|
112 | + */ |
|
113 | + public function viewExists($view) |
|
114 | + { |
|
115 | + try { |
|
116 | + $this->finder->find($view); |
|
117 | + } catch(InvalidArgumentException $e) { |
|
118 | + return false; |
|
119 | + } |
|
120 | 120 | |
121 | - return true; |
|
122 | - } |
|
121 | + return true; |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * Global and local data are merged and extracted to create local variables within the view file. |
|
126 | - * Renders the view object to a string. |
|
127 | - * |
|
128 | - * @example $output = $view->make(); |
|
129 | - * |
|
130 | - * @param string $view View filename |
|
131 | - * @param array $data Array of values |
|
132 | - * |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public function make($view, $data = []) |
|
136 | - { |
|
137 | - $path = $this->finder->find( |
|
138 | - $view = $this->normalizeName($view) |
|
139 | - ); |
|
124 | + /** |
|
125 | + * Global and local data are merged and extracted to create local variables within the view file. |
|
126 | + * Renders the view object to a string. |
|
127 | + * |
|
128 | + * @example $output = $view->make(); |
|
129 | + * |
|
130 | + * @param string $view View filename |
|
131 | + * @param array $data Array of values |
|
132 | + * |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public function make($view, $data = []) |
|
136 | + { |
|
137 | + $path = $this->finder->find( |
|
138 | + $view = $this->normalizeName($view) |
|
139 | + ); |
|
140 | 140 | |
141 | - // Loader class instance. |
|
142 | - return take($this->viewInstance($view, $path, $data), function ($view) { |
|
143 | - $this->callCreator($view); |
|
144 | - }); |
|
145 | - } |
|
141 | + // Loader class instance. |
|
142 | + return take($this->viewInstance($view, $path, $data), function ($view) { |
|
143 | + $this->callCreator($view); |
|
144 | + }); |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * Normalize a view name. |
|
149 | - * |
|
150 | - * @param string $name |
|
151 | - * |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - protected function normalizeName($name) |
|
147 | + /** |
|
148 | + * Normalize a view name. |
|
149 | + * |
|
150 | + * @param string $name |
|
151 | + * |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + protected function normalizeName($name) |
|
155 | 155 | { |
156 | - return ViewName::normalize($name); |
|
156 | + return ViewName::normalize($name); |
|
157 | 157 | } |
158 | 158 | |
159 | - /** |
|
160 | - * Create a new view instance from the given arguments. |
|
161 | - * |
|
162 | - * @param string $file View filename |
|
163 | - * * @param string $path Path filename |
|
164 | - * @param array $data Array of values |
|
165 | - * |
|
166 | - * @return \Syscodes\Contracts\View\View |
|
167 | - */ |
|
168 | - protected function viewInstance($view, $path, $data) |
|
169 | - { |
|
170 | - return new View($this, $this->getEngineFromPath($path), $view, $path, $data); |
|
171 | - } |
|
159 | + /** |
|
160 | + * Create a new view instance from the given arguments. |
|
161 | + * |
|
162 | + * @param string $file View filename |
|
163 | + * * @param string $path Path filename |
|
164 | + * @param array $data Array of values |
|
165 | + * |
|
166 | + * @return \Syscodes\Contracts\View\View |
|
167 | + */ |
|
168 | + protected function viewInstance($view, $path, $data) |
|
169 | + { |
|
170 | + return new View($this, $this->getEngineFromPath($path), $view, $path, $data); |
|
171 | + } |
|
172 | 172 | |
173 | - /** |
|
174 | - * Get the appropriate view engine for the given path. |
|
175 | - * |
|
176 | - * @param string $path |
|
177 | - * |
|
178 | - * @return \Illuminate\Contracts\View\Engine |
|
179 | - * |
|
180 | - * @throws \InvalidArgumentException |
|
181 | - */ |
|
182 | - public function getEngineFromPath($path) |
|
183 | - { |
|
184 | - if ( ! $extension = $this->getExtension($path)) { |
|
185 | - throw new InvalidArgumentException("Unrecognized extension in file: {$path}"); |
|
186 | - } |
|
173 | + /** |
|
174 | + * Get the appropriate view engine for the given path. |
|
175 | + * |
|
176 | + * @param string $path |
|
177 | + * |
|
178 | + * @return \Illuminate\Contracts\View\Engine |
|
179 | + * |
|
180 | + * @throws \InvalidArgumentException |
|
181 | + */ |
|
182 | + public function getEngineFromPath($path) |
|
183 | + { |
|
184 | + if ( ! $extension = $this->getExtension($path)) { |
|
185 | + throw new InvalidArgumentException("Unrecognized extension in file: {$path}"); |
|
186 | + } |
|
187 | 187 | |
188 | - $engine = $this->extensions[$extension]; |
|
188 | + $engine = $this->extensions[$extension]; |
|
189 | 189 | |
190 | - return $this->engines->resolve($engine); |
|
191 | - } |
|
190 | + return $this->engines->resolve($engine); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * Get the extension used by the view file. |
|
195 | - * |
|
196 | - * @param string $path |
|
197 | - * |
|
198 | - * @return string |
|
199 | - */ |
|
200 | - protected function getExtension($path) |
|
201 | - { |
|
202 | - $extensions = array_keys($this->extensions); |
|
193 | + /** |
|
194 | + * Get the extension used by the view file. |
|
195 | + * |
|
196 | + * @param string $path |
|
197 | + * |
|
198 | + * @return string |
|
199 | + */ |
|
200 | + protected function getExtension($path) |
|
201 | + { |
|
202 | + $extensions = array_keys($this->extensions); |
|
203 | 203 | |
204 | - return Arr::first($extensions, function($key, $value) use ($path) { |
|
205 | - return Str::endsWith($path, '.'.$value); |
|
206 | - }); |
|
207 | - } |
|
204 | + return Arr::first($extensions, function($key, $value) use ($path) { |
|
205 | + return Str::endsWith($path, '.'.$value); |
|
206 | + }); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Call the creator for a given view. |
|
211 | - * |
|
212 | - * @param \Syscodes\View\View $view |
|
213 | - * |
|
214 | - * @return void |
|
215 | - */ |
|
216 | - public function callCreator(View $view) |
|
217 | - { |
|
218 | - $this->events->dispatch('creating: '.$view->getView(), [$view]); |
|
219 | - } |
|
209 | + /** |
|
210 | + * Call the creator for a given view. |
|
211 | + * |
|
212 | + * @param \Syscodes\View\View $view |
|
213 | + * |
|
214 | + * @return void |
|
215 | + */ |
|
216 | + public function callCreator(View $view) |
|
217 | + { |
|
218 | + $this->events->dispatch('creating: '.$view->getView(), [$view]); |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Get the extension to engine bindings. |
|
223 | - * |
|
224 | - * @return array |
|
225 | - */ |
|
226 | - public function getExtensions() |
|
227 | - { |
|
228 | - return $this->extensions; |
|
229 | - } |
|
221 | + /** |
|
222 | + * Get the extension to engine bindings. |
|
223 | + * |
|
224 | + * @return array |
|
225 | + */ |
|
226 | + public function getExtensions() |
|
227 | + { |
|
228 | + return $this->extensions; |
|
229 | + } |
|
230 | 230 | |
231 | - /** |
|
232 | - * Add a piece of shared data to the environment. |
|
233 | - * |
|
234 | - * @param array|string $key |
|
235 | - * @param mixed|null $value (null by default) |
|
236 | - * |
|
237 | - * @return mixed |
|
238 | - */ |
|
239 | - public function share($key, $value = null) |
|
240 | - { |
|
241 | - $keys = is_array($key) ? $key : [$key => $value]; |
|
231 | + /** |
|
232 | + * Add a piece of shared data to the environment. |
|
233 | + * |
|
234 | + * @param array|string $key |
|
235 | + * @param mixed|null $value (null by default) |
|
236 | + * |
|
237 | + * @return mixed |
|
238 | + */ |
|
239 | + public function share($key, $value = null) |
|
240 | + { |
|
241 | + $keys = is_array($key) ? $key : [$key => $value]; |
|
242 | 242 | |
243 | - foreach ($keys as $key => $value) { |
|
244 | - $this->shared[$key] = $value; |
|
245 | - } |
|
243 | + foreach ($keys as $key => $value) { |
|
244 | + $this->shared[$key] = $value; |
|
245 | + } |
|
246 | 246 | |
247 | - return $value; |
|
248 | - } |
|
247 | + return $value; |
|
248 | + } |
|
249 | 249 | |
250 | - /** |
|
251 | - * Replace the namespace hints for the given namespace. |
|
252 | - * |
|
253 | - * @param string $namespace |
|
254 | - * @param string|array $hints |
|
255 | - * |
|
256 | - * @return $this |
|
257 | - */ |
|
258 | - public function replaceNamespace($namespace, $hints) |
|
259 | - { |
|
260 | - $this->finder->replaceNamespace($namespace, $hints); |
|
250 | + /** |
|
251 | + * Replace the namespace hints for the given namespace. |
|
252 | + * |
|
253 | + * @param string $namespace |
|
254 | + * @param string|array $hints |
|
255 | + * |
|
256 | + * @return $this |
|
257 | + */ |
|
258 | + public function replaceNamespace($namespace, $hints) |
|
259 | + { |
|
260 | + $this->finder->replaceNamespace($namespace, $hints); |
|
261 | 261 | |
262 | - return $this; |
|
263 | - } |
|
262 | + return $this; |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * Increment the rendering counter. |
|
267 | - * |
|
268 | - * @return void |
|
269 | - */ |
|
270 | - public function increment() |
|
271 | - { |
|
272 | - return $this->renderCount++; |
|
273 | - } |
|
265 | + /** |
|
266 | + * Increment the rendering counter. |
|
267 | + * |
|
268 | + * @return void |
|
269 | + */ |
|
270 | + public function increment() |
|
271 | + { |
|
272 | + return $this->renderCount++; |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * Decrement the rendering counter. |
|
277 | - * |
|
278 | - * @return void |
|
279 | - */ |
|
280 | - public function decrement() |
|
281 | - { |
|
282 | - return $this->renderCount--; |
|
283 | - } |
|
275 | + /** |
|
276 | + * Decrement the rendering counter. |
|
277 | + * |
|
278 | + * @return void |
|
279 | + */ |
|
280 | + public function decrement() |
|
281 | + { |
|
282 | + return $this->renderCount--; |
|
283 | + } |
|
284 | 284 | |
285 | - /** |
|
286 | - * Check if there are no active render operations. |
|
287 | - * |
|
288 | - * @return bool |
|
289 | - */ |
|
290 | - public function doneRendering() |
|
291 | - { |
|
292 | - return $this->renderCount == 0; |
|
293 | - } |
|
285 | + /** |
|
286 | + * Check if there are no active render operations. |
|
287 | + * |
|
288 | + * @return bool |
|
289 | + */ |
|
290 | + public function doneRendering() |
|
291 | + { |
|
292 | + return $this->renderCount == 0; |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * Flush all of the parser state like sections. |
|
297 | - * |
|
298 | - * @return void |
|
299 | - */ |
|
300 | - public function flushState() |
|
301 | - { |
|
302 | - $this->renderCount = 0; |
|
295 | + /** |
|
296 | + * Flush all of the parser state like sections. |
|
297 | + * |
|
298 | + * @return void |
|
299 | + */ |
|
300 | + public function flushState() |
|
301 | + { |
|
302 | + $this->renderCount = 0; |
|
303 | 303 | |
304 | - $this->flushSections(); |
|
305 | - } |
|
304 | + $this->flushSections(); |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * Flush all of the section contents if done rendering. |
|
309 | - * |
|
310 | - * @return void |
|
311 | - */ |
|
312 | - public function flushStateIfDoneRendering() |
|
313 | - { |
|
314 | - if ($this->doneRendering()) { |
|
315 | - $this->flushState(); |
|
316 | - } |
|
317 | - } |
|
307 | + /** |
|
308 | + * Flush all of the section contents if done rendering. |
|
309 | + * |
|
310 | + * @return void |
|
311 | + */ |
|
312 | + public function flushStateIfDoneRendering() |
|
313 | + { |
|
314 | + if ($this->doneRendering()) { |
|
315 | + $this->flushState(); |
|
316 | + } |
|
317 | + } |
|
318 | 318 | |
319 | - /** |
|
320 | - * Get all of the shared data for the environment. |
|
321 | - * |
|
322 | - * @return void |
|
323 | - */ |
|
324 | - public function getShared() |
|
325 | - { |
|
326 | - return $this->shared; |
|
327 | - } |
|
319 | + /** |
|
320 | + * Get all of the shared data for the environment. |
|
321 | + * |
|
322 | + * @return void |
|
323 | + */ |
|
324 | + public function getShared() |
|
325 | + { |
|
326 | + return $this->shared; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * Get the IoC container instance. |
|
331 | - * |
|
332 | - * @return \Syscodes\Contracts\Container\Container |
|
333 | - */ |
|
334 | - public function getContainer() |
|
335 | - { |
|
336 | - return $this->container; |
|
337 | - } |
|
329 | + /** |
|
330 | + * Get the IoC container instance. |
|
331 | + * |
|
332 | + * @return \Syscodes\Contracts\Container\Container |
|
333 | + */ |
|
334 | + public function getContainer() |
|
335 | + { |
|
336 | + return $this->container; |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * Set the IoC container instance. |
|
341 | - * |
|
342 | - * @param \Syscodes\Contracts\Container\Container $container |
|
343 | - * |
|
344 | - * @return void |
|
345 | - */ |
|
346 | - public function setContainer($container) |
|
347 | - { |
|
348 | - $this->container = $container; |
|
349 | - } |
|
339 | + /** |
|
340 | + * Set the IoC container instance. |
|
341 | + * |
|
342 | + * @param \Syscodes\Contracts\Container\Container $container |
|
343 | + * |
|
344 | + * @return void |
|
345 | + */ |
|
346 | + public function setContainer($container) |
|
347 | + { |
|
348 | + $this->container = $container; |
|
349 | + } |
|
350 | 350 | } |
351 | 351 | \ No newline at end of file |
@@ -32,178 +32,178 @@ |
||
32 | 32 | */ |
33 | 33 | trait ManagesLayouts |
34 | 34 | { |
35 | - /** |
|
36 | - * Started blocks. |
|
37 | - * |
|
38 | - * @var array $sections |
|
39 | - */ |
|
40 | - protected $sections = []; |
|
35 | + /** |
|
36 | + * Started blocks. |
|
37 | + * |
|
38 | + * @var array $sections |
|
39 | + */ |
|
40 | + protected $sections = []; |
|
41 | 41 | |
42 | - /** |
|
43 | - * The stack of in-progress sections. |
|
44 | - * |
|
45 | - * @var array $sectionStack |
|
46 | - */ |
|
47 | - protected $sectionStack = []; |
|
42 | + /** |
|
43 | + * The stack of in-progress sections. |
|
44 | + * |
|
45 | + * @var array $sectionStack |
|
46 | + */ |
|
47 | + protected $sectionStack = []; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Starting section. |
|
51 | - * |
|
52 | - * @param string $section |
|
53 | - * @param string|null $content (null by default) |
|
54 | - * |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function beginSection($section, $content = null) |
|
58 | - { |
|
59 | - if (null === $content) { |
|
60 | - if (ob_start()) { |
|
61 | - $this->sectionStack[] = $section; |
|
62 | - } |
|
63 | - } else { |
|
64 | - $this->extendSection($section, $content instanceof View ? $content : e($content)); |
|
65 | - } |
|
66 | - } |
|
49 | + /** |
|
50 | + * Starting section. |
|
51 | + * |
|
52 | + * @param string $section |
|
53 | + * @param string|null $content (null by default) |
|
54 | + * |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function beginSection($section, $content = null) |
|
58 | + { |
|
59 | + if (null === $content) { |
|
60 | + if (ob_start()) { |
|
61 | + $this->sectionStack[] = $section; |
|
62 | + } |
|
63 | + } else { |
|
64 | + $this->extendSection($section, $content instanceof View ? $content : e($content)); |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Append content to a given section. |
|
70 | - * |
|
71 | - * @param string $section |
|
72 | - * @param string $content |
|
73 | - * |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - protected function extendSection($section, $content) |
|
77 | - { |
|
78 | - if (isset($this->sections[$section])) { |
|
79 | - $content = str_replace(static::parent(), $content, $this->sections[$section]); |
|
80 | - } |
|
68 | + /** |
|
69 | + * Append content to a given section. |
|
70 | + * |
|
71 | + * @param string $section |
|
72 | + * @param string $content |
|
73 | + * |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + protected function extendSection($section, $content) |
|
77 | + { |
|
78 | + if (isset($this->sections[$section])) { |
|
79 | + $content = str_replace(static::parent(), $content, $this->sections[$section]); |
|
80 | + } |
|
81 | 81 | |
82 | - $this->sections[$section] = $content; |
|
83 | - } |
|
82 | + $this->sections[$section] = $content; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Close and printing section. |
|
87 | - * |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function showSection() |
|
91 | - { |
|
92 | - if (empty($this->sectionStack)) { |
|
93 | - return ''; |
|
94 | - } |
|
85 | + /** |
|
86 | + * Close and printing section. |
|
87 | + * |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function showSection() |
|
91 | + { |
|
92 | + if (empty($this->sectionStack)) { |
|
93 | + return ''; |
|
94 | + } |
|
95 | 95 | |
96 | - return $this->giveContent($this->stopSection()); |
|
97 | - } |
|
96 | + return $this->giveContent($this->stopSection()); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Give sections the page view from the master page. |
|
101 | - * |
|
102 | - * @param string $name |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function giveContent($name, $default = '') |
|
107 | - { |
|
108 | - $sectionContent = $default instanceof View ? $default : e($default); |
|
99 | + /** |
|
100 | + * Give sections the page view from the master page. |
|
101 | + * |
|
102 | + * @param string $name |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function giveContent($name, $default = '') |
|
107 | + { |
|
108 | + $sectionContent = $default instanceof View ? $default : e($default); |
|
109 | 109 | |
110 | - if (isset($this->sections[$name])) { |
|
111 | - $sectionContent = $this->sections[$name]; |
|
112 | - } |
|
110 | + if (isset($this->sections[$name])) { |
|
111 | + $sectionContent = $this->sections[$name]; |
|
112 | + } |
|
113 | 113 | |
114 | - return str_replace(static::parent(), '', $sectionContent); |
|
115 | - } |
|
114 | + return str_replace(static::parent(), '', $sectionContent); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Closing section. |
|
119 | - * |
|
120 | - * @param bool $overwrite (false by default) |
|
121 | - * |
|
122 | - * @return mixed |
|
123 | - * |
|
124 | - * @throws \InvalidArgumentException |
|
125 | - */ |
|
126 | - public function stopSection($overwrite = false) |
|
127 | - { |
|
128 | - if (empty($this->sectionStack)) { |
|
129 | - throw new InvalidArgumentException('You cannot finish a section without first starting with one.'); |
|
130 | - } |
|
117 | + /** |
|
118 | + * Closing section. |
|
119 | + * |
|
120 | + * @param bool $overwrite (false by default) |
|
121 | + * |
|
122 | + * @return mixed |
|
123 | + * |
|
124 | + * @throws \InvalidArgumentException |
|
125 | + */ |
|
126 | + public function stopSection($overwrite = false) |
|
127 | + { |
|
128 | + if (empty($this->sectionStack)) { |
|
129 | + throw new InvalidArgumentException('You cannot finish a section without first starting with one.'); |
|
130 | + } |
|
131 | 131 | |
132 | - $last = array_pop($this->sectionStack); |
|
132 | + $last = array_pop($this->sectionStack); |
|
133 | 133 | |
134 | - if ($overwrite) { |
|
135 | - $this->sections[$last] = ob_get_clean(); |
|
136 | - } else { |
|
137 | - $this->extendSection($last, ob_get_clean()); |
|
138 | - } |
|
134 | + if ($overwrite) { |
|
135 | + $this->sections[$last] = ob_get_clean(); |
|
136 | + } else { |
|
137 | + $this->extendSection($last, ob_get_clean()); |
|
138 | + } |
|
139 | 139 | |
140 | - return $last; |
|
141 | - } |
|
140 | + return $last; |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * Stop injecting content into a section and append it. |
|
145 | - * |
|
146 | - * @return string |
|
147 | - * |
|
148 | - * @throws \InvalidArgumentException |
|
149 | - */ |
|
150 | - public function appendSection() |
|
151 | - { |
|
152 | - if (empty($this->sectionStack)) { |
|
153 | - throw new InvalidArgumentException('You cannot finish a section without first starting with one.'); |
|
154 | - } |
|
143 | + /** |
|
144 | + * Stop injecting content into a section and append it. |
|
145 | + * |
|
146 | + * @return string |
|
147 | + * |
|
148 | + * @throws \InvalidArgumentException |
|
149 | + */ |
|
150 | + public function appendSection() |
|
151 | + { |
|
152 | + if (empty($this->sectionStack)) { |
|
153 | + throw new InvalidArgumentException('You cannot finish a section without first starting with one.'); |
|
154 | + } |
|
155 | 155 | |
156 | - $last = array_pop($this->sectionStack); |
|
156 | + $last = array_pop($this->sectionStack); |
|
157 | 157 | |
158 | - if (isset($this->sections[$last])) { |
|
159 | - $this->sections[$last] .= ob_get_clean(); |
|
160 | - } else { |
|
161 | - $this->sections[$last] = ob_get_clean(); |
|
162 | - } |
|
158 | + if (isset($this->sections[$last])) { |
|
159 | + $this->sections[$last] .= ob_get_clean(); |
|
160 | + } else { |
|
161 | + $this->sections[$last] = ob_get_clean(); |
|
162 | + } |
|
163 | 163 | |
164 | - return $last; |
|
165 | - } |
|
164 | + return $last; |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Check if section exists. |
|
169 | - * |
|
170 | - * @param string $name |
|
171 | - * |
|
172 | - * @return bool |
|
173 | - */ |
|
174 | - public function hasSection($name) |
|
175 | - { |
|
176 | - return array_key_exists($name, $this->sections); |
|
177 | - } |
|
167 | + /** |
|
168 | + * Check if section exists. |
|
169 | + * |
|
170 | + * @param string $name |
|
171 | + * |
|
172 | + * @return bool |
|
173 | + */ |
|
174 | + public function hasSection($name) |
|
175 | + { |
|
176 | + return array_key_exists($name, $this->sections); |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Get the entire array of sections. |
|
181 | - * |
|
182 | - * @return array |
|
183 | - */ |
|
184 | - public function getSections() |
|
185 | - { |
|
186 | - return $this->sections; |
|
187 | - } |
|
179 | + /** |
|
180 | + * Get the entire array of sections. |
|
181 | + * |
|
182 | + * @return array |
|
183 | + */ |
|
184 | + public function getSections() |
|
185 | + { |
|
186 | + return $this->sections; |
|
187 | + } |
|
188 | 188 | |
189 | - /** |
|
190 | - * Replace the @parent directive to a placeholder. |
|
191 | - * |
|
192 | - * @return string |
|
193 | - */ |
|
194 | - public static function parent() |
|
195 | - { |
|
196 | - return '@parent'; |
|
197 | - } |
|
189 | + /** |
|
190 | + * Replace the @parent directive to a placeholder. |
|
191 | + * |
|
192 | + * @return string |
|
193 | + */ |
|
194 | + public static function parent() |
|
195 | + { |
|
196 | + return '@parent'; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Flush all of the section contents. |
|
201 | - * |
|
202 | - * @return void |
|
203 | - */ |
|
204 | - public function flushSections() |
|
205 | - { |
|
206 | - $this->sections = []; |
|
207 | - $this->sectionStack = []; |
|
208 | - } |
|
199 | + /** |
|
200 | + * Flush all of the section contents. |
|
201 | + * |
|
202 | + * @return void |
|
203 | + */ |
|
204 | + public function flushSections() |
|
205 | + { |
|
206 | + $this->sections = []; |
|
207 | + $this->sectionStack = []; |
|
208 | + } |
|
209 | 209 | } |
210 | 210 | \ No newline at end of file |
@@ -42,469 +42,469 @@ |
||
42 | 42 | */ |
43 | 43 | class View implements ArrayAccess, Webable, ViewContract |
44 | 44 | { |
45 | - /** |
|
46 | - * Array of local variables. |
|
47 | - * |
|
48 | - * @var array $data |
|
49 | - */ |
|
50 | - protected $data = []; |
|
51 | - |
|
52 | - /** |
|
53 | - * The engine implementation. |
|
54 | - * |
|
55 | - * @var \Syscodes\Contracts\View\Engine $engine |
|
56 | - */ |
|
57 | - protected $engine; |
|
58 | - |
|
59 | - /** |
|
60 | - * The view factory instance. |
|
61 | - * |
|
62 | - * @var \Syscodes\View\factory $factory |
|
63 | - */ |
|
64 | - protected $factory; |
|
65 | - |
|
66 | - /** |
|
67 | - * The path to the view file. |
|
68 | - * |
|
69 | - * @var string $path |
|
70 | - */ |
|
71 | - protected $path; |
|
72 | - |
|
73 | - /** |
|
74 | - * Get the name of the view. |
|
75 | - * |
|
76 | - * @var string $view |
|
77 | - */ |
|
78 | - protected $view; |
|
79 | - |
|
80 | - /** |
|
81 | - * Constructor: Create a new view instance. |
|
82 | - * |
|
83 | - * @param \Syscodes\View\factory $factory |
|
84 | - * @param \Syscodes\Contracts\View\Engine $engine |
|
85 | - * @param string $view |
|
86 | - * @param string $path |
|
87 | - * @param array $data |
|
88 | - * |
|
89 | - * @return void |
|
90 | - * |
|
91 | - * @throws \InvalidArgumentException |
|
92 | - */ |
|
93 | - public function __construct(Factory $factory, Engine $engine, $view, $path, $data = []) |
|
94 | - { |
|
95 | - $this->factory = $factory; |
|
96 | - $this->engine = $engine; |
|
97 | - $this->view = $view; |
|
98 | - $this->path = $path; |
|
99 | - $this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Get the string contents of the view. |
|
104 | - * |
|
105 | - * @example View::render(); |
|
106 | - * |
|
107 | - * @param \Callable|null $callback (null by default) |
|
108 | - * |
|
109 | - * @return array|string |
|
110 | - * |
|
111 | - * @throws \Throwable |
|
112 | - */ |
|
113 | - public function render(Callable $callback = null) |
|
114 | - { |
|
115 | - try { |
|
116 | - $contents = $this->renderContents(); |
|
117 | - |
|
118 | - $response = isset($callback) ? $callback($this, $contents) : null; |
|
119 | - |
|
120 | - $this->factory->flushStateIfDoneRendering(); |
|
121 | - |
|
122 | - return ! is_null($response) ? $response : $contents; |
|
123 | - } catch(Throwable $e) { |
|
124 | - $this->factory->flushState(); |
|
125 | - |
|
126 | - throw $e; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get the contents of the view instance. |
|
132 | - * |
|
133 | - * @return void |
|
134 | - */ |
|
135 | - protected function renderContents() |
|
136 | - { |
|
137 | - $this->factory->increment(); |
|
138 | - |
|
139 | - $contents = $this->getContents(); |
|
140 | - |
|
141 | - $this->factory->decrement(); |
|
142 | - |
|
143 | - return $contents; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get the evaluated contents of the view. |
|
148 | - * |
|
149 | - * @return void |
|
150 | - */ |
|
151 | - protected function getContents() |
|
152 | - { |
|
153 | - return $this->engine->get($this->path, $this->getArrayData()); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * The view data will be extracted. |
|
158 | - * |
|
159 | - * @return array |
|
160 | - */ |
|
161 | - public function getArrayData() |
|
162 | - { |
|
163 | - $data = array_merge($this->factory->getShared(), $this->data); |
|
45 | + /** |
|
46 | + * Array of local variables. |
|
47 | + * |
|
48 | + * @var array $data |
|
49 | + */ |
|
50 | + protected $data = []; |
|
51 | + |
|
52 | + /** |
|
53 | + * The engine implementation. |
|
54 | + * |
|
55 | + * @var \Syscodes\Contracts\View\Engine $engine |
|
56 | + */ |
|
57 | + protected $engine; |
|
58 | + |
|
59 | + /** |
|
60 | + * The view factory instance. |
|
61 | + * |
|
62 | + * @var \Syscodes\View\factory $factory |
|
63 | + */ |
|
64 | + protected $factory; |
|
65 | + |
|
66 | + /** |
|
67 | + * The path to the view file. |
|
68 | + * |
|
69 | + * @var string $path |
|
70 | + */ |
|
71 | + protected $path; |
|
72 | + |
|
73 | + /** |
|
74 | + * Get the name of the view. |
|
75 | + * |
|
76 | + * @var string $view |
|
77 | + */ |
|
78 | + protected $view; |
|
79 | + |
|
80 | + /** |
|
81 | + * Constructor: Create a new view instance. |
|
82 | + * |
|
83 | + * @param \Syscodes\View\factory $factory |
|
84 | + * @param \Syscodes\Contracts\View\Engine $engine |
|
85 | + * @param string $view |
|
86 | + * @param string $path |
|
87 | + * @param array $data |
|
88 | + * |
|
89 | + * @return void |
|
90 | + * |
|
91 | + * @throws \InvalidArgumentException |
|
92 | + */ |
|
93 | + public function __construct(Factory $factory, Engine $engine, $view, $path, $data = []) |
|
94 | + { |
|
95 | + $this->factory = $factory; |
|
96 | + $this->engine = $engine; |
|
97 | + $this->view = $view; |
|
98 | + $this->path = $path; |
|
99 | + $this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Get the string contents of the view. |
|
104 | + * |
|
105 | + * @example View::render(); |
|
106 | + * |
|
107 | + * @param \Callable|null $callback (null by default) |
|
108 | + * |
|
109 | + * @return array|string |
|
110 | + * |
|
111 | + * @throws \Throwable |
|
112 | + */ |
|
113 | + public function render(Callable $callback = null) |
|
114 | + { |
|
115 | + try { |
|
116 | + $contents = $this->renderContents(); |
|
117 | + |
|
118 | + $response = isset($callback) ? $callback($this, $contents) : null; |
|
119 | + |
|
120 | + $this->factory->flushStateIfDoneRendering(); |
|
121 | + |
|
122 | + return ! is_null($response) ? $response : $contents; |
|
123 | + } catch(Throwable $e) { |
|
124 | + $this->factory->flushState(); |
|
125 | + |
|
126 | + throw $e; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get the contents of the view instance. |
|
132 | + * |
|
133 | + * @return void |
|
134 | + */ |
|
135 | + protected function renderContents() |
|
136 | + { |
|
137 | + $this->factory->increment(); |
|
138 | + |
|
139 | + $contents = $this->getContents(); |
|
140 | + |
|
141 | + $this->factory->decrement(); |
|
142 | + |
|
143 | + return $contents; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get the evaluated contents of the view. |
|
148 | + * |
|
149 | + * @return void |
|
150 | + */ |
|
151 | + protected function getContents() |
|
152 | + { |
|
153 | + return $this->engine->get($this->path, $this->getArrayData()); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * The view data will be extracted. |
|
158 | + * |
|
159 | + * @return array |
|
160 | + */ |
|
161 | + public function getArrayData() |
|
162 | + { |
|
163 | + $data = array_merge($this->factory->getShared(), $this->data); |
|
164 | 164 | |
165 | - return array_map(function ($value) { |
|
166 | - return ($value instanceof Renderable) ? $value->render() : $value; |
|
167 | - }, $data); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Get the sections of the rendered view. |
|
172 | - * |
|
173 | - * @return array |
|
174 | - * |
|
175 | - * @throws \Throwable |
|
176 | - */ |
|
177 | - public function renderSections() |
|
178 | - { |
|
179 | - return $this->render(function () { |
|
180 | - return $this->factory->getSections(); |
|
181 | - }); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Add a piece of data to the view. |
|
186 | - * |
|
187 | - * @example $view->assign($content, $data); |
|
188 | - * |
|
189 | - * @param string|array $key |
|
190 | - * @param mixed $value (null by default) |
|
191 | - * |
|
192 | - * @return $this |
|
193 | - */ |
|
194 | - public function assign($key, $value = null) |
|
195 | - { |
|
196 | - if (is_array($key)) { |
|
197 | - $this->data = array_merge($this->data, $key); |
|
198 | - } else { |
|
199 | - $this->data = [$key => $value]; |
|
200 | - } |
|
201 | - |
|
202 | - return $this; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Assigns a value by reference. The benefit of binding is that values can be altered |
|
207 | - * without re-setting them. It is also possible to bind variables before they have values. |
|
208 | - * Assigned values will be available as a variable within the view file: |
|
209 | - * |
|
210 | - * @example $view->bind('ref', $bar); |
|
211 | - * |
|
212 | - * @param string $key Variable name |
|
213 | - * @param mixed $value Referenced variable |
|
214 | - * |
|
215 | - * @return $this |
|
216 | - */ |
|
217 | - public function bind($key, & $value) |
|
218 | - { |
|
219 | - $this->data[$key] =& $value; |
|
220 | - |
|
221 | - return $this; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Get the array of view data. |
|
226 | - * |
|
227 | - * @return array |
|
228 | - */ |
|
229 | - public function getData() |
|
230 | - { |
|
231 | - return $this->data; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Get the name of the view. |
|
236 | - * |
|
237 | - * @return string |
|
238 | - */ |
|
239 | - public function getView() |
|
240 | - { |
|
241 | - return $this->view; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Get the path to the view file. |
|
246 | - * |
|
247 | - * @return string |
|
248 | - */ |
|
249 | - public function getPath() |
|
250 | - { |
|
251 | - return $this->path; |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Set the path to the view file. |
|
256 | - * |
|
257 | - * @param string $path |
|
258 | - * |
|
259 | - * @return void |
|
260 | - */ |
|
261 | - public function setPath($path) |
|
262 | - { |
|
263 | - $this->path = $path; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Get the view factory instance. |
|
268 | - * |
|
269 | - * @return \Syscodes\View\factory |
|
270 | - */ |
|
271 | - public function getFactory() |
|
272 | - { |
|
273 | - return $this->factory; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Get the view's rendering engine. |
|
278 | - * |
|
279 | - * @return \Syscodes\Contracts\View\Engine |
|
280 | - */ |
|
281 | - public function getEngine() |
|
282 | - { |
|
283 | - return $this->engine; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Searches for the given variable and returns its value. |
|
288 | - * Local variables will be returned before global variables. |
|
289 | - * |
|
290 | - * @example $value = $view->get('foo', 'bar'); |
|
291 | - * |
|
292 | - * If the key is not given or null, the entire data array is returned. |
|
293 | - * |
|
294 | - * @param string $key The variable name |
|
295 | - * @param mixed $default The default value to return |
|
296 | - * |
|
297 | - * @return mixed |
|
298 | - * |
|
299 | - * @uses \Syscodes\Contracts\Core\Lenevor |
|
300 | - * |
|
301 | - * @throws \InvalidArgumentException |
|
302 | - */ |
|
303 | - public function &get($key, $default = null) |
|
304 | - { |
|
305 | - if (strpos($key, '.') === false) { |
|
306 | - if (array_key_exists($key, $this->data)) { |
|
307 | - return $this->data[$key]; |
|
308 | - } else { |
|
309 | - throw new InvalidArgumentException(__('view.variableNotSet')); |
|
310 | - } |
|
311 | - } else { |
|
312 | - return value($default); |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Assigns a variable by name. Assigned values will be available as a |
|
318 | - * variable within the view file: |
|
319 | - * |
|
320 | - * This value can be accessed as $var within the view |
|
321 | - * @example $view->set(array('food' => 'bread', 'beverage' => 'water')); |
|
322 | - * |
|
323 | - * @param string|array $key Variable name |
|
324 | - * @param mixed $value Value |
|
325 | - * |
|
326 | - * @return $this |
|
327 | - * |
|
328 | - * @uses instanceof \Traversable |
|
329 | - */ |
|
330 | - public function set($key, $value = null) |
|
331 | - { |
|
332 | - if (is_array($key) || $key instanceof Traversable) { |
|
333 | - foreach ($key as $name => $value) { |
|
334 | - $this->set($name, $value); |
|
335 | - } |
|
336 | - } else { |
|
337 | - if (strpos($key, '.') === false) { |
|
338 | - $this->data[$key] = $value; |
|
339 | - } else { |
|
340 | - Arr::set($this->data, $key, $value); |
|
341 | - } |
|
342 | - } |
|
343 | - |
|
344 | - return $this; |
|
345 | - } |
|
346 | - |
|
347 | - /* |
|
165 | + return array_map(function ($value) { |
|
166 | + return ($value instanceof Renderable) ? $value->render() : $value; |
|
167 | + }, $data); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Get the sections of the rendered view. |
|
172 | + * |
|
173 | + * @return array |
|
174 | + * |
|
175 | + * @throws \Throwable |
|
176 | + */ |
|
177 | + public function renderSections() |
|
178 | + { |
|
179 | + return $this->render(function () { |
|
180 | + return $this->factory->getSections(); |
|
181 | + }); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Add a piece of data to the view. |
|
186 | + * |
|
187 | + * @example $view->assign($content, $data); |
|
188 | + * |
|
189 | + * @param string|array $key |
|
190 | + * @param mixed $value (null by default) |
|
191 | + * |
|
192 | + * @return $this |
|
193 | + */ |
|
194 | + public function assign($key, $value = null) |
|
195 | + { |
|
196 | + if (is_array($key)) { |
|
197 | + $this->data = array_merge($this->data, $key); |
|
198 | + } else { |
|
199 | + $this->data = [$key => $value]; |
|
200 | + } |
|
201 | + |
|
202 | + return $this; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Assigns a value by reference. The benefit of binding is that values can be altered |
|
207 | + * without re-setting them. It is also possible to bind variables before they have values. |
|
208 | + * Assigned values will be available as a variable within the view file: |
|
209 | + * |
|
210 | + * @example $view->bind('ref', $bar); |
|
211 | + * |
|
212 | + * @param string $key Variable name |
|
213 | + * @param mixed $value Referenced variable |
|
214 | + * |
|
215 | + * @return $this |
|
216 | + */ |
|
217 | + public function bind($key, & $value) |
|
218 | + { |
|
219 | + $this->data[$key] =& $value; |
|
220 | + |
|
221 | + return $this; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Get the array of view data. |
|
226 | + * |
|
227 | + * @return array |
|
228 | + */ |
|
229 | + public function getData() |
|
230 | + { |
|
231 | + return $this->data; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Get the name of the view. |
|
236 | + * |
|
237 | + * @return string |
|
238 | + */ |
|
239 | + public function getView() |
|
240 | + { |
|
241 | + return $this->view; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Get the path to the view file. |
|
246 | + * |
|
247 | + * @return string |
|
248 | + */ |
|
249 | + public function getPath() |
|
250 | + { |
|
251 | + return $this->path; |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Set the path to the view file. |
|
256 | + * |
|
257 | + * @param string $path |
|
258 | + * |
|
259 | + * @return void |
|
260 | + */ |
|
261 | + public function setPath($path) |
|
262 | + { |
|
263 | + $this->path = $path; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Get the view factory instance. |
|
268 | + * |
|
269 | + * @return \Syscodes\View\factory |
|
270 | + */ |
|
271 | + public function getFactory() |
|
272 | + { |
|
273 | + return $this->factory; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Get the view's rendering engine. |
|
278 | + * |
|
279 | + * @return \Syscodes\Contracts\View\Engine |
|
280 | + */ |
|
281 | + public function getEngine() |
|
282 | + { |
|
283 | + return $this->engine; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Searches for the given variable and returns its value. |
|
288 | + * Local variables will be returned before global variables. |
|
289 | + * |
|
290 | + * @example $value = $view->get('foo', 'bar'); |
|
291 | + * |
|
292 | + * If the key is not given or null, the entire data array is returned. |
|
293 | + * |
|
294 | + * @param string $key The variable name |
|
295 | + * @param mixed $default The default value to return |
|
296 | + * |
|
297 | + * @return mixed |
|
298 | + * |
|
299 | + * @uses \Syscodes\Contracts\Core\Lenevor |
|
300 | + * |
|
301 | + * @throws \InvalidArgumentException |
|
302 | + */ |
|
303 | + public function &get($key, $default = null) |
|
304 | + { |
|
305 | + if (strpos($key, '.') === false) { |
|
306 | + if (array_key_exists($key, $this->data)) { |
|
307 | + return $this->data[$key]; |
|
308 | + } else { |
|
309 | + throw new InvalidArgumentException(__('view.variableNotSet')); |
|
310 | + } |
|
311 | + } else { |
|
312 | + return value($default); |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Assigns a variable by name. Assigned values will be available as a |
|
318 | + * variable within the view file: |
|
319 | + * |
|
320 | + * This value can be accessed as $var within the view |
|
321 | + * @example $view->set(array('food' => 'bread', 'beverage' => 'water')); |
|
322 | + * |
|
323 | + * @param string|array $key Variable name |
|
324 | + * @param mixed $value Value |
|
325 | + * |
|
326 | + * @return $this |
|
327 | + * |
|
328 | + * @uses instanceof \Traversable |
|
329 | + */ |
|
330 | + public function set($key, $value = null) |
|
331 | + { |
|
332 | + if (is_array($key) || $key instanceof Traversable) { |
|
333 | + foreach ($key as $name => $value) { |
|
334 | + $this->set($name, $value); |
|
335 | + } |
|
336 | + } else { |
|
337 | + if (strpos($key, '.') === false) { |
|
338 | + $this->data[$key] = $value; |
|
339 | + } else { |
|
340 | + Arr::set($this->data, $key, $value); |
|
341 | + } |
|
342 | + } |
|
343 | + |
|
344 | + return $this; |
|
345 | + } |
|
346 | + |
|
347 | + /* |
|
348 | 348 | |----------------------------------------------------------------- |
349 | 349 | | ArrayAccess Methods |
350 | 350 | |----------------------------------------------------------------- |
351 | 351 | */ |
352 | 352 | |
353 | - /** |
|
354 | - * Whether or not an offset exists. |
|
355 | - * |
|
356 | - * @param string $offset |
|
357 | - * |
|
358 | - * @return bool |
|
359 | - */ |
|
360 | - public function offsetExists($offset) |
|
361 | - { |
|
362 | - return array_key_exists($offset, $this->data); |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Returns the value at specified offset. |
|
367 | - * |
|
368 | - * @param string $offset |
|
369 | - * |
|
370 | - * @return mixed |
|
371 | - */ |
|
372 | - public function offsetGet($offset) |
|
373 | - { |
|
374 | - return $this->data[$offset]; |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Assigns a value to the specified offset |
|
379 | - * |
|
380 | - * @param string $offset |
|
381 | - * @param mixed $value |
|
382 | - * |
|
383 | - * @return void |
|
384 | - */ |
|
385 | - public function offsetSet($offset, $value) |
|
386 | - { |
|
387 | - $this->assign($offset, $value); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Unsets an offset. |
|
392 | - * |
|
393 | - * @param string $offset |
|
394 | - * |
|
395 | - * @return void |
|
396 | - */ |
|
397 | - public function offsetUnset($offset) |
|
398 | - { |
|
399 | - unset($this->data[$offset]); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Magic method. Searches for the given variable and returns its value. |
|
404 | - * Local variables will be returned before global variables. |
|
405 | - * |
|
406 | - * @example $value = $view->var; |
|
407 | - * |
|
408 | - * @param string $key Variable name |
|
409 | - * |
|
410 | - * @return mixed |
|
411 | - * |
|
412 | - * @throws \Syscodes\LenevorException |
|
413 | - */ |
|
414 | - public function &__get($key) |
|
415 | - { |
|
416 | - return $this->get($key); |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Magic method. Calls [$this->set] with the same parameters. |
|
421 | - * |
|
422 | - * @example $view->var = 'something'; |
|
423 | - * |
|
424 | - * @param string $key Variable name |
|
425 | - * @param mixed $value Value |
|
426 | - * |
|
427 | - * @return void |
|
428 | - */ |
|
429 | - public function __set($key, $value) |
|
430 | - { |
|
431 | - $this->set($key, $value); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Magic method. Determines if a variable is set. |
|
436 | - * |
|
437 | - * @example isset($view->foo); |
|
438 | - * |
|
439 | - * Variables are not considered to be set. |
|
440 | - * |
|
441 | - * @param string $key variable name |
|
442 | - * |
|
443 | - * @return boolean |
|
444 | - */ |
|
445 | - public function __isset($key) |
|
446 | - { |
|
447 | - return isset($this->data[$key]); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Magic method. Unsets a given variable. |
|
452 | - * |
|
453 | - * @example unset($view->var); |
|
454 | - * |
|
455 | - * @param string $key Variable name |
|
456 | - * |
|
457 | - * @return void |
|
458 | - */ |
|
459 | - public function __unset($key) |
|
460 | - { |
|
461 | - unset($this->data[$key]); |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Magic Method for handling dynamic functions. |
|
466 | - * |
|
467 | - * @param string $method |
|
468 | - * @param array $parameters |
|
469 | - * |
|
470 | - * @return mixed |
|
471 | - * |
|
472 | - * @throws \BadMethodCallException; |
|
473 | - */ |
|
474 | - public function __call($method, $parameters) |
|
475 | - { |
|
476 | - if (Str::startsWith($method, 'assign')) { |
|
477 | - $name = Str::camelcase(substr($method, 4)); |
|
478 | - |
|
479 | - return $this->assign($name, $parameters[0]); |
|
480 | - } |
|
481 | - |
|
482 | - throw new BadMethodCallException(sprintf( |
|
483 | - 'Method %s::%s does not exist.', static::class, $method) |
|
484 | - ); |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * Get content as a string of HTML. |
|
489 | - * |
|
490 | - * @return string |
|
491 | - */ |
|
492 | - public function toHtml() |
|
493 | - { |
|
494 | - return $this->render(); |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Magic method. Returns the output of [static::render]. |
|
499 | - * |
|
500 | - * @return string |
|
501 | - * |
|
502 | - * @uses View->render() |
|
503 | - * |
|
504 | - * @throws \Throwable |
|
505 | - */ |
|
506 | - public function __toString() |
|
507 | - { |
|
508 | - return $this->render(); |
|
509 | - } |
|
353 | + /** |
|
354 | + * Whether or not an offset exists. |
|
355 | + * |
|
356 | + * @param string $offset |
|
357 | + * |
|
358 | + * @return bool |
|
359 | + */ |
|
360 | + public function offsetExists($offset) |
|
361 | + { |
|
362 | + return array_key_exists($offset, $this->data); |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Returns the value at specified offset. |
|
367 | + * |
|
368 | + * @param string $offset |
|
369 | + * |
|
370 | + * @return mixed |
|
371 | + */ |
|
372 | + public function offsetGet($offset) |
|
373 | + { |
|
374 | + return $this->data[$offset]; |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Assigns a value to the specified offset |
|
379 | + * |
|
380 | + * @param string $offset |
|
381 | + * @param mixed $value |
|
382 | + * |
|
383 | + * @return void |
|
384 | + */ |
|
385 | + public function offsetSet($offset, $value) |
|
386 | + { |
|
387 | + $this->assign($offset, $value); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Unsets an offset. |
|
392 | + * |
|
393 | + * @param string $offset |
|
394 | + * |
|
395 | + * @return void |
|
396 | + */ |
|
397 | + public function offsetUnset($offset) |
|
398 | + { |
|
399 | + unset($this->data[$offset]); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Magic method. Searches for the given variable and returns its value. |
|
404 | + * Local variables will be returned before global variables. |
|
405 | + * |
|
406 | + * @example $value = $view->var; |
|
407 | + * |
|
408 | + * @param string $key Variable name |
|
409 | + * |
|
410 | + * @return mixed |
|
411 | + * |
|
412 | + * @throws \Syscodes\LenevorException |
|
413 | + */ |
|
414 | + public function &__get($key) |
|
415 | + { |
|
416 | + return $this->get($key); |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Magic method. Calls [$this->set] with the same parameters. |
|
421 | + * |
|
422 | + * @example $view->var = 'something'; |
|
423 | + * |
|
424 | + * @param string $key Variable name |
|
425 | + * @param mixed $value Value |
|
426 | + * |
|
427 | + * @return void |
|
428 | + */ |
|
429 | + public function __set($key, $value) |
|
430 | + { |
|
431 | + $this->set($key, $value); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Magic method. Determines if a variable is set. |
|
436 | + * |
|
437 | + * @example isset($view->foo); |
|
438 | + * |
|
439 | + * Variables are not considered to be set. |
|
440 | + * |
|
441 | + * @param string $key variable name |
|
442 | + * |
|
443 | + * @return boolean |
|
444 | + */ |
|
445 | + public function __isset($key) |
|
446 | + { |
|
447 | + return isset($this->data[$key]); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Magic method. Unsets a given variable. |
|
452 | + * |
|
453 | + * @example unset($view->var); |
|
454 | + * |
|
455 | + * @param string $key Variable name |
|
456 | + * |
|
457 | + * @return void |
|
458 | + */ |
|
459 | + public function __unset($key) |
|
460 | + { |
|
461 | + unset($this->data[$key]); |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Magic Method for handling dynamic functions. |
|
466 | + * |
|
467 | + * @param string $method |
|
468 | + * @param array $parameters |
|
469 | + * |
|
470 | + * @return mixed |
|
471 | + * |
|
472 | + * @throws \BadMethodCallException; |
|
473 | + */ |
|
474 | + public function __call($method, $parameters) |
|
475 | + { |
|
476 | + if (Str::startsWith($method, 'assign')) { |
|
477 | + $name = Str::camelcase(substr($method, 4)); |
|
478 | + |
|
479 | + return $this->assign($name, $parameters[0]); |
|
480 | + } |
|
481 | + |
|
482 | + throw new BadMethodCallException(sprintf( |
|
483 | + 'Method %s::%s does not exist.', static::class, $method) |
|
484 | + ); |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * Get content as a string of HTML. |
|
489 | + * |
|
490 | + * @return string |
|
491 | + */ |
|
492 | + public function toHtml() |
|
493 | + { |
|
494 | + return $this->render(); |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Magic method. Returns the output of [static::render]. |
|
499 | + * |
|
500 | + * @return string |
|
501 | + * |
|
502 | + * @uses View->render() |
|
503 | + * |
|
504 | + * @throws \Throwable |
|
505 | + */ |
|
506 | + public function __toString() |
|
507 | + { |
|
508 | + return $this->render(); |
|
509 | + } |
|
510 | 510 | } |
511 | 511 | \ No newline at end of file |
@@ -58,12 +58,12 @@ discard block |
||
58 | 58 | $this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); |
59 | 59 | |
60 | 60 | if ( ! empty($time)) { |
61 | - if (is_string($time) && static::hasRelativeKeywords($time)) { |
|
62 | - $dateTime = new DateTime('now', $this->timezone); |
|
63 | - $dateTime->modify($time); |
|
61 | + if (is_string($time) && static::hasRelativeKeywords($time)) { |
|
62 | + $dateTime = new DateTime('now', $this->timezone); |
|
63 | + $dateTime->modify($time); |
|
64 | 64 | |
65 | - $time = $dateTime->format('Y-m-d H:i:s'); |
|
66 | - } |
|
65 | + $time = $dateTime->format('Y-m-d H:i:s'); |
|
66 | + } |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | return parent::__construct($time, $this->timezone); |
@@ -118,8 +118,8 @@ discard block |
||
118 | 118 | * @return \Syscodes\Support\Chronos\Time |
119 | 119 | */ |
120 | 120 | public static function yesterday($timezone = null, string $locale = null) |
121 | - { |
|
122 | - return static::parse(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale); |
|
121 | + { |
|
122 | + return static::parse(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -131,8 +131,8 @@ discard block |
||
131 | 131 | * @return \Syscodes\Support\Chronos\Time |
132 | 132 | */ |
133 | 133 | public static function tomorrow($timezone = null, string $locale = null) |
134 | - { |
|
135 | - return static::parse(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale); |
|
134 | + { |
|
135 | + return static::parse(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | |
265 | 265 | /** |
266 | 266 | * Creates an instance of Time that will be returned during testing |
267 | - * when calling 'Time::now' instead of the current time. |
|
267 | + * when calling 'Time::now' instead of the current time. |
|
268 | 268 | * |
269 | 269 | * @param \Syscodes\Support\Chronos\Time|string $datetime (null by default) |
270 | 270 | * @param string|null $timezone (null by default) |
@@ -164,7 +164,7 @@ |
||
164 | 164 | |
165 | 165 | foreach ($transitions as $transition) { |
166 | 166 | if ($transition['time'] > $this->format('U')) { |
167 | - $dayLightSaving = (bool) $transition['isdst'] ?? $dayLightSaving; |
|
167 | + $dayLightSaving = (bool) $transition['isdst'] ?? $dayLightSaving; |
|
168 | 168 | } |
169 | 169 | } |
170 | 170 |
@@ -40,663 +40,663 @@ |
||
40 | 40 | */ |
41 | 41 | class Route |
42 | 42 | { |
43 | - use Concerns\RouteCondition, |
|
44 | - Concerns\RouteDependencyResolver; |
|
43 | + use Concerns\RouteCondition, |
|
44 | + Concerns\RouteDependencyResolver; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Action that the route will use when called. |
|
48 | - * |
|
49 | - * @var \Closure|string|array $action |
|
50 | - */ |
|
51 | - public $action; |
|
52 | - |
|
53 | - /** |
|
54 | - * The container instance used by the route. |
|
55 | - * |
|
56 | - * @var \Syscodes\Container\Container $container |
|
57 | - */ |
|
58 | - protected $container; |
|
59 | - |
|
60 | - /** |
|
61 | - * The controller instance. |
|
62 | - * |
|
63 | - * @var string $controller |
|
64 | - */ |
|
65 | - public $controller; |
|
66 | - |
|
67 | - /** |
|
68 | - * The default values for the route. |
|
69 | - * |
|
70 | - * @var array $defaults |
|
71 | - */ |
|
72 | - public $defaults = []; |
|
73 | - |
|
74 | - /** |
|
75 | - * Variable of HTTP method. |
|
76 | - * |
|
77 | - * @var array|string $method |
|
78 | - */ |
|
79 | - public $method; |
|
80 | - |
|
81 | - /** |
|
82 | - * The array of matched parameters. |
|
83 | - * |
|
84 | - * @var array $parameters |
|
85 | - */ |
|
86 | - public $parameters = []; |
|
87 | - |
|
88 | - /** |
|
89 | - * The parameter names for the route. |
|
90 | - * |
|
91 | - * @var string|null $parameterNames |
|
92 | - */ |
|
93 | - public $parameterNames; |
|
94 | - |
|
95 | - /** |
|
96 | - * Patterns that should be replaced. |
|
97 | - * |
|
98 | - * @var array $patterns |
|
99 | - */ |
|
100 | - public $patterns = [ |
|
101 | - '~/~' => '\/', // Slash |
|
102 | - '~{an:[^\/{}]+}~' => '([0-9a-zA-Z]++)', // Placeholder accepts alphabetic and numeric chars |
|
103 | - '~{n:[^\/{}]+}~' => '([0-9]++)', // Placeholder accepts only numeric |
|
104 | - '~{a:[^\/{}]+}~' => '([a-zA-Z]++)', // Placeholder accepts only alphabetic chars |
|
105 | - '~{w:[^\/{}]+}~' => '([0-9a-zA-Z-_]++)', // Placeholder accepts alphanumeric and underscore |
|
106 | - '~{\*:[^\/{}]+}~' => '(.++)', // Placeholder match rest of url |
|
107 | - '~(\\\/)?{\?:[^\/{}]+}~' => '\/?([^\/]*)', // Optional placeholder |
|
108 | - '~{[^\/{}]+}~' => '([^\/]++)' // Normal placeholder |
|
109 | - ]; |
|
110 | - |
|
111 | - /** |
|
112 | - * The URI pattern the route responds to. |
|
113 | - * |
|
114 | - * @var array $uri |
|
115 | - */ |
|
116 | - public $uri = []; |
|
117 | - |
|
118 | - /** |
|
119 | - * Contains the arguments of the current route. |
|
120 | - * |
|
121 | - * @var array $where |
|
122 | - */ |
|
123 | - public $wheres = []; |
|
124 | - |
|
125 | - /** |
|
126 | - * Constructor. Initialize route. |
|
127 | - * |
|
128 | - * @param array|string|null $method (null by default) |
|
129 | - * @param string|null $uri (null by default) |
|
130 | - * @param \Closure|string|null $action (null by default) |
|
131 | - * |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - public function __construct($method = null, $uri = null, $action = null) |
|
135 | - { |
|
136 | - $this->uri = $uri; |
|
137 | - |
|
138 | - // Set the method |
|
139 | - $this->parseMethod($method); |
|
140 | - |
|
141 | - // Set the action |
|
142 | - $this->parseAction($action); |
|
143 | - |
|
144 | - if (is_null($prefix = Arr::get($this->action, 'prefix'))) { |
|
145 | - $this->prefix($prefix); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - // Getters |
|
150 | - |
|
151 | - /** |
|
152 | - * Get the action of the current route. |
|
153 | - * |
|
154 | - * @return \Closure|string |
|
155 | - */ |
|
156 | - public function getAction() |
|
157 | - { |
|
158 | - return $this->action; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get the arguments of the current route. |
|
163 | - * |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public function getArguments() |
|
167 | - { |
|
168 | - return $this->wheres; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get the controller instance for the route. |
|
173 | - * |
|
174 | - * @return mixed |
|
175 | - */ |
|
176 | - public function getController() |
|
177 | - { |
|
178 | - if ( ! $this->controller) { |
|
179 | - $class = $this->parseControllerCallback()[0]; |
|
46 | + /** |
|
47 | + * Action that the route will use when called. |
|
48 | + * |
|
49 | + * @var \Closure|string|array $action |
|
50 | + */ |
|
51 | + public $action; |
|
52 | + |
|
53 | + /** |
|
54 | + * The container instance used by the route. |
|
55 | + * |
|
56 | + * @var \Syscodes\Container\Container $container |
|
57 | + */ |
|
58 | + protected $container; |
|
59 | + |
|
60 | + /** |
|
61 | + * The controller instance. |
|
62 | + * |
|
63 | + * @var string $controller |
|
64 | + */ |
|
65 | + public $controller; |
|
66 | + |
|
67 | + /** |
|
68 | + * The default values for the route. |
|
69 | + * |
|
70 | + * @var array $defaults |
|
71 | + */ |
|
72 | + public $defaults = []; |
|
73 | + |
|
74 | + /** |
|
75 | + * Variable of HTTP method. |
|
76 | + * |
|
77 | + * @var array|string $method |
|
78 | + */ |
|
79 | + public $method; |
|
80 | + |
|
81 | + /** |
|
82 | + * The array of matched parameters. |
|
83 | + * |
|
84 | + * @var array $parameters |
|
85 | + */ |
|
86 | + public $parameters = []; |
|
87 | + |
|
88 | + /** |
|
89 | + * The parameter names for the route. |
|
90 | + * |
|
91 | + * @var string|null $parameterNames |
|
92 | + */ |
|
93 | + public $parameterNames; |
|
94 | + |
|
95 | + /** |
|
96 | + * Patterns that should be replaced. |
|
97 | + * |
|
98 | + * @var array $patterns |
|
99 | + */ |
|
100 | + public $patterns = [ |
|
101 | + '~/~' => '\/', // Slash |
|
102 | + '~{an:[^\/{}]+}~' => '([0-9a-zA-Z]++)', // Placeholder accepts alphabetic and numeric chars |
|
103 | + '~{n:[^\/{}]+}~' => '([0-9]++)', // Placeholder accepts only numeric |
|
104 | + '~{a:[^\/{}]+}~' => '([a-zA-Z]++)', // Placeholder accepts only alphabetic chars |
|
105 | + '~{w:[^\/{}]+}~' => '([0-9a-zA-Z-_]++)', // Placeholder accepts alphanumeric and underscore |
|
106 | + '~{\*:[^\/{}]+}~' => '(.++)', // Placeholder match rest of url |
|
107 | + '~(\\\/)?{\?:[^\/{}]+}~' => '\/?([^\/]*)', // Optional placeholder |
|
108 | + '~{[^\/{}]+}~' => '([^\/]++)' // Normal placeholder |
|
109 | + ]; |
|
110 | + |
|
111 | + /** |
|
112 | + * The URI pattern the route responds to. |
|
113 | + * |
|
114 | + * @var array $uri |
|
115 | + */ |
|
116 | + public $uri = []; |
|
117 | + |
|
118 | + /** |
|
119 | + * Contains the arguments of the current route. |
|
120 | + * |
|
121 | + * @var array $where |
|
122 | + */ |
|
123 | + public $wheres = []; |
|
124 | + |
|
125 | + /** |
|
126 | + * Constructor. Initialize route. |
|
127 | + * |
|
128 | + * @param array|string|null $method (null by default) |
|
129 | + * @param string|null $uri (null by default) |
|
130 | + * @param \Closure|string|null $action (null by default) |
|
131 | + * |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + public function __construct($method = null, $uri = null, $action = null) |
|
135 | + { |
|
136 | + $this->uri = $uri; |
|
137 | + |
|
138 | + // Set the method |
|
139 | + $this->parseMethod($method); |
|
140 | + |
|
141 | + // Set the action |
|
142 | + $this->parseAction($action); |
|
143 | + |
|
144 | + if (is_null($prefix = Arr::get($this->action, 'prefix'))) { |
|
145 | + $this->prefix($prefix); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + // Getters |
|
150 | + |
|
151 | + /** |
|
152 | + * Get the action of the current route. |
|
153 | + * |
|
154 | + * @return \Closure|string |
|
155 | + */ |
|
156 | + public function getAction() |
|
157 | + { |
|
158 | + return $this->action; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get the arguments of the current route. |
|
163 | + * |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public function getArguments() |
|
167 | + { |
|
168 | + return $this->wheres; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get the controller instance for the route. |
|
173 | + * |
|
174 | + * @return mixed |
|
175 | + */ |
|
176 | + public function getController() |
|
177 | + { |
|
178 | + if ( ! $this->controller) { |
|
179 | + $class = $this->parseControllerCallback()[0]; |
|
180 | 180 | |
181 | - $this->controller = $this->container->make(ltrim($class, '\\')); |
|
182 | - } |
|
183 | - |
|
184 | - return $this->controller; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Get the controller method used for the route. |
|
189 | - * |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - public function getControllerMethod() |
|
193 | - { |
|
194 | - return $this->parseControllerCallback()[1]; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Get the request method of the current route. |
|
199 | - * |
|
200 | - * @return array |
|
201 | - */ |
|
202 | - public function getMethod() |
|
203 | - { |
|
204 | - return $this->method; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Get the url of the current route. |
|
209 | - * |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - public function getName() |
|
213 | - { |
|
214 | - return $this->action['as'] ?? null; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Get the url of the current route. |
|
219 | - * |
|
220 | - * @return string |
|
221 | - */ |
|
222 | - public function getRoute() |
|
223 | - { |
|
224 | - return $this->uri; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Get or set the domain for the route. |
|
229 | - * |
|
230 | - * @param string|null $domain (null by default) |
|
231 | - * |
|
232 | - * @return $this |
|
233 | - */ |
|
234 | - public function domain($domain = null) |
|
235 | - { |
|
236 | - if (is_null($domain)) { |
|
237 | - return $this->getDomain(); |
|
238 | - } |
|
239 | - |
|
240 | - $this->action['domain'] = $this->parseRoute($domain); |
|
241 | - |
|
242 | - return $this; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Get the domain defined for the route. |
|
247 | - * |
|
248 | - * @return string|null |
|
249 | - */ |
|
250 | - public function getDomain() |
|
251 | - { |
|
252 | - return isset($this->action['domain']) |
|
253 | - ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
254 | - : null; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Parse the controller. |
|
259 | - * |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - public function parseControllerCallback() |
|
263 | - { |
|
264 | - return Str::parseCallback($this->action['uses']); |
|
265 | - } |
|
181 | + $this->controller = $this->container->make(ltrim($class, '\\')); |
|
182 | + } |
|
183 | + |
|
184 | + return $this->controller; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Get the controller method used for the route. |
|
189 | + * |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + public function getControllerMethod() |
|
193 | + { |
|
194 | + return $this->parseControllerCallback()[1]; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Get the request method of the current route. |
|
199 | + * |
|
200 | + * @return array |
|
201 | + */ |
|
202 | + public function getMethod() |
|
203 | + { |
|
204 | + return $this->method; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Get the url of the current route. |
|
209 | + * |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + public function getName() |
|
213 | + { |
|
214 | + return $this->action['as'] ?? null; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Get the url of the current route. |
|
219 | + * |
|
220 | + * @return string |
|
221 | + */ |
|
222 | + public function getRoute() |
|
223 | + { |
|
224 | + return $this->uri; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Get or set the domain for the route. |
|
229 | + * |
|
230 | + * @param string|null $domain (null by default) |
|
231 | + * |
|
232 | + * @return $this |
|
233 | + */ |
|
234 | + public function domain($domain = null) |
|
235 | + { |
|
236 | + if (is_null($domain)) { |
|
237 | + return $this->getDomain(); |
|
238 | + } |
|
239 | + |
|
240 | + $this->action['domain'] = $this->parseRoute($domain); |
|
241 | + |
|
242 | + return $this; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Get the domain defined for the route. |
|
247 | + * |
|
248 | + * @return string|null |
|
249 | + */ |
|
250 | + public function getDomain() |
|
251 | + { |
|
252 | + return isset($this->action['domain']) |
|
253 | + ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
254 | + : null; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Parse the controller. |
|
259 | + * |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + public function parseControllerCallback() |
|
263 | + { |
|
264 | + return Str::parseCallback($this->action['uses']); |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Checks whether the route's action is a controller. |
|
269 | - * |
|
270 | - * @return bool |
|
271 | - */ |
|
272 | - public function isControllerAction() |
|
273 | - { |
|
274 | - return is_string($this->action['uses']); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Get the dispatcher for the route's controller. |
|
279 | - * |
|
280 | - * @return \Syscodes\Controller\ControllerDispatcher |
|
281 | - */ |
|
282 | - private function controllerDispatcher() |
|
283 | - { |
|
284 | - return new ControllerDispatcher($this->container); |
|
285 | - } |
|
286 | - |
|
287 | - // Setters |
|
267 | + /** |
|
268 | + * Checks whether the route's action is a controller. |
|
269 | + * |
|
270 | + * @return bool |
|
271 | + */ |
|
272 | + public function isControllerAction() |
|
273 | + { |
|
274 | + return is_string($this->action['uses']); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Get the dispatcher for the route's controller. |
|
279 | + * |
|
280 | + * @return \Syscodes\Controller\ControllerDispatcher |
|
281 | + */ |
|
282 | + private function controllerDispatcher() |
|
283 | + { |
|
284 | + return new ControllerDispatcher($this->container); |
|
285 | + } |
|
286 | + |
|
287 | + // Setters |
|
288 | 288 | |
289 | - /** |
|
290 | - * Run the route action and return the response. |
|
291 | - * |
|
292 | - * @return mixed |
|
293 | - */ |
|
294 | - public function runResolver() |
|
295 | - { |
|
296 | - $this->container = $this->container ?: new Container; |
|
297 | - |
|
298 | - try { |
|
299 | - if ($this->isControllerAction()) { |
|
300 | - return $this->runResolverController(); |
|
301 | - } |
|
302 | - |
|
303 | - return $this->runResolverCallable(); |
|
304 | - } catch (HttpResponseException $e) { |
|
305 | - return $e->getResponse(); |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Run the route action and return the response. |
|
311 | - * |
|
312 | - * @return mixed |
|
313 | - */ |
|
314 | - protected function runResolverCallable() |
|
315 | - { |
|
316 | - $callable = $this->action['uses']; |
|
317 | - |
|
318 | - return $callable(...array_values($this->resolveMethodDependencies( |
|
319 | - $this->parametersWithouNulls(), new ReflectionFunction($this->action['uses']) |
|
320 | - ))); |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Run the route action and return the response. |
|
325 | - * |
|
326 | - * @return mixed |
|
327 | - */ |
|
328 | - protected function runResolverController() |
|
329 | - { |
|
330 | - return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Set the action. |
|
335 | - * |
|
336 | - * @param \Closure|string $action |
|
337 | - * |
|
338 | - * @return $this |
|
339 | - * |
|
340 | - * @throws \InvalidArgumentException |
|
341 | - */ |
|
342 | - public function parseAction($action) |
|
343 | - { |
|
344 | - if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
345 | - throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
346 | - } |
|
347 | - |
|
348 | - $this->action = RouteAction::parse($this->uri, $action); |
|
349 | - |
|
350 | - return $this; |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Set the method of the current route. |
|
355 | - * |
|
356 | - * @param array $method |
|
357 | - * |
|
358 | - * @return string $this |
|
359 | - * |
|
360 | - * @throws \InvalidArgumentException |
|
361 | - */ |
|
362 | - public function parseMethod($method) |
|
363 | - { |
|
364 | - if ($method === null || ! is_array($method) || empty($method)) { |
|
365 | - throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
289 | + /** |
|
290 | + * Run the route action and return the response. |
|
291 | + * |
|
292 | + * @return mixed |
|
293 | + */ |
|
294 | + public function runResolver() |
|
295 | + { |
|
296 | + $this->container = $this->container ?: new Container; |
|
297 | + |
|
298 | + try { |
|
299 | + if ($this->isControllerAction()) { |
|
300 | + return $this->runResolverController(); |
|
301 | + } |
|
302 | + |
|
303 | + return $this->runResolverCallable(); |
|
304 | + } catch (HttpResponseException $e) { |
|
305 | + return $e->getResponse(); |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Run the route action and return the response. |
|
311 | + * |
|
312 | + * @return mixed |
|
313 | + */ |
|
314 | + protected function runResolverCallable() |
|
315 | + { |
|
316 | + $callable = $this->action['uses']; |
|
317 | + |
|
318 | + return $callable(...array_values($this->resolveMethodDependencies( |
|
319 | + $this->parametersWithouNulls(), new ReflectionFunction($this->action['uses']) |
|
320 | + ))); |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Run the route action and return the response. |
|
325 | + * |
|
326 | + * @return mixed |
|
327 | + */ |
|
328 | + protected function runResolverController() |
|
329 | + { |
|
330 | + return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Set the action. |
|
335 | + * |
|
336 | + * @param \Closure|string $action |
|
337 | + * |
|
338 | + * @return $this |
|
339 | + * |
|
340 | + * @throws \InvalidArgumentException |
|
341 | + */ |
|
342 | + public function parseAction($action) |
|
343 | + { |
|
344 | + if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
345 | + throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
346 | + } |
|
347 | + |
|
348 | + $this->action = RouteAction::parse($this->uri, $action); |
|
349 | + |
|
350 | + return $this; |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Set the method of the current route. |
|
355 | + * |
|
356 | + * @param array $method |
|
357 | + * |
|
358 | + * @return string $this |
|
359 | + * |
|
360 | + * @throws \InvalidArgumentException |
|
361 | + */ |
|
362 | + public function parseMethod($method) |
|
363 | + { |
|
364 | + if ($method === null || ! is_array($method) || empty($method)) { |
|
365 | + throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
366 | 366 | |
367 | - } |
|
368 | - |
|
369 | - foreach ($method as $httpMethod) { |
|
370 | - if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
371 | - throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
372 | - } |
|
373 | - } |
|
374 | - |
|
375 | - $this->method = $method; |
|
376 | - |
|
377 | - return $this; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Set the route. |
|
382 | - * |
|
383 | - * @param string $uri |
|
384 | - * |
|
385 | - * @return string |
|
386 | - * |
|
387 | - * @throws \InvalidArgumentException |
|
388 | - */ |
|
389 | - public function parseRoute($uri) |
|
390 | - { |
|
391 | - if ($uri === null) { |
|
392 | - throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
393 | - } |
|
394 | - |
|
395 | - $this->uri = $this->parseRoutePath($uri); |
|
396 | - |
|
397 | - return $this; |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Replace word patterns with regex in route uri. |
|
402 | - * |
|
403 | - * @param string $uri |
|
404 | - * |
|
405 | - * @return string |
|
406 | - */ |
|
407 | - protected function parseRoutePath($uri) |
|
408 | - { |
|
409 | - $uri = trim($uri, '\/?'); |
|
410 | - $uri = trim($uri, '\/'); |
|
367 | + } |
|
368 | + |
|
369 | + foreach ($method as $httpMethod) { |
|
370 | + if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
371 | + throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
372 | + } |
|
373 | + } |
|
374 | + |
|
375 | + $this->method = $method; |
|
376 | + |
|
377 | + return $this; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Set the route. |
|
382 | + * |
|
383 | + * @param string $uri |
|
384 | + * |
|
385 | + * @return string |
|
386 | + * |
|
387 | + * @throws \InvalidArgumentException |
|
388 | + */ |
|
389 | + public function parseRoute($uri) |
|
390 | + { |
|
391 | + if ($uri === null) { |
|
392 | + throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
393 | + } |
|
394 | + |
|
395 | + $this->uri = $this->parseRoutePath($uri); |
|
396 | + |
|
397 | + return $this; |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Replace word patterns with regex in route uri. |
|
402 | + * |
|
403 | + * @param string $uri |
|
404 | + * |
|
405 | + * @return string |
|
406 | + */ |
|
407 | + protected function parseRoutePath($uri) |
|
408 | + { |
|
409 | + $uri = trim($uri, '\/?'); |
|
410 | + $uri = trim($uri, '\/'); |
|
411 | 411 | |
412 | - preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); |
|
412 | + preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); |
|
413 | 413 | |
414 | - foreach ($matches[1] as $match) { |
|
415 | - if (strpos($match, ':') === false) { |
|
416 | - continue; |
|
417 | - } |
|
414 | + foreach ($matches[1] as $match) { |
|
415 | + if (strpos($match, ':') === false) { |
|
416 | + continue; |
|
417 | + } |
|
418 | 418 | |
419 | - $pattern = array_keys($this->patterns); |
|
420 | - $replace = array_values($this->patterns); |
|
421 | - $segments = explode(':', trim($match, '{}?')); |
|
419 | + $pattern = array_keys($this->patterns); |
|
420 | + $replace = array_values($this->patterns); |
|
421 | + $segments = explode(':', trim($match, '{}?')); |
|
422 | 422 | |
423 | - $uri = strpos($match, ':') !== false |
|
424 | - ? preg_replace($pattern, $replace, $uri) |
|
425 | - : str_replace($match, '{'.$segments[0].'}', $uri); |
|
426 | - } |
|
423 | + $uri = strpos($match, ':') !== false |
|
424 | + ? preg_replace($pattern, $replace, $uri) |
|
425 | + : str_replace($match, '{'.$segments[0].'}', $uri); |
|
426 | + } |
|
427 | 427 | |
428 | - return $uri; |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Add a prefix to the route URI. |
|
433 | - * |
|
434 | - * @param string $prefix |
|
435 | - * |
|
436 | - * @return $this |
|
437 | - */ |
|
438 | - public function prefix($prefix) |
|
439 | - { |
|
440 | - if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
441 | - $this->action['prefix'] = $newPrefix; |
|
442 | - } |
|
428 | + return $uri; |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Add a prefix to the route URI. |
|
433 | + * |
|
434 | + * @param string $prefix |
|
435 | + * |
|
436 | + * @return $this |
|
437 | + */ |
|
438 | + public function prefix($prefix) |
|
439 | + { |
|
440 | + if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
441 | + $this->action['prefix'] = $newPrefix; |
|
442 | + } |
|
443 | 443 | |
444 | - $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
444 | + $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
445 | 445 | |
446 | - return $this->parseRoute($uri !== '/' ? trim($uri, '/') : $uri); |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Set the action array for the route. |
|
451 | - * |
|
452 | - * @param array $action |
|
453 | - * |
|
454 | - * @return $this |
|
455 | - */ |
|
456 | - public function setAction(array $action) |
|
457 | - { |
|
458 | - $this->action = $action; |
|
459 | - |
|
460 | - if (isset($this->action['domain'])) { |
|
461 | - $this->domain($this->action['domain']); |
|
462 | - } |
|
446 | + return $this->parseRoute($uri !== '/' ? trim($uri, '/') : $uri); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Set the action array for the route. |
|
451 | + * |
|
452 | + * @param array $action |
|
453 | + * |
|
454 | + * @return $this |
|
455 | + */ |
|
456 | + public function setAction(array $action) |
|
457 | + { |
|
458 | + $this->action = $action; |
|
459 | + |
|
460 | + if (isset($this->action['domain'])) { |
|
461 | + $this->domain($this->action['domain']); |
|
462 | + } |
|
463 | 463 | |
464 | - return $this; |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Set the name. |
|
469 | - * |
|
470 | - * @param string $name |
|
471 | - * |
|
472 | - * @return string |
|
473 | - */ |
|
474 | - public function name($name) |
|
475 | - { |
|
476 | - $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
477 | - |
|
478 | - return $this; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Determine whether the route's name matches the given patterns. |
|
483 | - * |
|
484 | - * @param mixed ...$patterns |
|
485 | - * |
|
486 | - * @return bool |
|
487 | - */ |
|
488 | - public function named(...$patterns) |
|
489 | - { |
|
490 | - if (is_null($routeName = $this->getName())) { |
|
491 | - return false; |
|
492 | - } |
|
493 | - |
|
494 | - foreach ($patterns as $pattern) { |
|
495 | - if (Str::is($pattern, $routeName)) { |
|
496 | - return true; |
|
497 | - } |
|
498 | - } |
|
499 | - |
|
500 | - return false; |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * Set a default value for the route. |
|
505 | - * |
|
506 | - * @param string $key |
|
507 | - * @param mixed $value |
|
508 | - * |
|
509 | - * @return $this |
|
510 | - */ |
|
511 | - public function defaults($key, $value) |
|
512 | - { |
|
513 | - $this->defaults[$key] = $value; |
|
514 | - |
|
515 | - return $this; |
|
516 | - } |
|
517 | - |
|
518 | - /** |
|
519 | - * Set a default values for the route. |
|
520 | - * |
|
521 | - * @param string $defaults |
|
522 | - * |
|
523 | - * @return $this |
|
524 | - */ |
|
525 | - public function setDefaults(array $defaults) |
|
526 | - { |
|
527 | - $this->defaults = $defaults; |
|
528 | - |
|
529 | - return $this; |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * Set the where. |
|
534 | - * |
|
535 | - * @param array|string $name |
|
536 | - * @param string|null $expression (null by default) |
|
537 | - * |
|
538 | - * @return $this |
|
539 | - */ |
|
540 | - public function where($name, string $expression = null) |
|
541 | - { |
|
542 | - $wheres = is_array($name) ? $name : [$name => $expression]; |
|
464 | + return $this; |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Set the name. |
|
469 | + * |
|
470 | + * @param string $name |
|
471 | + * |
|
472 | + * @return string |
|
473 | + */ |
|
474 | + public function name($name) |
|
475 | + { |
|
476 | + $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
477 | + |
|
478 | + return $this; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Determine whether the route's name matches the given patterns. |
|
483 | + * |
|
484 | + * @param mixed ...$patterns |
|
485 | + * |
|
486 | + * @return bool |
|
487 | + */ |
|
488 | + public function named(...$patterns) |
|
489 | + { |
|
490 | + if (is_null($routeName = $this->getName())) { |
|
491 | + return false; |
|
492 | + } |
|
493 | + |
|
494 | + foreach ($patterns as $pattern) { |
|
495 | + if (Str::is($pattern, $routeName)) { |
|
496 | + return true; |
|
497 | + } |
|
498 | + } |
|
499 | + |
|
500 | + return false; |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * Set a default value for the route. |
|
505 | + * |
|
506 | + * @param string $key |
|
507 | + * @param mixed $value |
|
508 | + * |
|
509 | + * @return $this |
|
510 | + */ |
|
511 | + public function defaults($key, $value) |
|
512 | + { |
|
513 | + $this->defaults[$key] = $value; |
|
514 | + |
|
515 | + return $this; |
|
516 | + } |
|
517 | + |
|
518 | + /** |
|
519 | + * Set a default values for the route. |
|
520 | + * |
|
521 | + * @param string $defaults |
|
522 | + * |
|
523 | + * @return $this |
|
524 | + */ |
|
525 | + public function setDefaults(array $defaults) |
|
526 | + { |
|
527 | + $this->defaults = $defaults; |
|
528 | + |
|
529 | + return $this; |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * Set the where. |
|
534 | + * |
|
535 | + * @param array|string $name |
|
536 | + * @param string|null $expression (null by default) |
|
537 | + * |
|
538 | + * @return $this |
|
539 | + */ |
|
540 | + public function where($name, string $expression = null) |
|
541 | + { |
|
542 | + $wheres = is_array($name) ? $name : [$name => $expression]; |
|
543 | 543 | |
544 | - foreach ($wheres as $name => $expression) { |
|
545 | - $this->wheres[$name] = $expression; |
|
546 | - } |
|
547 | - |
|
548 | - return $this; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Bind the route to a given request for execution. |
|
553 | - * |
|
554 | - * @param \Syscodes\Http\Request $request |
|
555 | - * |
|
556 | - * @return $this |
|
557 | - */ |
|
558 | - public function bind(Request $request) |
|
559 | - { |
|
560 | - $this->parameters = (new RouteParamBinding($this))->parameters($request); |
|
561 | - |
|
562 | - return $this; |
|
563 | - } |
|
564 | - |
|
565 | - /** |
|
566 | - * Get all of the parameter names for the route. |
|
567 | - * |
|
568 | - * @return array |
|
569 | - */ |
|
570 | - public function parameterNames() |
|
571 | - { |
|
572 | - if (isset($this->parameterNames)) { |
|
573 | - return $this->parameterNames; |
|
574 | - } |
|
575 | - |
|
576 | - return $this->parameterNames = $this->compileParamNames(); |
|
577 | - } |
|
578 | - |
|
579 | - /** |
|
580 | - * Get the parameter names for the route. |
|
581 | - * |
|
582 | - * @return array |
|
583 | - */ |
|
584 | - protected function compileParamNames() |
|
585 | - { |
|
586 | - preg_match_all('~[^\/\{(.*?)\}]~', $this->domain().$this->uri, $matches); |
|
587 | - |
|
588 | - return array_map(function ($match) { |
|
589 | - return trim($match, '?'); |
|
590 | - }, $matches[0]); |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Get a given parameter from the route. |
|
595 | - * |
|
596 | - * @param string $name |
|
597 | - * @param mixed $default (null by default) |
|
598 | - * |
|
599 | - * @return array |
|
600 | - */ |
|
601 | - public function parameter($name, $default = null) |
|
602 | - { |
|
603 | - return Arr::get($this->parameters(), $name, $default); |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Set a parameter to the given value. |
|
608 | - * |
|
609 | - * @param string $name |
|
610 | - * @param mixed $value |
|
611 | - * |
|
612 | - * @return array |
|
613 | - */ |
|
614 | - public function setParameter($name, $value) |
|
615 | - { |
|
616 | - $this->parameters(); |
|
617 | - |
|
618 | - $this->parameters[$name] = $value; |
|
619 | - } |
|
620 | - |
|
621 | - /** |
|
622 | - * Get the key / value list of parameters without null values. |
|
623 | - * |
|
624 | - * @return array |
|
625 | - */ |
|
626 | - public function parametersWithouNulls() |
|
627 | - { |
|
628 | - return array_filter($this->parameters(), function ($parameter) { |
|
629 | - return ! is_null($parameter); |
|
630 | - }); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * Get the key / value list of parameters for the route. |
|
635 | - * |
|
636 | - * @return array |
|
637 | - */ |
|
638 | - public function parameters() |
|
639 | - { |
|
640 | - if (isset($this->parameters)) { |
|
641 | - return $this->parameters; |
|
642 | - } |
|
643 | - |
|
644 | - throw new LogicException('The route is not bound.'); |
|
645 | - } |
|
646 | - |
|
647 | - /** |
|
648 | - * Determine if the route only responds to HTTP requests. |
|
649 | - * |
|
650 | - * @return bool |
|
651 | - */ |
|
652 | - public function httpOnly() |
|
653 | - { |
|
654 | - return in_array('http', $this->action, true); |
|
655 | - } |
|
656 | - |
|
657 | - /** |
|
658 | - * Determine if the route only responds to HTTPS requests. |
|
659 | - * |
|
660 | - * @return bool |
|
661 | - */ |
|
662 | - public function httpsOnly() |
|
663 | - { |
|
664 | - return $this->secure(); |
|
665 | - } |
|
666 | - |
|
667 | - /** |
|
668 | - * Determine if the route only responds to HTTPS requests. |
|
669 | - * |
|
670 | - * @return bool |
|
671 | - */ |
|
672 | - public function secure() |
|
673 | - { |
|
674 | - return in_array('https', $this->action, true); |
|
675 | - } |
|
676 | - |
|
677 | - /** |
|
678 | - * Set the container instance on the route. |
|
679 | - * |
|
680 | - * @param \Syscodes\Container\Container $container |
|
681 | - * |
|
682 | - * @return $this |
|
683 | - */ |
|
684 | - public function setContainer(Container $container) |
|
685 | - { |
|
686 | - $this->container = $container; |
|
687 | - |
|
688 | - return $this; |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * Dynamically access route parameters. |
|
693 | - * |
|
694 | - * @param string $key |
|
695 | - * |
|
696 | - * @return mixed |
|
697 | - */ |
|
698 | - public function __get($key) |
|
699 | - { |
|
700 | - return $this->parameter($key); |
|
701 | - } |
|
544 | + foreach ($wheres as $name => $expression) { |
|
545 | + $this->wheres[$name] = $expression; |
|
546 | + } |
|
547 | + |
|
548 | + return $this; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Bind the route to a given request for execution. |
|
553 | + * |
|
554 | + * @param \Syscodes\Http\Request $request |
|
555 | + * |
|
556 | + * @return $this |
|
557 | + */ |
|
558 | + public function bind(Request $request) |
|
559 | + { |
|
560 | + $this->parameters = (new RouteParamBinding($this))->parameters($request); |
|
561 | + |
|
562 | + return $this; |
|
563 | + } |
|
564 | + |
|
565 | + /** |
|
566 | + * Get all of the parameter names for the route. |
|
567 | + * |
|
568 | + * @return array |
|
569 | + */ |
|
570 | + public function parameterNames() |
|
571 | + { |
|
572 | + if (isset($this->parameterNames)) { |
|
573 | + return $this->parameterNames; |
|
574 | + } |
|
575 | + |
|
576 | + return $this->parameterNames = $this->compileParamNames(); |
|
577 | + } |
|
578 | + |
|
579 | + /** |
|
580 | + * Get the parameter names for the route. |
|
581 | + * |
|
582 | + * @return array |
|
583 | + */ |
|
584 | + protected function compileParamNames() |
|
585 | + { |
|
586 | + preg_match_all('~[^\/\{(.*?)\}]~', $this->domain().$this->uri, $matches); |
|
587 | + |
|
588 | + return array_map(function ($match) { |
|
589 | + return trim($match, '?'); |
|
590 | + }, $matches[0]); |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Get a given parameter from the route. |
|
595 | + * |
|
596 | + * @param string $name |
|
597 | + * @param mixed $default (null by default) |
|
598 | + * |
|
599 | + * @return array |
|
600 | + */ |
|
601 | + public function parameter($name, $default = null) |
|
602 | + { |
|
603 | + return Arr::get($this->parameters(), $name, $default); |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Set a parameter to the given value. |
|
608 | + * |
|
609 | + * @param string $name |
|
610 | + * @param mixed $value |
|
611 | + * |
|
612 | + * @return array |
|
613 | + */ |
|
614 | + public function setParameter($name, $value) |
|
615 | + { |
|
616 | + $this->parameters(); |
|
617 | + |
|
618 | + $this->parameters[$name] = $value; |
|
619 | + } |
|
620 | + |
|
621 | + /** |
|
622 | + * Get the key / value list of parameters without null values. |
|
623 | + * |
|
624 | + * @return array |
|
625 | + */ |
|
626 | + public function parametersWithouNulls() |
|
627 | + { |
|
628 | + return array_filter($this->parameters(), function ($parameter) { |
|
629 | + return ! is_null($parameter); |
|
630 | + }); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * Get the key / value list of parameters for the route. |
|
635 | + * |
|
636 | + * @return array |
|
637 | + */ |
|
638 | + public function parameters() |
|
639 | + { |
|
640 | + if (isset($this->parameters)) { |
|
641 | + return $this->parameters; |
|
642 | + } |
|
643 | + |
|
644 | + throw new LogicException('The route is not bound.'); |
|
645 | + } |
|
646 | + |
|
647 | + /** |
|
648 | + * Determine if the route only responds to HTTP requests. |
|
649 | + * |
|
650 | + * @return bool |
|
651 | + */ |
|
652 | + public function httpOnly() |
|
653 | + { |
|
654 | + return in_array('http', $this->action, true); |
|
655 | + } |
|
656 | + |
|
657 | + /** |
|
658 | + * Determine if the route only responds to HTTPS requests. |
|
659 | + * |
|
660 | + * @return bool |
|
661 | + */ |
|
662 | + public function httpsOnly() |
|
663 | + { |
|
664 | + return $this->secure(); |
|
665 | + } |
|
666 | + |
|
667 | + /** |
|
668 | + * Determine if the route only responds to HTTPS requests. |
|
669 | + * |
|
670 | + * @return bool |
|
671 | + */ |
|
672 | + public function secure() |
|
673 | + { |
|
674 | + return in_array('https', $this->action, true); |
|
675 | + } |
|
676 | + |
|
677 | + /** |
|
678 | + * Set the container instance on the route. |
|
679 | + * |
|
680 | + * @param \Syscodes\Container\Container $container |
|
681 | + * |
|
682 | + * @return $this |
|
683 | + */ |
|
684 | + public function setContainer(Container $container) |
|
685 | + { |
|
686 | + $this->container = $container; |
|
687 | + |
|
688 | + return $this; |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * Dynamically access route parameters. |
|
693 | + * |
|
694 | + * @param string $key |
|
695 | + * |
|
696 | + * @return mixed |
|
697 | + */ |
|
698 | + public function __get($key) |
|
699 | + { |
|
700 | + return $this->parameter($key); |
|
701 | + } |
|
702 | 702 | } |
703 | 703 | \ No newline at end of file |
@@ -37,96 +37,96 @@ |
||
37 | 37 | */ |
38 | 38 | trait RouteResolver |
39 | 39 | { |
40 | - /** |
|
41 | - * The currently dispatched route instance. |
|
42 | - * |
|
43 | - * @var \Syscodes\Routing\Route|null |
|
44 | - */ |
|
45 | - protected $current; |
|
40 | + /** |
|
41 | + * The currently dispatched route instance. |
|
42 | + * |
|
43 | + * @var \Syscodes\Routing\Route|null |
|
44 | + */ |
|
45 | + protected $current; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Resolve the given route and call the method that belongs to the route. |
|
49 | - * |
|
50 | - * @param \Syscodes\Http\Request $request |
|
51 | - * @param \Syscodes\Routing\RouteCollection $route |
|
52 | - * |
|
53 | - * @return \Syscodes\Http\Response |
|
54 | - */ |
|
55 | - public function resolve($request, Routecollection $route) |
|
56 | - { |
|
57 | - return $this->dispatchToRoute($request, $route); |
|
58 | - } |
|
47 | + /** |
|
48 | + * Resolve the given route and call the method that belongs to the route. |
|
49 | + * |
|
50 | + * @param \Syscodes\Http\Request $request |
|
51 | + * @param \Syscodes\Routing\RouteCollection $route |
|
52 | + * |
|
53 | + * @return \Syscodes\Http\Response |
|
54 | + */ |
|
55 | + public function resolve($request, Routecollection $route) |
|
56 | + { |
|
57 | + return $this->dispatchToRoute($request, $route); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Dispatch the request to a route and return the response. |
|
62 | - * |
|
63 | - * @param \Syscodes\Http\Request $request |
|
64 | - * @param \Syscodes\Routing\RouteCollection $route |
|
65 | - * |
|
66 | - * @return \Syscodes\Http\Response |
|
67 | - */ |
|
68 | - protected function dispatchToRoute($request, $route) |
|
69 | - { |
|
70 | - return $this->runRoute($request, |
|
71 | - $this->findRoute($request, $route) |
|
72 | - ); |
|
73 | - } |
|
60 | + /** |
|
61 | + * Dispatch the request to a route and return the response. |
|
62 | + * |
|
63 | + * @param \Syscodes\Http\Request $request |
|
64 | + * @param \Syscodes\Routing\RouteCollection $route |
|
65 | + * |
|
66 | + * @return \Syscodes\Http\Response |
|
67 | + */ |
|
68 | + protected function dispatchToRoute($request, $route) |
|
69 | + { |
|
70 | + return $this->runRoute($request, |
|
71 | + $this->findRoute($request, $route) |
|
72 | + ); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Return the response for the given route. |
|
77 | - * |
|
78 | - * @param \Syscodes\Http\Request $request |
|
79 | - * @param \Syscodes\Routing\Route $route |
|
80 | - * |
|
81 | - * @return \Syscodes\Http\Response |
|
82 | - */ |
|
83 | - protected function runRoute($request, $route) |
|
84 | - { |
|
85 | - return (new Pipeline($this->container)) |
|
86 | - ->send($request) |
|
87 | - ->then(function ($request) use ($route) { |
|
88 | - return $this->callResponse( |
|
89 | - $request, $route->runResolver() |
|
90 | - ); |
|
91 | - }); |
|
92 | - } |
|
75 | + /** |
|
76 | + * Return the response for the given route. |
|
77 | + * |
|
78 | + * @param \Syscodes\Http\Request $request |
|
79 | + * @param \Syscodes\Routing\Route $route |
|
80 | + * |
|
81 | + * @return \Syscodes\Http\Response |
|
82 | + */ |
|
83 | + protected function runRoute($request, $route) |
|
84 | + { |
|
85 | + return (new Pipeline($this->container)) |
|
86 | + ->send($request) |
|
87 | + ->then(function ($request) use ($route) { |
|
88 | + return $this->callResponse( |
|
89 | + $request, $route->runResolver() |
|
90 | + ); |
|
91 | + }); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Create a response instance from the given value. |
|
96 | - * |
|
97 | - * @param \Syscodes\Http\Request $request |
|
98 | - * @param mixed $response |
|
99 | - * |
|
100 | - * @return \Syscodes\Http\Response |
|
101 | - */ |
|
102 | - protected function callResponse($request, $response) |
|
103 | - { |
|
104 | - if ( ! $response instanceof Response && |
|
105 | - ($response instanceof Jsonserializable || |
|
106 | - is_array($response))) { |
|
107 | - $response = new JsonResponse($response); |
|
108 | - } elseif ( ! $response instanceof Response) { |
|
109 | - $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
110 | - } |
|
94 | + /** |
|
95 | + * Create a response instance from the given value. |
|
96 | + * |
|
97 | + * @param \Syscodes\Http\Request $request |
|
98 | + * @param mixed $response |
|
99 | + * |
|
100 | + * @return \Syscodes\Http\Response |
|
101 | + */ |
|
102 | + protected function callResponse($request, $response) |
|
103 | + { |
|
104 | + if ( ! $response instanceof Response && |
|
105 | + ($response instanceof Jsonserializable || |
|
106 | + is_array($response))) { |
|
107 | + $response = new JsonResponse($response); |
|
108 | + } elseif ( ! $response instanceof Response) { |
|
109 | + $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
110 | + } |
|
111 | 111 | |
112 | - return $response->prepare($request); |
|
113 | - } |
|
112 | + return $response->prepare($request); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Find the route matching a given request. |
|
117 | - * |
|
118 | - * @param \Syscodes\Http\Request $request |
|
119 | - * @param \Syscodes\Routing\RouteCollection $route |
|
120 | - * |
|
121 | - * @return \Syscodes\Routing\Route |
|
122 | - */ |
|
123 | - protected function findRoute($request, $route) |
|
124 | - { |
|
125 | - // Get all register routes with the same request method |
|
126 | - $this->current = $route = $route->match($request); |
|
115 | + /** |
|
116 | + * Find the route matching a given request. |
|
117 | + * |
|
118 | + * @param \Syscodes\Http\Request $request |
|
119 | + * @param \Syscodes\Routing\RouteCollection $route |
|
120 | + * |
|
121 | + * @return \Syscodes\Routing\Route |
|
122 | + */ |
|
123 | + protected function findRoute($request, $route) |
|
124 | + { |
|
125 | + // Get all register routes with the same request method |
|
126 | + $this->current = $route = $route->match($request); |
|
127 | 127 | |
128 | - $this->container->instance(Route::class, $route); |
|
128 | + $this->container->instance(Route::class, $route); |
|
129 | 129 | |
130 | - return $route; |
|
131 | - } |
|
130 | + return $route; |
|
131 | + } |
|
132 | 132 | } |
133 | 133 | \ No newline at end of file |
@@ -39,581 +39,581 @@ |
||
39 | 39 | */ |
40 | 40 | class Router implements Routable |
41 | 41 | { |
42 | - use Concerns\RouteMap, |
|
43 | - Concerns\RouteResolver; |
|
44 | - |
|
45 | - /** |
|
46 | - * The registered route value binders. |
|
47 | - * |
|
48 | - * @var array $binders |
|
49 | - */ |
|
50 | - protected $binders = []; |
|
51 | - |
|
52 | - /** |
|
53 | - * The container instance used by the router. |
|
54 | - * |
|
55 | - * @var \Syscodes\Contracts\Container\Container $container |
|
56 | - */ |
|
57 | - protected $container; |
|
58 | - |
|
59 | - /** |
|
60 | - * Variable of group route. |
|
61 | - * |
|
62 | - * @var array $groupStack |
|
63 | - */ |
|
64 | - protected $groupStack = []; |
|
42 | + use Concerns\RouteMap, |
|
43 | + Concerns\RouteResolver; |
|
44 | + |
|
45 | + /** |
|
46 | + * The registered route value binders. |
|
47 | + * |
|
48 | + * @var array $binders |
|
49 | + */ |
|
50 | + protected $binders = []; |
|
51 | + |
|
52 | + /** |
|
53 | + * The container instance used by the router. |
|
54 | + * |
|
55 | + * @var \Syscodes\Contracts\Container\Container $container |
|
56 | + */ |
|
57 | + protected $container; |
|
58 | + |
|
59 | + /** |
|
60 | + * Variable of group route. |
|
61 | + * |
|
62 | + * @var array $groupStack |
|
63 | + */ |
|
64 | + protected $groupStack = []; |
|
65 | 65 | |
66 | - /** |
|
67 | - * The registered string macros. |
|
68 | - * |
|
69 | - * @var array $macros |
|
70 | - */ |
|
71 | - protected $macros = []; |
|
72 | - |
|
73 | - /** |
|
74 | - * Middleware for function of filters |
|
75 | - * |
|
76 | - * @var string[] $middleware |
|
77 | - */ |
|
78 | - protected $middleware = []; |
|
66 | + /** |
|
67 | + * The registered string macros. |
|
68 | + * |
|
69 | + * @var array $macros |
|
70 | + */ |
|
71 | + protected $macros = []; |
|
72 | + |
|
73 | + /** |
|
74 | + * Middleware for function of filters |
|
75 | + * |
|
76 | + * @var string[] $middleware |
|
77 | + */ |
|
78 | + protected $middleware = []; |
|
79 | 79 | |
80 | - /** |
|
81 | - * The globally available parameter patterns. |
|
82 | - * |
|
83 | - * @var array $patterns |
|
84 | - */ |
|
85 | - protected $patterns = []; |
|
86 | - |
|
87 | - /** |
|
88 | - * The route collection instance. |
|
89 | - * |
|
90 | - * @var \Syscodes\Routing\RouteCollection $routes |
|
91 | - */ |
|
92 | - protected $routes; |
|
93 | - |
|
94 | - /** |
|
95 | - * The Resource instance. |
|
96 | - * |
|
97 | - * @var \Syscodes\Routing\ResourceRegister $resources |
|
98 | - */ |
|
99 | - protected $resources; |
|
100 | - |
|
101 | - /** |
|
102 | - * Constructor. Create a new Router instance. |
|
103 | - * |
|
104 | - * @param \Syscodes\Contracts\Container\Container|null $container (null by default) |
|
105 | - * |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - public function __construct(Container $container = null) |
|
109 | - { |
|
110 | - $this->routes = new RouteCollection(); |
|
111 | - |
|
112 | - $this->container = $container ?: new Container; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get the prefix from the group on the stack. |
|
117 | - * |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function getGroupPrefix() |
|
121 | - { |
|
122 | - if ( ! empty($this->groupStack)) { |
|
123 | - $last = end($this->groupStack); |
|
124 | - |
|
125 | - return $last['prefix'] ?? ''; |
|
126 | - } |
|
127 | - |
|
128 | - return ''; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Group a series of routes under a single URL segment. This is handy |
|
133 | - * for grouping items into an admin area, like: |
|
134 | - * |
|
135 | - * Example: |
|
136 | - * // Creates route: /admin show the word 'User' |
|
137 | - * Route::group(['prefix' => 'admin'], function() { |
|
138 | - * |
|
139 | - * Route::get('/user', function() { |
|
140 | - * echo 'Hello world..!'; |
|
141 | - * }); |
|
142 | - * |
|
143 | - * }); /admin/user |
|
144 | - * |
|
145 | - * @param array $attributes |
|
146 | - * @param \Closure|string $callback |
|
147 | - * |
|
148 | - * @return void |
|
149 | - */ |
|
150 | - public function group(array $attributes, $callback) |
|
151 | - { |
|
152 | - $this->updateGroupStack($attributes); |
|
153 | - |
|
154 | - $this->loadRoutes($callback); |
|
155 | - |
|
156 | - array_pop($this->groupStack); |
|
157 | - |
|
158 | - return $this; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Update the group stack with the given attributes. |
|
163 | - * |
|
164 | - * @param array $attributes |
|
165 | - * |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - protected function updateGroupStack(array $attributes) |
|
169 | - { |
|
170 | - if ( ! empty($this->groupStack)) { |
|
171 | - $attributes = $this->mergeGroup($attributes); |
|
172 | - } |
|
173 | - |
|
174 | - $this->groupStack[] = $attributes; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Merge the given group attributes. |
|
179 | - * |
|
180 | - * @param array $new |
|
181 | - * |
|
182 | - * @return array |
|
183 | - */ |
|
184 | - protected function mergeGroup($new) |
|
185 | - { |
|
186 | - return RouteGroup::mergeGroup($new, end($this->groupStack)); |
|
187 | - } |
|
80 | + /** |
|
81 | + * The globally available parameter patterns. |
|
82 | + * |
|
83 | + * @var array $patterns |
|
84 | + */ |
|
85 | + protected $patterns = []; |
|
86 | + |
|
87 | + /** |
|
88 | + * The route collection instance. |
|
89 | + * |
|
90 | + * @var \Syscodes\Routing\RouteCollection $routes |
|
91 | + */ |
|
92 | + protected $routes; |
|
93 | + |
|
94 | + /** |
|
95 | + * The Resource instance. |
|
96 | + * |
|
97 | + * @var \Syscodes\Routing\ResourceRegister $resources |
|
98 | + */ |
|
99 | + protected $resources; |
|
100 | + |
|
101 | + /** |
|
102 | + * Constructor. Create a new Router instance. |
|
103 | + * |
|
104 | + * @param \Syscodes\Contracts\Container\Container|null $container (null by default) |
|
105 | + * |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + public function __construct(Container $container = null) |
|
109 | + { |
|
110 | + $this->routes = new RouteCollection(); |
|
111 | + |
|
112 | + $this->container = $container ?: new Container; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get the prefix from the group on the stack. |
|
117 | + * |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function getGroupPrefix() |
|
121 | + { |
|
122 | + if ( ! empty($this->groupStack)) { |
|
123 | + $last = end($this->groupStack); |
|
124 | + |
|
125 | + return $last['prefix'] ?? ''; |
|
126 | + } |
|
127 | + |
|
128 | + return ''; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Group a series of routes under a single URL segment. This is handy |
|
133 | + * for grouping items into an admin area, like: |
|
134 | + * |
|
135 | + * Example: |
|
136 | + * // Creates route: /admin show the word 'User' |
|
137 | + * Route::group(['prefix' => 'admin'], function() { |
|
138 | + * |
|
139 | + * Route::get('/user', function() { |
|
140 | + * echo 'Hello world..!'; |
|
141 | + * }); |
|
142 | + * |
|
143 | + * }); /admin/user |
|
144 | + * |
|
145 | + * @param array $attributes |
|
146 | + * @param \Closure|string $callback |
|
147 | + * |
|
148 | + * @return void |
|
149 | + */ |
|
150 | + public function group(array $attributes, $callback) |
|
151 | + { |
|
152 | + $this->updateGroupStack($attributes); |
|
153 | + |
|
154 | + $this->loadRoutes($callback); |
|
155 | + |
|
156 | + array_pop($this->groupStack); |
|
157 | + |
|
158 | + return $this; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Update the group stack with the given attributes. |
|
163 | + * |
|
164 | + * @param array $attributes |
|
165 | + * |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + protected function updateGroupStack(array $attributes) |
|
169 | + { |
|
170 | + if ( ! empty($this->groupStack)) { |
|
171 | + $attributes = $this->mergeGroup($attributes); |
|
172 | + } |
|
173 | + |
|
174 | + $this->groupStack[] = $attributes; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Merge the given group attributes. |
|
179 | + * |
|
180 | + * @param array $new |
|
181 | + * |
|
182 | + * @return array |
|
183 | + */ |
|
184 | + protected function mergeGroup($new) |
|
185 | + { |
|
186 | + return RouteGroup::mergeGroup($new, end($this->groupStack)); |
|
187 | + } |
|
188 | 188 | |
189 | - /** |
|
190 | - * Load the provided routes. |
|
191 | - * |
|
192 | - * @param \Closure|string $callback |
|
193 | - * |
|
194 | - * @return void |
|
195 | - */ |
|
196 | - protected function loadRoutes($callback) |
|
197 | - { |
|
198 | - if ($callback instanceof Closure) { |
|
199 | - $callback($this); |
|
200 | - } else { |
|
201 | - (new RouteFileRegister($this))->register($callback); |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Add a route to the underlying route collection. |
|
207 | - * |
|
208 | - * @param array|string $method |
|
209 | - * @param string $route |
|
210 | - * @param mixed $action |
|
211 | - * |
|
212 | - * @return \Syscodes\Routing\Route |
|
213 | - */ |
|
214 | - public function addRoute($method, $route, $action) |
|
215 | - { |
|
216 | - return $this->routes->add($this->map($method, $route, $action)); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Create a redirect from one URI to another. |
|
221 | - * |
|
222 | - * @param string $uri |
|
223 | - * @param string $destination |
|
224 | - * @param int $status (302 by default) |
|
225 | - * |
|
226 | - * @return \Syscodes\Routing\Route |
|
227 | - */ |
|
228 | - public function redirect($uri, $destination, $status = 302) |
|
229 | - { |
|
230 | - return $this->any($uri, function () use ($destination, $status) { |
|
231 | - return new RedirectResponse($destination, $status); |
|
232 | - }); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Register a new route that returns a view. |
|
237 | - * |
|
238 | - * @param string $uri |
|
239 | - * @param string $view |
|
240 | - * @param array $data |
|
241 | - * |
|
242 | - * @return \Syscodes\Routing\Route |
|
243 | - */ |
|
244 | - public function view($uri, $view, $data = []) |
|
245 | - { |
|
246 | - return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
247 | - return $this->container->make('view')->make($view, $data); |
|
248 | - }); |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Add new route to routes array. |
|
253 | - * |
|
254 | - * @param array|string $method |
|
255 | - * @param string $route |
|
256 | - * @param mixed $action |
|
257 | - * |
|
258 | - * @return void |
|
259 | - * |
|
260 | - * @throws \InvalidArgumentException |
|
261 | - */ |
|
262 | - public function map($method, $route, $action) |
|
263 | - { |
|
264 | - if ($this->actionReferencesController($action)) { |
|
265 | - $action = $this->convertToControllerAction($action); |
|
266 | - } |
|
267 | - |
|
268 | - $route = $this->newRoute( |
|
269 | - array_map('strtoupper', (array) $method), |
|
270 | - $this->prefix($route), |
|
271 | - $action |
|
272 | - ); |
|
273 | - |
|
274 | - if ($this->hasGroupStack()) { |
|
275 | - $this->mergeGroupAttributesIntoRoute($route); |
|
276 | - } |
|
277 | - |
|
278 | - $this->addWhereClausesToRoute($route); |
|
189 | + /** |
|
190 | + * Load the provided routes. |
|
191 | + * |
|
192 | + * @param \Closure|string $callback |
|
193 | + * |
|
194 | + * @return void |
|
195 | + */ |
|
196 | + protected function loadRoutes($callback) |
|
197 | + { |
|
198 | + if ($callback instanceof Closure) { |
|
199 | + $callback($this); |
|
200 | + } else { |
|
201 | + (new RouteFileRegister($this))->register($callback); |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Add a route to the underlying route collection. |
|
207 | + * |
|
208 | + * @param array|string $method |
|
209 | + * @param string $route |
|
210 | + * @param mixed $action |
|
211 | + * |
|
212 | + * @return \Syscodes\Routing\Route |
|
213 | + */ |
|
214 | + public function addRoute($method, $route, $action) |
|
215 | + { |
|
216 | + return $this->routes->add($this->map($method, $route, $action)); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Create a redirect from one URI to another. |
|
221 | + * |
|
222 | + * @param string $uri |
|
223 | + * @param string $destination |
|
224 | + * @param int $status (302 by default) |
|
225 | + * |
|
226 | + * @return \Syscodes\Routing\Route |
|
227 | + */ |
|
228 | + public function redirect($uri, $destination, $status = 302) |
|
229 | + { |
|
230 | + return $this->any($uri, function () use ($destination, $status) { |
|
231 | + return new RedirectResponse($destination, $status); |
|
232 | + }); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Register a new route that returns a view. |
|
237 | + * |
|
238 | + * @param string $uri |
|
239 | + * @param string $view |
|
240 | + * @param array $data |
|
241 | + * |
|
242 | + * @return \Syscodes\Routing\Route |
|
243 | + */ |
|
244 | + public function view($uri, $view, $data = []) |
|
245 | + { |
|
246 | + return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
247 | + return $this->container->make('view')->make($view, $data); |
|
248 | + }); |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Add new route to routes array. |
|
253 | + * |
|
254 | + * @param array|string $method |
|
255 | + * @param string $route |
|
256 | + * @param mixed $action |
|
257 | + * |
|
258 | + * @return void |
|
259 | + * |
|
260 | + * @throws \InvalidArgumentException |
|
261 | + */ |
|
262 | + public function map($method, $route, $action) |
|
263 | + { |
|
264 | + if ($this->actionReferencesController($action)) { |
|
265 | + $action = $this->convertToControllerAction($action); |
|
266 | + } |
|
267 | + |
|
268 | + $route = $this->newRoute( |
|
269 | + array_map('strtoupper', (array) $method), |
|
270 | + $this->prefix($route), |
|
271 | + $action |
|
272 | + ); |
|
273 | + |
|
274 | + if ($this->hasGroupStack()) { |
|
275 | + $this->mergeGroupAttributesIntoRoute($route); |
|
276 | + } |
|
277 | + |
|
278 | + $this->addWhereClausesToRoute($route); |
|
279 | 279 | |
280 | - return $route; |
|
281 | - } |
|
280 | + return $route; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Determine if the action is routing to a controller. |
|
285 | - * |
|
286 | - * @param array $action |
|
287 | - * |
|
288 | - * @return bool |
|
289 | - */ |
|
290 | - protected function actionReferencesController($action) |
|
291 | - { |
|
292 | - if ($action instanceof Closure) { |
|
293 | - return false; |
|
294 | - } |
|
283 | + /** |
|
284 | + * Determine if the action is routing to a controller. |
|
285 | + * |
|
286 | + * @param array $action |
|
287 | + * |
|
288 | + * @return bool |
|
289 | + */ |
|
290 | + protected function actionReferencesController($action) |
|
291 | + { |
|
292 | + if ($action instanceof Closure) { |
|
293 | + return false; |
|
294 | + } |
|
295 | 295 | |
296 | - return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
297 | - } |
|
296 | + return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Add a controller based route action to the action array. |
|
301 | - * |
|
302 | - * @param array|string $action |
|
303 | - * |
|
304 | - * @return array |
|
305 | - */ |
|
306 | - protected function convertToControllerAction($action) |
|
307 | - { |
|
308 | - if (is_string($action)) { |
|
309 | - $action = ['uses' => $action]; |
|
310 | - } |
|
299 | + /** |
|
300 | + * Add a controller based route action to the action array. |
|
301 | + * |
|
302 | + * @param array|string $action |
|
303 | + * |
|
304 | + * @return array |
|
305 | + */ |
|
306 | + protected function convertToControllerAction($action) |
|
307 | + { |
|
308 | + if (is_string($action)) { |
|
309 | + $action = ['uses' => $action]; |
|
310 | + } |
|
311 | 311 | |
312 | - if ( ! empty($this->groupStack)) { |
|
313 | - $action['uses'] = $this->prependGroupUses($action['uses']); |
|
314 | - } |
|
312 | + if ( ! empty($this->groupStack)) { |
|
313 | + $action['uses'] = $this->prependGroupUses($action['uses']); |
|
314 | + } |
|
315 | 315 | |
316 | - $action['controller'] = $action['uses']; |
|
316 | + $action['controller'] = $action['uses']; |
|
317 | 317 | |
318 | - return $action; |
|
319 | - } |
|
318 | + return $action; |
|
319 | + } |
|
320 | 320 | |
321 | - /** |
|
322 | - * Prepend the last group uses onto the use clause. |
|
323 | - * |
|
324 | - * @param string $uses |
|
325 | - * |
|
326 | - * @return string |
|
327 | - */ |
|
328 | - protected function prependGroupUses($uses) |
|
329 | - { |
|
330 | - $group = end($this->groupStack); |
|
321 | + /** |
|
322 | + * Prepend the last group uses onto the use clause. |
|
323 | + * |
|
324 | + * @param string $uses |
|
325 | + * |
|
326 | + * @return string |
|
327 | + */ |
|
328 | + protected function prependGroupUses($uses) |
|
329 | + { |
|
330 | + $group = end($this->groupStack); |
|
331 | 331 | |
332 | - return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Create a new Route object. |
|
337 | - * |
|
338 | - * @param array|string $method |
|
339 | - * @param string $uri |
|
340 | - * @param mixed $action |
|
341 | - * |
|
342 | - * @return \Syscodes\Routing\Route |
|
343 | - */ |
|
344 | - public function newRoute($method, $uri, $action) |
|
345 | - { |
|
346 | - return take(new Route($method, $uri, $action)) |
|
347 | - ->setContainer($this->container); |
|
348 | - } |
|
332 | + return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Create a new Route object. |
|
337 | + * |
|
338 | + * @param array|string $method |
|
339 | + * @param string $uri |
|
340 | + * @param mixed $action |
|
341 | + * |
|
342 | + * @return \Syscodes\Routing\Route |
|
343 | + */ |
|
344 | + public function newRoute($method, $uri, $action) |
|
345 | + { |
|
346 | + return take(new Route($method, $uri, $action)) |
|
347 | + ->setContainer($this->container); |
|
348 | + } |
|
349 | 349 | |
350 | - /** |
|
351 | - * Determine if the router currently has a group stack. |
|
352 | - * |
|
353 | - * @return bool |
|
354 | - */ |
|
355 | - public function hasGroupStack() |
|
356 | - { |
|
357 | - return ! empty($this->groupStack); |
|
358 | - } |
|
350 | + /** |
|
351 | + * Determine if the router currently has a group stack. |
|
352 | + * |
|
353 | + * @return bool |
|
354 | + */ |
|
355 | + public function hasGroupStack() |
|
356 | + { |
|
357 | + return ! empty($this->groupStack); |
|
358 | + } |
|
359 | 359 | |
360 | - /** |
|
361 | - * Merge the group stack with the controller action. |
|
362 | - * |
|
363 | - * @param \Syscpde\Routing\Route $route |
|
364 | - * |
|
365 | - * @return void |
|
366 | - */ |
|
367 | - protected function mergeGroupAttributesIntoRoute($route) |
|
368 | - { |
|
369 | - $action = static::mergeGroup($route->getAction(), end($this->groupStack)); |
|
360 | + /** |
|
361 | + * Merge the group stack with the controller action. |
|
362 | + * |
|
363 | + * @param \Syscpde\Routing\Route $route |
|
364 | + * |
|
365 | + * @return void |
|
366 | + */ |
|
367 | + protected function mergeGroupAttributesIntoRoute($route) |
|
368 | + { |
|
369 | + $action = static::mergeGroup($route->getAction(), end($this->groupStack)); |
|
370 | 370 | |
371 | - $route->setAction($action); |
|
372 | - } |
|
371 | + $route->setAction($action); |
|
372 | + } |
|
373 | 373 | |
374 | - /** |
|
375 | - * Add the necessary where clauses to the route based on its initial registration. |
|
376 | - * |
|
377 | - * @param \Syscodes\Routing\Route $route |
|
378 | - * |
|
379 | - * @return \Syscodes\Routing\Route |
|
380 | - */ |
|
381 | - protected function addWhereClausesToRoute($route) |
|
382 | - { |
|
383 | - return $route->where(array_merge( |
|
384 | - $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
385 | - )); |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * Add a prefix to the route URI. |
|
390 | - * |
|
391 | - * @param string $uri |
|
392 | - * |
|
393 | - * @return string |
|
394 | - */ |
|
395 | - protected function prefix($uri) |
|
396 | - { |
|
397 | - $uri = is_null($uri) ? '' : trim($uri, '/').'/'; |
|
398 | - |
|
399 | - $uri = filter_var($uri, FILTER_SANITIZE_STRING); |
|
400 | - |
|
401 | - // While we want to add a route within a group of '/', |
|
402 | - // it doens't work with matching, so remove them... |
|
403 | - if ($uri != '/') { |
|
404 | - $uri = ltrim($uri, '/'); |
|
405 | - } |
|
406 | - |
|
407 | - return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Set a global where pattern on all routes. |
|
412 | - * |
|
413 | - * @param string $name |
|
414 | - * @param string $pattern |
|
415 | - * |
|
416 | - * @return void |
|
417 | - */ |
|
418 | - public function pattern($name, $pattern) |
|
419 | - { |
|
420 | - return $this->patterns[$name] = $pattern; |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Set a group of global where patterns on all routes. |
|
425 | - * |
|
426 | - * @param array $patterns |
|
427 | - * |
|
428 | - * @return void |
|
429 | - */ |
|
430 | - public function patterns($patterns) |
|
431 | - { |
|
432 | - foreach ($patterns as $key => $pattern) { |
|
433 | - $this->patterns[$key] = $pattern; |
|
434 | - } |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * Get a Resource instance. |
|
439 | - * |
|
440 | - * @return \Syscodes\Routing\ResourceRegister |
|
441 | - */ |
|
442 | - public function getResource() |
|
443 | - { |
|
444 | - if (isset($this->resources)) { |
|
445 | - return $this->resources; |
|
446 | - } |
|
447 | - |
|
448 | - return $this->resources = new ResourceRegister($this); |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Dispatches the given url and call the method that belongs to the route. |
|
453 | - * |
|
454 | - * @param \Syscodes\Http\Request $request |
|
455 | - * |
|
456 | - * @return mixed |
|
457 | - */ |
|
458 | - public function dispatch(Request $request) |
|
459 | - { |
|
460 | - return $this->resolve($request, $this->routes); |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Check if a route with the given name exists. |
|
465 | - * |
|
466 | - * @param string $name |
|
467 | - * |
|
468 | - * @return bool |
|
469 | - */ |
|
470 | - public function has($name) |
|
471 | - { |
|
472 | - $names = is_array($name) ? $name : func_get_args(); |
|
473 | - |
|
474 | - foreach ($names as $value) { |
|
475 | - if ( ! $this->routes->hasNamedRoute($value)) { |
|
476 | - return false; |
|
477 | - } |
|
478 | - } |
|
479 | - |
|
480 | - return true; |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Get the currently dispatched route instance. |
|
485 | - * |
|
486 | - * @return \Syscodes\Routing\Route|null |
|
487 | - */ |
|
488 | - public function current() |
|
489 | - { |
|
490 | - return $this->current; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * Determine if the current route matches a pattern. |
|
495 | - * |
|
496 | - * @param mixed ...$patterns |
|
497 | - * |
|
498 | - * @return bool |
|
499 | - */ |
|
500 | - public function is(...$patterns) |
|
501 | - { |
|
502 | - return $this->currentRouteNamed(...$patterns); |
|
503 | - } |
|
504 | - |
|
505 | - /** |
|
506 | - * Determine if the current route matches a pattern. |
|
507 | - * |
|
508 | - * @param mixed ...$patterns |
|
509 | - * |
|
510 | - * @return bool |
|
511 | - */ |
|
512 | - public function currentRouteNamed(...$patterns) |
|
513 | - { |
|
514 | - return $this->current() && $this->current()->named(...$patterns); |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Register an array of resource controllers. |
|
519 | - * |
|
520 | - * @param array $resources |
|
521 | - * @param array $options |
|
522 | - * |
|
523 | - * @return void |
|
524 | - */ |
|
525 | - public function resources(array $resources, array $options = []) |
|
526 | - { |
|
527 | - foreach ($resources as $name => $controller) { |
|
528 | - $this->resource($name, $controller, $options); |
|
529 | - } |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * Route a resource to a controller. |
|
534 | - * |
|
535 | - * @param string $name |
|
536 | - * @param string $controller |
|
537 | - * @param array $options |
|
538 | - * |
|
539 | - * @return \Syscodes\Routing\AwaitingResourceRegistration |
|
540 | - */ |
|
541 | - public function resource($name, $controller, array $options = []) |
|
542 | - { |
|
543 | - if ($this->container) { |
|
544 | - $register = $this->container->make(ResourceRegister::class); |
|
545 | - } else { |
|
546 | - $register = new ResourceRegister($this); |
|
547 | - } |
|
548 | - |
|
549 | - return new AwaitingResourceRegistration( |
|
550 | - $register, $name, $controller, $options |
|
551 | - ); |
|
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * Get the route collection. |
|
556 | - * |
|
557 | - * @return array |
|
558 | - */ |
|
559 | - public function getRoutes() |
|
560 | - { |
|
561 | - return $this->routes; |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * Get or set the verbs used in the resource URIs. |
|
566 | - * |
|
567 | - * @param array $verbs |
|
568 | - * |
|
569 | - * @return array|null |
|
570 | - */ |
|
571 | - public function resourceVerbs(array $verbs = []) |
|
572 | - { |
|
573 | - ResourceRegister::verbs($verbs); |
|
574 | - } |
|
374 | + /** |
|
375 | + * Add the necessary where clauses to the route based on its initial registration. |
|
376 | + * |
|
377 | + * @param \Syscodes\Routing\Route $route |
|
378 | + * |
|
379 | + * @return \Syscodes\Routing\Route |
|
380 | + */ |
|
381 | + protected function addWhereClausesToRoute($route) |
|
382 | + { |
|
383 | + return $route->where(array_merge( |
|
384 | + $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
385 | + )); |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * Add a prefix to the route URI. |
|
390 | + * |
|
391 | + * @param string $uri |
|
392 | + * |
|
393 | + * @return string |
|
394 | + */ |
|
395 | + protected function prefix($uri) |
|
396 | + { |
|
397 | + $uri = is_null($uri) ? '' : trim($uri, '/').'/'; |
|
398 | + |
|
399 | + $uri = filter_var($uri, FILTER_SANITIZE_STRING); |
|
400 | + |
|
401 | + // While we want to add a route within a group of '/', |
|
402 | + // it doens't work with matching, so remove them... |
|
403 | + if ($uri != '/') { |
|
404 | + $uri = ltrim($uri, '/'); |
|
405 | + } |
|
406 | + |
|
407 | + return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Set a global where pattern on all routes. |
|
412 | + * |
|
413 | + * @param string $name |
|
414 | + * @param string $pattern |
|
415 | + * |
|
416 | + * @return void |
|
417 | + */ |
|
418 | + public function pattern($name, $pattern) |
|
419 | + { |
|
420 | + return $this->patterns[$name] = $pattern; |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Set a group of global where patterns on all routes. |
|
425 | + * |
|
426 | + * @param array $patterns |
|
427 | + * |
|
428 | + * @return void |
|
429 | + */ |
|
430 | + public function patterns($patterns) |
|
431 | + { |
|
432 | + foreach ($patterns as $key => $pattern) { |
|
433 | + $this->patterns[$key] = $pattern; |
|
434 | + } |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * Get a Resource instance. |
|
439 | + * |
|
440 | + * @return \Syscodes\Routing\ResourceRegister |
|
441 | + */ |
|
442 | + public function getResource() |
|
443 | + { |
|
444 | + if (isset($this->resources)) { |
|
445 | + return $this->resources; |
|
446 | + } |
|
447 | + |
|
448 | + return $this->resources = new ResourceRegister($this); |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Dispatches the given url and call the method that belongs to the route. |
|
453 | + * |
|
454 | + * @param \Syscodes\Http\Request $request |
|
455 | + * |
|
456 | + * @return mixed |
|
457 | + */ |
|
458 | + public function dispatch(Request $request) |
|
459 | + { |
|
460 | + return $this->resolve($request, $this->routes); |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Check if a route with the given name exists. |
|
465 | + * |
|
466 | + * @param string $name |
|
467 | + * |
|
468 | + * @return bool |
|
469 | + */ |
|
470 | + public function has($name) |
|
471 | + { |
|
472 | + $names = is_array($name) ? $name : func_get_args(); |
|
473 | + |
|
474 | + foreach ($names as $value) { |
|
475 | + if ( ! $this->routes->hasNamedRoute($value)) { |
|
476 | + return false; |
|
477 | + } |
|
478 | + } |
|
479 | + |
|
480 | + return true; |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Get the currently dispatched route instance. |
|
485 | + * |
|
486 | + * @return \Syscodes\Routing\Route|null |
|
487 | + */ |
|
488 | + public function current() |
|
489 | + { |
|
490 | + return $this->current; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * Determine if the current route matches a pattern. |
|
495 | + * |
|
496 | + * @param mixed ...$patterns |
|
497 | + * |
|
498 | + * @return bool |
|
499 | + */ |
|
500 | + public function is(...$patterns) |
|
501 | + { |
|
502 | + return $this->currentRouteNamed(...$patterns); |
|
503 | + } |
|
504 | + |
|
505 | + /** |
|
506 | + * Determine if the current route matches a pattern. |
|
507 | + * |
|
508 | + * @param mixed ...$patterns |
|
509 | + * |
|
510 | + * @return bool |
|
511 | + */ |
|
512 | + public function currentRouteNamed(...$patterns) |
|
513 | + { |
|
514 | + return $this->current() && $this->current()->named(...$patterns); |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Register an array of resource controllers. |
|
519 | + * |
|
520 | + * @param array $resources |
|
521 | + * @param array $options |
|
522 | + * |
|
523 | + * @return void |
|
524 | + */ |
|
525 | + public function resources(array $resources, array $options = []) |
|
526 | + { |
|
527 | + foreach ($resources as $name => $controller) { |
|
528 | + $this->resource($name, $controller, $options); |
|
529 | + } |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * Route a resource to a controller. |
|
534 | + * |
|
535 | + * @param string $name |
|
536 | + * @param string $controller |
|
537 | + * @param array $options |
|
538 | + * |
|
539 | + * @return \Syscodes\Routing\AwaitingResourceRegistration |
|
540 | + */ |
|
541 | + public function resource($name, $controller, array $options = []) |
|
542 | + { |
|
543 | + if ($this->container) { |
|
544 | + $register = $this->container->make(ResourceRegister::class); |
|
545 | + } else { |
|
546 | + $register = new ResourceRegister($this); |
|
547 | + } |
|
548 | + |
|
549 | + return new AwaitingResourceRegistration( |
|
550 | + $register, $name, $controller, $options |
|
551 | + ); |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * Get the route collection. |
|
556 | + * |
|
557 | + * @return array |
|
558 | + */ |
|
559 | + public function getRoutes() |
|
560 | + { |
|
561 | + return $this->routes; |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * Get or set the verbs used in the resource URIs. |
|
566 | + * |
|
567 | + * @param array $verbs |
|
568 | + * |
|
569 | + * @return array|null |
|
570 | + */ |
|
571 | + public function resourceVerbs(array $verbs = []) |
|
572 | + { |
|
573 | + ResourceRegister::verbs($verbs); |
|
574 | + } |
|
575 | 575 | |
576 | - /** |
|
577 | - * Register a custom macro. |
|
578 | - * |
|
579 | - * @param string $name |
|
580 | - * @param callable $callback |
|
581 | - * |
|
582 | - * @return void |
|
583 | - */ |
|
584 | - public function macro($name, callable $callback) |
|
585 | - { |
|
586 | - $this->macros[$name] = $callback; |
|
587 | - } |
|
576 | + /** |
|
577 | + * Register a custom macro. |
|
578 | + * |
|
579 | + * @param string $name |
|
580 | + * @param callable $callback |
|
581 | + * |
|
582 | + * @return void |
|
583 | + */ |
|
584 | + public function macro($name, callable $callback) |
|
585 | + { |
|
586 | + $this->macros[$name] = $callback; |
|
587 | + } |
|
588 | 588 | |
589 | - /** |
|
590 | - * Checks if macro is registered. |
|
591 | - * |
|
592 | - * @param string $name |
|
593 | - * |
|
594 | - * @return boolean |
|
595 | - */ |
|
596 | - public function hasMacro($name) |
|
597 | - { |
|
598 | - return isset($this->macros[$name]); |
|
599 | - } |
|
589 | + /** |
|
590 | + * Checks if macro is registered. |
|
591 | + * |
|
592 | + * @param string $name |
|
593 | + * |
|
594 | + * @return boolean |
|
595 | + */ |
|
596 | + public function hasMacro($name) |
|
597 | + { |
|
598 | + return isset($this->macros[$name]); |
|
599 | + } |
|
600 | 600 | |
601 | - /** |
|
602 | - * Dynamically handle calls into the router instance. |
|
603 | - * |
|
604 | - * @param string $method |
|
605 | - * @param array $parameters |
|
606 | - * |
|
607 | - * @return mixed |
|
608 | - */ |
|
609 | - public function __call($method, $parameters) |
|
610 | - { |
|
611 | - if (isset($this->macros[$method])) { |
|
612 | - $callback = $this->macros[$method]; |
|
613 | - |
|
614 | - return call_user_func_array($callback, $parameters); |
|
615 | - } |
|
601 | + /** |
|
602 | + * Dynamically handle calls into the router instance. |
|
603 | + * |
|
604 | + * @param string $method |
|
605 | + * @param array $parameters |
|
606 | + * |
|
607 | + * @return mixed |
|
608 | + */ |
|
609 | + public function __call($method, $parameters) |
|
610 | + { |
|
611 | + if (isset($this->macros[$method])) { |
|
612 | + $callback = $this->macros[$method]; |
|
613 | + |
|
614 | + return call_user_func_array($callback, $parameters); |
|
615 | + } |
|
616 | 616 | |
617 | - return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
618 | - } |
|
617 | + return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
618 | + } |
|
619 | 619 | } |
620 | 620 | \ No newline at end of file |