Skip to content

Instantly share code, notes, and snippets.

@fouedmoussi
Last active January 5, 2020 11:14
Show Gist options
  • Save fouedmoussi/b926f071910733332c3354817bd2abaa to your computer and use it in GitHub Desktop.
Save fouedmoussi/b926f071910733332c3354817bd2abaa to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class GenerateView extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:view {name : The name of the layout} {--D|dir=default : the directory}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'generate a new view';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$fileName = $this->argument('name');
if(! $this->endsWith($this->argument('name'), '.blade.php')) {
$fileName .= '.blade.php';
}
$dir = $this->option('dir');
$filePath = ($dir && $dir != 'default') ? $dir : '';
if($filePath) {
$fileName = $dir . '/' . $fileName;
if (!file_exists(resource_path('views/' . $filePath))) {
mkdir(resource_path('views/' . $filePath), 0777, true);
}
}
if(!is_file(resource_path('views/' . $fileName))) {
file_put_contents(resource_path('views/' . $fileName), "//Your HTML code goes here");
$this->info('View created successfully');
return;
}
$this->error('View with same name already exists!');
}
/**
* Function to check the string is ends with given substring or not
*
* @return boolean
*/
public function endsWith($string, $endString)
{
$len = strlen($endString);
return (substr($string, -$len) === $endString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment