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\ActiveRecord;
13
14 /**
15 * Returns the requested model.
16 *
17 * @param string $id Model identifier.
18 *
19 * @return Model
20 */
21 function get_model($id)
22 {
23 return Helpers::get_model($id);
24 }
25
26 /**
27 * Patchable helpers of the ActiveRecord package.
28 */
29 class Helpers
30 {
31 static private $jumptable = [
32
33 'get_model' => [ __CLASS__, 'get_model' ]
34
35 ];
36
37 /**
38 * Calls the callback of a patchable function.
39 *
40 * @param string $name Name of the function.
41 * @param array $arguments Arguments.
42 *
43 * @return mixed
44 */
45 static public function __callstatic($name, array $arguments)
46 {
47 return call_user_func_array(self::$jumptable[$name], $arguments);
48 }
49
50 /**
51 * Patches a patchable function.
52 *
53 * @param string $name Name of the function.
54 * @param collable $callback Callback.
55 *
56 * @throws \RuntimeException is attempt to patch an undefined function.
57 */
58 static public function patch($name, $callback)
59 {
60 if (empty(self::$jumptable[$name]))
61 {
62 throw new \RuntimeException("Undefined patchable: $name.");
63 }
64
65 self::$jumptable[$name] = $callback;
66 }
67
68 /*
69 * Default implementations
70 */
71
72 static private function get_model($id)
73 {
74 throw new \RuntimeException("The function " . __FUNCTION__ . "() needs to be patched.");
75 }
76 }