|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Utility; |
|
13
|
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\Action; |
|
15
|
|
|
use Cake\Core\Configure; |
|
16
|
|
|
use Cake\Routing\Router; |
|
17
|
|
|
use Cake\Utility\Inflector; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ReverseRouting |
|
21
|
|
|
* |
|
22
|
|
|
* @package CakeDC\Api\Service\Response |
|
23
|
|
|
*/ |
|
24
|
|
|
class ReverseRouting |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Builds link to action. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $name Link name |
|
31
|
|
|
* @param string $path Link path. |
|
32
|
|
|
* @param string $method Action method. |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
13 |
|
public function link($name, $path, $method = 'GET') |
|
36
|
|
|
{ |
|
37
|
13 |
|
$prefix = Configure::read('Api.routeBase') ?: '/api'; |
|
38
|
13 |
|
$baseRoute = $prefix . $path; |
|
39
|
13 |
|
$fullRoute = Router::url($baseRoute, true); |
|
40
|
|
|
|
|
41
|
|
|
return [ |
|
42
|
13 |
|
'name' => $name, |
|
43
|
13 |
|
'href' => $fullRoute, |
|
44
|
13 |
|
'rel' => $baseRoute, |
|
45
|
13 |
|
'method' => $method, |
|
46
|
13 |
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Builds path to the index action. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Action $action An Action instance. |
|
53
|
|
|
* @param callable $beforeReverse Callback. |
|
54
|
|
|
* @return array|string |
|
55
|
|
|
*/ |
|
56
|
11 |
|
public function indexPath(Action $action, $beforeReverse = null) |
|
57
|
|
|
{ |
|
58
|
11 |
|
$indexRoute = $action->getRoute(); |
|
59
|
11 |
|
$parent = $action->getService()->getParentService(); |
|
60
|
11 |
|
$path = null; |
|
61
|
11 |
|
if ($parent !== null) { |
|
62
|
1 |
|
$parentRoutes = $parent->routes(); |
|
63
|
1 |
|
$currentRoute = $this->findRoute($indexRoute, $parentRoutes); |
|
|
|
|
|
|
64
|
1 |
|
if ($currentRoute !== null) { |
|
65
|
1 |
|
if (is_callable($beforeReverse)) { |
|
66
|
|
|
$indexRoute = $beforeReverse($indexRoute); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return $parent->routeReverse($indexRoute); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $path; |
|
73
|
|
|
} else { |
|
74
|
10 |
|
if (is_callable($beforeReverse)) { |
|
75
|
8 |
|
$indexRoute = $beforeReverse($indexRoute); |
|
76
|
8 |
|
} |
|
77
|
|
|
|
|
78
|
10 |
|
return $action->getService()->routeReverse($indexRoute); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Builds path to the parent view action. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $parentName Action name. |
|
86
|
|
|
* @param Action $action An Action instance. |
|
87
|
|
|
* @param string $type Type of action. |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function parentViewPath($parentName, $action, $type) |
|
91
|
|
|
{ |
|
92
|
2 |
|
$baseRoute = $action->getRoute(); |
|
93
|
2 |
|
$parent = $action->getService()->getParentService(); |
|
94
|
2 |
|
$parentId = Inflector::singularize(Inflector::underscore($parent->getName())) . '_id'; |
|
95
|
2 |
|
$route = collection($parent->routes()) |
|
96
|
2 |
|
->filter(function ($item) use ($parentName) { |
|
97
|
2 |
|
return $item->getName() == $parentName; |
|
98
|
2 |
|
}) |
|
99
|
2 |
|
->first(); |
|
100
|
2 |
|
$routeDefault = $route->defaults; |
|
101
|
2 |
|
if (array_key_exists($parentId, $baseRoute)) { |
|
102
|
2 |
|
if ($type == 'view') { |
|
103
|
2 |
|
$routeDefault['pass']['id'] = $baseRoute[$parentId]; |
|
104
|
2 |
|
} |
|
105
|
2 |
|
if ($type == 'index') { |
|
106
|
1 |
|
$routeDefault[$parentId] = $baseRoute[$parentId]; |
|
107
|
1 |
|
} |
|
108
|
2 |
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
return $parent->routeReverse($routeDefault); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Extract matching route from routes list. |
|
115
|
|
|
* |
|
116
|
|
|
* @param array $route Route array. |
|
117
|
|
|
* @param array $routes List of all routes. |
|
118
|
|
|
* @return null |
|
119
|
|
|
*/ |
|
120
|
2 |
|
public function findRoute($route, $routes) |
|
121
|
|
|
{ |
|
122
|
2 |
|
foreach ($routes as $item) { |
|
123
|
2 |
|
if ($this->compareDefaults($item->defaults, $route)) { |
|
124
|
2 |
|
return $item; |
|
125
|
|
|
} |
|
126
|
2 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
return null; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Compares two routes. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $route1 First route description instance. |
|
135
|
|
|
* @param array $route2 Second route description instance. |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function compareDefaults($route1, $route2) |
|
139
|
|
|
{ |
|
140
|
2 |
|
$result = true; |
|
141
|
2 |
|
$fields = ['controller', 'action', 'plugin']; |
|
142
|
2 |
|
foreach ($fields as $field) { |
|
143
|
2 |
|
$result = $result && $route1[$field] === $route2[$field]; |
|
144
|
2 |
|
} |
|
145
|
2 |
|
$result = $result && ( |
|
146
|
2 |
|
is_string($route1['_method']) && $route1['_method'] === $route2['_method'] || |
|
147
|
|
|
is_array($route1['_method']) && in_array($route2['_method'], $route1['_method']) |
|
148
|
2 |
|
); |
|
149
|
|
|
|
|
150
|
2 |
|
return $result; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.