| @@ -24,8 +24,12 @@ | ||
| 24 | 24 | * @inheritDoc |
| 25 | 25 | */ |
| 26 | 26 | public function register() |
| 27 | 27 | { |
| 28 | + if ( ! $this->isFakerInstalled()) { | |
| 29 | + return; | |
| 30 | + } | |
| 31 | + | |
| 28 | 32 | // Instead of passing around an instance, bind a singleton to the container. |
| 29 | 33 | give()->singleton( |
| 30 | 34 | FakerGenerator::class, |
| 31 | 35 | function () { |
| @@ -38,8 +42,12 @@ | ||
| 38 | 42 | * @inheritDoc |
| 39 | 43 | */ |
| 40 | 44 | public function boot() |
| 41 | 45 | { |
| 46 | + if ( ! $this->isFakerInstalled()) { | |
| 47 | + return; | |
| 48 | + } | |
| 49 | + | |
| 42 | 50 | // Add CLI commands |
| 43 | 51 | if (defined('WP_CLI') && WP_CLI) { |
| 44 | 52 | $this->addCommands(); |
| 45 | 53 | |
| @@ -86,6 +94,21 @@ | ||
| 86 | 94 | WP_CLI::add_command('give test-donation-statuses', give()->make(DonationStatusCommand::class)); |
| 87 | 95 | WP_CLI::add_command('give test-demonstration-page', give()->make(PageSeedCommand::class)); |
| 88 | 96 | WP_CLI::add_command('give test-donation-form', give()->make(FormSeedCommand::class)); |
| 89 | 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); | |
| 90 | 113 | } |
| 91 | 114 | } |