Skip to content

Commit 8c52332

Browse files
Merge branch 'feat/make-command' into feat/custom-states
2 parents eb45c32 + 1db20fc commit 8c52332

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Farbcode\StatefulResources\Console\Commands;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Input\InputOption;
7+
8+
#[AsCommand(name: 'make:stateful-resource')]
9+
class StatefulResourceMakeCommand extends \Illuminate\Console\GeneratorCommand
10+
{
11+
/**
12+
* The console command name.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'make:stateful-resource';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Create a new stateful resource';
24+
25+
/**
26+
* The type of class being generated.
27+
*
28+
* @var string
29+
*/
30+
protected $type = 'Stateful Resource';
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return void
36+
*/
37+
public function handle()
38+
{
39+
parent::handle();
40+
}
41+
42+
/**
43+
* Get the stub file for the generator.
44+
*
45+
* @return string
46+
*/
47+
protected function getStub()
48+
{
49+
return $this->resolveStubPath('/stubs/stateful-resource.stub');
50+
}
51+
52+
/**
53+
* Resolve the fully-qualified path to the stub.
54+
*
55+
* @param string $stub
56+
* @return string
57+
*/
58+
protected function resolveStubPath($stub)
59+
{
60+
return __DIR__.$stub;
61+
}
62+
63+
/**
64+
* Get the default namespace for the class.
65+
*
66+
* @param string $rootNamespace
67+
* @return string
68+
*/
69+
protected function getDefaultNamespace($rootNamespace)
70+
{
71+
return $rootNamespace.'\Http\Resources';
72+
}
73+
74+
/**
75+
* Get the console command options.
76+
*
77+
* @return array
78+
*/
79+
protected function getOptions()
80+
{
81+
return [
82+
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the resource already exists'],
83+
];
84+
}
85+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Http\Request;
6+
use Farbcode\StatefulResources\StatefulJsonResource;
7+
8+
class {{ class }} extends StatefulJsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return parent::toArray($request);
18+
}
19+
}

src/StatefulResourcesServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function configurePackage(Package $package): void
1717
*/
1818
$package
1919
->name('stateful-resources')
20+
->hasCommands([
21+
Console\Commands\StatefulResourceMakeCommand::class,
22+
])
2023
->hasConfigFile();
2124
}
2225

tests/Feature/MakeCommandTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Farbcode\StatefulResources\Tests\TestCase;
4+
use Illuminate\Support\Facades\File;
5+
6+
beforeEach(function () {
7+
$this->testResourcePath = app_path('Http/Resources/TestResource.php');
8+
});
9+
10+
afterEach(function () {
11+
if (File::exists($this->testResourcePath)) {
12+
File::delete($this->testResourcePath);
13+
}
14+
});
15+
16+
it('can create a new stateful resource', function () {
17+
/** @var TestCase $this */
18+
$this->artisan('make:stateful-resource', [
19+
'name' => 'TestResource',
20+
])
21+
->assertExitCode(0)
22+
->expectsOutputToContain('created successfully.');
23+
24+
$this->assertFileExists(app_path('Http/Resources/TestResource.php'));
25+
});

workbench/app/Models/Cat.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Workbench\App\Models;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
/**
@@ -15,5 +16,12 @@
1516
*/
1617
class Cat extends Model
1718
{
19+
use HasFactory;
20+
21+
/**
22+
* The attributes that are mass assignable.
23+
*
24+
* @var array<string>
25+
*/
1826
protected $fillable = ['name', 'breed', 'fluffyness', 'color'];
1927
}

0 commit comments

Comments
 (0)