First, you should take a look at how it's currently done for models or controllers. Most of these make commands are in Illuminate\Foundation\Console namespace and are called *MakeCommand.
If you wan't, you can learn about Symfony Console component, I think Jeffrey has a video about it. It might help you understand the commands better.
It would be best if you could utilize existing Laravel commands, but I don't think that would work for you, because apart from creating a class you'd need to fill some methods with code.
The best option, I think, is to extend the GeneratorCommand class(like eg. ModelMakeCommand do) for each task of the process you mentioned and then call all of them from one wrapper command.
You'll need stubs
Here's an example controller.plain.stub:
<?php
namespace DummyNamespace;
use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\Controller;
class DummyClass extends Controller
{
//
}
Notice the Dummy* keywords. These are replaced with the relevant content by the generator command.
Let's assume you want to create a controller with the index method filled. Here's how the stub would look like:
<?php
namespace DummyNamespace;
use DummyModelNamespace\DummyModel;
use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\Controller;
class DummyClass extends Controller
{
public function index()
{
$DummyTableName = DummyModel::orderBy('id', 'desc')->paginate(5);
return view('DummyTableName/index', compact('DummyTableName'));
}
}
Then, you'll need to redefine the buildClass method inherited from GeneratorCommand:
protected function buildClass($name)
{
$controllerNamespace = $this->getNamespace($name);
$replace = [
'DummyModelNamespace' => $this->modelNamespace,
'DummyModel' => $this->modelName,
'DummyTableName' => $this->tableName,
"use {$controllerNamespace}\Controller;\n" => '',
];
return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);
}
- DummyModelNamespace. If the model you created is eg.
App\Forum\Post, it would beApp\Forum - DummyModel would be then just
Post - DummyTableName will serve as view directory name and the variable name. If model is called
Post, it would beposts. For this you can utilize theModel::getTable()method.
Last replacement is a cosmetic procedure to remove the use App\Http\Controllers\Controller; statement if the new controller is in the App\Http\Controllers namespace.
This method first calls the parent::buildClass() which refers to GeneratorCommand::buildClass and replaces DummyClass, DummyNamespace and DummyRootNamespace and then replaces the rest of Dummy keywords in the output of that call.
You'll probably follow similar procedure for all other commands and then join them in one wrapping command like this:
$this->call('make:foo', $fooArguments);
$this->call('make:bar', $barArguments);
$this->call('make:baz', $bazArguments);