Skip to content

Commit

Permalink
feat: #3 add command to create new modules on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
m-triassi committed Nov 20, 2023
1 parent 3f36bda commit 9ac70c5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Commands/ContentMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Plank\Contentable\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Facades\File;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'make:content')]
class ContentMakeCommand extends GeneratorCommand
{
protected $signature = "make:content {name}";

protected $description = "Create a new content model class to act as a receptacle for content";

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Model';

public function handle()
{
if (!is_dir(app_path('Models/Content')) && $this->confirm('Would you like to use App\\Models\\Content as the name space for all generated content?')) {
File::makeDirectory(app_path('Models/Content'));
}

parent::handle();
}

protected function getStub()
{
return $this->resolveStubPath("/stubs/content.php.stub");
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return is_dir(app_path('Models/Content')) ? $rootNamespace.'\\Models\\Content' : $rootNamespace.'\\Models';
}

}
17 changes: 17 additions & 0 deletions src/Commands/stubs/content.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Plank\Contentable\Concerns\CanRender;
use Plank\Contentable\Models\Module;

class {{ class }} extends Module
{
use HasFactory;

public function renderHtml() {
// TODO: Replace generated stubs
return "";
}
}

0 comments on commit 9ac70c5

Please sign in to comment.