We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
2 | |||
3 | use Backpack\CRUD\app\Http\Controllers\CrudController; |
||
4 | use Backpack\CRUD\app\Library\CrudPanel\CrudColumn; |
||
5 | use Backpack\CRUD\app\Library\CrudPanel\CrudField; |
||
6 | use Backpack\CRUD\app\Library\Uploaders\Support\RegisterUploadEvents; |
||
7 | use Illuminate\Support\Facades\Route; |
||
8 | use Illuminate\Support\Str; |
||
9 | |||
10 | /** |
||
11 | * This macro adds the ability to convert a dot.notation string into a [bracket][notation] with some special |
||
12 | * options that helps us in our usecases. |
||
13 | * |
||
14 | * - $ignore: useful when you want to convert a laravel validator rule for nested items and you |
||
15 | * would like to ignore the `*` element from the string. |
||
16 | * |
||
17 | * - $keyFirst: when true, we will use the first part of the string as key and only bracket the remaining elements. |
||
18 | * eg: `address.street` |
||
19 | * - when true: `address[street]` |
||
20 | * - when false: `[address][street]` |
||
21 | */ |
||
22 | if (! Str::hasMacro('dotsToSquareBrackets')) { |
||
23 | Str::macro('dotsToSquareBrackets', function ($string, $ignore = [], $keyFirst = true) { |
||
24 | $stringParts = explode('.', $string); |
||
25 | $result = ''; |
||
26 | |||
27 | foreach ($stringParts as $key => $part) { |
||
28 | if (in_array($part, $ignore)) { |
||
29 | continue; |
||
30 | } |
||
31 | $result .= ($key === 0 && $keyFirst) ? $part : '['.$part.']'; |
||
32 | } |
||
33 | |||
34 | return $result; |
||
35 | }); |
||
36 | } |
||
37 | if (! CrudColumn::hasMacro('withFiles')) { |
||
38 | CrudColumn::macro('withFiles', function ($uploadDefinition = [], $subfield = null, $registerUploaderEvents = true) { |
||
39 | /** @var CrudColumn $this */ |
||
40 | $uploadDefinition = is_array($uploadDefinition) ? $uploadDefinition : []; |
||
41 | $this->setAttributeValue('withFiles', $uploadDefinition); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
42 | $this->save(); |
||
43 | RegisterUploadEvents::handle($this, $uploadDefinition, 'withFiles', $subfield, $registerUploaderEvents); |
||
44 | |||
45 | return $this; |
||
46 | }); |
||
47 | } |
||
48 | |||
49 | if (! CrudField::hasMacro('withFiles')) { |
||
50 | CrudField::macro('withFiles', function ($uploadDefinition = [], $subfield = null, $registerUploaderEvents = true) { |
||
51 | /** @var CrudField $this */ |
||
52 | $uploadDefinition = is_array($uploadDefinition) ? $uploadDefinition : []; |
||
53 | $this->setAttributeValue('withFiles', $uploadDefinition); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
54 | $this->save(); |
||
55 | RegisterUploadEvents::handle($this, $uploadDefinition, 'withFiles', $subfield, $registerUploaderEvents); |
||
56 | |||
57 | return $this; |
||
58 | }); |
||
59 | } |
||
60 | |||
61 | if (! CrudColumn::hasMacro('linkTo')) { |
||
62 | CrudColumn::macro('linkTo', function (string|array|Closure $routeOrConfiguration, ?array $parameters = []): static { |
||
63 | $wrapper = $this->attributes['wrapper'] ?? []; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
64 | |||
65 | // parse the function input to get the actual route and parameters we'll be working with |
||
66 | if (is_array($routeOrConfiguration)) { |
||
67 | $route = $routeOrConfiguration['route'] ?? null; |
||
68 | $parameters = $routeOrConfiguration['parameters'] ?? []; |
||
69 | } else { |
||
70 | $route = $routeOrConfiguration; |
||
71 | } |
||
72 | |||
73 | // if the route is a closure, we'll just call it |
||
74 | if ($route instanceof Closure) { |
||
75 | $wrapper['href'] = function ($crud, $column, $entry, $related_key) use ($route) { |
||
76 | return $route($entry, $related_key, $column, $crud); |
||
77 | }; |
||
78 | $this->wrapper($wrapper); |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | // if the route doesn't exist, we'll throw an exception |
||
84 | if (! $routeInstance = Route::getRoutes()->getByName($route)) { |
||
85 | throw new Exception("Route [{$route}] not found while building the link for column [{$this->attributes['name']}]."); |
||
86 | } |
||
87 | |||
88 | // calculate the parameters we'll be using for the route() call |
||
89 | // (eg. if there's only one parameter and user didn't provide it, we'll assume it's the entry's related key) |
||
90 | $parameters = (function () use ($parameters, $routeInstance, $route) { |
||
91 | $expectedParameters = $routeInstance->parameterNames(); |
||
92 | |||
93 | if (count($expectedParameters) === 0) { |
||
94 | return $parameters; |
||
95 | } |
||
96 | |||
97 | $autoInferredParameter = array_diff($expectedParameters, array_keys($parameters)); |
||
98 | if (count($autoInferredParameter) > 1) { |
||
99 | throw new Exception("Route [{$route}] expects parameters [".implode(', ', $expectedParameters)."]. Insufficient parameters provided in column: [{$this->attributes['name']}]."); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
100 | } |
||
101 | $autoInferredParameter = current($autoInferredParameter) ? [current($autoInferredParameter) => function ($entry, $related_key, $column, $crud) { |
||
102 | $entity = $crud->isAttributeInRelationString($column) ? Str::before($column['entity'], '.') : $column['entity']; |
||
103 | |||
104 | return $related_key ?? $entry->{$entity}?->getKey(); |
||
105 | }] : []; |
||
106 | |||
107 | return array_merge($autoInferredParameter, $parameters); |
||
108 | })(); |
||
109 | |||
110 | // set up the wrapper href attribute |
||
111 | $wrapper['href'] = function ($crud, $column, $entry, $related_key) use ($route, $parameters) { |
||
112 | // if the parameter is callable, we'll call it |
||
113 | $parameters = collect($parameters)->map(fn ($item) => is_callable($item) ? $item($entry, $related_key, $column, $crud) : $item)->toArray(); |
||
114 | |||
115 | try { |
||
116 | return route($route, $parameters); |
||
117 | } catch (Exception $e) { |
||
118 | return false; |
||
119 | } |
||
120 | }; |
||
121 | |||
122 | $this->wrapper($wrapper); |
||
123 | |||
124 | return $this; |
||
125 | }); |
||
126 | } |
||
127 | |||
128 | if (! CrudColumn::hasMacro('linkToShow')) { |
||
129 | CrudColumn::macro('linkToShow', function (): static { |
||
130 | $name = $this->attributes['name']; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
131 | $entity = $this->attributes['entity'] ?? null; |
||
132 | $route = "$entity.show"; |
||
133 | |||
134 | if (! $entity) { |
||
135 | throw new Exception("Entity not found while building the link for column [{$name}]."); |
||
136 | } |
||
137 | |||
138 | if (! Route::getRoutes()->getByName($route)) { |
||
139 | throw new Exception("Route '{$route}' not found while building the link for column [{$name}]."); |
||
140 | } |
||
141 | |||
142 | // set up the link to the show page |
||
143 | $this->linkTo($route); |
||
144 | |||
145 | return $this; |
||
146 | }); |
||
147 | } |
||
148 | |||
149 | if (! CrudColumn::hasMacro('linkTarget')) { |
||
150 | CrudColumn::macro('linkTarget', function (string $target = '_self'): static { |
||
151 | $this->wrapper([ |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
152 | ...$this->attributes['wrapper'] ?? [], |
||
153 | 'target' => $target, |
||
154 | ]); |
||
155 | |||
156 | return $this; |
||
157 | }); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * The route macro allows developers to generate the routes for a CrudController, |
||
162 | * for all operations, using a simple syntax: Route::crud(). |
||
163 | * |
||
164 | * It will go to the given CrudController and get the setupRoutes() method on it. |
||
165 | */ |
||
166 | if (! Route::hasMacro('crud')) { |
||
167 | Route::macro('crud', function ($name, $controller) { |
||
168 | // put together the route name prefix, |
||
169 | // as passed to the Route::group() statements |
||
170 | $routeName = ''; |
||
171 | if ($this->hasGroupStack()) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
172 | foreach ($this->getGroupStack() as $key => $groupStack) { |
||
173 | if (isset($groupStack['name'])) { |
||
174 | if (is_array($groupStack['name'])) { |
||
175 | $routeName = implode('', $groupStack['name']); |
||
176 | } else { |
||
177 | $routeName = $groupStack['name']; |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | // add the name of the current entity to the route name prefix |
||
183 | // the result will be the current route name (not ending in dot) |
||
184 | $routeName .= $name; |
||
185 | |||
186 | // get an instance of the controller |
||
187 | if ($this->hasGroupStack()) { |
||
188 | $groupStack = $this->getGroupStack(); |
||
189 | $groupNamespace = $groupStack && isset(end($groupStack)['namespace']) ? end($groupStack)['namespace'].'\\' : ''; |
||
190 | } else { |
||
191 | $groupNamespace = ''; |
||
192 | } |
||
193 | |||
194 | Backpack\CRUD\app\Library\CrudPanel\CrudRouter::setupControllerRoutes($name, $routeName, $controller, $groupNamespace); |
||
195 | }); |
||
196 | } |
||
197 |