-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackageService.php
93 lines (76 loc) · 2.59 KB
/
PackageService.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
<?php
namespace Spatian\Ziglite\Services;
use Illuminate\Support\Str;
use Illuminate\Contracts\Foundation\Application;
use Spatian\Ziglite\Interfaces\OutputGeneratorInterface;
use Illuminate\View\Compilers\BladeCompiler;
use Spatian\Ziglite\Generators\JavascriptDataTagGenerator;
final class PackageService {
private array $composer;
private string $name;
public function __construct() {
$this->composer = json_decode(
file_get_contents($this->rootDir('composer.json')),
true,
512,
JSON_THROW_ON_ERROR
);
$this->name = explode('/', $this->composer['name'])[1];
}
public function version(): string {
return '1.2.0';
}
public function name(): string {
return $this->name;
}
public function website(): string {
return $this->composer['homepage'] ?? '';
}
public function displayName(): string {
return 'Ziglite';
}
public function rootDir(string $sub = null): string {
return __DIR__ . '/../../' . ($sub ?? '');
}
public function distDir(string $sub = null): string {
return $this->rootDir('dist/' . $sub);
}
public function configFile(): string {
return $this->rootDir('config/config.php');
}
public function envPropName(string $name): string {
return Str::upper($this->name() . '_' . $name);
}
public function config(string $name, mixed $default = null): string {
return config($this->name() . '.' . $name, $default);
}
public function setupBladeDirective(
string $name = null,
OutputGeneratorInterface $generator = null
): void {
$app = app();
/** @var Application $app */
if ($app->resolved('blade.compiler')) {
$this->registerBladeDirective($app['blade.compiler'], $name, $generator);
} else {
$app->afterResolving(
'blade.compiler',
fn (BladeCompiler $blade) => $this->registerBladeDirective($blade, $name, $generator)
);
}
}
private function registerBladeDirective(
BladeCompiler $blade,
string $directive = null,
OutputGeneratorInterface $generator = null,
): void {
$directive = $directive ?? $this->name();
$class = is_null($generator) ? JavascriptDataTagGenerator::class : $generator::class;
$blade->directive(
$directive,
function (string $expr = '') use ($class) {
return "<?php echo (new \\{$class}())->make({$expr}); ?>";
}
);
}
}