summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2014-03-05 23:28:55 +0100
committerAndrey Andreev <narf@devilix.net>2014-03-05 23:28:55 +0100
commit8351404ee1698076192317fd39f093c1f52bf48f (patch)
treeb814be909e78ed252d41c39147dd432216fc3d34
parent18767e31711656e9e2648fbe051b74ebbefd3f2e (diff)
Add support for simpler (Callable) hooks (issue #2917)
-rw-r--r--system/core/Hooks.php11
-rw-r--r--user_guide_src/source/changelog.rst1
-rw-r--r--user_guide_src/source/general/hooks.rst10
3 files changed, 20 insertions, 2 deletions
diff --git a/system/core/Hooks.php b/system/core/Hooks.php
index f6eff4b3f..767dc4cd9 100644
--- a/system/core/Hooks.php
+++ b/system/core/Hooks.php
@@ -154,7 +154,16 @@ class CI_Hooks {
*/
protected function _run_hook($data)
{
- if ( ! is_array($data))
+ // Closures/lambda functions and array($object, 'method') callables
+ if (is_callable($data))
+ {
+ is_array($data)
+ ? $data[0]->{$data[1]}()
+ : $data();
+
+ return TRUE;
+ }
+ elseif ( ! is_array($data))
{
return FALSE;
}
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index a2250dfda..423755b89 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -509,6 +509,7 @@ Release Date: Not Released
- :doc:`Hooks Library <general/hooks>` changes include:
+ - Added support for closure hooks (or anything that ``is_callable()`` returns TRUE for).
- Renamed method ``_call_hook()`` to ``call_hook()``.
- Class instances are now stored in order to maintain their state.
diff --git a/user_guide_src/source/general/hooks.rst b/user_guide_src/source/general/hooks.rst
index 1046c48ae..ffe3fef39 100644
--- a/user_guide_src/source/general/hooks.rst
+++ b/user_guide_src/source/general/hooks.rst
@@ -23,7 +23,7 @@ following item in the **application/config/config.php** file::
Defining a Hook
===============
-Hooks are defined in **application/config/hooks.php** file.
+Hooks are defined in the **application/config/hooks.php** file.
Each hook is specified as an array with this prototype::
$hook['pre_controller'] = array(
@@ -56,6 +56,14 @@ defined in your associative hook array:
- **params** Any parameters you wish to pass to your script. This item
is optional.
+If you're running PHP 5.3+, you can also use lambda/anoymous functions
+(or closures) as hooks, with a simpler syntax::
+
+ $hook['post_controller'] = function()
+ {
+ /* do something here */
+ };
+
Multiple Calls to the Same Hook
===============================