Skip to content

Commit 5d7f2fa

Browse files
committed
adding OptimizeActionsCommand command
1 parent c6aacb6 commit 5d7f2fa

File tree

3 files changed

+109
-7
lines changed

3 files changed

+109
-7
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fraction\Console;
6+
7+
use Illuminate\Console\Command;
8+
9+
/** @codeCoverageIgnore */
10+
class OptimizeActionsCommand extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'optimize:actions';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Optimize the action files.';
25+
26+
/**
27+
* Execute the console command.
28+
*/
29+
public function handle(): int
30+
{
31+
$path = config('fraction.path');
32+
33+
if (! is_dir($path)) {
34+
$this->components->error("The path [{$path}] does not exist. Skipping optimization.");
35+
36+
return self::SUCCESS;
37+
}
38+
39+
$files = glob($path.'/*.php');
40+
41+
if (empty($files)) {
42+
$this->components->error('No action files found to optimize. Skipping optimization.');
43+
44+
return self::SUCCESS;
45+
}
46+
47+
$cached = [];
48+
49+
foreach ($files as $file) {
50+
$content = file_get_contents($file);
51+
52+
if (mb_strpos($content, 'namespace') !== false || mb_strpos($content, 'execute') === false) {
53+
continue;
54+
}
55+
56+
$cached[] = $file;
57+
}
58+
59+
if ($cached === []) {
60+
$this->components->error('No action files found to optimize. Skipping optimization.');
61+
62+
return self::SUCCESS;
63+
}
64+
65+
if (file_exists(base_path('bootstrap/cache/actions.php'))) {
66+
@unlink(base_path('bootstrap/cache/actions.php'));
67+
}
68+
69+
file_put_contents(
70+
base_path('bootstrap/cache/actions.php'),
71+
'<?php return '.var_export($cached, true).';'
72+
);
73+
74+
$this->components->info('Action files optimized successfully.');
75+
76+
return self::SUCCESS;
77+
}
78+
}

src/FractionManager.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,35 @@ public function get(string|UnitEnum $action): mixed
5454

5555
public function boot(): void
5656
{
57-
$files = glob(config('fraction.path').'/*.php');
57+
$cached = [];
5858

59-
foreach ($files as $file) {
60-
$content = file_get_contents($file);
59+
if ($this->application->isProduction() && file_exists($path = base_path('bootstrap/cache/actions.php'))) {
60+
$files = require $path;
6161

62-
if (mb_strpos($content, 'namespace') !== false || mb_strpos($content, 'execute') === false) {
63-
continue;
62+
foreach ($files as $file) {
63+
require_once $file;
6464
}
65+
} else {
66+
$files = glob(config('fraction.path').'/*.php');
6567

66-
require_once $file;
68+
foreach ($files as $file) {
69+
$content = file_get_contents($file);
70+
71+
if (mb_strpos($content, 'namespace') !== false || mb_strpos($content, 'execute') === false) {
72+
continue;
73+
}
74+
75+
$cached[] = $file;
76+
77+
require_once $file;
78+
}
79+
80+
if ($cached !== []) {
81+
file_put_contents(
82+
base_path('bootstrap/cache/actions.php'),
83+
'<?php return '.var_export($cached, true).';'
84+
);
85+
}
6786
}
6887
}
6988
}

src/FractionServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public function register(): void
2020
return;
2121
}
2222

23-
$this->commands(Console\MakeActionCommand::class);
23+
$this->commands([
24+
Console\MakeActionCommand::class,
25+
Console\OptimizeActionsCommand::class,
26+
]);
2427
}
2528

2629
public function boot(): void
@@ -29,6 +32,8 @@ public function boot(): void
2932
__DIR__.'/../config.php' => config_path('fraction.php'),
3033
]);
3134

35+
$this->optimizes(Console\OptimizeActionsCommand::class, Console\OptimizeActionsCommand::class, 'actions');
36+
3237
Fraction::boot();
3338
}
3439
}

0 commit comments

Comments
 (0)