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