Skip to content

Commit 5a4d0e2

Browse files
authored
Add make:data-provider command. (#28)
Introduced `make:data-provider` console command. Added support for Laravel Idea.
1 parent c90bb44 commit 5a4d0e2

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

ide.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
3+
"codeGenerations": [
4+
{
5+
"id": "webfox.create-inertia-data-provider",
6+
"name": "Create Inertia Data Provider",
7+
"classSuffix": "DataProvider",
8+
"files": [
9+
{
10+
"appNamespace": "Http\\DataProviders",
11+
"name": "${INPUT_CLASS|className|upperCamelCase}.php",
12+
"template": {
13+
"type": "stub",
14+
"path": "/stubs/inertia-data-provider.stub",
15+
"fallbackPath": "stubs/inertia-data-provider.stub",
16+
"parameters": {
17+
"{{ class }}": "${INPUT_CLASS|className|upperCamelCase}",
18+
"{{ namespace }}": "${INPUT_FQN|namespace}"
19+
}
20+
}
21+
}
22+
]
23+
}
24+
]
25+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Webfox\InertiaDataProviders;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Symfony\Component\Console\Attribute\AsCommand;
7+
use Symfony\Component\Console\Input\InputOption;
8+
9+
#[AsCommand(name: 'make:data-provider')]
10+
class InertiaDataProviderMakeCommand extends GeneratorCommand
11+
{
12+
protected $name = 'make:data-provider';
13+
14+
protected $description = 'Create a new inertia data provider';
15+
16+
protected $type = 'Inertia Data Provider';
17+
18+
protected function alreadyExists($rawName)
19+
{
20+
return class_exists($rawName) ||
21+
$this->files->exists($this->getPath($this->qualifyClass($rawName)));
22+
}
23+
24+
/**
25+
* Get the stub file for the generator.
26+
*
27+
* @return string
28+
*/
29+
protected function getStub()
30+
{
31+
return $this->resolveStubPath('/stubs/inertia-data-provider.stub');
32+
}
33+
34+
/**
35+
* Resolve the fully-qualified path to the stub.
36+
*
37+
* @param string $stub
38+
* @return string
39+
*/
40+
protected function resolveStubPath($stub)
41+
{
42+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
43+
? $customPath
44+
: realpath(__DIR__ . '/..' . $stub);
45+
}
46+
47+
/**
48+
* Get the default namespace for the class.
49+
*
50+
* @param string $rootNamespace
51+
* @return string
52+
*/
53+
protected function getDefaultNamespace($rootNamespace)
54+
{
55+
return $rootNamespace.'\Http\DataProviders';
56+
}
57+
58+
/**
59+
* Get the console command options.
60+
*
61+
* @return array
62+
*/
63+
protected function getOptions()
64+
{
65+
return [
66+
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the data provider already exists'],
67+
];
68+
}
69+
}

src/InertiaDataProvidersServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ public function configurePackage(Package $package): void
1212
$package
1313
->name('laravel-inertia-dataproviders')
1414
->hasConfigFile('inertia-dataproviders');
15+
16+
$package->hasConsoleCommand(InertiaDataProviderMakeCommand::class);
17+
1518
}
1619
}

stubs/inertia-data-provider.stub

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Webfox\InertiaDataProviders\DataProvider;
6+
7+
class {{ class }} extends DataProvider
8+
{
9+
10+
public function __construct()
11+
{
12+
$this->staticData = [
13+
];
14+
}
15+
16+
}

0 commit comments

Comments
 (0)