Skip to content

Commit

Permalink
Added resourceful view creation
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminhansen committed Mar 25, 2024
1 parent 48657bc commit f6f94b6
Showing 1 changed file with 64 additions and 26 deletions.
90 changes: 64 additions & 26 deletions src/MakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MakeView extends Command
*
* @var string
*/
protected $signature = "make:view {viewname} {--extends=} {--bootstrap=} {--empty}";
protected $signature = "make:view {viewname} {--extends=} {--bootstrap=} {--empty} {--resourceful}";

/**
* The console command description.
Expand Down Expand Up @@ -40,36 +40,74 @@ public function handle()
$extends = $this->option('extends') ?? env('BASE_VIEW');
$bootstrap = $this->option('bootstrap');
$empty = $this->option('empty');
$resourceful = $this->option('resourceful');

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

// handle the actual file creation for the given blade view
if(str_contains($viewname, '.')) {
// we are dealing with at least one folder (the string includes a ".")
$parts = explode(".", $viewname);
$count = count($parts);

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

// loop over the entire array, except for the last element (which is the actual file)
// and create the necessary directories
for($i = 0; $i < $count-1; $i++) {
$folder = $parts[$i];
$view_path .= "/{$folder}";

if(!file_exists($view_path)) {
mkdir($view_path);
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);
$count = count($parts);

for($i = 0; $i < $count; $i++) {
$folder = $parts[$i];
$view_path .= "/{$folder}";

if(!file_exists($view_path)) {
mkdir($view_path);
}
}

foreach($resource_files as $file) {
$file_view_path = "{$view_path}/{$file}";
touch($file_view_path);

if($extends) {
$content = file_get_contents(__DIR__."/shells/extends.txt");
$content = str_replace("{{BASE_VIEW}}", $extends, $content);

file_put_contents($file_view_path, $content);
}
}

if($extends) {
$this->info("Resourceful child views created at [$viewname]");
} else {
$this->info("Resourceful views created at [$viewname]");
}
}

$full_view_path = "{$view_path}/{$blade_file}";
if(!file_exists($full_view_path)) {
touch($full_view_path);
} else {
$this->error("View [$viewname] already exists!");
return;
} else {
// we are dealing with at least one folder (the string includes a ".")
$parts = explode(".", $viewname);
$count = count($parts);

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

// loop over the entire array, except for the last element (which is the actual file)
// and create the necessary directories
for($i = 0; $i < $count-1; $i++) {
$folder = $parts[$i];
$view_path .= "/{$folder}";

if(!file_exists($view_path)) {
mkdir($view_path);
}
}

$full_view_path = "{$view_path}/{$blade_file}";
if(!file_exists($full_view_path)) {
touch($full_view_path);
} else {
$this->error("View [$viewname] already exists!");
return;
}
}
} else {
// we are dealing with a single/top-level blade file
Expand All @@ -85,7 +123,7 @@ public function handle()

if($empty || !$extends) {
// if we are creating an empty view file, bail out here
$this->info("Empty view [$viewname] created successfully!");
$this->info("Empty view [$viewname] created");
return;
}

Expand All @@ -108,7 +146,7 @@ public function handle()

file_put_contents($full_view_path, $html);

$this->info("Layout view [$viewname] created successfully!");
$this->info("Layout view [$viewname] created");

return;
}
Expand All @@ -119,6 +157,6 @@ public function handle()

file_put_contents($full_view_path, $content);

$this->info("Child view [$viewname] created successfully!");
$this->info("Child view [$viewname] created");
}
}

0 comments on commit f6f94b6

Please sign in to comment.