Skip to content
Snippets Groups Projects
Verified Commit 64a0769a authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat: add hook driver backward compatible with wp-builder

parent 309d1742
No related branches found
No related tags found
3 merge requests!3improve into wp-hook and some additional unfinished things,!21.x,!1Draft: Basic implementation of plugin initializer
<?php
namespace WPDesk\Init\HookDriver\Legacy;
use WPDesk\PluginBuilder\Plugin\Conditional;
use WPDesk\PluginBuilder\Plugin\Hookable;
use WPDesk\PluginBuilder\Plugin\HookablePluginDependant;
trait HookableParent {
/** @var HooksRegistry|null */
private $registry;
/**
* @param class-string<Hookable>|Hookable $hookable_object
*/
public function add_hookable( $hookable_object ) {
if ( $this->registry === null ) {
$this->registry = HooksRegistry::instance();
}
$this->registry->add( $hookable_object );
}
/**
* @param class-string<Hookable> $class_name
*
* @return false|Hookable
*/
public function get_hookable_instance_by_class_name( $class_name ) {
if ( $this->registry === null ) {
return;
}
foreach ( $this->registry as $hookable_object ) {
if ( $hookable_object instanceof $class_name ) {
return $hookable_object;
}
}
return false;
}
/**
* Run hooks method on all hookable objects.
*/
protected function hooks_on_hookable_objects() {
if ( $this->registry === null ) {
return;
}
foreach ( $this->registry as $hookable_object ) {
if (
$hookable_object instanceof Conditional &&
! $hookable_object::is_needed()
) {
continue;
}
if ( $hookable_object instanceof HookablePluginDependant ) {
$hookable_object->set_plugin( $this );
}
$hookable_object->hooks();
}
}
}
<?php
namespace WPDesk\Init\HookDriver\Legacy;
use Psr\Container\ContainerInterface;
use Traversable;
use WPDesk\PluginBuilder\Plugin\Hookable;
/**
* @implements IteratorAggregate<int,Hookable>
*/
final class HooksRegistry implements \IteratorAggregate
{
private static $instance;
/** @var array<class-string<Hookable>|Hookable> */
private $callbacks = [];
private $container;
private function __construct() {}
public function inject_container( ContainerInterface $c ) {
$this->container = $c;
}
public static function instance(): HooksRegistry {
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}
public function getIterator(): Traversable {
return new \ArrayIterator(
array_map(
function ( $hookable ) {
if ( is_string( $hookable ) ) {
return $this->container->get( $hookable );
}
return $hookable;
},
$this->callbacks
)
);
}
public function add( $hookable ) {
$this->callbacks[] = $hookable;
}
}
......@@ -5,6 +5,7 @@ namespace WPDesk\Init\HookDriver;
use Psr\Container\ContainerInterface;
use WPDesk\Init\Configuration\ReadableConfig;
use WPDesk\Init\HookDriver\Legacy\HooksRegistry;
use WPDesk\Init\Plugin;
class LegacyHookableDriver implements HookDriver {
......@@ -15,6 +16,8 @@ class LegacyHookableDriver implements HookDriver {
$class_name = $info->get_class_name();
$p = new $class_name($info);
$reg = HooksRegistry::instance();
$reg->inject_container($container);
add_action('plugins_loaded', [$p, 'init'], -45);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment