diff --git a/lib/abstracts/Underpin.php b/lib/abstracts/Underpin.php index 6441b48..52cec83 100644 --- a/lib/abstracts/Underpin.php +++ b/lib/abstracts/Underpin.php @@ -149,6 +149,15 @@ abstract class Underpin { */ protected $template_dir; + /** + * Plugin name. + * + * Used to identify this plugin in debug logs. + * + * @var string + */ + public $name = 'Underpin'; + /** * Function to setup this plugin. * @@ -356,6 +365,11 @@ public function is_debug_mode_enabled() { return true; } + // If WP DEBUG is enabled, turn on debug mode. + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { + return true; + } + return apply_filters( 'underpin/debug_mode_enabled', false, get_called_class() ); } @@ -390,7 +404,7 @@ public static function export() { foreach ( Underpin::$instances as $key => $instance ) { if ( $instance instanceof Underpin ) { - $results = Underpin\underpin()->get( $file, $class )->export_registered_items( $results ); + $results = Underpin::get_by_id( $key )->export_registered_items( $results ); } } @@ -720,6 +734,27 @@ public function get_registry_key( $file = '', $class = '' ) { return md5( $class . $file ); } + /** + * Fetch an Underpin Instance by the registry key. + * + * @since 1.2 + * + * @param string $key The instance key. + * + * @return Underpin|WP_Error The underpin instance if found, otherwise WP_Error. + */ + public static function get_by_id( $key ) { + if ( isset( self::$instances[ $key ] ) ) { + return self::$instances[ $key ]; + } + + return new WP_Error( + 'instance_not_found', + 'The instance key provided is not associated with an Underpin instance', + [ 'key' => $key ] + ); + } + /** * Fires up the plugin. * diff --git a/lib/abstracts/registries/Loader_Registry.php b/lib/abstracts/registries/Loader_Registry.php index aab8111..b5312d0 100644 --- a/lib/abstracts/registries/Loader_Registry.php +++ b/lib/abstracts/registries/Loader_Registry.php @@ -12,6 +12,7 @@ use Underpin\Abstracts\Feature_Extension; use Underpin\Abstracts\Underpin; +use Underpin\Traits\With_Parent; use function Underpin\underpin; if ( ! defined( 'ABSPATH' ) ) { @@ -26,6 +27,8 @@ */ abstract class Loader_Registry extends Registry { + use With_Parent; + /** * The abstraction class name. * This is used to validate that the items in this service locator are extended @@ -49,7 +52,10 @@ abstract class Loader_Registry extends Registry { * Loader_Registry constructor. * */ - public function __construct() { + public function __construct( $parent_id = false ) { + if ( false !== $parent_id ) { + $this->parent_id = $parent_id; + } parent::__construct( $this->get_registry_id() ); } @@ -73,7 +79,7 @@ public function add( $key, $value ) { $valid = $this->validate_item( $key, $value ); if ( true === $valid ) { $this[ $key ] = Underpin::make_class( $value, $this->default_factory ); - } else{ + } else { $this[ $key ] = $valid; } @@ -81,7 +87,7 @@ public function add( $key, $value ) { if ( self::has_trait( 'Underpin\Traits\Feature_Extension', $this->get( $key ) ) ) { $this->get( $key )->do_actions(); - if ( !$this instanceof \Underpin_Logger\Loaders\Logger && ! is_wp_error( underpin()->logger() ) ) { + if ( ! $this instanceof \Underpin_Logger\Loaders\Logger && ! is_wp_error( underpin()->logger() ) ) { underpin()->logger()->log( 'notice', 'loader_actions_ran', @@ -91,6 +97,10 @@ public function add( $key, $value ) { } } + if ( ! is_wp_error( $valid ) ) { + do_action( 'underpin/loader_registered', $key, $value, get_called_class(), $this->parent_id ); + } + return $valid; } diff --git a/lib/abstracts/registries/Registry.php b/lib/abstracts/registries/Registry.php index 46a8a4a..ec73311 100644 --- a/lib/abstracts/registries/Registry.php +++ b/lib/abstracts/registries/Registry.php @@ -111,8 +111,8 @@ public function add( $key, $value ) { underpin()->logger()->log( 'notice', 'valid_event_added', - 'A valid item for the ' . $this->registry_id . ' registry called ' . $key . ' was registered.', - [ 'ref' => $this->registry_id, 'key' => $key, 'value' => $value ] + 'A valid item registry item was registered.', + [ 'ref' => $this->registry_id, 'key' => $key, 'value' => $value, 'class' => get_called_class() ] ); } } else { diff --git a/lib/factories/Loader_Registry.php b/lib/factories/Loader_Registry.php index 422f00b..1a7b2ca 100644 --- a/lib/factories/Loader_Registry.php +++ b/lib/factories/Loader_Registry.php @@ -3,6 +3,7 @@ namespace Underpin\Factories; use Underpin\Abstracts\Registries\Registry; +use Underpin\Traits\With_Parent; use WP_Error; use function Underpin\underpin; @@ -13,6 +14,13 @@ class Loader_Registry extends Registry { + use With_Parent; + + public function __construct( $registry_id ) { + $this->parent_id = $registry_id; + parent::__construct( $registry_id ); + } + protected function set_default_items() { // Loaders are added externally. } @@ -31,7 +39,8 @@ public function get( $key ) { // Maybe instantiate loader item. if ( is_array( $valid ) && isset( $valid['registry'] ) && is_string( $valid['registry'] ) ) { - $this[ $key ]['registry'] = new $valid['registry']; + /* @var $valid \Underpin\Abstracts\Registries\Loader_Registry */ + $this[ $key ]['registry'] = new $valid['registry']( $this->registry_id ); } return $this[ $key ]['registry']; @@ -41,7 +50,11 @@ protected function _add( $key, $value ) { // Maybe auto-set the registry. if ( ! isset( $value['registry'] ) ) { $default = isset( $value['default'] ) ? $value['default'] : ''; - $value['registry'] = new Loader_Registry_Item( $value['instance'], $default ); + $value['registry'] = new Loader_Registry_Item( [ + 'abstraction_class' => $value['instance'], + 'default_factory' => $default, + 'parent_id' => $this->registry_id, + ] ); } return parent::_add( $key, $value ); diff --git a/lib/factories/Loader_Registry_Item.php b/lib/factories/Loader_Registry_Item.php index e02c3ff..c7e8e94 100644 --- a/lib/factories/Loader_Registry_Item.php +++ b/lib/factories/Loader_Registry_Item.php @@ -2,18 +2,19 @@ namespace Underpin\Factories; +use Underpin\Traits\Instance_Setter; + if ( ! defined( 'ABSPATH' ) ) { exit; } class Loader_Registry_Item extends \Underpin\Abstracts\Registries\Loader_Registry { + use Instance_Setter; - private $default_items; + private $default_items = []; - public function __construct( $abstraction_class, $default_factory, $default_items = [] ) { - $this->abstraction_class = $abstraction_class; - $this->default_factory = $default_factory; - $this->default_items = $default_items; + public function __construct( $args ) { + $this->set_values($args); parent::__construct(); } diff --git a/lib/traits/With_Parent.php b/lib/traits/With_Parent.php new file mode 100644 index 0000000..4f47ac8 --- /dev/null +++ b/lib/traits/With_Parent.php @@ -0,0 +1,19 @@ +parent_id ); + } + +} \ No newline at end of file