Skip to content

Commit 4db105c

Browse files
committed
feat: add BuilderMakeCommand to generate builder classes
1 parent 5732896 commit 4db105c

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace MrPunyapal\LaravelExtendedCommands\Commands;
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:builder')]
10+
class BuilderMakeCommand extends GeneratorCommand
11+
{
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'make:builder';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Create a new builder class';
25+
26+
/**
27+
* The type of class being generated.
28+
*
29+
* @var string
30+
*/
31+
protected $type = 'Builder';
32+
33+
/**
34+
* Determine if the class already exists.
35+
*
36+
* @param string $rawName
37+
* @return bool
38+
*/
39+
protected function alreadyExists($rawName)
40+
{
41+
return class_exists($rawName) ||
42+
$this->files->exists($this->getPath($this->qualifyClass($rawName)));
43+
}
44+
45+
/**
46+
* Get the stub file for the generator.
47+
*
48+
* @return string
49+
*/
50+
protected function getStub()
51+
{
52+
return $this->resolveStubPath('/../../stubs/builder.stub');
53+
}
54+
55+
/**
56+
* Resolve the fully-qualified path to the stub.
57+
*
58+
* @param string $stub
59+
* @return string
60+
*/
61+
protected function resolveStubPath($stub)
62+
{
63+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
64+
? $customPath
65+
: __DIR__.$stub;
66+
}
67+
68+
/**
69+
* Get the default namespace for the class.
70+
*
71+
* @param string $rootNamespace
72+
* @return string
73+
*/
74+
protected function getDefaultNamespace($rootNamespace)
75+
{
76+
return $rootNamespace.'\Models\Builders';
77+
}
78+
79+
/**
80+
* Build the class with the given name.
81+
*
82+
* @param string $name
83+
* @return string
84+
*
85+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
86+
*/
87+
protected function buildClass($name)
88+
{
89+
$replace = $this->buildModelReplacements();
90+
91+
return str_replace(
92+
array_keys($replace), array_values($replace), parent::buildClass($name)
93+
);
94+
}
95+
96+
/**
97+
* Build the replacements for a model.
98+
*
99+
* @return array<string, string>
100+
*/
101+
protected function buildModelReplacements()
102+
{
103+
$replacements = [];
104+
105+
if ($this->option('model')) {
106+
$modelNamespace = $this->option('model');
107+
108+
$replacements['{{ modelPhpdoc }}'] = <<<EOT
109+
/**
110+
* @template TModel of $modelNamespace
111+
*
112+
* @extends Builder<$modelNamespace>
113+
*/
114+
EOT;
115+
} else {
116+
$replacements["{{ modelPhpdoc }}\n"] = '';
117+
$replacements["{{ modelPhpdoc }}\r\n"] = '';
118+
}
119+
120+
return $replacements;
121+
}
122+
123+
/**
124+
* Get the console command options.
125+
*
126+
* @return array
127+
*/
128+
protected function getOptions()
129+
{
130+
return [
131+
['model', 'm', InputOption::VALUE_REQUIRED, 'The model that the builder applies to'],
132+
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the event already exists'],
133+
];
134+
}
135+
}

src/LaravelExtendedCommandsServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MrPunyapal\LaravelExtendedCommands;
44

5+
use MrPunyapal\LaravelExtendedCommands\Commands\BuilderMakeCommand;
56
use Spatie\LaravelPackageTools\Package;
67
use Spatie\LaravelPackageTools\PackageServiceProvider;
78

@@ -16,6 +17,7 @@ public function configurePackage(Package $package): void
1617
*/
1718
$package
1819
->name('laravel-extended-commands')
19-
->hasConfigFile();
20+
->hasConfigFile()
21+
->hasCommand(BuilderMakeCommand::class);
2022
}
2123
}

stubs/builder.stub

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
7+
{{ modelPhpdoc }}
8+
class {{ class }} extends Builder
9+
{
10+
//
11+
}

0 commit comments

Comments
 (0)