1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer; |
8
|
|
|
|
9
|
|
|
class MetadataControllerProvider extends ServiceProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Bootstrap the application services. Post-boot. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
68 |
|
public function boot() |
17
|
|
|
{ |
18
|
68 |
|
$isCaching = env('APP_METADATA_CACHING', false); |
19
|
|
|
|
20
|
68 |
|
if ($isCaching && Cache::has('metadataControllers')) { |
21
|
|
|
$meta = Cache::get('metadataControllers'); |
22
|
|
|
$this->app->instance('metadataControllers', $meta); |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
68 |
|
$meta = $this->app->make('metadataControllers'); |
27
|
|
|
|
28
|
68 |
|
$classes = get_declared_classes(); |
29
|
68 |
|
$AutoClass = null; |
30
|
68 |
|
foreach ($classes as $class) { |
31
|
68 |
|
if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) { |
32
|
68 |
|
$AutoClass = $class; |
33
|
68 |
|
} |
34
|
68 |
|
} |
35
|
|
|
|
36
|
68 |
|
$metamix = []; |
37
|
68 |
|
$ends = array(); |
38
|
68 |
|
$Classes = $AutoClass::$classMap; |
39
|
68 |
|
foreach ($Classes as $name => $file) { |
40
|
|
|
try { |
41
|
68 |
|
if (in_array( |
42
|
68 |
|
"AlgoWeb\\PODataLaravel\\Controllers\\MetadataControllerTrait", |
43
|
68 |
|
class_uses($name, false) |
44
|
68 |
|
)) { |
45
|
67 |
|
$ends[] = new $name(); |
46
|
67 |
|
} |
47
|
68 |
|
} catch (\Exception $e) { |
48
|
|
|
// Squash exceptions thrown here so app can continue booting |
49
|
|
|
} |
50
|
68 |
|
} |
51
|
|
|
|
52
|
|
|
// now process each class that uses the metadata controller trait and stick results in $metamix |
53
|
68 |
|
$map = null; |
|
|
|
|
54
|
68 |
|
foreach ($ends as $end) { |
55
|
67 |
|
$map = $end->getMappings(); |
56
|
|
|
// verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for |
57
|
|
|
// a model can glom onto different controllers |
58
|
67 |
|
foreach ($map as $key => $lock) { |
59
|
67 |
|
if (!array_key_exists($key, $metamix)) { |
60
|
|
|
// if we haven't yet got a mapping for this model, grab it holus-bolus |
61
|
67 |
|
$metamix[$key] = $lock; |
62
|
67 |
|
continue; |
63
|
|
|
} |
64
|
|
|
// if we do, make sure we aren't re-adding mappings for any of the CRUD verbs |
65
|
|
|
foreach ($lock as $barrel => $roll) { |
66
|
|
|
assert( |
67
|
|
|
!array_key_exists($barrel, $metamix[$key]), |
68
|
|
|
'Mapping already defined for model '.$key.' and CRUD verb '.$barrel |
69
|
|
|
); |
70
|
|
|
$metamix[$key][$barrel] = $roll; |
71
|
|
|
} |
72
|
|
|
|
73
|
67 |
|
} |
74
|
68 |
|
} |
75
|
|
|
|
76
|
68 |
|
$meta->setMetadata($metamix); |
77
|
|
|
|
78
|
68 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Register the application services. Boot-time only. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function register() |
86
|
|
|
{ |
87
|
68 |
|
$this->app->singleton('metadataControllers', function ($app) { |
|
|
|
|
88
|
68 |
|
return new MetadataControllerContainer(); |
89
|
68 |
|
}); |
90
|
68 |
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.