/
castor.php
90 lines (68 loc) · 2.61 KB
/
castor.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
<?php
use Castor\Attribute\AsListener;
use Castor\Attribute\AsTask;
use Castor\Event\AfterApplicationInitializationEvent;
use Castor\TaskDescriptorCollection;
use Symfony\Component\Finder\Finder;
use function Castor\io;
use function Castor\run;
#[AsTask(description: 'Replace Twig Include Syntax')]
function replace(string $path): void
{
$files = Finder::create()
->in($path)
->files()
;
foreach ($files as $file) {
$content = file_get_contents($file);
if (!str_contains($content, 'include')) {
continue;
}
$callback = function (array $matches) {
$layout = '{{ include(\'%s\'%s%s) }}';
$templateName = $matches['templateName'];
$templateName = str_replace(':', '/', $templateName);
$templateName = ltrim($templateName, '/');
$variables = $only = '';
if (isset($matches['variables'])) {
$variables = ', {'.$matches['variables'].'}';
}
if (isset($matches['only'])) {
$only = ', with_context = false';
}
return sprintf($layout, $templateName, $variables, $only);
};
$pattern = '#{%[\s\\n]+include(?:\()?[\s\\n\(]+[\'"\(](?<templateName>[\w+\.\-_/@]+)[\'"](?:\))?(?:[\s\\n]+with[\s\\n]+{(?<variables>(?:.|\n)*)})?(?:[\s\\n]+(?<only>only)[\s\\n]+)?[\s\\n]*%}#U';
$content = preg_replace_callback($pattern, $callback, $content);
file_put_contents($file, $content);
}
}
#[Internal]
#[AsTask(description: "Package the application in a static binary file")]
function package(): void
{
if (Phar::running(false)) {
throw new RuntimeException('This task must be run outside a phar.');
}
io()->title('Packaging application');
io()->section('Installing vendor');
run(['composer', 'install', '--no-dev', '--optimize-autoloader']);
io()->section('Compiling phar');
run(['castor', 'repack', '--app-name', 'twig-include-syntax']);
io()->section('Compiling static binary');
run(['castor', 'compile', 'twig-include-syntax.linux.phar']);
}
#[Attribute(Attribute::TARGET_FUNCTION)]
class Internal
{
}
#[AsListener(AfterApplicationInitializationEvent::class)]
function afterApplicationInitialization(AfterApplicationInitializationEvent $event): void
{
if (!Phar::running(false)) {
return;
}
$tasks = $event->taskDescriptorCollection->taskDescriptors;
$tasks = array_filter($tasks, fn ($task) => !(bool) ($task->function->getAttributes(Internal::class)[0] ?? false));
$event->taskDescriptorCollection = new TaskDescriptorCollection($tasks, []);
}