Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 8 additions & 48 deletions src/Console/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,31 @@
namespace ATehnix\LaravelStubs\Console;

use Illuminate\Database\Console\Factories\FactoryMakeCommand as BaseFactoryMakeCommand;
use Illuminate\Support\Str;

class FactoryMakeCommand extends BaseFactoryMakeCommand
{
use Modulable;

/**
* The Laravel application instance.
*
* @var \Illuminate\Foundation\Application
*/
protected $laravel;

/**
* Build the class with the given name.
*
* @param string $name
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildClass($name)
{
$model = $this->option('model')
? $this->parseModel($this->option('model'))
: 'Model';

$stub = $this->files->get($this->getStub());
$stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);

return str_replace(
'DummyModel', $model, $stub
);
}

/**
* Get the fully-qualified model class name.
* Get the stub file for the generator.
*
* @param string $model
* @return string
*/
protected function parseModel($model)
protected function getStub()
{
$model = trim(str_replace('/', '\\', $model), '\\');

$namespace =
trim($this->laravel->getNamespace(), '\\')
. $this->getModuleNamespace()
. config('stubs.namespaces.model') . '\\';

if (!Str::startsWith($model, $namespace)) {
$model = $namespace . $model;
}
$stub = config('stubs.path') . '/factory.stub';

return $model;
return file_exists($stub) ? $stub : parent::getStub();
}

/**
* Get the stub file for the generator.
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getStub()
protected function getDefaultNamespace($rootNamespace)
{
$stub = config('stubs.path') . '/factory.stub';

return file_exists($stub) ? $stub : parent::getStub();
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.model');
}
}