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
|
|
|
public function boot() |
21
|
|
|
{ |
22
|
|
|
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
|
|
|
if (!Schema::hasTable('migrations')) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
} catch (\Exception $e) { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
self::setupRoute(); |
33
|
|
|
$isCaching = env('APP_METADATA_CACHING', false); |
34
|
|
|
|
35
|
|
|
if ($isCaching && Cache::has('metadata')) { |
36
|
|
|
$meta = Cache::get('metadata'); |
37
|
|
|
$this->app->instance('metadata', $meta); |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
$meta = $this->app->make('metadata'); |
41
|
|
|
|
42
|
|
|
$classes = get_declared_classes(); |
43
|
|
|
$AutoClass = null; |
44
|
|
|
foreach ($classes as $class) { |
45
|
|
|
if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) { |
46
|
|
|
$AutoClass = $class; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
$ends = array(); |
50
|
|
|
$Classes = $AutoClass::$classMap; |
51
|
|
View Code Duplication |
foreach ($Classes as $name => $file) { |
|
|
|
|
52
|
|
|
if (\Illuminate\Support\Str::startsWith($name, "App")) { |
53
|
|
|
if (in_array("AlgoWeb\\PODataLaravel\\Models\\MetadataTrait", class_uses($name))) { |
54
|
|
|
$ends[] = $name; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$EntityTypes = array(); |
60
|
|
|
$ResourceSets = array(); |
61
|
|
|
$begins = []; |
62
|
|
|
|
63
|
|
|
for ($i = 0; $i < count($ends); $i++) { |
|
|
|
|
64
|
|
|
$bitter = $ends[$i]; |
65
|
|
|
$fqModelName = $bitter; //$bitter->fqName(); |
|
|
|
|
66
|
|
|
$name = substr($bitter, strrpos($bitter, '\\')+1); |
67
|
|
|
//$fqModelName = $bitter; |
|
|
|
|
68
|
|
|
$instance = new $fqModelName(); |
69
|
|
|
$metaSchema = $instance->getXmlSchema(); |
70
|
|
|
// if for whatever reason we don't get an XML schema, move on to next entry and drop current one from |
71
|
|
|
// further processing |
72
|
|
|
if (null == $metaSchema) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
$EntityTypes[$fqModelName] = $metaSchema; |
76
|
|
|
$ResourceSets[$fqModelName] = $meta->addResourceSet( |
77
|
|
|
strtolower($name), |
78
|
|
|
$EntityTypes[$fqModelName] |
79
|
|
|
); |
80
|
|
|
$begins[] = $bitter; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$ends = $begins; |
84
|
|
|
unset($begins); |
85
|
|
|
|
86
|
|
|
// now that endpoints are hooked up, tackle the relationships |
87
|
|
|
// if we'd tried earlier, we'd be guaranteed to try to hook a relation up to null, which would be bad |
88
|
|
|
foreach ($ends as $bitter) { |
89
|
|
|
$fqModelName = $bitter; |
90
|
|
|
$instance = new $fqModelName(); |
91
|
|
|
$instance->hookUpRelationships($EntityTypes, $ResourceSets); |
92
|
|
|
} |
93
|
|
|
if ($isCaching) { |
94
|
|
|
Cache::put('metadata', $meta, 10); |
95
|
|
|
} else { |
96
|
|
|
Cache::forget('metadata'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private static function setupRoute() |
101
|
|
|
{ |
102
|
|
|
$valueArray = []; |
|
|
|
|
103
|
|
|
|
104
|
|
|
Route::any('odata.svc/{section}', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index') |
105
|
|
|
->where(['section' => '.*']); |
106
|
|
|
Route::any('odata.svc', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Register the application services. Boot-time only. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function register() |
115
|
|
|
{ |
116
|
|
|
$this->app->singleton('metadata', function ($app) { |
|
|
|
|
117
|
|
|
return new SimpleMetadataProvider('Data', self::$METANAMESPACE); |
118
|
|
|
}); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.