Skip to content

Commit

Permalink
Release/1.3.1 (#39)
Browse files Browse the repository at this point in the history
* Introduces has_middleware method.

* Adds check ot make it possible to skip event logging for a registry

* Bumps logger version

* Introduces registry factory
  • Loading branch information
alexstandiford authored Aug 5, 2021
1 parent fe58799 commit 926cb0a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
},
"require": {
"composer/installers": "^1.10",
"underpin/logger-loader": "^1.0"
"underpin/logger-loader": "^2.0"
}
}
33 changes: 16 additions & 17 deletions lib/abstracts/registries/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ abstract class Registry extends ArrayIterator {
*/
public $name = '';

/**
* Set to true to force this registry to skip logging.
*
* @since 1.3.1
*
* @var bool
*/
protected $skip_logging = false;

/**
* Registry constructor.
*
Expand Down Expand Up @@ -97,31 +106,21 @@ public function add( $key, $value ) {
if ( true === $valid ) {
$this->_add( $key, $value );

/**
* Fires action after an item is added to the registry.
*
* @since 1.0.0
*
* @param string $registry_id Unique registry ID in which this item was added.
* @param string $key The key that was added to the registry.
* @param mixed $value The value of the item that was added.
*/
do_action( 'underpin/registry/after_added_item', $this->registry_id, $key, $value );
if ( ! is_wp_error( underpin()->logger() ) ) {
if ( false === $this->skip_logging && ! is_wp_error( underpin()->logger() ) ) {
underpin()->logger()->log(
'debug',
'valid_event_added',
'A valid item registry item was registered.',
'valid_item_added',
'A valid registry item was registered.',
[
'ref' => $this->registry_id,
'key' => $key,
'ref' => $this->registry_id,
'key' => $key,
'value' => $value,
'class' => get_called_class()
'class' => get_called_class(),
]
);
}
} else {
if ( ! is_wp_error( underpin()->logger() ) ) {
if ( false === $this->skip_logging && ! is_wp_error( underpin()->logger() ) ) {
underpin()->logger()->log(
'warning',
'invalid_event',
Expand Down
39 changes: 39 additions & 0 deletions lib/factories/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Underpin\Factories;


use Underpin\Traits\Instance_Setter;
use WP_Error;

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

class Registry extends \Underpin\Abstracts\Registries\Registry {

use Instance_Setter;

protected $default_items = [];

protected $validate_callback;

public function __construct( $args ) {
$registry_id = $args['registry_id'];
$this->set_values( $args );
parent::__construct( $registry_id );
}

protected function set_default_items() {
foreach ( $this->default_items as $key => $value ) {
$this->add( $key, $value );
}

unset( $this->default_items );
}

protected function validate_item( $key, $value ) {
return $this->set_callable( $this->validate_callback, $key, $value );
}

}
37 changes: 37 additions & 0 deletions lib/traits/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,41 @@ public function add_middleware( $middleware ) {
$this->middlewares[] = $middleware;
}

/**
* Returns true if any of the middlware is an instance of the specified item.
*
* @since 1.3.1
*
* @param $middleware
*
* @return bool true if this instance has middleware, otherwise false.
*/
public function has_middleware( $middleware ): bool {

// If there's no middleware, just return false.
if ( empty( $this->middlewares ) ) {
return false;
}

$cache_key = md5( maybe_serialize( array_map( 'get_class', $this->middlewares ) ) . $middleware );

// Check the cache first.
$cached = wp_cache_get( $cache_key );

if ( false !== $cached ) {
return (bool) $cached;
}

// Otherwise, search to see if the middleware is set.
foreach ( $this->middlewares as $middleware_item ) {
if ( $middleware === $middleware_item || is_subclass_of( $middleware_item, $middleware ) || $middleware_item instanceof $middleware ) {
wp_cache_add( $cache_key, 1 );
return true;
}
}

wp_cache_add( $cache_key, 0 );
return false;
}

}

0 comments on commit 926cb0a

Please sign in to comment.