Skip to content

Instantly share code, notes, and snippets.

@sahibalejandro
Created April 13, 2015 04:45
Show Gist options
  • Save sahibalejandro/1030d43259a6fe95f79f to your computer and use it in GitHub Desktop.
Save sahibalejandro/1030d43259a6fe95f79f to your computer and use it in GitHub Desktop.
Laravel Command make:view
<?php namespace Sahib\Generators\Commands;
use File;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
/**
* Class MakeViewCommand
* @package Sahib\Generators\Commands
*/
class MakeViewCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:view';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new blade template.';
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$view = $this->argument('view');
$path = $this->viewPath($view);
$this->createDir($path);
if (File::exists($path))
{
$this->error("File {$path} already exists!");
return;
}
File::put($path, $path);
$this->info("File {$path} created.");
}
/**
* Get the view full path.
*
* @param string $view
*
* @return string
*/
public function viewPath($view)
{
$view = str_replace('.', '/', $view) . '.blade.php';
$path = "resources/views/{$view}";
return $path;
}
/**
* Create view directory if not exists.
*
* @param $path
*/
public function createDir($path)
{
$dir = dirname($path);
if (!file_exists($dir))
{
mkdir($dir, 0777, true);
}
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['view', InputArgument::REQUIRED, 'The view.'],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment