Skip to content

Commit 008a572

Browse files
committed
Adding Hooks docs.
1 parent a4dcf8b commit 008a572

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

system/CodeIgniter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@
224224
$benchmark->stop('routing');
225225

226226
//--------------------------------------------------------------------
227-
// Are there any "pre-system" hooks?
227+
// Are there any "pre-controller" hooks?
228228
//--------------------------------------------------------------------
229229

230-
Hooks::trigger('pre_system');
230+
Hooks::trigger('pre_controller');
231231

232232
ob_start();
233233

system/Hooks/Hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static function on($event_name, callable $callback, $priority = HOOKS_PRI
110110
*
111111
* @return bool
112112
*/
113-
public static function trigger($event_name, array $arguments=[]): bool
113+
public static function trigger($event_name, ...$arguments): bool
114114
{
115115
// Read in our Config/events file so that we have them all!
116116
if ( ! self::$haveReadFromFile)

tests/Hooks/HooksTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testHandleEvent()
3737
$result = $arg;
3838
});
3939

40-
$this->assertTrue(Hooks::trigger('foo', ['bar']) );
40+
$this->assertTrue(Hooks::trigger('foo', 'bar') );
4141

4242
$this->assertEquals('bar', $result);
4343
}
@@ -58,7 +58,7 @@ public function testCancelEvent()
5858
$result = 2;
5959
});
6060

61-
$this->assertFalse(Hooks::trigger('foo', ['bar']));
61+
$this->assertFalse(Hooks::trigger('foo', 'bar'));
6262
$this->assertEquals(1, $result);
6363
}
6464

@@ -79,7 +79,7 @@ public function testPriority()
7979
return false;
8080
}, HOOKS_PRIORITY_HIGH);
8181

82-
$this->assertFalse(Hooks::trigger('foo', ['bar']));
82+
$this->assertFalse(Hooks::trigger('foo', 'bar'));
8383
$this->assertEquals(2, $result);
8484
}
8585

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
####################################
2+
Hooks - Extending the Framework Core
3+
####################################
4+
5+
CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking
6+
core files. When CodeIgniter runs it follows a specific execution process. There may be instances, however, when you'd
7+
like to cause some action to take place at a particular stage in the execution process. For example, you might want to run
8+
a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts
9+
in some other location.
10+
11+
Hooks work on a *publish/subscribe* pattern, where a hook, or event, is triggered at some point during the script execution.
12+
Other scripts can "subscribe" to that event by registering with the Hooks class to let it know they want to perform an
13+
action when that hook is triggered.
14+
15+
Enabling Hooks
16+
==============
17+
18+
Hooks are always enabled, and are available globally.
19+
20+
Defining a Hook
21+
===============
22+
23+
Most hooks are defined within the **application/Config/Hooks.php** file. You can subscribe an action to a hook with
24+
the Hooks class' ``on()`` method. The first parameter is the name of the hook to subscribe to. The second parameter is
25+
a callable that will be run when that event is triggered.::
26+
27+
use CodeIgniter\Hooks\Hooks;
28+
29+
Hooks::on('pre_controller', ['MyClass', 'MyFunction']);
30+
31+
In this example, whenever the **pre_controller** hook is executed, an instance of ``MyClass`` is created and the
32+
``MyFunction`` method is ran. Note that the second parameter can be *any* form of
33+
`callable <http://php.net/manual/en/function.is-callable.php>`_ that PHP recognizes::
34+
35+
// Call a standalone function
36+
Hooks::on('pre_controller', 'some_function');
37+
38+
// Call on an instance method
39+
$user = new User();
40+
Hooks::on('pre_controller', [$user, 'some_method']);
41+
42+
// Call on a static method
43+
Hooks::on('pre_controller', 'SomeClass::someMethod');
44+
45+
// Use a Closure
46+
Hooks::on('pre_controller', function(...$params)
47+
{
48+
. . .
49+
});
50+
51+
Setting Priorities
52+
------------------
53+
54+
Since multiple methods can be subscribed to a single event, you will need a way to define in what order those methods
55+
are called. You can do this by passing a priority value as the third parameter of the ``on()`` method. Lower values
56+
are executed first, with a value of 1 having the highest priority, and there being no limit on the lower values.::
57+
58+
Hooks::on('pre_controller', 'some_function', 25);
59+
60+
Any subscribers with the same priority will be executed in the order they were defined.
61+
62+
Three constants are defined for your use, that set some helpful ranges on the values. You are not required to use these
63+
but you might find they aid readability.::
64+
65+
define('HOOKS_PRIORITY_LOW', 200);
66+
define('HOOKS_PRIORITY_NORMAL', 100);
67+
define('HOOKS_PRIORITY_HIGH', 10);
68+
69+
Once sorted, all subscribers are executed in order. If any subscriber returns a boolean false value, then execution of
70+
the subscribers will stop.
71+
72+
Publishing your own Hooks
73+
=========================
74+
75+
The Hooks library makes it simple for you to create hooks into your own code, also. To use this feature, you would simply
76+
need to call the ``trigger()`` method on the **Hooks** class with the name of the hook::
77+
78+
\CodeIgniter\Hooks\Hooks::trigger('some_hook');
79+
80+
You can pass any number of arguments to the subscribers by adding them as additional parameters. Subscribers will be
81+
given the arguments in the same order as defined.::
82+
83+
\CodeIgniter\Hooks\Hooks::trigger('some_hook', $foo, $bar, $baz);
84+
85+
Hooks::on('some_hook', function($foo, $bar, $baz) {
86+
...
87+
});
88+
89+
Hook Points
90+
===========
91+
92+
The following is a list of available hook points:
93+
94+
* **pre_system** Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.
95+
* **pre_controller** Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.
96+
* **post_controller_constructor** Called immediately after your controller is instantiated, but prior to any method calls happening.
97+
* **post_controller** Called immediately after your controller is fully executed.
98+
* **post_system** Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

0 commit comments

Comments
 (0)