/
TwillCapsules.php
157 lines (129 loc) · 4.29 KB
/
TwillCapsules.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace A17\Twill;
use A17\Twill\Exceptions\NoCapsuleFoundException;
use A17\Twill\Helpers\Capsule;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
class TwillCapsules
{
/**
* @var \A17\Twill\Helpers\Capsule[]
*/
public $registeredCapsules = [];
public function registerPackageCapsule(
string $name,
string $namespace,
string $path,
string $singular = null,
bool $enabled = true,
bool $automaticNavigation = true
): Capsule {
$capsule = new Capsule($name, $namespace, $path, $singular, $enabled, true, $automaticNavigation);
$this->registerCapsule($capsule);
return $this->registeredCapsules[$name];
}
public function registerCapsule(Capsule $capsule): void
{
$this->registeredCapsules[$capsule->name] = $capsule;
}
/**
* Generates a non package capsule object.
*/
public function makeProjectCapsule(string $name): Capsule
{
return new Capsule(
$name,
$this->capsuleNamespace($name),
config('twill.capsules.path') . DIRECTORY_SEPARATOR . $name
);
}
/**
* @throws \A17\Twill\Exceptions\NoCapsuleFoundException
*/
public function getCapsuleForModule(string $module): Capsule
{
$capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($module) {
return $capsule->getModule() === $module;
});
if (!$capsule) {
throw new NoCapsuleFoundException($module);
}
return $capsule;
}
/**
* @throws \A17\Twill\Exceptions\NoCapsuleFoundException
*/
public function getCapsuleForModel(string $model): Capsule
{
$capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($model) {
return $capsule->getSingular() === $model;
});
if (!$capsule) {
throw new NoCapsuleFoundException($model);
}
return $capsule;
}
/**
* @return Collection<Capsule>
*/
public function getRegisteredCapsules(): Collection
{
$this->loadProjectCapsules();
return collect($this->registeredCapsules);
}
public function loadProjectCapsules(): void
{
$path = config('twill.capsules.path');
$list = collect(config('twill.capsules.list'));
$list
->where('enabled', true)
->filter(function ($capsule) {
return !isset($this->registeredCapsules[$capsule['name']]);
})
->map(function ($capsule) use ($path) {
$this->registerCapsule(
new Capsule(
$capsule['name'],
$this->capsuleNamespace($capsule['name']),
$path . DIRECTORY_SEPARATOR . $capsule['name'],
$capsule['singular'] ?? null,
$capsule['enabled'] ?? true
)
);
});
}
public function capsuleNamespace($capsuleName, $type = null): string
{
// @todo: Read from capsules to get this data.
$base = config('twill.capsules.namespaces.base');
$type = config("twill.capsules.namespaces.$type");
return "$base\\$capsuleName" . (filled($type) ? "\\$type" : '');
}
public function capsuleNamespaceToPath(
$namespace,
$capsuleNamespace,
$rootPath
): string {
$namespace = Str::after($namespace, $capsuleNamespace . '\\');
return $rootPath . DIRECTORY_SEPARATOR . $this->getProjectCapsulesSubdirectory() . str_replace(
'\\',
DIRECTORY_SEPARATOR,
$namespace
);
}
public function getProjectCapsulesPath(): string
{
return config('twill.capsules.path') . $this->getProjectCapsulesSubdirectory();
}
private function getProjectCapsulesSubdirectory(): string
{
$subdirectory = config('twill.capsules.namespaces.subdir');
return filled($subdirectory) ? $subdirectory . DIRECTORY_SEPARATOR : '';
}
public function getAutoloader()
{
return app()->bound('autoloader')
? app('autoloader')
: require base_path('vendor/autoload.php');
}
}