Laravel-Backpack /
CRUD
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 | $uploadDefinition = is_array($uploadDefinition) ? $uploadDefinition : []; |
||
| 40 | /** @var CrudField|CrudColumn $this */ |
||
| 41 | RegisterUploadEvents::handle($this, $uploadDefinition, 'withFiles', $subfield, $registerUploaderEvents); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 42 | |||
| 43 | return $this; |
||
| 44 | }); |
||
| 45 | } |
||
| 46 | |||
| 47 | if (! CrudField::hasMacro('withFiles')) { |
||
| 48 | CrudField::macro('withFiles', function ($uploadDefinition = [], $subfield = null, $registerUploaderEvents = true) { |
||
| 49 | $uploadDefinition = is_array($uploadDefinition) ? $uploadDefinition : []; |
||
| 50 | /** @var CrudField|CrudColumn $this */ |
||
| 51 | RegisterUploadEvents::handle($this, $uploadDefinition, 'withFiles', $subfield, $registerUploaderEvents); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 52 | |||
| 53 | return $this; |
||
| 54 | }); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (! CrudColumn::hasMacro('linkTo')) { |
||
| 58 | CrudColumn::macro('linkTo', function (string|array|Closure $routeOrConfiguration, ?array $parameters = []): static { |
||
| 59 | $wrapper = $this->attributes['wrapper'] ?? []; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 60 | |||
| 61 | // parse the function input to get the actual route and parameters we'll be working with |
||
| 62 | if (is_array($routeOrConfiguration)) { |
||
| 63 | $route = $routeOrConfiguration['route'] ?? null; |
||
| 64 | $parameters = $routeOrConfiguration['parameters'] ?? []; |
||
| 65 | } else { |
||
| 66 | $route = $routeOrConfiguration; |
||
| 67 | } |
||
| 68 | |||
| 69 | // if the route is a closure, we'll just call it |
||
| 70 | if ($route instanceof Closure) { |
||
| 71 | $wrapper['href'] = function ($crud, $column, $entry, $related_key) use ($route) { |
||
| 72 | return $route($entry, $related_key, $column, $crud); |
||
| 73 | }; |
||
| 74 | $this->wrapper($wrapper); |
||
| 75 | |||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | // if the route doesn't exist, we'll throw an exception |
||
| 80 | if (! $routeInstance = Route::getRoutes()->getByName($route)) { |
||
| 81 | throw new \Exception("Route [{$route}] not found while building the link for column [{$this->attributes['name']}]."); |
||
| 82 | } |
||
| 83 | |||
| 84 | // calculate the parameters we'll be using for the route() call |
||
| 85 | // (eg. if there's only one parameter and user didn't provide it, we'll assume it's the entry's related key) |
||
| 86 | $parameters = (function () use ($parameters, $routeInstance, $route) { |
||
| 87 | $expectedParameters = $routeInstance->parameterNames(); |
||
| 88 | |||
| 89 | if (count($expectedParameters) === 0) { |
||
| 90 | return $parameters; |
||
| 91 | } |
||
| 92 | |||
| 93 | $autoInferredParameter = array_diff($expectedParameters, array_keys($parameters)); |
||
| 94 | if (count($autoInferredParameter) > 1) { |
||
| 95 | 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
|
|||
| 96 | } |
||
| 97 | $autoInferredParameter = current($autoInferredParameter) ? [current($autoInferredParameter) => function ($entry, $related_key, $column, $crud) { |
||
| 98 | $entity = $crud->isAttributeInRelationString($column) ? Str::before($column['entity'], '.') : $column['entity']; |
||
| 99 | |||
| 100 | return $related_key ?? $entry->{$entity}?->getKey(); |
||
| 101 | }] : []; |
||
| 102 | |||
| 103 | return array_merge($autoInferredParameter, $parameters); |
||
| 104 | })(); |
||
| 105 | |||
| 106 | // set up the wrapper href attribute |
||
| 107 | $wrapper['href'] = function ($crud, $column, $entry, $related_key) use ($route, $parameters) { |
||
| 108 | // if the parameter is callable, we'll call it |
||
| 109 | $parameters = collect($parameters)->map(fn ($item) => is_callable($item) ? $item($entry, $related_key, $column, $crud) : $item)->toArray(); |
||
| 110 | |||
| 111 | try { |
||
| 112 | return route($route, $parameters); |
||
| 113 | } catch (\Exception $e) { |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | }; |
||
| 117 | |||
| 118 | $this->wrapper($wrapper); |
||
| 119 | |||
| 120 | return $this; |
||
| 121 | }); |
||
| 122 | } |
||
| 123 | |||
| 124 | if (! CrudColumn::hasMacro('linkToShow')) { |
||
| 125 | CrudColumn::macro('linkToShow', function (): static { |
||
| 126 | $name = $this->attributes['name']; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 127 | $entity = $this->attributes['entity'] ?? null; |
||
| 128 | $route = "$entity.show"; |
||
| 129 | |||
| 130 | if (! $entity) { |
||
| 131 | throw new \Exception("Entity not found while building the link for column [{$name}]."); |
||
| 132 | } |
||
| 133 | |||
| 134 | if (! Route::getRoutes()->getByName($route)) { |
||
| 135 | throw new \Exception("Route '{$route}' not found while building the link for column [{$name}]."); |
||
| 136 | } |
||
| 137 | |||
| 138 | // set up the link to the show page |
||
| 139 | $this->linkTo($route); |
||
| 140 | |||
| 141 | return $this; |
||
| 142 | }); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (! CrudColumn::hasMacro('linkTarget')) { |
||
| 146 | CrudColumn::macro('linkTarget', function (string $target = '_self'): static { |
||
| 147 | $this->wrapper([ |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 148 | ...$this->attributes['wrapper'] ?? [], |
||
| 149 | 'target' => $target, |
||
| 150 | ]); |
||
| 151 | |||
| 152 | return $this; |
||
| 153 | }); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The route macro allows developers to generate the routes for a CrudController, |
||
| 158 | * for all operations, using a simple syntax: Route::crud(). |
||
| 159 | * |
||
| 160 | * It will go to the given CrudController and get the setupRoutes() method on it. |
||
| 161 | */ |
||
| 162 | if (! Route::hasMacro('crud')) { |
||
| 163 | Route::macro('crud', function ($name, $controller) { |
||
| 164 | // put together the route name prefix, |
||
| 165 | // as passed to the Route::group() statements |
||
| 166 | $routeName = ''; |
||
| 167 | if ($this->hasGroupStack()) { |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 168 | foreach ($this->getGroupStack() as $key => $groupStack) { |
||
| 169 | if (isset($groupStack['name'])) { |
||
| 170 | if (is_array($groupStack['name'])) { |
||
| 171 | $routeName = implode('', $groupStack['name']); |
||
| 172 | } else { |
||
| 173 | $routeName = $groupStack['name']; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | // add the name of the current entity to the route name prefix |
||
| 179 | // the result will be the current route name (not ending in dot) |
||
| 180 | $routeName .= $name; |
||
| 181 | |||
| 182 | // get an instance of the controller |
||
| 183 | if ($this->hasGroupStack()) { |
||
| 184 | $groupStack = $this->getGroupStack(); |
||
| 185 | $groupNamespace = $groupStack && isset(end($groupStack)['namespace']) ? end($groupStack)['namespace'].'\\' : ''; |
||
| 186 | } else { |
||
| 187 | $groupNamespace = ''; |
||
| 188 | } |
||
| 189 | |||
| 190 | \Backpack\CRUD\app\Library\CrudPanel\CrudRouter::setupControllerRoutes($name, $routeName, $controller, $groupNamespace); |
||
| 191 | }); |
||
| 192 | } |
||
| 193 |