Skip to content

Commit 1893388

Browse files
committed
Fix factory creating
1 parent b9342ac commit 1893388

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

src/Core/Command.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ protected function prepare()
3939
* Generate module namespace
4040
*
4141
* @param string|null $ns
42+
* @param boolean $append
4243
* @return string
4344
*/
44-
protected function getNamespace(?string $ns = null)
45+
protected function getNamespace(?string $ns = null, bool $append = true)
4546
{
4647
if ($ns) {
47-
$ns = explode('/', trim($ns, '/') . ($this->sliceName ? '/' . $this->sliceName : null));
48+
$ns = explode(
49+
'/',
50+
trim($ns, '/') .
51+
($append ? ($this->sliceName ? '/' . $this->sliceName : null) : null)
52+
);
4853
$ns = '\\' . implode('\\', $ns);
4954
}
5055

src/Core/Commands/MakeFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@ public function exec()
2020

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

2426
// STUB
2527
$stubContent = file_get_contents(__DIR__ . '/stubs/factory.stub');
2628
$stubContent = str_replace('{{ factory }}', $this->name, $stubContent);
2729
$stubContent = str_replace('{{ factoryNamespace }}', $this->getNamespace('database/factories'), $stubContent);
30+
$stubContent = str_replace('{{ namespacedModel }}', $this->getNamespace('Models/' . $fullNameWithoutFactory, false), $stubContent);
31+
$stubContent = str_replace('{{ model }}', $nameWithoutFactory, $stubContent);
32+
2833

2934
// SAVING FACTORY
3035
if (file_exists($path = $this->getPath('database/factories/' . $this->fullName . '.php'))) {

src/Core/Commands/stubs/factory.stub

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace {{ factoryNamespace }};
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use {{ namespacedModel }};
67

7-
class {{ factory }}Factory extends Factory
8+
class {{ factory }} extends Factory
89
{
10+
protected $model = {{ model }}::class;
11+
912
/**
1013
* Define the model's default state.
1114
*

src/Core/ServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pharaonic\Laravel\Modulator\Core;
44

5+
use Illuminate\Database\Eloquent\Factories\Factory;
56
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
67

78
class ServiceProvider extends IlluminateServiceProvider
@@ -30,8 +31,13 @@ public function __construct($app)
3031
$this->loadMigrations();
3132
$this->registerCommands();
3233
$this->loadTranslations();
33-
}
3434

35+
$this->booting(function(){
36+
Factory::guessFactoryNamesUsing(function ($modelName) {
37+
return 'App\Modules\\' . str_replace('/', '\\', static::$module) . '\database\factories\\' . class_basename($modelName) . 'Factory';
38+
});
39+
});
40+
}
3541
/**
3642
* Load Module Helpers.
3743
*

0 commit comments

Comments
 (0)