Texy\Texy::addHandler PHP Method

addHandler() final public method

Add new event handler.
final public addHandler ( $event, $callback ) : void
return void
    public final function addHandler($event, $callback)
    {
        if (!is_callable($callback)) {
            $able = is_callable($callback, TRUE, $textual);
            throw new \InvalidArgumentException("Handler '{$textual}' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
        }
        $this->handlers[$event][] = $callback;
    }

Usage Example

Beispiel #1
0
 /**
  * @param \Texy|\Texy\Texy $texy
  * @param ImagePipe $imagePipe
  */
 public static function register($texy, ImagePipe $imagePipe)
 {
     if (!$texy instanceof \Texy && !$texy instanceof \Texy\Texy) {
         throw new \InvalidArgumentException('The $texy parameter is not instance of Texy or Texy\\Texy');
     }
     $texy->addHandler("image", function ($invocation, $image, $link) use($imagePipe) {
         if (!$invocation instanceof \TexyHandlerInvocation && !$invocation instanceof \Texy\HandlerInvocation) {
             throw new \InvalidArgumentException('The $invocation parameter is not instance of TexyHandlerInvocation or Texy\\HandlerInvocation');
         }
         if (!$image instanceof \TexyImage && !$image instanceof \Texy\Image) {
             throw new \InvalidArgumentException('The $image parameter is not instance of TexyImage or Texy\\Image');
         }
         $arguments = Helpers::prepareMacroArguments($image->URL);
         try {
             $image->URL = $imagePipe->setNamespace($arguments["namespace"])->request($arguments["name"], $arguments["size"], $arguments["flags"], TRUE);
         } catch (FileNotFoundException $e) {
             $image->URL = $arguments["name"];
             if (!empty($arguments["size"])) {
                 list($image->width, $image->height) = explode("x", $arguments["size"]);
             }
         }
         return $invocation->proceed($image, $link);
     });
 }