1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie\Event;
13
14 class Hooks
15 {
16 /**
17 * Synthesizes a configuration suitable to create {@link Events} instances, from the "hooks"
18 * config.
19 *
20 * @param array $fragments Configuration fragments.
21 *
22 * @throws \InvalidArgumentException in attempt to specify an invalid event callback.
23 *
24 * @return array
25 */
26 static public function synthesize_config(array $fragments)
27 {
28 $events = [];
29
30 foreach ($fragments as $pathname => $fragment)
31 {
32 if (empty($fragment['events']))
33 {
34 continue;
35 }
36
37 foreach ($fragment['events'] as $type => $callback)
38 {
39 if (!is_callable($callback, true))
40 {
41 throw new \InvalidArgumentException(format
42 (
43 'Event callback must be a string, %type given: :callback in %path.', [
44
45 'type' => gettype($callback),
46 'callback' => $callback,
47 'path' => $pathname
48
49 ]
50 ));
51 }
52
53 #
54 # because modules are ordered by weight (most important are first), we can
55 # push callbacks instead of unshifting them.
56 #
57
58 $events[$type][] = $callback;
59 }
60 }
61
62 return $events;
63 }
64
65 /**
66 * Returns an {@link Events} instance created with the hooks from the `events` config.
67 *
68 * @param \ICanBoogie\Core $core
69 *
70 * @return \ICanBoogie\Events
71 */
72 static public function core_lazy_get_events(\ICanBoogie\Core $core)
73 {
74 return new \ICanBoogie\Events($core->configs['events']);
75 }
76 }