src/Console/RepositoryMakeCommand.php
<?php
namespace Orkhanahmadov\EloquentRepository\Console;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
class RepositoryMakeCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:repository';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new eloquent model repository';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Repository';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
$stub = null;
if ($this->option('model')) {
$stub = '/stubs/repository.model.stub';
} else {
$stub = '/stubs/repository.stub';
}
return __DIR__ . $stub;
}
/**
* Build the class with the given name.
*
* @param string $name
* @return string
* @throws FileNotFoundException
*/
protected function buildClass($name)
{
$replace = [];
if ($this->option('model')) {
$replace = $this->buildModelReplacements($replace);
}
return str_replace(
array_keys($replace),
array_values($replace),
parent::buildClass($name)
);
}
/**
* Build the model replacement values.
*
* @param array $replace
* @return array
*/
protected function buildModelReplacements(array $replace)
{
$modelClass = $this->parseModel($this->option('model'));
if (! class_exists($modelClass)) {
if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {
$this->call('make:model', ['name' => $modelClass]);
}
}
return array_merge($replace, [
'DummyFullModelClass' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
]);
}
/**
* Get the fully-qualified model class name.
*
* @param string $model
* @return string
*
* @throws InvalidArgumentException
*/
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}
$model = trim(str_replace('/', '\\', $model), '\\');
if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) {
$model = $rootNamespace . $model;
}
return $model;
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a repository for the given model.'],
];
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Repositories';
}
}