| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\TestData; |
| 4 |
|
| 5 |
use Faker\Factory as FakerFactory; |
| 6 |
use Faker\Generator as FakerGenerator; |
| 7 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 8 |
use Give\ServiceProviders\ServiceProvider as GiveServiceProvider; |
| 9 |
use Give\TestData\Commands\DonationSeedCommand; |
| 10 |
use Give\TestData\Commands\DonationStatusCommand; |
| 11 |
use Give\TestData\Commands\DonorSeedCommand; |
| 12 |
use Give\TestData\Commands\FormSeedCommand; |
| 13 |
use Give\TestData\Commands\LogsSeedCommand; |
| 14 |
use Give\TestData\Commands\PageSeedCommand; |
| 15 |
use WP_CLI; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class ServiceProvider |
| 19 |
* @package Give\TestData |
| 20 |
*/ |
| 21 |
class ServiceProvider implements GiveServiceProvider |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @inheritDoc |
| 25 |
*/ |
| 26 |
public function register() |
| 27 |
{ |
| 28 |
// Instead of passing around an instance, bind a singleton to the container. |
| 29 |
give()->singleton( |
| 30 |
FakerGenerator::class, |
| 31 |
function () { |
| 32 |
return FakerFactory::create(); |
| 33 |
} |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @inheritDoc |
| 39 |
*/ |
| 40 |
public function boot() |
| 41 |
{ |
| 42 |
// Add CLI commands |
| 43 |
if (defined('WP_CLI') && WP_CLI) { |
| 44 |
$this->addCommands(); |
| 45 |
|
| 46 |
try { |
| 47 |
$this->loadAddonsServiceProviders(); |
| 48 |
} catch (InvalidArgumentException $e) { |
| 49 |
exit($e->getMessage()); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Load addons service providers for TestData |
| 56 |
*/ |
| 57 |
private function loadAddonsServiceProviders() |
| 58 |
{ |
| 59 |
$providers = []; |
| 60 |
|
| 61 |
// Load Test Data add-ons Service Providers as they are not handled by Give |
| 62 |
foreach (Addons::getActiveAddons() as $addon) { |
| 63 |
if ( ! is_subclass_of($addon['serviceProvider'], GiveServiceProvider::class)) { |
| 64 |
throw new InvalidArgumentException( |
| 65 |
"{$addon['serviceProvider']} class must implement the ServiceProvider interface" |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
$addonServiceProvider = new $addon['serviceProvider'](); |
| 70 |
$addonServiceProvider->register(); |
| 71 |
$providers[] = $addonServiceProvider; |
| 72 |
} |
| 73 |
|
| 74 |
foreach ($providers as $addonServiceProvider) { |
| 75 |
$addonServiceProvider->boot(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Add CLI comands |
| 81 |
*/ |
| 82 |
private function addCommands() |
| 83 |
{ |
| 84 |
WP_CLI::add_command('give test-donors', give()->make(DonorSeedCommand::class)); |
| 85 |
WP_CLI::add_command('give test-donations', give()->make(DonationSeedCommand::class)); |
| 86 |
WP_CLI::add_command('give test-donation-statuses', give()->make(DonationStatusCommand::class)); |
| 87 |
WP_CLI::add_command('give test-demonstration-page', give()->make(PageSeedCommand::class)); |
| 88 |
WP_CLI::add_command('give test-donation-form', give()->make(FormSeedCommand::class)); |
| 89 |
WP_CLI::add_command('give test-logs', give()->make(LogsSeedCommand::class)); |
| 90 |
} |
| 91 |
} |
| 92 |
|