PHP code example of vaibhavpandeyvpz / soochak

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

    

vaibhavpandeyvpz / soochak example snippets




use Soochak\EventManager;
use Soochak\Event;

$em = new EventManager();

// Attach a listener to an event (using class name)
$em->attach(Event::class, function (Event $event) {
    echo "Event dispatched!";
});

// Dispatch the event
$em->dispatch(new Event());



use Soochak\EventManager;
use Soochak\Event;

$em = new EventManager();
$event = new Event();

// Attach listener
$em->attach(Event::class, function (object $event) {
    // Handle the event
});

// Dispatch using PSR-14 interface
$em->dispatch($event);

$em = new EventManager();

// Lower priority (executed last)
$em->attach(Event::class, function (Event $event) {
    echo "Sending email notification...\n";
}, 10);

// Higher priority (executed first)
$em->attach(Event::class, function (Event $event) {
    echo "Updating inventory...\n";
}, 20);

$em->dispatch(new Event());
// Output:
// Updating inventory...
// Sending email notification...

$em = new EventManager();

$em->attach(Event::class, function (Event $event) {
    $event->stopPropagation(true);
    echo "Validation failed, stopping propagation\n";
});

$em->attach(Event::class, function (Event $event) {
    echo "This won't execute if propagation was stopped\n";
});

$event = new Event();
$em->dispatch($event);

use Soochak\Event;

// Create an event
$event = new Event();

// Stop propagation if needed
$event->stopPropagation(true);

// Check if propagation is stopped
if ($event->isPropagationStopped()) {
    echo "Propagation is stopped\n";
}

// Dispatch the event
$em->dispatch($event);

class UserRegisteredEvent
{
    public function __construct(
        public readonly int $userId,
        public readonly string $email
    ) {}
}

$em = new EventManager();

// Attach listener using class name
$em->attach(UserRegisteredEvent::class, function (UserRegisteredEvent $event) {
    echo "User {$event->userId} registered with email {$event->email}\n";
});

// Or attach using instance
$event = new UserRegisteredEvent(123, '[email protected]');
$em->attach($event, function (UserRegisteredEvent $e) {
    // Handle event
});

// Dispatch
$em->dispatch($event);

$em = new EventManager();

$listener = function (Event $event) {
    echo "Listener executed\n";
};

// Attach
$em->attach(Event::class, $listener);

// Detach
$em->detach(Event::class, $listener);

// Clear all listeners for an event
$em->clear(Event::class);

$em = new EventManager();

$em->attach(Event::class, function () {}, 10);
$em->attach(Event::class, function () {}, 20);

$event = new Event();
$listeners = iterator_to_array($em->getListenersForEvent($event));

echo count($listeners); // 2