|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerTrait; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer; |
|
9
|
|
|
use Illuminate\Support\Facades\App; |
|
10
|
|
|
|
|
11
|
|
|
class MetadataControllerProvider extends ServiceProvider |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Bootstrap the application services. Post-boot. |
|
15
|
|
|
* |
|
16
|
68 |
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
68 |
|
public function boot() |
|
19
|
|
|
{ |
|
20
|
68 |
|
$isCaching = env('APP_METADATA_CACHING', false); |
|
21
|
|
|
|
|
22
|
|
|
if ($isCaching && Cache::has('metadataControllers')) { |
|
23
|
|
|
$meta = Cache::get('metadataControllers'); |
|
24
|
|
|
App::instance('metadataControllers', $meta); |
|
|
|
|
|
|
25
|
|
|
return; |
|
26
|
68 |
|
} |
|
27
|
|
|
|
|
28
|
68 |
|
$meta = App::make('metadataControllers'); |
|
29
|
68 |
|
|
|
30
|
68 |
|
$Classes = $this->getClassMap(); |
|
31
|
68 |
|
$ends = $this->getCandidateControllers($Classes); |
|
32
|
68 |
|
|
|
33
|
68 |
|
// now process each class that uses the metadata controller trait and stick results in $metamix |
|
34
|
68 |
|
$metamix = []; |
|
35
|
|
|
$map = null; |
|
|
|
|
|
|
36
|
68 |
|
foreach ($ends as $end) { |
|
37
|
68 |
|
$map = $end->getMappings(); |
|
38
|
68 |
|
// verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for |
|
39
|
68 |
|
// a model can glom onto different controllers |
|
40
|
|
|
foreach ($map as $key => $lock) { |
|
41
|
68 |
|
if (!array_key_exists($key, $metamix)) { |
|
42
|
68 |
|
// if we haven't yet got a mapping for this model, grab it holus-bolus |
|
43
|
68 |
|
$metamix[$key] = $lock; |
|
44
|
68 |
|
continue; |
|
45
|
67 |
|
} |
|
46
|
67 |
|
// if we do, make sure we aren't re-adding mappings for any of the CRUD verbs |
|
47
|
68 |
|
foreach ($lock as $barrel => $roll) { |
|
48
|
|
|
assert( |
|
49
|
|
|
!array_key_exists($barrel, $metamix[$key]), |
|
50
|
68 |
|
'Mapping already defined for model '.$key.' and CRUD verb '.$barrel |
|
51
|
|
|
); |
|
52
|
|
|
$metamix[$key][$barrel] = $roll; |
|
53
|
68 |
|
} |
|
54
|
68 |
|
} |
|
55
|
67 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
$meta->setMetadata($metamix); |
|
58
|
67 |
|
|
|
59
|
67 |
|
} |
|
60
|
|
|
|
|
61
|
67 |
|
/** |
|
62
|
67 |
|
* Register the application services. Boot-time only. |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function register() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->app->singleton('metadataControllers', function ($app) { |
|
|
|
|
|
|
69
|
|
|
return new MetadataControllerContainer(); |
|
70
|
|
|
}); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
67 |
|
/** |
|
74
|
68 |
|
* @param $Classes |
|
75
|
|
|
* @return array |
|
76
|
68 |
|
* @throws \Exception |
|
77
|
|
|
*/ |
|
78
|
68 |
|
protected function getCandidateControllers($Classes) |
|
79
|
|
|
{ |
|
80
|
|
|
$ends = []; |
|
81
|
|
|
$startName = defined('PODATA_LARAVEL_APP_ROOT_NAMESPACE') ? PODATA_LARAVEL_APP_ROOT_NAMESPACE : "App"; |
|
82
|
|
|
foreach ($Classes as $name) { |
|
83
|
|
|
// not in app namespace, keep moving |
|
84
|
|
|
if (!\Illuminate\Support\Str::startsWith($name, $startName)) { |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
68 |
|
// if class doesn't exist (for whatever reason), skip it now rather than muck about later |
|
88
|
68 |
|
if (!class_exists($name)) { |
|
89
|
68 |
|
continue; |
|
90
|
68 |
|
} |
|
91
|
|
|
try { |
|
92
|
|
|
if (in_array(MetadataControllerTrait::class, class_uses($name, false))) { |
|
93
|
|
|
$ends[] = App::make($name); |
|
94
|
|
|
} |
|
95
|
|
|
} catch (\Exception $e) { |
|
96
|
|
|
if (!App::runningInConsole()) { |
|
97
|
|
|
throw $e; |
|
98
|
|
|
} |
|
99
|
|
|
// Squash exceptions thrown here when running from CLI so app can continue booting |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
return $ends; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $classMap |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getClassMap() |
|
110
|
|
|
{ |
|
111
|
|
|
$classes = get_declared_classes(); |
|
112
|
|
|
$AutoClass = null; |
|
113
|
|
|
foreach ($classes as $class) { |
|
114
|
|
|
if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) { |
|
115
|
|
|
$AutoClass = $class; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$Classes = $AutoClass::$classMap; |
|
120
|
|
|
return array_keys($Classes); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.