Skip to content

Commit e9680d7

Browse files
Fixes preloader command.
1 parent 5f192c4 commit e9680d7

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

src/Console/Commands/Stub.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Contracts\Config\Repository as ConfigContract;
77
use Illuminate\Filesystem\Filesystem;
8+
use Laragear\Preload\Preloader;
89
use Symfony\Component\Console\Attribute\AsCommand;
10+
use const DIRECTORY_SEPARATOR;
911

1012
/**
1113
* @internal
@@ -39,25 +41,30 @@ class Stub extends Command
3941
*/
4042
public function handle(Filesystem $file, ConfigContract $config): void
4143
{
42-
$path = $config->get('preload.path');
44+
$dir = $config->get('preload.path');
45+
$filePath = $dir . '/' . Preloader::NAME_PRELOAD;
4346

44-
if ($file->exists($path)) {
47+
$file->ensureDirectoryExists($dir);
48+
49+
if ($file->exists($filePath)) {
4550
$this->info('A preload script file already exists.');
4651

4752
return;
4853
}
4954

50-
$file->put($path, <<<'STUB'
55+
$file->put($filePath, <<<'STUB'
5156
<?php
5257
53-
\fwrite(\STDOUT, 'Info: This is a stub file to be replaced for the application at runtime.');
58+
$date = (new DateTime())->format('d-m-Y H:i:s');
59+
60+
echo "[$date] Info: This is a preload stub file to be replaced for the application at runtime.";
5461

5562
STUB
5663
);
5764

58-
$this->info("Stub copied at [$path].");
65+
$this->info("Stub copied at [$filePath].");
5966
$this->newLine();
6067
$this->comment('Remember to edit your [php.ini] file:');
61-
$this->comment("opcache.preload = $path");
68+
$this->comment("opcache.preload = $filePath");
6269
}
6370
}

src/Preloader.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
class Preloader
1111
{
1212
/**
13-
* The location of the statistic file.
13+
* The filename for the statistics file.
1414
*
1515
* @const string
1616
*/
17-
public const STUB_STATISTICS = __DIR__.'/../stubs/statistics.md';
17+
public const NAME_STATISTICS = 'statistics.md';
1818

1919
/**
20-
* The location of the preload script stub.
20+
* The filename for the preload script file.
21+
*
22+
* @const string
2123
*/
22-
public const STUB_PRELOAD = __DIR__.'/../stubs/preload.php.stub';
24+
public const NAME_PRELOAD = 'preload.php';
2325

2426
/**
25-
* The filename for the statistics file.
27+
* The location of the statistic file.
2628
*
2729
* @const string
2830
*/
29-
public const NAME_STATISTICS = 'statistics.md';
31+
public const STUB_STATISTICS = __DIR__.'/../stubs/' . self::NAME_STATISTICS;
3032

3133
/**
32-
* The filename for the preload script file.
33-
*
34-
* @const string
34+
* The location of the preload script stub.
3535
*/
36-
public const NAME_PRELOAD = 'preload.php';
36+
public const STUB_PRELOAD = __DIR__.'/../stubs/' . self::NAME_PRELOAD . '.stub';
3737

3838
/**
3939
* The filename of the list of preload files.

tests/Console/Commands/StubTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,44 @@
33
namespace Tests\Console\Commands;
44

55
use Illuminate\Filesystem\Filesystem;
6+
use Laragear\Preload\Preloader;
67
use Mockery\MockInterface;
78
use Tests\TestCase;
89

910
class StubTest extends TestCase
1011
{
1112
public function test_stores_placeholder_in_output_path(): void
1213
{
13-
$path = $this->app->basePath();
14+
$dir = $this->app->basePath();
15+
$filePath = $dir . '/' . Preloader::NAME_PRELOAD;
1416

15-
$this->mock(Filesystem::class, function (MockInterface $mock) use ($path) {
16-
$mock->expects('exists')->andReturnFalse();
17-
$mock->expects('put')->with($path, <<<'PHP'
17+
$this->mock(Filesystem::class, function (MockInterface $mock) use ($dir, $filePath) {
18+
$mock->expects('ensureDirectoryExists')->with($dir);
19+
$mock->expects('exists')->with($filePath)->andReturnFalse();
20+
$mock->expects('put')->with($filePath, <<<'PHP'
1821
<?php
1922
20-
\fwrite(\STDOUT, 'Info: This is a stub file to be replaced for the application at runtime.');
23+
$date = (new DateTime())->format('d-m-Y H:i:s');
24+
25+
echo "[$date] Info: This is a preload stub file to be replaced for the application at runtime.";
2126

2227
PHP
2328
);
2429
});
2530

2631
$command = $this->artisan('preload:stub');
2732

28-
$command->expectsOutput("Stub copied at [$path].");
33+
$command->expectsOutput("Stub copied at [$filePath].");
2934
$command->expectsOutput('Remember to edit your [php.ini] file:');
30-
$command->expectsOutput("opcache.preload = $path");
35+
$command->expectsOutput("opcache.preload = $filePath");
3136

3237
$command->assertSuccessful();
3338
}
3439

3540
public function test_doesnt_overwrite_same_placeholder(): void
3641
{
3742
$this->mock(Filesystem::class, function (MockInterface $mock) {
43+
$mock->expects('ensureDirectoryExists');
3844
$mock->expects('exists')->andReturnTrue();
3945
$mock->expects('put')->never();
4046
});

0 commit comments

Comments
 (0)