Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

CallbackHandler.php

Zuletzt modifiziert: 09.10.2024, 12:55 - Dateigröße: 5.87 KiB


001  <?php
002  /**
003   * Zend Framework (http://framework.zend.com/)
004   *
005   * @link      http://github.com/zendframework/zf2 for the canonical source repository
006   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
007   * @license   http://framework.zend.com/license/new-bsd New BSD License
008   */
009   
010  namespace Zend\Stdlib;
011   
012  use ReflectionClass;
013   
014  /**
015   * CallbackHandler
016   *
017   * A handler for an event, event, filterchain, etc. Abstracts PHP callbacks,
018   * primarily to allow for lazy-loading and ensuring availability of default
019   * arguments (currying).
020   */
021  class CallbackHandler
022  {
023      /**
024       * @var string|array|callable PHP callback to invoke
025       */
026      protected $callback;
027   
028      /**
029       * Callback metadata, if any
030       * @var array
031       */
032      protected $metadata;
033   
034      /**
035       * PHP version is greater as 5.4rc1?
036       * @var bool
037       */
038      protected static $isPhp54;
039   
040      /**
041       * Constructor
042       *
043       * @param  string|array|object|callable $callback PHP callback
044       * @param  array                        $metadata  Callback metadata
045       */
046      public function __construct($callback, array $metadata = array())
047      {
048          $this->metadata  = $metadata;
049          $this->registerCallback($callback);
050      }
051   
052      /**
053       * Registers the callback provided in the constructor
054       *
055       * @param  callable $callback
056       * @throws Exception\InvalidCallbackException
057       * @return void
058       */
059      protected function registerCallback($callback)
060      {
061          if (!is_callable($callback)) {
062              throw new Exception\InvalidCallbackException('Invalid callback provided; not callable');
063          }
064   
065          $this->callback = $callback;
066      }
067   
068      /**
069       * Retrieve registered callback
070       *
071       * @return callable
072       */
073      public function getCallback()
074      {
075          return $this->callback;
076      }
077   
078      /**
079       * Invoke handler
080       *
081       * @param  array $args Arguments to pass to callback
082       * @return mixed
083       */
084      public function call(array $args = array())
085      {
086          $callback = $this->getCallback();
087   
088          // Minor performance tweak, if the callback gets called more than once
089          if (!isset(static::$isPhp54)) {
090              static::$isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>=');
091          }
092   
093          $argCount = count($args);
094   
095          if (static::$isPhp54 && is_string($callback)) {
096              $result = $this->validateStringCallbackFor54($callback);
097   
098              if ($result !== true && $argCount <= 3) {
099                  $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  }
218