jalismrs/symfony.common.event-subscriber

This package is abandoned and no longer maintained. The author suggests using the jalismrs/symfony.common.event package instead.

Adds Symfony event subscriber abstract classes

1.0.1 2020-11-18 15:20 UTC

This package is auto-updated.

Last update: 2020-11-24 15:35:17 UTC


README

Adds Symfony event subscriber abstract classes

Test

phpunit or vendor/bin/phpunit

coverage reports will be available in var/coverage

Use

EventSubscriberAbstract

use Jalismrs\Symfony\Common\EventSubscriberAbstract;
use Symfony\Contracts\EventDispatcher\Event;

class EventSubscriber extends EventSubscriberAbstract {
    public static function getSubscribedEvents(): array {
        return [
            Event::class => 'onEvent',
        ];
    }
    
    public function onEvent(
        Event $event
    ): Event {
        if ($this->isActive()) {
            // do something
        }
    
        return $event;
    }
}

class SomeClass {
    private EventSubscriber $eventSubscriber;

    public function someFunction(): void {
        $this->eventSubscriber->activate();
        
        // dispatch events
    }
}

ConsoleEventSubscriberAbstract

use Jalismrs\Symfony\Common\ConsoleEventSubscriberAbstract;
use Symfony\Component\Console\Command\Command;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class ConsoleEventSubscriber extends ConsoleEventSubscriberAbstract {
    public static function getSubscribedEvents(): array {
        return [
            Event::class => 'onEvent',
        ];
    }
    
    public function onEvent(
        Event $event
    ): Event {
        if ($this->isActive()) {
            $style = $this->getStyle();
        
            // do something
        }
    
        return $event;
    }
}

class SomeCommand extends Command {
    private ConsoleEventSubscriber $consoleEventSubscriber;

    protected function initialize(
        InputInterface $input,
        OutputInterface $output
    ): void {
        $style = new SymfonyStyle(
            $input,
            $output
        );
        
        $this->consoleEventSubscriber->setStyle($style);
    }
    
    protected function execute(
        InputInterface $input,
        OutputInterface $output
    ): int {
        $this->consoleEventSubscriber->activate();
        
        // dispatch events
        
        return Command::SUCCESS;
    }
}