Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builder command #292

Draft
wants to merge 5 commits into
base: nightly
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions src/Console/MakeBuilderCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

namespace Fusion\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class MakeBuilderCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:builder {name}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new FusionCMS builer';


/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->info('Verifying a few things...');
$this->verify();
$this->info('Setting up your Builder...');

// Building model..
$this->createBuildingModel();
$this->createBuildingModelObserver();
$this->createBuilderModelFolder();

// Builder class..
$this->createBuilderClass();

// Builder model..
$this->createBuilderModel();

$this->info('Builder created successfully.');
}

/**
* Create Building Model.
*
* @return void
*/
protected function createBuildingModel()
{
list($path, $stub) = $this->getPath('building_model');

$name = Str::studly($this->argument('name'));
$content = File::get($stub);

File::put($path, strtr($content, [
'{name}' => $name,
]));
}

/**
* Create Building Model Observer.
*
* @return void
*/
protected function createBuildingModelObserver()
{
list($path, $stub) = $this->getPath('building_model_observer');

$name = Str::studly($this->argument('name'));
$handle = str_handle($name);
$content = File::get($stub);

File::put($path, strtr($content, [
'{name}' => $name,
'{handle}' => $handle
]));
}

/**
* Ensure building model's folder path exists.
*
* @return void
*/
protected function createBuilderModelFolder()
{
$name = Str::of($this->argument('name'))->studly()->plural()->__toString();
$path = fusion_path("src/Models/{$name}");

File::ensureDirectoryExists($path, 0755, true);

if (!File::exists("{$path}/.gitignore")) {
File::put("{$path}/.gitignore", "*\n!.gitignore");
}
}

/**
* Create Builder Class.
*
* @return void
*/
protected function createBuilderClass()
{
list($path, $stub) = $this->getPath('builder_class');

$name = Str::studly($this->argument('name'));
$handle = str_handle($name);
$content = File::get($stub);

File::put($path, strtr($content, [
'{name}' => $name,
'{handle}' => $handle
]));
}

/**
* Create Builder Model stub file.
*
* @return void
*/
protected function createBuilderModel()
{
list($path, $stub) = $this->getPath('builder_model');

$name = Str::of($this->argument('name'))->studly()->plural()->__toString();
$content = File::get($stub);

File::put($path, strtr($content, [
'{name}' => $name,
]));
}

/**
* Helper.
*
* @throws Exception
* @return void
*/
private function verify()
{
foreach ($this->getPaths() as $key => $value) {
if (File::exists($value[0])) {
throw new \Exception($value[0] . ' already exists!');
}
}
}

/**
* Helper.
*
* @param string $key
* @access private
* @return array
*/
private function getPath($key)
{
return $this->getPaths()[$key];
}

/**
* Helper.
*
* @access private
* @return array
*/
private function getPaths()
{
$modelName = Str::studly($this->argument('name'));
$stubName = Str::camel($this->argument('name'));

return [
'building_model' => [
fusion_path("/src/Models/{$modelName}.php"),
fusion_path('stubs/builders/console/buildingModel.stub'),
],
'building_model_observer' => [
fusion_path("/src/Observers/{$modelName}Observer.php"),
fusion_path('stubs/builders/console/buildingModelObserver.stub'),
],
'builder_class' => [
fusion_path("/src/Services/Builders/{$modelName}"),
fusion_path('stubs/builders/console/builderClass.stub'),
],
'builder_model' => [
$path = fusion_path("/stubs/builders/{$stubName}.stub"),
fusion_path('stubs/builders/console/builderModel.stub'),
],
];
}
}
28 changes: 28 additions & 0 deletions stubs/builders/console/builderClass.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Fusion\Services\Builders;

use Fusion\Models\{name} as Model;

class {name} extends Builder
{
/**
* New Builder instance.
*
* @param string $handle
*/
public function __construct($handle)
{
$this->source = Model::where('handle', $handle)->firstOrFail();
}

/**
* Builder table prefix.
*
* @var string
*/
public static function prefix()
{
return '{handle}';
}
}
32 changes: 32 additions & 0 deletions stubs/builders/console/builderModel.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Fusion\Models\{name};

use Fusion\Database\Eloquent\Model;

class {class} extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = '{table}';

/**
* Mass assignment protection.
*
* @var array
*/
protected $fillable = [{fillable}];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [{casts}];


{relationships}
}
13 changes: 13 additions & 0 deletions stubs/builders/console/buildingModel.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Fusion\Models;

use Fusion\Concerns\HasBuilder;
use Fusion\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class {name} extends Model
{
use HasBuilder;
use HasFactory;
}
59 changes: 59 additions & 0 deletions stubs/builders/console/buildingModelObserver.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Fusion\Observers;

use Fusion\Contracts\BuilderObserver;
use Fusion\Models\{name};
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class {name}Observer implements BuilderObserver
{
/**
* Handle the "created" event.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function created(Model $model)
{
Schema::create($model->getBuilderTable(), function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('{handle}_id');

// ..add additional columns here..

$table->timestamps();
});
}

/**
* Handle the "updating" event.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function updating(Model $model)
{
$old = {name}::find($model->id);

if ($old->getBuilderTable() !== $model->getBuilderTable()) {
Schema::rename($old->getBuilderTable(), $model->getBuilderTable());
}
}

/**
* Handle the "deleted" event.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function deleted(Model $model)
{
Schema::dropIfExists($model->getBuilderTable());
}
}