Skip to content

Commit

Permalink
Fix factory creating
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny committed May 24, 2024
1 parent b9342ac commit 1893388
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/Core/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ protected function prepare()
* Generate module namespace
*
* @param string|null $ns
* @param boolean $append
* @return string
*/
protected function getNamespace(?string $ns = null)
protected function getNamespace(?string $ns = null, bool $append = true)
{
if ($ns) {
$ns = explode('/', trim($ns, '/') . ($this->sliceName ? '/' . $this->sliceName : null));
$ns = explode(
'/',
trim($ns, '/') .
($append ? ($this->sliceName ? '/' . $this->sliceName : null) : null)
);
$ns = '\\' . implode('\\', $ns);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Core/Commands/MakeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ public function exec()

// FACTORY NAME
$this->appendName(substr(strtolower($this->name), -7) != 'factory', 'Factory');
$fullNameWithoutFactory = substr($this->fullName, 0, -7);
$nameWithoutFactory = substr($this->name, 0, -7);

// STUB
$stubContent = file_get_contents(__DIR__ . '/stubs/factory.stub');
$stubContent = str_replace('{{ factory }}', $this->name, $stubContent);
$stubContent = str_replace('{{ factoryNamespace }}', $this->getNamespace('database/factories'), $stubContent);
$stubContent = str_replace('{{ namespacedModel }}', $this->getNamespace('Models/' . $fullNameWithoutFactory, false), $stubContent);
$stubContent = str_replace('{{ model }}', $nameWithoutFactory, $stubContent);


// SAVING FACTORY
if (file_exists($path = $this->getPath('database/factories/' . $this->fullName . '.php'))) {
Expand Down
5 changes: 4 additions & 1 deletion src/Core/Commands/stubs/factory.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace {{ factoryNamespace }};

use Illuminate\Database\Eloquent\Factories\Factory;
use {{ namespacedModel }};

class {{ factory }}Factory extends Factory
class {{ factory }} extends Factory
{
protected $model = {{ model }}::class;

/**
* Define the model's default state.
*
Expand Down
8 changes: 7 additions & 1 deletion src/Core/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Pharaonic\Laravel\Modulator\Core;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;

class ServiceProvider extends IlluminateServiceProvider
Expand Down Expand Up @@ -30,8 +31,13 @@ public function __construct($app)
$this->loadMigrations();
$this->registerCommands();
$this->loadTranslations();
}

$this->booting(function(){
Factory::guessFactoryNamesUsing(function ($modelName) {
return 'App\Modules\\' . str_replace('/', '\\', static::$module) . '\database\factories\\' . class_basename($modelName) . 'Factory';
});
});
}
/**
* Load Module Helpers.
*
Expand Down

0 comments on commit 1893388

Please sign in to comment.