| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Database; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\Database\Seeder\CouponSeeder; |
| 7 |
use FluentCart\Database\Seeder\CustomerAddressSeeder; |
| 8 |
use FluentCart\Database\Seeder\OrderAddressSeeder; |
| 9 |
use FluentCart\Database\Seeder\OrderOperationSeeder; |
| 10 |
use FluentCart\Database\Seeder\ProductSeeder; |
| 11 |
use FluentCart\Database\Seeder\CustomerSeeder; |
| 12 |
use FluentCart\Database\Seeder\OrderSeeder; |
| 13 |
use FluentCart\Database\Seeder\AppliedCouponsSeeder; |
| 14 |
use FluentCart\Database\Seeder\OrderMetaSeeder; |
| 15 |
use FluentCart\Database\Seeder\SubscriptionSeeder; |
| 16 |
use FluentCart\Database\Seeder\TaxSeeder; |
| 17 |
|
| 18 |
class DBSeeder |
| 19 |
{ |
| 20 |
public static function run($count = 10, $entity = null, $checkDev = true, $assoc_args = []) |
| 21 |
{ |
| 22 |
static::ensureFakerAliases(); |
| 23 |
|
| 24 |
$seeders = [ |
| 25 |
'customer' => CustomerSeeder::class, |
| 26 |
'customer_address' => CustomerAddressSeeder::class, |
| 27 |
'coupon' => CouponSeeder::class, |
| 28 |
'product' => ProductSeeder::class, |
| 29 |
'order' => OrderSeeder::class, |
| 30 |
'order_operation' => OrderOperationSeeder::class, |
| 31 |
'order_address' => OrderAddressSeeder::class, |
| 32 |
'subscription' => SubscriptionSeeder::class, |
| 33 |
'tax' => TaxSeeder::class, |
| 34 |
]; |
| 35 |
|
| 36 |
|
| 37 |
if (empty($entity)) { |
| 38 |
foreach ($seeders as $value) { |
| 39 |
/** |
| 40 |
* @var CustomerSeeder|ProductSeeder|OrderSeeder|OrderOperationSeeder|OrderAddressSeeder|CouponSeeder|SubscriptionSeeder $value |
| 41 |
*/ |
| 42 |
$value::seed($count, $assoc_args); |
| 43 |
} |
| 44 |
} else { |
| 45 |
if ($entity === 'order') { |
| 46 |
/** |
| 47 |
* @var OrderSeeder $seeders ['order'] |
| 48 |
*/ |
| 49 |
$seeders['order']::seed($count, $assoc_args); |
| 50 |
$seeders['order_operation']::seed($count, $assoc_args); |
| 51 |
} elseif (isset($seeders[$entity])) { |
| 52 |
/** |
| 53 |
* @var CustomerSeeder|ProductSeeder|OrderAddressSeeder $seeders ['order'] |
| 54 |
*/ |
| 55 |
$seeders[$entity]::seed($count, $assoc_args); |
| 56 |
} |
| 57 |
} |
| 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 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|