1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use POData\Providers\Metadata\SimpleMetadataProvider; |
8
|
|
|
use Illuminate\Support\Facades\Route; |
9
|
|
|
use Illuminate\Support\Facades\Schema as Schema; |
10
|
|
|
|
11
|
|
|
class MetadataProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
protected static $METANAMESPACE = "Data"; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Bootstrap the application services. Post-boot. |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
1 |
|
public function boot() |
21
|
|
|
{ |
22
|
1 |
|
self::$METANAMESPACE = env('ODataMetaNamespace', 'Data'); |
23
|
|
|
// If we aren't migrated, there's no DB tables to pull metadata _from_, so bail out early |
24
|
|
|
try { |
25
|
1 |
|
if (!Schema::hasTable('migrations')) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
1 |
|
} catch (\Exception $e) { |
29
|
1 |
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
self::setupRoute(); |
33
|
|
|
$isCaching = env('APP_METADATA_CACHING', false); |
34
|
|
|
$hasCache = Cache::has('metadata'); |
35
|
|
|
|
36
|
|
|
if ($isCaching && $hasCache) { |
37
|
|
|
$meta = Cache::get('metadata'); |
38
|
|
|
$this->app->instance('metadata', $meta); |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
$meta = $this->app->make('metadata'); |
42
|
|
|
|
43
|
|
|
$modelNames = $this->getCandidateModels(); |
44
|
|
|
|
45
|
|
|
list($EntityTypes, $ResourceSets, $ends) = $this->getEntityTypesAndResourceSets($meta, $modelNames); |
46
|
|
|
|
47
|
|
|
// now that endpoints are hooked up, tackle the relationships |
48
|
|
|
// if we'd tried earlier, we'd be guaranteed to try to hook a relation up to null, which would be bad |
49
|
|
|
foreach ($ends as $bitter) { |
50
|
|
|
$fqModelName = $bitter; |
51
|
|
|
$instance = new $fqModelName(); |
52
|
|
|
$instance->hookUpRelationships($EntityTypes, $ResourceSets); |
53
|
|
|
} |
54
|
|
|
if ($isCaching) { |
55
|
|
|
if (!$hasCache) { |
56
|
|
|
$cacheTime = env('APP_METADATA_CACHE_DURATION', 10); |
57
|
|
|
$cacheTime = !is_numeric($cacheTime) ? 10 : abs($cacheTime); |
58
|
|
|
Cache::put('metadata', $meta, $cacheTime); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
Cache::forget('metadata'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private static function setupRoute() |
66
|
|
|
{ |
67
|
|
|
$valueArray = []; |
|
|
|
|
68
|
|
|
|
69
|
|
|
Route::any('odata.svc/{section}', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index') |
70
|
|
|
->where(['section' => '.*']); |
71
|
|
|
Route::any('odata.svc', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Register the application services. Boot-time only. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function register() |
80
|
|
|
{ |
81
|
|
|
$this->app->singleton('metadata', function ($app) { |
|
|
|
|
82
|
|
|
return new SimpleMetadataProvider('Data', self::$METANAMESPACE); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
protected function getCandidateModels() |
90
|
|
|
{ |
91
|
|
|
$classes = get_declared_classes(); |
92
|
|
|
$AutoClass = null; |
93
|
|
|
foreach ($classes as $class) { |
94
|
|
|
if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) { |
95
|
|
|
$AutoClass = $class; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$ends = []; |
99
|
|
|
$Classes = $AutoClass::$classMap; |
100
|
|
|
$startName = defined('PODATA_LARAVEL_APP_ROOT_NAMESPACE') ? PODATA_LARAVEL_APP_ROOT_NAMESPACE : "App"; |
101
|
|
|
foreach ($Classes as $name => $file) { |
102
|
|
|
if (\Illuminate\Support\Str::startsWith($name, $startName)) { |
103
|
|
|
if (in_array("AlgoWeb\\PODataLaravel\\Models\\MetadataTrait", class_uses($name))) { |
104
|
|
|
$ends[] = $name; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
return $ends; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $meta |
113
|
|
|
* @param $ends |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
protected function getEntityTypesAndResourceSets($meta, $ends) |
117
|
|
|
{ |
118
|
|
|
$EntityTypes = []; |
119
|
|
|
$ResourceSets = []; |
120
|
|
|
$begins = []; |
121
|
|
|
$numEnds = count($ends); |
122
|
|
|
|
123
|
|
|
for ($i = 0; $i < $numEnds; $i++) { |
124
|
|
|
$bitter = $ends[$i]; |
125
|
|
|
$fqModelName = $bitter; |
126
|
|
|
|
127
|
|
|
$instance = new $fqModelName(); |
128
|
|
|
$name = strtolower($instance->getEndpointName()); |
129
|
|
|
$metaSchema = $instance->getXmlSchema(); |
130
|
|
|
// if for whatever reason we don't get an XML schema, move on to next entry and drop current one from |
131
|
|
|
// further processing |
132
|
|
|
if (null == $metaSchema) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
$EntityTypes[$fqModelName] = $metaSchema; |
136
|
|
|
$ResourceSets[$fqModelName] = $meta->addResourceSet($name, $metaSchema); |
137
|
|
|
$begins[] = $bitter; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return array($EntityTypes, $ResourceSets, $begins); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.