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