diff options
author | Jakob Sack <kde@jakobsack.de> | 2011-07-27 19:07:28 +0200 |
---|---|---|
committer | Jakob Sack <kde@jakobsack.de> | 2011-07-27 19:07:28 +0200 |
commit | f5c0dbd947d45520550b68dae77a894f871a0758 (patch) | |
tree | 5e3c8fe6a12af97990fd529fb0d692ddc9400386 /lib/hook.php | |
parent | 59e55b711b36a41c4e4c621b5a46f9d601e15c38 (diff) | |
download | nextcloud-server-f5c0dbd947d45520550b68dae77a894f871a0758.tar.gz nextcloud-server-f5c0dbd947d45520550b68dae77a894f871a0758.zip |
Use autoload
Diffstat (limited to 'lib/hook.php')
-rw-r--r-- | lib/hook.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/hook.php b/lib/hook.php new file mode 100644 index 00000000000..08b6d5e8b6b --- /dev/null +++ b/lib/hook.php @@ -0,0 +1,69 @@ +<?php + +/** + * This class manages the hooks. It basically provides two functions: adding + * slots and emitting signals. + */ +class OC_HOOK{ + static private $registered = array(); + + /** + * @brief connects a function to a hook + * @param $signalclass class name of emitter + * @param $signalname name of signal + * @param $slotclass class name of slot + * @param $slotname name of slot + * @returns true/false + * + * This function makes it very easy to connect to use hooks. + * + * TODO: write example + */ + static public function connect( $signalclass, $signalname, $slotclass, $slotname ){ + // Cerate the data structure + if( !array_key_exists( $signalclass, self::$registered )){ + self::$registered[$signalclass] = array(); + } + if( !array_key_exists( $signalname, self::$registered[$signalclass] )){ + self::$registered[$signalclass][$signalname] = array(); + } + + // register hook + self::$registered[$signalclass][$signalname][] = array( + "class" => $slotclass, + "name" => $slotname ); + + // No chance for failure ;-) + return true; + } + + /** + * @brief emitts a signal + * @param $signalclass class name of emitter + * @param $signalname name of signal + * @param $params defautl: array() array with additional data + * @returns true if slots exists or false if not + * + * Emits a signal. To get data from the slot use references! + * + * TODO: write example + */ + static public function emit( $signalclass, $signalname, $params = array()){ + // Return false if there are no slots + if( !array_key_exists( $signalclass, self::$registered )){ + return false; + } + if( !array_key_exists( $signalname, self::$registered[$signalclass] )){ + return false; + } + + // Call all slots + foreach( self::$registered[$signalclass][$signalname] as $i ){ + call_user_func( array( $i["class"], $i["name"] ), $params ); + } + + // return true + return true; + } +} + |