rixxi/event

dev-master 2014-06-18 11:39 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:23:55 UTC


README

这种模式引入了比解决的问题更多的复杂性和问题。请使用 Nette 事件与 rixxi/redirector 一起,它比自定义事件类更健壮且简单。

已停止支持 - 您可以使用它,但已被警告。

解决 onEvent 问题的 KISS 模式,使用带一些代码(IEvent)的篮子。

以文明的方式为您解决 $presenter->redirect([$code, ], $destination[, $arguments])

<?php

use Rixxi\Event\IEvent;
use Rixxi\Event\IRedirect;
use Rixxi\Event\Redirect as RedirectTrait;

class ShitHappensEvent implements IEvent, IRedirect
{

  use RedirectTrait;

}

class Service extends Nette\Object
{

  public $onShitHappens = [];


  public function __construct(Rixxi\Event\Redirector $redirector)
  {
    $this->redirector = $redirector;
  }
  
  
  public function makeShitHappen()
  {
    $this->onShitHappens($event = ShitHappensEvent);
    $this->redirector->handle($event);
  }

}


// register extension and let DI do its magic

$service = new Service();

$service->onShitHappen[] = function (ShitHappensEvent $e) {
  if (/* some important condition */) {
    $e->redirect('Presenter:action', [ 'arguments ' ]);
  }
};

$service->onShitHappen[] = function () {
  echo 'Default $presenter->redirect(...) would kill request before I can write something. So grateful!';
};

// you can easily add default redirection
$service->onShitHappen[] = Rixxi\Event\Helper::defaultRedirect('Homepage:default');

$service->makeShitHappen();
// shit will happen and then redirection too