Skip to content

Commit

Permalink
Corrects underpin autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstandiford committed May 11, 2021
1 parent 0ffd9ff commit 8164420
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 13 deletions.
37 changes: 36 additions & 1 deletion lib/abstracts/Underpin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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() );
}

Expand Down Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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.
*
Expand Down
16 changes: 13 additions & 3 deletions lib/abstracts/registries/Loader_Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand All @@ -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
Expand All @@ -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() );
}

Expand All @@ -73,15 +79,15 @@ 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;
}

// If this implements registry actions, go ahead and start those up, too.
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',
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/abstracts/registries/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 15 additions & 2 deletions lib/factories/Loader_Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Underpin\Factories;

use Underpin\Abstracts\Registries\Registry;
use Underpin\Traits\With_Parent;
use WP_Error;
use function Underpin\underpin;

Expand All @@ -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.
}
Expand All @@ -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'];
Expand All @@ -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 );
Expand Down
11 changes: 6 additions & 5 deletions lib/factories/Loader_Registry_Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
19 changes: 19 additions & 0 deletions lib/traits/With_Parent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Underpin\Traits;

use Underpin\Abstracts\Underpin;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

trait With_Parent {

protected $parent_id;

public function parent() {
return Underpin::get_by_id( $this->parent_id );
}

}

0 comments on commit 8164420

Please sign in to comment.