Skip to content

Commit 6a41a31

Browse files
committed
bootable class
1 parent 13a3697 commit 6a41a31

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

src/FractionManager.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use Fraction\Exceptions\ActionNotRegistered;
99
use Fraction\Exceptions\UnallowedActionDuplication;
10+
use Fraction\Support\Bootable;
1011
use Fraction\Support\FractionName;
1112
use Illuminate\Foundation\Application;
1213
use UnitEnum;
@@ -61,35 +62,6 @@ public function get(string|UnitEnum $action): mixed
6162
*/
6263
public function boot(): void
6364
{
64-
$cached = [];
65-
66-
if ($this->application->isProduction() && file_exists($path = base_path('bootstrap/cache/actions.php'))) {
67-
$files = require $path;
68-
69-
foreach ($files as $file) {
70-
require_once $file;
71-
}
72-
} else {
73-
$files = glob(config('fraction.path').'/*.php');
74-
75-
foreach ($files as $file) {
76-
$content = file_get_contents($file);
77-
78-
if (mb_strpos($content, 'namespace') !== false || mb_strpos($content, 'execute') === false) {
79-
continue;
80-
}
81-
82-
$cached[] = $file;
83-
84-
require_once $file;
85-
}
86-
87-
if ($cached !== []) {
88-
file_put_contents(
89-
base_path('bootstrap/cache/actions.php'),
90-
'<?php return '.var_export($cached, true).';'
91-
);
92-
}
93-
}
65+
Bootable::fire($this->application);
9466
}
9567
}

src/Support/Bootable.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fraction\Support;
6+
7+
use Illuminate\Foundation\Application;
8+
9+
/** @internal */
10+
final readonly class Bootable
11+
{
12+
public function __construct(private Application $application)
13+
{
14+
//
15+
}
16+
17+
/**
18+
* Boot the fraction.
19+
*/
20+
public static function fire(Application $application): void
21+
{
22+
$class = new self($application);
23+
24+
$files = $class->files();
25+
26+
$class->require($files);
27+
}
28+
29+
/**
30+
* Get the files to be booted.
31+
*/
32+
private function files(): mixed
33+
{
34+
if ($this->application->isProduction() && file_exists($path = base_path('bootstrap/cache/actions.php'))) {
35+
return require $path;
36+
}
37+
38+
return glob(config('fraction.path').'/*.php');
39+
}
40+
41+
/**
42+
* Require the files and cache them.
43+
*/
44+
private function require(array $files): void
45+
{
46+
$cached = [];
47+
48+
foreach ($files as $file) {
49+
$cached[] = $file;
50+
51+
require_once $file;
52+
}
53+
54+
if ($this->application->isProduction() && $cached !== []) {
55+
file_put_contents(
56+
base_path('bootstrap/cache/actions.php'),
57+
'<?php return '.var_export($cached, true).';'
58+
);
59+
}
60+
}
61+
}

tests/Architecture/ArchitectureTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Fraction\Handlers\AsSync;
2222
use Fraction\Handlers\Concerns\ShareableInterpreter;
2323
use Fraction\Jobs\FractionJob;
24+
use Fraction\Support\Bootable;
2425
use Fraction\Support\DependencyResolver;
2526
use Fraction\Support\FractionName;
2627
use Fraction\ValueObjects\Then;
@@ -128,6 +129,17 @@
128129
expect($content)->toBe($original);
129130
});
130131

132+
arch()
133+
->expect(Bootable::class)
134+
->toBeFinal()
135+
->toHaveConstructor()
136+
->toOnlyBeUsedIn(FractionManager::class)
137+
->toHaveMethods([
138+
'files',
139+
'files',
140+
'require',
141+
]);
142+
131143
arch()
132144
->expect(DependencyResolver::class)
133145
->toBeFinal()

0 commit comments

Comments
 (0)