From: Tim Düsterhus Date: Tue, 18 Oct 2011 15:01:08 +0000 (+0200) Subject: Adding Callback-Typehint X-Git-Tag: 2.0.0_Beta_1~1680^2~8^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=c19ff714ba53b39d5c5f39de08bb4e181a336fd2;p=GitHub%2FWoltLab%2FWCF.git Adding Callback-Typehint You can use wcf\system\Callback as typehint if you want to force a callable function as parameter. --- diff --git a/wcfsetup/install/files/lib/system/Callback.class.php b/wcfsetup/install/files/lib/system/Callback.class.php new file mode 100644 index 0000000000..01013f3291 --- /dev/null +++ b/wcfsetup/install/files/lib/system/Callback.class.php @@ -0,0 +1,44 @@ + + * @package com.woltlab.wcf + * @subpackage system + * @category Community Framework + */ +final class Callback { + /** + * The callback + * + * @var callback + */ + private $callback = null; + + /** + * Checks whether the callback is callable. + * + * @param callback $callback + */ + public function __construct($callback) { + if (!is_callable($callback)) { + throw new SystemException('Given callback is not callable.'); + } + + $this->callback = $callback; + } + + /** + * Invokes our callback. All parameters are simply passed through. + * + * @return mixed + */ + public function __invoke() { + return call_user_func_array($this->callback, func_get_args()); + } +} diff --git a/wcfsetup/install/files/lib/system/package/PackageInstallationNodeBuilder.class.php b/wcfsetup/install/files/lib/system/package/PackageInstallationNodeBuilder.class.php index 219865add0..7e2e8066ed 100644 --- a/wcfsetup/install/files/lib/system/package/PackageInstallationNodeBuilder.class.php +++ b/wcfsetup/install/files/lib/system/package/PackageInstallationNodeBuilder.class.php @@ -2,6 +2,7 @@ namespace wcf\system\package; use wcf\data\package\installation\queue\PackageInstallationQueue; use wcf\data\package\installation\queue\PackageInstallationQueueEditor; +use wcf\system\Callback; use wcf\system\WCF; use wcf\util\FileUtil; use wcf\util\StringUtil; @@ -282,15 +283,10 @@ class PackageInstallationNodeBuilder { * nodes to provide to be descendants of the new node. If you intend * to insert more than a single node, you should prefer shiftNodes(). * - * @param string $beforeNode - * @param function $callback + * @param string $beforeNode + * @param wcf\system\Callback $callback */ - public function insertNode($beforeNode, $callback) { - // verify if callback is a valid function - if (!is_callable($callback)) { - throw new SystemException("Provided callback is not a callable function."); - } - + public function insertNode($beforeNode, Callback $callback) { $newNode = $this->getToken(); // update descendants diff --git a/wcfsetup/install/files/lib/util/DirectoryUtil.class.php b/wcfsetup/install/files/lib/util/DirectoryUtil.class.php index 6bc10f1cd7..7e09a2c044 100644 --- a/wcfsetup/install/files/lib/util/DirectoryUtil.class.php +++ b/wcfsetup/install/files/lib/util/DirectoryUtil.class.php @@ -1,17 +1,18 @@ -* @package com.woltlab.wcf -* @subpackage util -* @category Community Framework -*/ + * Contains directory-related functions + * + * @author Tim Düsterhus + * @copyright 2011 Tim Düsterhus + * @license GNU Lesser General Public License + * @package com.woltlab.wcf + * @subpackage util + * @category Community Framework + */ class DirectoryUtil { /** * @var \DirectoryIterator @@ -254,16 +255,14 @@ class DirectoryUtil { /** * Executes a callback on each file and returns false if callback is invalid. * - * @param callback $callback valid callback - * @param string $pattern callback is only applied to files matching the given pattern + * @param wcf\system\Callback $callback + * @param string $pattern callback is only applied to files matching the given pattern * @return boolean */ - public function executeCallback($callback, $pattern = '') { - if (!is_callable($callback)) return false; - + public function executeCallback(Callback $callback, $pattern = '') { $files = $this->getFileObjects(self::SORT_NONE, $pattern); foreach ($files as $filename => $obj) { - call_user_func($callback, $filename, $obj); + $callback($filename, $obj); } return true;