[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/zendframework/zend-stdlib/src/ -> CallbackHandler.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Stdlib;
  11  
  12  use ReflectionClass;
  13  
  14  /**
  15   * CallbackHandler
  16   *
  17   * A handler for an event, event, filterchain, etc. Abstracts PHP callbacks,
  18   * primarily to allow for lazy-loading and ensuring availability of default
  19   * arguments (currying).
  20   */
  21  class CallbackHandler
  22  {
  23      /**
  24       * @var string|array|callable PHP callback to invoke
  25       */
  26      protected $callback;
  27  
  28      /**
  29       * Callback metadata, if any
  30       * @var array
  31       */
  32      protected $metadata;
  33  
  34      /**
  35       * PHP version is greater as 5.4rc1?
  36       * @var bool
  37       */
  38      protected static $isPhp54;
  39  
  40      /**
  41       * Constructor
  42       *
  43       * @param  string|array|object|callable $callback PHP callback
  44       * @param  array                        $metadata  Callback metadata
  45       */
  46      public function __construct($callback, array $metadata = array())
  47      {
  48          $this->metadata  = $metadata;
  49          $this->registerCallback($callback);
  50      }
  51  
  52      /**
  53       * Registers the callback provided in the constructor
  54       *
  55       * @param  callable $callback
  56       * @throws Exception\InvalidCallbackException
  57       * @return void
  58       */
  59      protected function registerCallback($callback)
  60      {
  61          if (!is_callable($callback)) {
  62              throw new Exception\InvalidCallbackException('Invalid callback provided; not callable');
  63          }
  64  
  65          $this->callback = $callback;
  66      }
  67  
  68      /**
  69       * Retrieve registered callback
  70       *
  71       * @return callable
  72       */
  73      public function getCallback()
  74      {
  75          return $this->callback;
  76      }
  77  
  78      /**
  79       * Invoke handler
  80       *
  81       * @param  array $args Arguments to pass to callback
  82       * @return mixed
  83       */
  84      public function call(array $args = array())
  85      {
  86          $callback = $this->getCallback();
  87  
  88          // Minor performance tweak, if the callback gets called more than once
  89          if (!isset(static::$isPhp54)) {
  90              static::$isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>=');
  91          }
  92  
  93          $argCount = count($args);
  94  
  95          if (static::$isPhp54 && is_string($callback)) {
  96              $result = $this->validateStringCallbackFor54($callback);
  97  
  98              if ($result !== true && $argCount <= 3) {
  99                  $callback       = $result;
 100                  // Minor performance tweak, if the callback gets called more
 101                  // than once
 102                  $this->callback = $result;
 103              }
 104          }
 105  
 106          // Minor performance tweak; use call_user_func() until > 3 arguments
 107          // reached
 108          switch ($argCount) {
 109              case 0:
 110                  if (static::$isPhp54) {
 111                      return $callback();
 112                  }
 113                  return call_user_func($callback);
 114              case 1:
 115                  if (static::$isPhp54) {
 116                      return $callback(array_shift($args));
 117                  }
 118                  return call_user_func($callback, array_shift($args));
 119              case 2:
 120                  $arg1 = array_shift($args);
 121                  $arg2 = array_shift($args);
 122                  if (static::$isPhp54) {
 123                      return $callback($arg1, $arg2);
 124                  }
 125                  return call_user_func($callback, $arg1, $arg2);
 126              case 3:
 127                  $arg1 = array_shift($args);
 128                  $arg2 = array_shift($args);
 129                  $arg3 = array_shift($args);
 130                  if (static::$isPhp54) {
 131                      return $callback($arg1, $arg2, $arg3);
 132                  }
 133                  return call_user_func($callback, $arg1, $arg2, $arg3);
 134              default:
 135                  return call_user_func_array($callback, $args);
 136          }
 137      }
 138  
 139      /**
 140       * Invoke as functor
 141       *
 142       * @return mixed
 143       */
 144      public function __invoke()
 145      {
 146          return $this->call(func_get_args());
 147      }
 148  
 149      /**
 150       * Get all callback metadata
 151       *
 152       * @return array
 153       */
 154      public function getMetadata()
 155      {
 156          return $this->metadata;
 157      }
 158  
 159      /**
 160       * Retrieve a single metadatum
 161       *
 162       * @param  string $name
 163       * @return mixed
 164       */
 165      public function getMetadatum($name)
 166      {
 167          if (array_key_exists($name, $this->metadata)) {
 168              return $this->metadata[$name];
 169          }
 170          return;
 171      }
 172  
 173      /**
 174       * Validate a static method call
 175       *
 176       * Validates that a static method call in PHP 5.4 will actually work
 177       *
 178       * @param  string $callback
 179       * @return true|array
 180       * @throws Exception\InvalidCallbackException if invalid
 181       */
 182      protected function validateStringCallbackFor54($callback)
 183      {
 184          if (!strstr($callback, '::')) {
 185              return true;
 186          }
 187  
 188          list($class, $method) = explode('::', $callback, 2);
 189  
 190          if (!class_exists($class)) {
 191              throw new Exception\InvalidCallbackException(sprintf(
 192                  'Static method call "%s" refers to a class that does not exist',
 193                  $callback
 194              ));
 195          }
 196  
 197          $r = new ReflectionClass($class);
 198          if (!$r->hasMethod($method)) {
 199              throw new Exception\InvalidCallbackException(sprintf(
 200                  'Static method call "%s" refers to a method that does not exist',
 201                  $callback
 202              ));
 203          }
 204          $m = $r->getMethod($method);
 205          if (!$m->isStatic()) {
 206              throw new Exception\InvalidCallbackException(sprintf(
 207                  'Static method call "%s" refers to a method that is not static',
 208                  $callback
 209              ));
 210          }
 211  
 212          // returning a non boolean value may not be nice for a validate method,
 213          // but that allows the usage of a static string callback without using
 214          // the call_user_func function.
 215          return array($class, $method);
 216      }
 217  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1