/
TwillPackageServiceProvider.php
111 lines (90 loc) · 3.33 KB
/
TwillPackageServiceProvider.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
<?php
namespace A17\Twill;
use A17\Twill\Facades\TwillBlocks;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use ReflectionClass;
abstract class TwillPackageServiceProvider extends ServiceProvider
{
protected $autoRegisterCapsules = true;
public function boot(): void
{
if ($this->autoRegisterCapsules) {
$this->registerCapsules('Twill/Capsules');
}
}
protected function registerCapsule(string $name): void
{
$namespace = $this->getCapsuleNamespace();
$namespace .= '\\Twill\\Capsules\\' . $name;
$dir = $this->getPackageDirectory() . DIRECTORY_SEPARATOR .
'src' . DIRECTORY_SEPARATOR .
'Twill' . DIRECTORY_SEPARATOR .
'Capsules' . DIRECTORY_SEPARATOR . $name;
\A17\Twill\Facades\TwillCapsules::registerPackageCapsule($name, $namespace, $dir);
}
protected function registerCapsules(string $directory): void
{
$storage = Storage::build([
'driver' => 'local',
'root' => $this->getPackageDirectory() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $directory,
]);
foreach ($storage->directories() as $capsuleName) {
$this->registerCapsule($capsuleName);
}
}
protected function getClassName(): string
{
$provider = explode('\\', get_class($this));
return array_pop($provider);
}
protected function getCapsuleNamespace(): string
{
$provider = explode('\\', get_class($this));
array_pop($provider);
return implode('\\', $provider);
}
protected function getPackageDirectory(): string
{
$class = new ReflectionClass(get_class($this));
$path = Str::replaceLast(DIRECTORY_SEPARATOR . $this->getClassName() . '.php', '', $class->getFileName());
if (Str::endsWith($path, DIRECTORY_SEPARATOR . 'src')) {
$path = Str::replaceLast(DIRECTORY_SEPARATOR . 'src', '', $path);
}
return $path;
}
protected function registerVueComponentsDirectory($path)
{
$this->publishes(
[$path => resource_path(config('twill.vendor_components_resource_path'))],
'components'
);
}
/**
* Register a blocks directory.
*
* If a namespace is provided for the render, Twill will assume it to be under:
* NAMESPACE::site.blocks.BLOCK-NAME
*
* Given `$this->loadViewsFrom(__DIR__ . '/../resources/views/site/blocks', 'package-name');`
*
* So if you have a block called "example" and you want your package to provide the
* preview. Your file should be in `resources\views\site\blocks\example.blade.php`.
*
* To make sure the end user can override the views, you should make them publishable:
* ```
* $this->publishes([
* __DIR__ . '/../resources/views' => resource_path('views/vendor/PACKAGE-NAME'),
* ]);
* ```
*/
protected function registerBlocksDirectory($path, string $renderNamespace = null)
{
TwillBlocks::registerPackageBlocksDirectory($path, $renderNamespace);
}
protected function registerRepeatersDirectory($path, string $renderNamespace = null)
{
TwillBlocks::registerPackageRepeatersDirectory($path, $renderNamespace);
}
}