diff --git a/src/Console/Command.php b/src/Console/Command.php index fb9141af..853b2ebc 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -66,6 +66,20 @@ abstract class Command */ protected $argv = []; + /** + * The plugin instance, available after calling loadWordPress(). + * + * @var mixed + */ + protected $plugin = null; + + /** + * Whether bootstrap/plugin.php has already been executed. + * + * @var bool + */ + protected bool $pluginBootstrapExecuted = false; + /** * */ @@ -274,6 +288,24 @@ public function setArgvAttribute($value): void $this->argv = $value; } + /** + * Set the plugin instance (injected by the Kernel or loadWordPress). + * + * @param mixed $value + */ + public function setPluginAttribute($value): void + { + if (is_object($value) || is_null($value)) { + $this->plugin = $value; + return; + } + + $this->warning( + 'Invalid plugin instance; expected object or null, got ' . get_debug_type($value) + ); + $this->plugin = null; + } + /** * Load WordPress environment. * @@ -281,9 +313,11 @@ public function setArgvAttribute($value): void */ protected function loadWordPress() { + // Plugin root is the working directory when running `php bones` + $currentDir = $_SERVER['PWD'] ?? getcwd(); + try { // We have to load the WordPress environment. - $currentDir = $_SERVER['PWD'] ?? __DIR__; $wpLoadPath = dirname(dirname(dirname($currentDir))) . '/wp-load.php'; if (!file_exists($wpLoadPath)) { @@ -306,8 +340,8 @@ protected function loadWordPress() * need to use it! Requiring it here means we don't have to load classes * manually. Feels great to relax. */ - if (file_exists(__DIR__ . '/vendor/autoload.php')) { - require __DIR__ . '/vendor/autoload.php'; + if (file_exists($currentDir . '/vendor/autoload.php')) { + require $currentDir . '/vendor/autoload.php'; } } catch (\Exception $e) { $this->error("Error! Can't load Composer autoload (" . $e->getMessage() . ')'); @@ -320,8 +354,10 @@ protected function loadWordPress() * Load this plugin env * -------------------------------------------------------------------------- */ - if (file_exists(__DIR__ . '/bootstrap/plugin.php')) { - require_once __DIR__ . '/bootstrap/plugin.php'; + if (!$this->pluginBootstrapExecuted && file_exists($currentDir . '/bootstrap/plugin.php')) { + $plugin = require $currentDir . '/bootstrap/plugin.php'; + $this->pluginBootstrapExecuted = true; + $this->setPluginAttribute($plugin); } } catch (\Exception $e) { $this->error("Error! Can't load the plugin env (" . $e->getMessage() . ')'); diff --git a/src/Console/bin/bones b/src/Console/bin/bones index e44212ad..4d368b28 100644 --- a/src/Console/bin/bones +++ b/src/Console/bin/bones @@ -615,6 +615,12 @@ namespace Bones\Traits { // WordPress loaded flag protected $wpLoaded = false; + // Plugin instance loaded from bootstrap/plugin.php + protected $plugin = null; + + // Whether bootstrap/plugin.php has already been executed. + protected bool $pluginBootstrapExecuted = false; + /* Protected version of the do_action function */ protected function do_action(...$args) { @@ -686,8 +692,18 @@ namespace Bones\Traits { * Load this plugin env * -------------------------------------------------------------------------- */ - if (file_exists(__DIR__ . '/bootstrap/plugin.php')) { - require_once __DIR__ . '/bootstrap/plugin.php'; + if (!$this->pluginBootstrapExecuted && file_exists(__DIR__ . '/bootstrap/plugin.php')) { + $plugin = require __DIR__ . '/bootstrap/plugin.php'; + $this->pluginBootstrapExecuted = true; + if (is_object($plugin) || is_null($plugin)) { + $this->plugin = $plugin; + } else { + $this->warning( + 'Invalid plugin instance returned from bootstrap/plugin.php; expected object or null, got ' . + get_debug_type($plugin) + ); + $this->plugin = null; + } } } catch (Exception $e) { $this->error("Error! Can't load the plugin env (" . $e->getMessage() . ')'); @@ -1425,6 +1441,9 @@ namespace Bones { $extended = false; if ($this->kernel) { + if (!is_null($this->plugin)) { + $this->kernel->setPlugin($this->plugin); + } $extended = $this->kernel->handle($this->arguments()); } @@ -3168,4 +3187,3 @@ namespace Bones { BonesCommandLine::run(); } - diff --git a/src/Foundation/Console/Kernel.php b/src/Foundation/Console/Kernel.php index b6f3182a..aa040817 100644 --- a/src/Foundation/Console/Kernel.php +++ b/src/Foundation/Console/Kernel.php @@ -21,6 +21,20 @@ public function hasCommands(): bool return !empty($this->instances); } + /** + * Inject the plugin instance into all registered command instances. + * + * @param mixed $plugin + */ + public function setPlugin($plugin): void + { + foreach ($this->instances as $commands) { + foreach ($commands as $command) { + $command->plugin = $plugin; + } + } + } + public function handle($argv) { // wpkirk:sample