Skip to content

Commit a123675

Browse files
committed
Revised and renamed artisan command
1 parent 95d9221 commit a123675

File tree

7 files changed

+252
-184
lines changed

7 files changed

+252
-184
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ the following line into the `providers` array:
130130
After the package is correctly installed the easiest way to get started is to
131131
run the provided generator:
132132

133-
php artisan baum:install MODEL
133+
```
134+
php artisan make:baum {model_name}
135+
```
134136

135137
Replace model by the class name you plan to use for your Nested Set model.
136138

src/Baum/Console/InstallCommand.php

-140
This file was deleted.

src/Baum/Console/ModelMakeCommand.php

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
namespace Baum\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
use Symfony\Component\Console\Input\InputOption;
8+
9+
class ModelMakeCommand extends GeneratorCommand
10+
{
11+
/**
12+
* The console command name.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'make:baum';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Create a new Baum model class';
24+
25+
/**
26+
* The type of class being generated.
27+
*
28+
* @var string
29+
*/
30+
protected $type = 'Model';
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return void
36+
*/
37+
public function handle()
38+
{
39+
if (parent::handle() === false && ! $this->option('force')) {
40+
return false;
41+
}
42+
43+
if ($this->option('all')) {
44+
$this->input->setOption('factory', true);
45+
$this->input->setOption('seed', true);
46+
$this->input->setOption('migration', true);
47+
$this->input->setOption('controller', true);
48+
$this->input->setOption('resource', true);
49+
}
50+
51+
if ($this->option('factory')) {
52+
$this->createFactory();
53+
}
54+
55+
if ($this->option('migration')) {
56+
$this->createMigration();
57+
}
58+
59+
if ($this->option('seed')) {
60+
$this->createSeeder();
61+
}
62+
63+
if ($this->option('controller') || $this->option('resource') || $this->option('api')) {
64+
$this->createController();
65+
}
66+
}
67+
68+
/**
69+
* Create a model factory for the model.
70+
*
71+
* @return void
72+
*/
73+
protected function createFactory()
74+
{
75+
$factory = Str::studly(class_basename($this->argument('name')));
76+
77+
$this->call('make:factory', [
78+
'name' => "{$factory}Factory",
79+
'--model' => $this->qualifyClass($this->getNameInput()),
80+
]);
81+
}
82+
83+
/**
84+
* Create a migration file for the model.
85+
*
86+
* @return void
87+
*/
88+
protected function createMigration()
89+
{
90+
$table = Str::snake(Str::pluralStudly(class_basename($this->argument('name'))));
91+
92+
// if ($this->option('pivot')) {
93+
// $table = Str::singular($table);
94+
// }
95+
96+
$this->call('make:migration', [
97+
'name' => "create_{$table}_table",
98+
'--create' => $table,
99+
]);
100+
}
101+
102+
/**
103+
* Create a seeder file for the model.
104+
*
105+
* @return void
106+
*/
107+
protected function createSeeder()
108+
{
109+
$seeder = Str::studly(class_basename($this->argument('name')));
110+
111+
$this->call('make:seed', [
112+
'name' => "{$seeder}Seeder",
113+
]);
114+
}
115+
116+
/**
117+
* Create a controller for the model.
118+
*
119+
* @return void
120+
*/
121+
protected function createController()
122+
{
123+
$controller = Str::studly(class_basename($this->argument('name')));
124+
125+
$modelName = $this->qualifyClass($this->getNameInput());
126+
127+
$this->call('make:controller', array_filter([
128+
'name' => "{$controller}Controller",
129+
'--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
130+
'--api' => $this->option('api'),
131+
]));
132+
}
133+
134+
/**
135+
* Get the stub file for the generator.
136+
*
137+
* @return string
138+
*/
139+
protected function getStub()
140+
{
141+
// return $this->option('pivot')
142+
// ? $this->resolveStubPath('/stubs/model.pivot.stub')
143+
// : $this->resolveStubPath('/stubs/baum.stub');
144+
return $this->resolveStubPath('/stubs/baum.stub');
145+
}
146+
147+
/**
148+
* Resolve the fully-qualified path to the stub.
149+
*
150+
* @param string $stub
151+
* @return string
152+
*/
153+
protected function resolveStubPath($stub)
154+
{
155+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
156+
? $customPath
157+
: __DIR__.$stub;
158+
}
159+
160+
/**
161+
* Get the console command options.
162+
*
163+
* @return array
164+
*/
165+
protected function getOptions()
166+
{
167+
return [
168+
['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, and resource controller for the model'],
169+
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'],
170+
['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'],
171+
['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists'],
172+
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model'],
173+
['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder file for the model'],
174+
// ['pivot', 'p', InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom intermediate table model'],
175+
['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'],
176+
['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an API controller'],
177+
];
178+
}
179+
}

src/Baum/Console/stubs/baum.stub

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Baum\Node;
6+
7+
class {{ class }} extends Node
8+
{
9+
// protected $fillable = ['name'];
10+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class {{class}} extends Migration {
7+
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up() {
14+
Schema::create('{{table}}', function(Blueprint $table) {
15+
// These columns are needed for Baum's Nested Set implementation to work.
16+
// Column names may be changed, but they *must* all exist and be modified
17+
// in the model.
18+
// Take a look at the model scaffold comments for details.
19+
// We add indexes on parent_id, lft, rgt columns by default.
20+
$table->increments('id');
21+
$table->integer('parent_id')->nullable()->index();
22+
$table->integer('lft')->nullable()->index();
23+
$table->integer('rgt')->nullable()->index();
24+
$table->integer('depth')->nullable();
25+
26+
// Add needed columns here (f.ex: name, slug, path, etc.)
27+
// $table->string('name', 255);
28+
29+
$table->timestamps();
30+
});
31+
}
32+
33+
/**
34+
* Reverse the migrations.
35+
*
36+
* @return void
37+
*/
38+
public function down() {
39+
Schema::drop('{{table}}');
40+
}
41+
42+
}

0 commit comments

Comments
 (0)