Skip to content

Commit 2c83165

Browse files
Merge pull request #123 from TheDragonCode/4.x
Added `actions:stub` console command
2 parents 93dd798 + 965be74 commit 2c83165

File tree

13 files changed

+238
-13
lines changed

13 files changed

+238
-13
lines changed

docs/getting-started/installation/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Installation
22

3-
To get the latest version of Laravel Actions, simply require the project using [Composer](https://getcomposer.org):
3+
To get the latest version of `Laravel Actions`, simply require the project using [Composer](https://getcomposer.org):
44

55
```bash
66
composer require dragon-code/laravel-actions
@@ -11,7 +11,7 @@ Or manually update `require` block of `composer.json` and run `composer update`
1111
```json
1212
{
1313
"require": {
14-
"dragon-code/laravel-actions": "^3.0"
14+
"dragon-code/laravel-actions": "^4.0"
1515
}
1616
}
1717
```

docs/helpers/execution-status.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ You can also override the `success` and `failed` methods, which are called on su
55
## If Success
66

77
```php
8-
use DragonCode\LaravelActions\Action;use Illuminate\Support\Facade\Log;
8+
use DragonCode\LaravelActions\Action;
9+
use Illuminate\Support\Facade\Log;
910

1011
return new class () extends Action
1112
{

docs/how-to-use/customize-stub.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Customize Stub
2+
3+
In order to use your action template, you first need to publish it using the console command:
4+
5+
```
6+
php artisan actions:stub
7+
```
8+
9+
As a result, the file `stubs/action.stub` will be created in the root of the project, which you can change for yourself.

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
* Prologue
1717
* [Upgrade Guide](prologue/upgrade-guide/index.md)
18-
* [To 4.x from 3.x](prologue/upgrade-guide/4.x.md)
19-
* [To 3.x from 2.x](prologue/upgrade-guide/3.x.md)
18+
* [To 4.x from 3.x](prologue/upgrade-guide/4.x.md)
19+
* [To 3.x from 2.x](prologue/upgrade-guide/3.x.md)
2020
* Getting Started
2121
* [Installation](getting-started/installation/index.md)
2222
* How to use

src/Console/StubPublish.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelActions\Console;
6+
7+
use DragonCode\LaravelActions\Constants\Names;
8+
use DragonCode\LaravelActions\Constants\Options;
9+
use DragonCode\LaravelActions\Processors\Processor;
10+
use DragonCode\LaravelActions\Processors\StubPublish as StubProcessor;
11+
12+
class StubPublish extends Command
13+
{
14+
protected $name = Names::STUB_PUBLISH;
15+
16+
protected $description = 'Publish stub that are available for customization';
17+
18+
protected Processor|string $processor = StubProcessor::class;
19+
20+
protected bool $secure = false;
21+
22+
protected array $options = [
23+
Options::FORCE,
24+
];
25+
}

src/Constants/Names.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
class Names
66
{
7+
public const ACTIONS = 'actions';
8+
79
public const FRESH = 'actions:fresh';
810

911
public const INSTALL = 'actions:install';
1012

1113
public const MAKE = 'make:action';
1214

13-
public const ACTIONS = 'actions';
14-
1515
public const REFRESH = 'actions:refresh';
1616

1717
public const RESET = 'actions:reset';
@@ -20,5 +20,7 @@ class Names
2020

2121
public const STATUS = 'actions:status';
2222

23+
public const STUB_PUBLISH = 'actions:stub';
24+
2325
public const UPGRADE = 'actions:upgrade';
2426
}

src/Processors/Make.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Make extends Processor
1212
{
1313
protected string $fallback = 'auto';
1414

15-
protected string $stub = __DIR__ . '/../../resources/stubs/action.stub';
15+
protected string $defaultStub = __DIR__ . '/../../resources/stubs/action.stub';
1616

1717
public function handle(): void
1818
{
@@ -29,7 +29,7 @@ protected function run(): void
2929

3030
protected function create(string $path): void
3131
{
32-
File::copy($this->stub, $path);
32+
File::copy($this->stubPath(), $path);
3333
}
3434

3535
protected function getName(): string
@@ -61,4 +61,13 @@ protected function getTime(): string
6161
{
6262
return date('Y_m_d_His_');
6363
}
64+
65+
protected function stubPath(): string
66+
{
67+
if ($path = realpath(base_path('stubs/action.stub'))) {
68+
return $path;
69+
}
70+
71+
return $this->defaultStub;
72+
}
6473
}

src/Processors/StubPublish.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelActions\Processors;
6+
7+
use DragonCode\Support\Facades\Filesystem\File;
8+
9+
class StubPublish extends Processor
10+
{
11+
protected string $stub = __DIR__ . '/../../resources/stubs/action.stub';
12+
13+
public function handle(): void
14+
{
15+
$this->allow()
16+
? $this->notification->task('Publishing', fn () => $this->publish())
17+
: $this->notification->info('Nothing to publish');
18+
}
19+
20+
protected function publish(): void
21+
{
22+
File::copy($this->stub, $this->path());
23+
}
24+
25+
protected function allow(): bool
26+
{
27+
return $this->options->force || $this->doesntExist();
28+
}
29+
30+
protected function doesntExist(): bool
31+
{
32+
return ! File::exists($this->path());
33+
}
34+
35+
protected function path(): string
36+
{
37+
return base_path('stubs/action.stub');
38+
}
39+
}

src/ServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ public function register(): void
3737
protected function registerCommands(): void
3838
{
3939
$this->commands([
40+
Console\Actions::class,
4041
Console\Fresh::class,
4142
Console\Install::class,
4243
Console\Make::class,
43-
Console\Actions::class,
4444
Console\Refresh::class,
4545
Console\Reset::class,
4646
Console\Rollback::class,
4747
Console\Status::class,
48+
Console\StubPublish::class,
4849
Console\Upgrade::class,
4950
]);
5051
}

tests/Commands/MakeTest.php

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

55
use DragonCode\LaravelActions\Constants\Names;
6+
use DragonCode\Support\Facades\Filesystem\File;
67
use Tests\TestCase;
78

89
class MakeTest extends TestCase
@@ -107,4 +108,34 @@ public function testNestedLeftSlashWithExtension()
107108
file_get_contents($path)
108109
);
109110
}
111+
112+
public function testFromCustomizedStub()
113+
{
114+
$name = 'MakeExample';
115+
116+
$stubPath = base_path('stubs/action.stub');
117+
118+
$actionPath = $this->getActionsPath() . '/' . date('Y_m_d_His') . '_make_example.php';
119+
120+
$this->assertFileDoesNotExist($stubPath);
121+
122+
File::copy(__DIR__ . '/../fixtures/app/stubs/customized.stub', $stubPath);
123+
124+
$this->assertFileExists($stubPath);
125+
$this->assertFileDoesNotExist($actionPath);
126+
127+
$this->artisan(Names::MAKE, compact('name'))->assertExitCode(0);
128+
129+
$this->assertFileExists($actionPath);
130+
131+
$content = file_get_contents($actionPath);
132+
133+
$this->assertStringContainsString('Foo\\Bar\\Some', $content);
134+
$this->assertStringContainsString('extends Some', $content);
135+
136+
$this->assertStringNotContainsString('DragonCode\\LaravelActions\\Action', $content);
137+
$this->assertStringNotContainsString('extends Action', $content);
138+
$this->assertStringNotContainsString('Run the actions.', $content);
139+
$this->assertStringNotContainsString('@return void', $content);
140+
}
110141
}

0 commit comments

Comments
 (0)