Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from mikemand/factories-update
Browse files Browse the repository at this point in the history
Update FactoryMakeCommand to follow Laravel's
  • Loading branch information
Andrei Sosnov authored Jul 6, 2019
2 parents e3b3991 + eef6fef commit 3c531ea
Showing 1 changed file with 8 additions and 48 deletions.
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');
}
}

0 comments on commit 3c531ea

Please sign in to comment.