|
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
|
|
|
$classes = get_declared_classes(); |
|
44
|
|
|
$AutoClass = null; |
|
45
|
|
|
foreach ($classes as $class) { |
|
46
|
|
|
if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) { |
|
47
|
|
|
$AutoClass = $class; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
$ends = array(); |
|
51
|
|
|
$Classes = $AutoClass::$classMap; |
|
52
|
|
|
foreach ($Classes as $name => $file) { |
|
53
|
|
|
if (\Illuminate\Support\Str::startsWith($name, "App")) { |
|
54
|
|
|
if (in_array("AlgoWeb\\PODataLaravel\\Models\\MetadataTrait", class_uses($name))) { |
|
55
|
|
|
$ends[] = $name; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$EntityTypes = array(); |
|
61
|
|
|
$ResourceSets = array(); |
|
62
|
|
|
$begins = []; |
|
63
|
|
|
$numEnds = count($ends); |
|
64
|
|
|
|
|
65
|
|
|
for ($i = 0; $i < $numEnds; $i++) { |
|
66
|
|
|
$bitter = $ends[$i]; |
|
67
|
|
|
$fqModelName = $bitter; |
|
68
|
|
|
|
|
69
|
|
|
$instance = new $fqModelName(); |
|
70
|
|
|
$name = $instance->getEndpointName(); |
|
71
|
|
|
$metaSchema = $instance->getXmlSchema(); |
|
72
|
|
|
// if for whatever reason we don't get an XML schema, move on to next entry and drop current one from |
|
73
|
|
|
// further processing |
|
74
|
|
|
if (null == $metaSchema) { |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
$EntityTypes[$fqModelName] = $metaSchema; |
|
78
|
|
|
$ResourceSets[$fqModelName] = $meta->addResourceSet( |
|
79
|
|
|
strtolower($name), |
|
80
|
|
|
$EntityTypes[$fqModelName] |
|
81
|
|
|
); |
|
82
|
|
|
$begins[] = $bitter; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$ends = $begins; |
|
86
|
|
|
unset($begins); |
|
87
|
|
|
|
|
88
|
|
|
// now that endpoints are hooked up, tackle the relationships |
|
89
|
|
|
// if we'd tried earlier, we'd be guaranteed to try to hook a relation up to null, which would be bad |
|
90
|
|
|
foreach ($ends as $bitter) { |
|
91
|
|
|
$fqModelName = $bitter; |
|
92
|
|
|
$instance = new $fqModelName(); |
|
93
|
|
|
$instance->hookUpRelationships($EntityTypes, $ResourceSets); |
|
94
|
|
|
} |
|
95
|
|
|
if ($isCaching) { |
|
96
|
|
|
if (!$hasCache) { |
|
97
|
|
|
$cacheTime = env('APP_METADATA_CACHE_DURATION', 10); |
|
98
|
|
|
$cacheTime = !is_numeric($cacheTime) ? 10 : abs($cacheTime); |
|
99
|
|
|
Cache::put('metadata', $meta, $cacheTime); |
|
100
|
|
|
} |
|
101
|
|
|
} else { |
|
102
|
|
|
Cache::forget('metadata'); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private static function setupRoute() |
|
107
|
|
|
{ |
|
108
|
|
|
$valueArray = []; |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
Route::any('odata.svc/{section}', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index') |
|
111
|
|
|
->where(['section' => '.*']); |
|
112
|
|
|
Route::any('odata.svc', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Register the application services. Boot-time only. |
|
117
|
|
|
* |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
public function register() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->app->singleton('metadata', function ($app) { |
|
|
|
|
|
|
123
|
|
|
return new SimpleMetadataProvider('Data', self::$METANAMESPACE); |
|
124
|
|
|
}); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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.