Exemple #1
0
 /**
  * Prepares the event to be used
  *
  * @see event_set
  * @link http://www.php.net/manual/en/function.event-set.php
  *
  * @see event_base_set
  * @link http://www.php.net/manual/en/function.event-base-set.php
  *
  * @param resource|mixed $fd Valid PHP stream resource. The stream must be castable to file descriptor,
  * so you most likely won't be able to use any of filtered streams.
  * @param int $events A set of flags indicating the desired event, can be EV_TIMEOUT, EV_READ, EV_WRITE and EV_SIGNAL.
  * The additional flag EV_PERSIST makes the event to persist until {@link event_del}() is
  * called, otherwise the callback is invoked only once.
  * @param callable $callback Callback function to be called when the matching event occurs.
  * @param array $arguments
  *
  * @throws EventException|\InvalidArgumentException
  *
  * @return Event
  */
 public function prepare($fd, $events, $callback, array $arguments = array())
 {
     if ($this->enabled) {
         $this->disable();
     }
     if (!is_callable($callback)) {
         throw new \InvalidArgumentException('Callback must be callable.');
     }
     if (!event_set($this->resource, $fd, $events, array($this, 'invoke'), $this)) {
         throw $this->exception('Could not prepare event (event_set).');
     }
     if ($events & EV_PERSIST) {
         $this->persist = true;
     }
     if (false === event_base_set($this->resource, $this->base->getResource())) {
         throw $this->exception('Could not set event base (event_base_set).');
     }
     $this->callback = $callback;
     $this->arguments = $arguments;
     $this->prepared = true;
     $this->base->registerEvent($this);
     return $this;
 }