PHP code example of speedyspec / speedyspec-wp-hook-infra-memory

1. Go to this page and download the library: Download speedyspec/speedyspec-wp-hook-infra-memory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

speedyspec / speedyspec-wp-hook-infra-memory example snippets


use SpeedySpec\WP\Hook\Domain\HookServiceContainer;
use SpeedySpec\WP\Hook\Domain\Contracts\HookContainerInterface;
use SpeedySpec\WP\Hook\Domain\Contracts\CurrentHookInterface;
use SpeedySpec\WP\Hook\Domain\Contracts\HookRunAmountInterface;
use SpeedySpec\WP\Hook\Domain\Services\CurrentHookService;
use SpeedySpec\WP\Hook\Domain\Services\HookRunAmountService;
use SpeedySpec\WP\Hook\Infra\Memory\Services\MemoryHookContainer;

// Get the service container
$container = HookServiceContainer::getInstance();

// Register the memory infrastructure services (using interfaces for flexibility)
$container->add(HookRunAmountInterface::class, fn() => new HookRunAmountService());
$container->add(CurrentHookInterface::class, fn() => new CurrentHookService());
$container->add(
    HookContainerInterface::class,
    fn($c) => new MemoryHookContainer(
        $c->get(HookRunAmountInterface::class),
        $c->get(CurrentHookInterface::class)
    )
);

// Now use the hook system
$hooks = $container->get(HookContainerInterface::class);

use SpeedySpec\WP\Hook\Infra\Memory\ServiceProvider;
use SpeedySpec\WP\Hook\Domain\HookServiceContainer;
use SpeedySpec\WP\Hook\Domain\Contracts\CalledDeprecatedHookInterface;
use SpeedySpec\WP\Hook\Infra\Memory\Services\CalledDeprecatedHook;

$provider = new ServiceProvider();
$provider->register();

// If you need deprecated hook support, register CalledDeprecatedHook
$container = HookServiceContainer::getInstance();
$container->add(
    CalledDeprecatedHookInterface::class,
    fn() => new CalledDeprecatedHook()
);

// Services are now registered and ready to use

use SpeedySpec\WP\Hook\Domain\Entities\ObjectHookInvoke;
use SpeedySpec\WP\Hook\Domain\ValueObject\StringHookName;

// Get the hook container
$hooks = HookServiceContainer::getInstance()->get(HookContainerInterface::class);

// Add a filter callback
$callback = new ObjectHookInvoke(
    fn(string $value) => strtoupper($value),
    priority: 10
);
$hooks->add(new StringHookName('my_filter'), $callback);

// Apply the filter
$result = $hooks->filter(new StringHookName('my_filter'), 'hello');
// Result: 'HELLO'

// Add an action callback
$actionCallback = new ObjectHookInvoke(
    fn(string $message) => error_log($message),
    priority: 10
);
$hooks->add(new StringHookName('my_action'), $actionCallback);

// Dispatch the action
$hooks->dispatch(new StringHookName('my_action'), 'Something happened');