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