1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Providers; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
6
|
|
|
use Doctrine\Common\Annotations\SimpleAnnotationReader; |
7
|
|
|
use Illuminate\Config\Repository as Config; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Terranet\Administrator\Collection\Mutable; |
10
|
|
|
use Terranet\Administrator\Contracts\Module; |
11
|
|
|
use Terranet\Administrator\Contracts\Module\Filtrable; |
12
|
|
|
use Terranet\Administrator\Contracts\Module\Sortable; |
13
|
|
|
use Terranet\Administrator\Contracts\Services\Finder; |
14
|
|
|
use Terranet\Administrator\Contracts\Services\TemplateProvider; |
15
|
|
|
use Terranet\Administrator\Dashboard\Manager; |
16
|
|
|
use Terranet\Administrator\Exception; |
17
|
|
|
use Terranet\Administrator\Filter; |
18
|
|
|
use Terranet\Administrator\Schema; |
19
|
|
|
use Terranet\Administrator\Services\Sorter; |
20
|
|
|
use Terranet\Administrator\Services\Template; |
21
|
|
|
use Terranet\Localizer\Locale; |
22
|
|
|
|
23
|
|
|
class ContainersServiceProvider extends ServiceProvider |
24
|
|
|
{ |
25
|
|
|
protected $containers = [ |
26
|
|
|
'AdminConfig' => 'scaffold.config', |
27
|
|
|
'AdminResource' => 'scaffold.module', |
28
|
|
|
'AdminModel' => 'scaffold.model', |
29
|
|
|
'AdminWidgets' => 'scaffold.widgets', |
30
|
|
|
'AdminSchema' => 'scaffold.schema', |
31
|
|
|
'AdminSortable' => 'scaffold.sortable', |
32
|
|
|
'AdminFilter' => 'scaffold.filter', |
33
|
|
|
'AdminColumns' => 'scaffold.columns', |
34
|
|
|
'AdminActions' => 'scaffold.actions', |
35
|
|
|
'AdminTemplate' => 'scaffold.template', |
36
|
|
|
'AdminForm' => 'scaffold.form', |
37
|
|
|
'AdminFinder' => 'scaffold.finder', |
38
|
|
|
'AdminBreadcrumbs' => 'scaffold.breadcrumbs', |
39
|
|
|
'AdminTranslations' => 'scaffold.translations', |
40
|
|
|
'AdminAnnotations' => 'scaffold.annotations', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
public function register() |
44
|
|
|
{ |
45
|
|
|
foreach (array_keys($this->containers) as $container) { |
46
|
|
|
$method = "register{$container}"; |
47
|
|
|
|
48
|
|
|
\call_user_func_array([$this, $method], []); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->app->bind(Module::class, function ($app) { |
52
|
|
|
return $app['scaffold.module']; |
53
|
|
|
}); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function registerAdminAnnotations() |
57
|
|
|
{ |
58
|
|
|
$this->app->singleton('scaffold.annotations', function () { |
59
|
|
|
AnnotationRegistry::registerUniqueLoader('class_exists'); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$reader = new SimpleAnnotationReader(); |
62
|
|
|
$reader->addNamespace("\\Terranet\\Administrator\\Annotations"); |
63
|
|
|
|
64
|
|
|
return $reader; |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function registerAdminConfig() |
69
|
|
|
{ |
70
|
|
|
$this->app->singleton('scaffold.config', function ($app) { |
71
|
|
|
$config = $app['config']['administrator']; |
72
|
|
|
|
73
|
|
|
return new Config((array) $config); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function registerAdminTranslations() |
78
|
|
|
{ |
79
|
|
|
// Draft: Mui configuration |
80
|
|
|
// Goal: sometimes there is a case when few content managers (admins) override the same translatable content (files, db, etc...) |
81
|
|
|
// This service allows to make some locales readonly: |
82
|
|
|
// 1. they are available in UI in order to preserve the context |
83
|
|
|
// 2. they are protected from saving process |
84
|
|
|
// Making locale(s) Readonly remains for Dev's side: the recommended way - use a custom Middleware. |
85
|
|
|
// ex.: app('scaffold.translations')->setReadonly([1, 2, 3]) |
86
|
|
|
$this->app->singleton('scaffold.translations', function ($app) { |
87
|
|
|
$service = new class() |
88
|
|
|
{ |
89
|
|
|
protected $readonly = []; |
90
|
|
|
|
91
|
|
|
public function __construct() |
92
|
|
|
{ |
93
|
|
|
$this->setReadonly(config('administrator.translations.readonly', [])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set ReadOnly locales. |
98
|
|
|
* |
99
|
|
|
* @param array $readonly |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
|
|
public function setReadonly(array $readonly = []): self |
104
|
|
|
{ |
105
|
|
|
$this->readonly = (array) $readonly; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if a Locale is ReadOnly. |
112
|
|
|
* |
113
|
|
|
* @param $locale |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function readonly($locale) |
118
|
|
|
{ |
119
|
|
|
if ($locale instanceof Locale) { |
120
|
|
|
$locale = $locale->id(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return \in_array((int) $locale, $this->readonly, true); |
124
|
|
|
} |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
return $service; |
128
|
|
|
}); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function registerAdminResource() |
132
|
|
|
{ |
133
|
|
|
$this->app->singleton('scaffold.module', function ($app) { |
134
|
|
|
if (\in_array($app['router']->currentRouteName(), ['scaffold.settings.edit', 'scaffold.settings.update'], true)) { |
135
|
|
|
return $app['scaffold.module.settings']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (($router = $app['router']->current()) && |
139
|
|
|
($module = $router->parameter('module')) && |
140
|
|
|
array_has($app, $key = "scaffold.module.{$module}") |
141
|
|
|
) { |
142
|
|
|
return array_get($app, $key); |
143
|
|
|
} |
144
|
|
|
}); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function registerAdminModel() |
148
|
|
|
{ |
149
|
|
|
$this->app->singleton('scaffold.model', function ($app) { |
150
|
|
|
if (($finder = app('scaffold.finder')) |
151
|
|
|
&& ($id = $app['router']->current()->parameter('id')) |
152
|
|
|
) { |
153
|
|
|
return $finder->find($id); |
154
|
|
|
} |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function registerAdminWidgets() |
159
|
|
|
{ |
160
|
|
|
$this->app->singleton('scaffold.widgets', function () { |
161
|
|
|
if (($module = app('scaffold.module'))) { |
162
|
|
|
return $module->widgets(new Manager()); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return new Manager(); |
166
|
|
|
}); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function registerAdminSchema() |
170
|
|
|
{ |
171
|
|
|
$this->app->singleton('scaffold.schema', function ($app) { |
172
|
|
|
if ($schema = $app['db']->connection()->getDoctrineSchemaManager()) { |
173
|
|
|
// fix dbal missing types |
174
|
|
|
$platform = $schema->getDatabasePlatform(); |
175
|
|
|
$platform->registerDoctrineTypeMapping('enum', 'string'); |
176
|
|
|
$platform->registerDoctrineTypeMapping('set', 'string'); |
177
|
|
|
|
178
|
|
|
return new Schema($schema); |
179
|
|
|
} |
180
|
|
|
}); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
protected function registerAdminSortable() |
184
|
|
|
{ |
185
|
|
|
$this->app->singleton('scaffold.sortable', function ($app) { |
186
|
|
|
if ($module = $app['scaffold.module']) { |
187
|
|
|
return new Sorter( |
188
|
|
|
$module instanceof Sortable ? $module->sortable() : [], |
189
|
|
|
method_exists($module, 'sortDirection') ? $module->sortDirection() : 'desc' |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
}); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function registerAdminColumns() |
196
|
|
|
{ |
197
|
|
|
$this->app->singleton('scaffold.columns', function ($app) { |
198
|
|
|
if ($module = $app['scaffold.module']) { |
199
|
|
|
return $module->columns(); |
200
|
|
|
} |
201
|
|
|
}); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function registerAdminActions() |
205
|
|
|
{ |
206
|
|
|
$this->app->singleton('scaffold.actions', function ($app) { |
207
|
|
|
if ($module = $app['scaffold.module']) { |
208
|
|
|
return $module->actionsManager(); |
209
|
|
|
} |
210
|
|
|
}); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function registerAdminTemplate() |
214
|
|
|
{ |
215
|
|
|
$this->app->singleton('scaffold.template', function ($app) { |
216
|
|
|
// check for resource template |
217
|
|
|
$handler = ($module = $app['scaffold.module']) ? $module->template() : Template::class; |
218
|
|
|
$handler = new $handler(); |
219
|
|
|
|
220
|
|
|
if (!$handler instanceof TemplateProvider) { |
221
|
|
|
throw new Exception('Templates handler must implement '.TemplateProvider::class.' contract'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $handler; |
225
|
|
|
}); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
protected function registerAdminForm() |
229
|
|
|
{ |
230
|
|
|
$this->app->singleton('scaffold.form', function ($app) { |
231
|
|
|
if ($module = $app['scaffold.module']) { |
232
|
|
|
return $module->form(); |
233
|
|
|
} |
234
|
|
|
}); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
protected function registerAdminFilter() |
238
|
|
|
{ |
239
|
|
|
$this->app->singleton('scaffold.filter', function ($app) { |
240
|
|
|
if ($module = $app['scaffold.module']) { |
241
|
|
|
$filters = $module instanceof Filtrable ? $module->filters() : null; |
242
|
|
|
$scopes = $module instanceof Filtrable ? $module->scopes() : null; |
243
|
|
|
|
244
|
|
|
return new Filter($app['request'], $filters, $scopes); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
}); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
protected function registerAdminFinder() |
250
|
|
|
{ |
251
|
|
|
$this->app->singleton('scaffold.finder', function ($app) { |
252
|
|
|
if ($module = $app['scaffold.module']) { |
253
|
|
|
// in order to register sortable columns, |
254
|
|
|
// resolve columns service before finder. |
255
|
|
|
$app->make('scaffold.columns'); |
256
|
|
|
|
257
|
|
|
$finder = $module->finder(); |
258
|
|
|
$finder = new $finder($module); |
259
|
|
|
|
260
|
|
|
if (!$finder instanceof Finder) { |
261
|
|
|
throw new Exception('Items Finder must implement '.Finder::class.' contract'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $finder; |
265
|
|
|
} |
266
|
|
|
}); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
protected function registerAdminBreadcrumbs() |
270
|
|
|
{ |
271
|
|
|
$this->app->singleton('scaffold.breadcrumbs', function ($app) { |
272
|
|
|
if ($module = $app['scaffold.module']) { |
273
|
|
|
$provider = $module->breadcrumbs(); |
274
|
|
|
|
275
|
|
|
return new $provider($app->make('breadcrumbs'), $app->make('scaffold.module')); |
276
|
|
|
} |
277
|
|
|
}); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.