PHP code example of slaxweb / hooks

1. Go to this page and download the library: Download slaxweb/hooks 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/ */

    

slaxweb / hooks example snippets



use SlaxWeb\Hooks\Container as Hooks;

 the Hooks container
$hooks = \SlaxWeb\Hooks\Factory::init($config);

// create a hook
$hook = \SlaxWeb\Hooks\Factory::newHook();
$hook->create("hook.name", function (Hooks $container) {
    // stuff..
    return "I ran!";
});

// add the hook to the container
$hook->addHook($hook);

// some app stuff

// execute the hook
if ($hooks->execute("hook.name") === "I ran!") {
    // stuff..
}


use SlaxWeb\Hooks\Container as Hooks;

ster the providers
$container->register(new \SlaxWeb\Config\Service\Provider);
$container->register(new \SlaxWeb\Logger\Service\Provider);
$container->register(new \SlaxWeb\Hooks\Service\Provider);

// load the config for the logger etc.

$hook = $container["newHook.factory"];
$hook->create("hook.name", function (Hooks $container) {
    // stuff ..
    return "I ran!";
});

// add the hook to the container
$container["hooks.service"]->addHook($hook);

// some app stuff..

// execute the hook
if ($container["hook.service"]->exec("hook.name") === "I ran!") {
    // stuff..
}

// instantiation of everything ainer["newHook.factory"];
$hook->create("hook.name", function (Hooks $container) {
    return 1;
});
$container["hooks.service"]->addHook($hook);

$hook = $container["newHook.factory"];
$hook->create("hook.name", function (Hooks $container) {
    return null;
});
$container["hooks.service"]->addHook($hook);

$hook = $container["newHook.factory"];
$hook->create("hook.name", function (Hooks $container) {
    return 3;
});
$container["hooks.service"]->addHook($hook);

// execute the hook
$container["hooks.service"]->exec("hook.name"); // will return: [1, 2]

// create 'no return' hook
$hook = $container["newHook.factory"];
$hook->create("noreturn.hook", function (Hooks $container) {
    // stuff
});
$container["hooks.service"]->addHook($hook);

// execute the hook
$container["hooks.service"]->exec("noreturn.hook"); // will return: []

// create hooks
$hook = $container["newHook.factory"];
$hook->create("hook.name", function (Hooks $container) {
    $container->stopExec();
    return 1;
});
$container["hooks.service"]->addHook($hook);

$hook = $container["newHook.factory"];
$hook->create("hook.name", function (Hooks $container) {
    return 2;
});
$container["hooks.service"]->addHook($hook);

$container["hooks.service"]->exec("hook.name"); // will return: 1

// create hook
$hook = $container["newHook.factory"];
$hook->create("hook.name", function (Hooks $container, string $myParam) {
    return $myParam;
});
$container["hooks.service"]->addHook($hook);

$container["hooks.service"]->exec("hook.name", "foo"); // will return: "foo"