1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
|
8
|
|
|
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer; |
9
|
|
|
|
10
|
|
|
class MetadataControllerProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Bootstrap the application services. Post-boot. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
67 |
|
public function boot() |
18
|
|
|
{ |
19
|
67 |
|
$isCaching = env('APP_METADATA_CACHING', false); |
20
|
|
|
|
21
|
67 |
|
if ($isCaching && Cache::has('metadataControllers')) { |
22
|
|
|
$meta = Cache::get('metadataControllers'); |
23
|
|
|
$this->app->instance('metadataControllers', $meta); |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
67 |
|
$meta = $this->app->make('metadataControllers'); |
28
|
|
|
|
29
|
67 |
|
$classes = get_declared_classes(); |
30
|
67 |
|
$AutoClass = null; |
31
|
67 |
|
foreach ($classes as $class) { |
32
|
67 |
|
if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) { |
33
|
67 |
|
$AutoClass = $class; |
34
|
67 |
|
} |
35
|
67 |
|
} |
36
|
|
|
|
37
|
67 |
|
$metamix = []; |
38
|
67 |
|
$ends = array(); |
39
|
67 |
|
$Classes = $AutoClass::$classMap; |
40
|
67 |
View Code Duplication |
foreach ($Classes as $name => $file) { |
|
|
|
|
41
|
67 |
|
if (\Illuminate\Support\Str::startsWith($name, "App")) { |
42
|
|
|
if (in_array("AlgoWeb\\PODataLaravel\\Controllers\\MetadataControllerTrait", class_uses($name))) { |
43
|
|
|
$ends[] = $name; |
44
|
|
|
} |
45
|
|
|
} |
46
|
67 |
|
} |
47
|
|
|
|
48
|
|
|
// now process each class that uses the metadata controller trait and stick results in $metamix |
49
|
67 |
|
$map = null; |
|
|
|
|
50
|
67 |
|
foreach ($ends as $end) { |
51
|
|
|
$map = $end->getMappings(); |
52
|
|
|
// verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for |
53
|
|
|
// a model can glom onto different controllers |
54
|
|
|
foreach ($map as $key => $lock) { |
55
|
|
|
if (!array_key_exists($key, $metamix)) { |
56
|
|
|
// if we haven't yet got a mapping for this model, grab it holus-bolus |
57
|
|
|
$metamix[$key] = $lock; |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
// if we do, make sure we aren't re-adding mappings for any of the CRUD verbs |
61
|
|
|
foreach ($lock as $barrel => $roll) { |
62
|
|
|
assert( |
63
|
|
|
!array_key_exists($barrel, $metamix[$key]), |
64
|
|
|
'Mapping already defined for model '.$key. ' and CRUD verb '.$barrel |
65
|
|
|
); |
66
|
|
|
$metamix[$key][$barrel] = $roll; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
67 |
|
} |
71
|
|
|
|
72
|
67 |
|
$meta->setMetadata($metamix); |
73
|
|
|
|
74
|
67 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Register the application services. Boot-time only. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function register() |
82
|
|
|
{ |
83
|
67 |
|
$this->app->singleton('metadataControllers', function ($app) { |
|
|
|
|
84
|
67 |
|
return new MetadataControllerContainer(); |
85
|
67 |
|
}); |
86
|
67 |
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.