caffeinated / modules

:package: Modules package for Laravel

Home Page:https://caffeinatedpackages.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ErrorException : Undefined variable: factory on Laravel 5.7

cbaconnier opened this issue · comments

Caffeinated version 5.0.1
Laravel version 5.7.20

I'm unable to create a factory in a module. When I create one, I cannot even run an artisan command since $factory is undefined.
Factories outside of module works fine

My factory is located under project/app/Modules/MyModule/database/factories/MyModuleFactory.php.

use Faker\Generator as Faker;
// HERE'S THE ERROR
$factory->define(\App\Modules\MyModule\Models\MyModule::class, function (Faker $faker) {
    return [];
});

Config file (I changed the path mapping, but the problem also appear when I don't)

return [
    'default_location' => 'app',
    'locations' => [
        'app' => [
            'driver'    => 'local',
            'path'      => app_path('Modules'),
            'namespace' => 'App\\Modules\\',
            'enabled'   => true,
            'provider'  => 'ModuleServiceProvider',
            'manifest'  => 'module.json',
            'mapping'   => [
                'Config'              => 'config',
                'Database/Factories'  => 'database/factories',
                'Database/Migrations' => 'database/migrations',
                'Database/Seeds'      => 'database/seeds',
                'Http/Controllers'    => 'Http/Controllers',
                'Http/Middleware'     => 'Http/Middleware',
                'Providers'           => 'Providers',
                'Resources/Lang'      => 'resources/lang',
                'Resources/Views'     => 'resources/views',
                'Routes'              => 'routes'
            ],
        ],
    ],
    'default_driver' => 'local',
    'drivers' => [
        'local' => 'Caffeinated\Modules\Repositories\LocalRepository',
    ],
];

And the ModuleServiceProvider

namespace App\Modules\MyModule\Providers;

use Caffeinated\Modules\Support\ServiceProvider;

class ModuleServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadTranslationsFrom(module_path('my_module', 'resources/lang', 'app'), 'my_module');
        $this->loadViewsFrom(module_path('my_module', 'resources/views', 'app'), 'my_module');
        $this->loadMigrationsFrom(module_path('my_module', 'database/migrations', 'app'));
        $this->loadConfigsFrom(module_path('my_module', 'config', 'app'));
        $this->loadFactoriesFrom(module_path('my_module', 'database/factories', 'app'));
    }

    public function register()
    {
        $this->app->register(RouteServiceProvider::class);
    }
}

Stracktrace I got from a Unit Test

ErrorException : Undefined variable: factory
 /var/www/project/app/Modules/MyModule/database/factories/MyModuleFactory.php:5
 /var/www/project/vendor/caffeinated/modules/src/Support/ServiceProvider.php:69
 /var/www/project/app/Modules/MyModule/Providers/ModuleServiceProvider.php:21
 /var/www/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
 /var/www/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
 /var/www/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
 /var/www/project/vendor/laravel/framework/src/Illuminate/Container/Container.php:572
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:795
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:778
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:779
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php:17
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:204
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:297
 /var/www/project/tests/CreatesApplication.php:18
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
 /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:68
 /var/www/project/tests/Unit/UserTest.php:20

composer dump-autoload returns

  Undefined variable: factory  
                              
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

I fixed the error by overriding loadFactoriesFrom($path) in ModuleServiceProvider with

use Illuminate\Database\Eloquent\Factory;

...

protected function loadFactoriesFrom($path)
{
    app(Factory::class)->load($path);
}

Any specific reason to requirethe factory files?

Originally:

protected function loadFactoriesFrom($path)
{
    foreach (glob($path.'/*.php') as $file) {
        require $file;
    }
}
commented

carried over from a previous PR; doesn't seem like it's really helping anything though - would you mind opening a PR with your fix?