Skip to content

Commit

Permalink
Added new suffix arg; cleaned up some main cmd logic; updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminhansen committed Aug 28, 2024
1 parent 710445a commit ac10317
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 41 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Open <code>app/Console/Kernel.php</code> and add <code>BenjaminHansen\LaravelMak

## Usage

<code>php artisan make:view view.name --extends=layouts.app --bootstrap=bs-version --empty --resourceful</code>
<code>php artisan make:view view.name --extends=layouts.app --uses=bootstrap4/bootstrap5 --empty --resourceful --suffix=something.php</code>

- <code>resourceful</code> used to create a view directory from <code>view.name</code> and then resourceful view files <code>index.blade.php</code>, <code>create.blade.php</code>, <code>show.blade.php</code>, and <code>edit.blade.php</code>

Expand All @@ -27,3 +27,5 @@ Open <code>app/Console/Kernel.php</code> and add <code>BenjaminHansen\LaravelMak

- <code>empty</code> option is optional. Creates an empty view file with no layout extension.
- When using the <code>empty</code> option all other options are ignored.

- <code>suffix</code> is optional if you want to override the default blade.php file suffix with something else.
37 changes: 14 additions & 23 deletions src/MakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@

class MakeView extends Command
{
protected $deprecated_bootstrap_versions = ['v3'];

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = "make:view {viewname} {--e|extends=} {--bs|bootstrap=} {--E|empty} {--r|resourceful}";
protected $signature = "make:view {viewname} {--extends=} {--uses=} {--empty} {--resourceful} {--suffix=}";

/**
* The console command description.
*
* @var string
*/
protected $description = 'Make a new Blade View with configurable options';
protected $description = 'Make a new Blade View with configurable options.';

/**
* Create a new command instance.
Expand All @@ -41,19 +39,20 @@ public function handle()
{
$viewname = $this->argument('viewname');
$extends = $this->option('extends') ?? env('BASE_VIEW');
$bootstrap = $this->option('bootstrap');
$uses = $this->option('uses');
$empty = $this->option('empty');
$resourceful = $this->option('resourceful');
$suffix = $this->option('suffix') ?? 'blade.php';

$view_path = base_path('resources/views');

// handle the actual file creation for the given blade view
if(str_contains($viewname, '.')) {
$parts = explode(".", $viewname);

if($resourceful) {
// we should create a view folder and resourceful view files inside (index, create, show, edit)
$resource_files = ['index.blade.php', 'create.blade.php', 'show.blade.php', 'edit.blade.php'];

$parts = explode(".", $viewname);
$resource_files = ["index.{$suffix}", "create.{$suffix}", "show.{$suffix}", "edit.{$suffix}"];

foreach($parts as $folder) {
$folder = strtolower($folder); // lowercase all folder names
Expand Down Expand Up @@ -91,12 +90,9 @@ public function handle()

return;
} else {
// we are dealing with at least one folder (the string includes a ".")
$parts = explode(".", $viewname);

// get the last element of the array, which is our blade view file
$blade_template = strtolower(end($parts));
$blade_file = "{$blade_template}.blade.php";
$blade_file = "{$blade_template}.{$suffix}";

// remove the last element from the array since it is our filename
array_pop($parts);
Expand Down Expand Up @@ -125,7 +121,7 @@ public function handle()
} else {
if($resourceful) {
// we should create a view folder and resourceful view files inside (index, create, show, edit)
$resource_files = ['index.blade.php', 'create.blade.php', 'show.blade.php', 'edit.blade.php'];
$resource_files = ["index.{$suffix}", "create.{$suffix}", "show.{$suffix}", "edit.{$suffix}"];

$view_path .= "/{$viewname}";

Expand Down Expand Up @@ -159,7 +155,7 @@ public function handle()
return;
} else {
// we are dealing with a single/top-level blade file
$blade_file = "{$viewname}.blade.php";
$blade_file = "{$viewname}.{$suffix}";
$full_view_path = "{$view_path}/{$blade_file}";
if(!file_exists($full_view_path)) {
touch($full_view_path);
Expand All @@ -176,20 +172,15 @@ public function handle()
return;
}

// handle any extends or bootstrap logic
// handle any extends or uses logic
if($viewname == $extends) {
// we are creating a layout/masterpage, get the requested template and then bail out
$html = match($bootstrap) {
"v3" => file_get_contents(__DIR__."/shells/bootstrap3.txt"),
"v4" => file_get_contents(__DIR__."/shells/bootstrap4.txt"),
"v5" => file_get_contents(__DIR__."/shells/bootstrap5.txt"),
$html = match($uses) {
"bootstrap4" => file_get_contents(__DIR__."/shells/bootstrap4.txt"),
"bootstrap5" => file_get_contents(__DIR__."/shells/bootstrap5.txt"),
default => file_get_contents(__DIR__."/shells/raw.txt")
};

if($bootstrap && in_array($bootstrap, $this->deprecated_bootstrap_versions)) {
$this->warn("Bootstrap {$bootstrap} is deprecated, and will be removed soon. Please switch to a newer version as soon as possible.");
}

file_put_contents($full_view_path, $html);

$this->info("Layout view [$viewname] created");
Expand Down
17 changes: 0 additions & 17 deletions src/shells/bootstrap3.txt

This file was deleted.

0 comments on commit ac10317

Please sign in to comment.