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