1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Routing\Route;
16:
17: use Cake\Utility\Inflector;
18:
19: /**
20: * This route class will transparently inflect the controller and plugin routing
21: * parameters, so that requesting `/my_controller` is parsed as `['controller' => 'MyController']`
22: */
23: class InflectedRoute extends Route
24: {
25:
26: /**
27: * Flag for tracking whether or not the defaults have been inflected.
28: *
29: * Default values need to be inflected so that they match the inflections that match()
30: * will create.
31: *
32: * @var bool
33: */
34: protected $_inflectedDefaults = false;
35:
36: /**
37: * Parses a string URL into an array. If it matches, it will convert the prefix, controller and
38: * plugin keys to their camelized form.
39: *
40: * @param string $url The URL to parse
41: * @param string $method The HTTP method being matched.
42: * @return array|false An array of request parameters, or false on failure.
43: */
44: public function parse($url, $method = '')
45: {
46: $params = parent::parse($url, $method);
47: if (!$params) {
48: return false;
49: }
50: if (!empty($params['controller'])) {
51: $params['controller'] = Inflector::camelize($params['controller']);
52: }
53: if (!empty($params['plugin'])) {
54: if (strpos($params['plugin'], '/') === false) {
55: $params['plugin'] = Inflector::camelize($params['plugin']);
56: } else {
57: list($vendor, $plugin) = explode('/', $params['plugin'], 2);
58: $params['plugin'] = Inflector::camelize($vendor) . '/' . Inflector::camelize($plugin);
59: }
60: }
61:
62: return $params;
63: }
64:
65: /**
66: * Underscores the prefix, controller and plugin params before passing them on to the
67: * parent class
68: *
69: * @param array $url Array of parameters to convert to a string.
70: * @param array $context An array of the current request context.
71: * Contains information such as the current host, scheme, port, and base
72: * directory.
73: * @return string|false Either a string URL for the parameters if they match or false.
74: */
75: public function match(array $url, array $context = [])
76: {
77: $url = $this->_underscore($url);
78: if (!$this->_inflectedDefaults) {
79: $this->_inflectedDefaults = true;
80: $this->defaults = $this->_underscore($this->defaults);
81: }
82:
83: return parent::match($url, $context);
84: }
85:
86: /**
87: * Helper method for underscoring keys in a URL array.
88: *
89: * @param array $url An array of URL keys.
90: * @return array
91: */
92: protected function _underscore($url)
93: {
94: if (!empty($url['controller'])) {
95: $url['controller'] = Inflector::underscore($url['controller']);
96: }
97: if (!empty($url['plugin'])) {
98: $url['plugin'] = Inflector::underscore($url['plugin']);
99: }
100:
101: return $url;
102: }
103: }
104: