| @@ -18,8 +18,10 @@ | ||
| 18 | 18 | class DBSeeder |
| 19 | 19 | { |
| 20 | 20 | public static function run($count = 10, $entity = null, $checkDev = true, $assoc_args = []) |
| 21 | 21 | { |
| 22 | + static::ensureFakerAliases(); | |
| 23 | + | |
| 22 | 24 | $seeders = [ |
| 23 | 25 | 'customer' => CustomerSeeder::class, |
| 24 | 26 | 'customer_address' => CustomerAddressSeeder::class, |
| 25 | 27 | 'coupon' => CouponSeeder::class, |
| @@ -53,6 +55,55 @@ | ||
| 53 | 55 | $seeders[$entity]::seed($count, $assoc_args); |
| 54 | 56 | } |
| 55 | 57 | } |
| 56 | 58 | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Boot Faker and make the namespace the seeders import resolvable. | |
| 63 | + * | |
| 64 | + * Two things stand between the seeders and Faker in a normal install: | |
| 65 | + * | |
| 66 | + * 1. The generated Composer autoloader is classmap-authoritative and | |
| 67 | + * composer.json excludes `/vendor/fakerphp/` from the classmap, so | |
| 68 | + * `Faker\Factory` never autoloads. Faker ships its own PSR-0 autoloader, | |
| 69 | + * so register that instead of touching the Composer config. | |
| 70 | + * 2. Every seeder imports `FluentCart\Faker\Factory` (and ProductNameProvide | |
| 71 | + * extends `FluentCart\Faker\Provider\Base`). That prefixed namespace only | |
| 72 | + * exists once dev/ComposerScript rewrites the vendor autoloader, and | |
| 73 | + * composer.json sets extra.wpfluent.excludes to ["*"], which skips generic | |
| 74 | + * packages such as fakerphp/faker — so alias the upstream classes onto it. | |
| 75 | + * | |
| 76 | + * Without this, every seeder fatals with | |
| 77 | + * "Class FluentCart\Faker\Factory not found". | |
| 78 | + * | |
| 79 | + * @return void | |
| 80 | + */ | |
| 81 | + protected static function ensureFakerAliases() | |
| 82 | + { | |
| 83 | + if (class_exists('FluentCart\\Faker\\Factory')) { | |
| 84 | + return; | |
| 85 | + } | |
| 86 | + | |
| 87 | + if (!class_exists('Faker\\Factory')) { | |
| 88 | + $fakerAutoload = FLUENTCART_PLUGIN_PATH . 'vendor/fakerphp/faker/src/autoload.php'; | |
| 89 | + | |
| 90 | + if (!file_exists($fakerAutoload)) { | |
| 91 | + return; | |
| 92 | + } | |
| 93 | + | |
| 94 | + require_once $fakerAutoload; | |
| 95 | + } | |
| 96 | + | |
| 97 | + $aliases = [ | |
| 98 | + 'Faker\\Factory' => 'FluentCart\\Faker\\Factory', | |
| 99 | + 'Faker\\Generator' => 'FluentCart\\Faker\\Generator', | |
| 100 | + 'Faker\\Provider\\Base' => 'FluentCart\\Faker\\Provider\\Base', | |
| 101 | + ]; | |
| 102 | + | |
| 103 | + foreach ($aliases as $original => $alias) { | |
| 104 | + if (class_exists($original) && !class_exists($alias, false)) { | |
| 105 | + class_alias($original, $alias); | |
| 106 | + } | |
| 107 | + } | |
| 57 | 108 | } |
| 58 | 109 | } |