Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
*/
Expand Down Expand Up @@ -274,16 +288,36 @@ 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.
*
* @return 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)) {
Expand All @@ -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() . ')');
Expand All @@ -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() . ')');
Expand Down
24 changes: 21 additions & 3 deletions src/Console/bin/bones
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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() . ')');
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -3168,4 +3187,3 @@ namespace Bones {

BonesCommandLine::run();
}

14 changes: 14 additions & 0 deletions src/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down