Skip to content

Commit 3f18123

Browse files
committed
Introduced make:data-provider console command.
Added support for Laravel Idea.
1 parent c90bb44 commit 3f18123

File tree

4 files changed

+114
-0
lines changed

4 files changed

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

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)