212 lines
4.6 KiB
PHP
212 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use DateTime;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Filesystem\Filesystem;
|
|
use Illuminate\Support\Composer;
|
|
use Illuminate\Support\Str;
|
|
use InvalidArgumentException;
|
|
|
|
class SeederMakeCommand extends Command
|
|
{
|
|
/**
|
|
* The console command signature.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'make:seeder {name : The name of the seeder}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Create a new seeder file';
|
|
|
|
/**
|
|
* The filesystem instance.
|
|
*
|
|
* @var \Illuminate\Filesystem\Filesystem
|
|
*/
|
|
protected $files;
|
|
|
|
/**
|
|
* The Composer instance.
|
|
*
|
|
* @var \Illuminate\Support\Composer
|
|
*/
|
|
protected $composer;
|
|
|
|
/**
|
|
* Create a new seeder make command instance.
|
|
*
|
|
* @param \Illuminate\Filesystem\Filesystem $files
|
|
* @param \Illuminate\Support\Composer $composer
|
|
* @return void
|
|
*/
|
|
public function __construct(Filesystem $files, Composer $composer)
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->files = $files;
|
|
$this->composer = $composer;
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$name = Str::snake(trim($this->input->getArgument('name')));
|
|
|
|
$this->writeSeeder($name);
|
|
|
|
$this->composer->dumpAutoloads();
|
|
}
|
|
|
|
/**
|
|
* Write the seeder file to disk.
|
|
*
|
|
* @param string $name
|
|
* @return string
|
|
*/
|
|
protected function writeSeeder($name)
|
|
{
|
|
$file = $this->create(
|
|
$name, $this->getSeederPath()
|
|
);
|
|
|
|
$file = pathinfo($file, PATHINFO_FILENAME);
|
|
|
|
$this->line("<info>Created Seeder:</info> {$file}");
|
|
}
|
|
|
|
/**
|
|
* Get seeder path.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function getSeederPath()
|
|
{
|
|
return $this->laravel->databasePath().DIRECTORY_SEPARATOR.'seeders';
|
|
}
|
|
|
|
/**
|
|
* Create a new seeder at the given path.
|
|
*
|
|
* @param string $name
|
|
* @param string $path
|
|
* @return string
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function create($name, $path)
|
|
{
|
|
$dateTime = new DateTime();
|
|
$className = $this->getClassName($dateTime, $name);
|
|
|
|
$this->ensureSeederDoesntAlreadyExist($className, $path);
|
|
|
|
$stub = $this->getStub();
|
|
$path = $this->getPath($dateTime, $name, $path);
|
|
|
|
$this->files->ensureDirectoryExists(dirname($path));
|
|
|
|
$this->files->put(
|
|
$path, $this->populateStub($className, $stub)
|
|
);
|
|
|
|
return $path;
|
|
}
|
|
|
|
/**
|
|
* Ensure that a seeder with the given name doesn't already exist.
|
|
*
|
|
* @param string $name
|
|
* @param string $seederPath
|
|
* @return void
|
|
*
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
protected function ensureSeederDoesntAlreadyExist($className, $seederPath = null)
|
|
{
|
|
if (! empty($seederPath)) {
|
|
$seederFiles = $this->files->glob($seederPath.'/*.php');
|
|
|
|
foreach ($seederFiles as $seederFile) {
|
|
$this->files->requireOnce($seederFile);
|
|
}
|
|
}
|
|
|
|
if (class_exists($className)) {
|
|
throw new InvalidArgumentException("A {$className} class already exists.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the seeder stub file.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function getStub()
|
|
{
|
|
$stub = $this->stubPath().'/seeder.stub';
|
|
|
|
return $this->files->get($stub);
|
|
}
|
|
|
|
/**
|
|
* Populate the place-holders in the seeder stub.
|
|
*
|
|
* @param string $className
|
|
* @param string $stub
|
|
* @return string
|
|
*/
|
|
protected function populateStub($className, $stub)
|
|
{
|
|
$stub = str_replace(
|
|
['DummyClass', '{{ class }}', '{{class}}'],
|
|
$className, $stub
|
|
);
|
|
|
|
return $stub;
|
|
}
|
|
|
|
/**
|
|
* Get the class name of a seeder name.
|
|
*
|
|
* @param string $name
|
|
* @return string
|
|
*/
|
|
protected function getClassName($dateTime, $name)
|
|
{
|
|
return Str::studly($name).'_'.$dateTime->format('YmdHis');
|
|
}
|
|
|
|
/**
|
|
* Get the full path to the seeder.
|
|
*
|
|
* @param string $name
|
|
* @param string $path
|
|
* @return string
|
|
*/
|
|
protected function getPath($dateTime, $name, $path)
|
|
{
|
|
return $path.'/'.$dateTime->format('Y_m_d_His').'_'.$name.'.php';
|
|
}
|
|
|
|
/**
|
|
* Get the path to the stubs.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function stubPath()
|
|
{
|
|
return $this->laravel->basePath().'/stubs';
|
|
}
|
|
}
|