|
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
|
68 |
|
if (in_array("AlgoWeb\\PODataLaravel\\Controllers\\MetadataControllerTrait", class_uses($name))) { |
|
41
|
68 |
|
$ends[] = new $name(); |
|
42
|
68 |
|
} |
|
43
|
68 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
// now process each class that uses the metadata controller trait and stick results in $metamix |
|
46
|
68 |
|
$map = null; |
|
|
|
|
|
|
47
|
68 |
|
foreach ($ends as $end) { |
|
48
|
68 |
|
$map = $end->getMappings(); |
|
49
|
|
|
// verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for |
|
50
|
|
|
// a model can glom onto different controllers |
|
51
|
68 |
|
foreach ($map as $key => $lock) { |
|
52
|
68 |
|
if (!array_key_exists($key, $metamix)) { |
|
53
|
|
|
// if we haven't yet got a mapping for this model, grab it holus-bolus |
|
54
|
68 |
|
$metamix[$key] = $lock; |
|
55
|
68 |
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
// if we do, make sure we aren't re-adding mappings for any of the CRUD verbs |
|
58
|
|
|
foreach ($lock as $barrel => $roll) { |
|
59
|
|
|
assert( |
|
60
|
|
|
!array_key_exists($barrel, $metamix[$key]), |
|
61
|
|
|
'Mapping already defined for model '.$key.' and CRUD verb '.$barrel |
|
62
|
|
|
); |
|
63
|
|
|
$metamix[$key][$barrel] = $roll; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
68 |
|
} |
|
67
|
68 |
|
} |
|
68
|
|
|
|
|
69
|
68 |
|
$meta->setMetadata($metamix); |
|
70
|
|
|
|
|
71
|
68 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Register the application services. Boot-time only. |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function register() |
|
79
|
|
|
{ |
|
80
|
68 |
|
$this->app->singleton('metadataControllers', function ($app) { |
|
|
|
|
|
|
81
|
68 |
|
return new MetadataControllerContainer(); |
|
82
|
68 |
|
}); |
|
83
|
68 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.