Adding Callback-Typehint
authorTim Düsterhus <timwolla@arcor.de>
Tue, 18 Oct 2011 15:01:08 +0000 (17:01 +0200)
committerTim Düsterhus <timwolla@arcor.de>
Tue, 18 Oct 2011 15:01:08 +0000 (17:01 +0200)
You can use wcf\system\Callback as typehint if you want to force a callable function as parameter.

wcfsetup/install/files/lib/system/Callback.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/package/PackageInstallationNodeBuilder.class.php
wcfsetup/install/files/lib/util/DirectoryUtil.class.php

diff --git a/wcfsetup/install/files/lib/system/Callback.class.php b/wcfsetup/install/files/lib/system/Callback.class.php
new file mode 100644 (file)
index 0000000..01013f3
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+namespace wcf\system;
+use wcf\system\exception\SystemException;
+
+/**
+ * Represents a callback
+ * 
+ * @author     Tim Düsterhus
+ * @copyright  2011 Tim Düsterhus
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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());
+       }
+}
index 219865add074040e19742dc388c64710bbb57cc5..7e2e8066ed53830e29275472b3ecf831b5286a3b 100644 (file)
@@ -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
index 6bc10f1cd7e05eb6034827026323962c1caf14b0..7e09a2c044233eb0002507d337de96ed57e8c5fe 100644 (file)
@@ -1,17 +1,18 @@
 <?php
 namespace wcf\util;
+use wcf\system\Callback;
 use wcf\system\exception\SystemException;
 
 /**
-* Contains directory-related functions
-* 
-* @author      Tim Düsterhus
-* @copyright   2011 Tim Düsterhus
-* @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
-* @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 <http://opensource.org/licenses/lgpl-license.php>
+ * @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;