1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Joesama\Pintu\Components; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Symfony\Component\Finder\Finder; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
use Joesama\Pintu\Components\Concerns\Grammar; |
11
|
|
|
|
12
|
|
|
class Manager |
13
|
|
|
{ |
14
|
|
|
use Grammar; |
15
|
|
|
|
16
|
|
|
private const FILE_NAME = 'component'; |
17
|
|
|
|
18
|
|
|
private const FILE_EXT = '.php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Component Path |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $componentPath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Component Object. |
29
|
|
|
* |
30
|
|
|
* @var Collection |
31
|
|
|
*/ |
32
|
|
|
protected $component; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Component controller namespace. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $controllerNamespace; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initiate Component Manager. |
43
|
|
|
* |
44
|
|
|
* @param ReflectionClass $providerReflection |
45
|
|
|
*/ |
46
|
6 |
|
public function __construct(ReflectionClass $providerReflection) |
47
|
|
|
{ |
48
|
6 |
|
if (!$providerReflection->isSubclassOf(ServiceProvider::class)) { |
49
|
1 |
|
throw new Exception( |
50
|
1 |
|
$providerReflection->getName() . ' must an instance of ' . \basename(ServiceProvider::class), |
51
|
1 |
|
1 |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
$this->componentPath = $this->componentPath($providerReflection); |
56
|
|
|
|
57
|
5 |
|
$this->component = $this->componentSource(); |
58
|
|
|
|
59
|
5 |
|
$this->controllerNamespace = $this->registerNamespace($providerReflection); |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return landing . |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
2 |
|
public function getLanding(): bool |
68
|
|
|
{ |
69
|
2 |
|
return $this->component->get('landing', false); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return component collection. |
74
|
|
|
* |
75
|
|
|
* @return Collection |
76
|
|
|
*/ |
77
|
3 |
|
public function getComponent(): Collection |
78
|
|
|
{ |
79
|
3 |
|
return Collection::make($this->component->get('component')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return component collection. |
84
|
|
|
* |
85
|
|
|
* @return Collection |
86
|
|
|
*/ |
87
|
3 |
|
public function getApi(): Collection |
88
|
|
|
{ |
89
|
3 |
|
return Collection::make($this->component->get('api')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the component file path. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
1 |
|
public function getComponentFilePath(): string |
98
|
|
|
{ |
99
|
1 |
|
return $this->componentPath . '/' . self::FILE_NAME . self::FILE_EXT; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the component file stub. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
1 |
|
public function getComponentFileStub(): string |
108
|
|
|
{ |
109
|
1 |
|
return __DIR__ . '/stubs/component.stub'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get default component name space. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
3 |
|
public function getComponentNameSpace(): array |
118
|
|
|
{ |
119
|
3 |
|
return $this->controllerNamespace; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the component resources. |
124
|
|
|
* |
125
|
|
|
* @return Collection |
126
|
|
|
*/ |
127
|
5 |
|
private function componentSource(): Collection |
128
|
|
|
{ |
129
|
5 |
|
$content = []; |
130
|
|
|
|
131
|
5 |
|
if (realpath($this->componentPath) !== false) { |
132
|
5 |
|
$componentFile = Collection::make( |
133
|
5 |
|
Finder::create() |
134
|
5 |
|
->files() |
135
|
5 |
|
->name(self::FILE_NAME . self::FILE_EXT) |
136
|
5 |
|
->in($this->componentPath) |
137
|
5 |
|
)->first(); |
138
|
|
|
|
139
|
5 |
|
if ($componentFile !== null) { |
140
|
5 |
|
$content = include $componentFile->getRealPath(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
5 |
|
return Collection::make($content); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the path for define component. |
149
|
|
|
* |
150
|
|
|
* @param ReflectionClass $providerReflection |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
5 |
|
private function componentPath(ReflectionClass $providerReflection): string |
155
|
|
|
{ |
156
|
5 |
|
$classNameSpace = Collection::make(explode('\\', $providerReflection->getNamespaceName())); |
157
|
|
|
|
158
|
5 |
|
$baseDir = dirname($providerReflection->getFileName(), $classNameSpace->count()); |
159
|
|
|
|
160
|
5 |
|
return $baseDir . '/' . self::FILE_NAME; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|