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\Prototype;
13
14 use ICanBoogie\Object;
15
16 /**
17 * Returns the value of an object's property.
18 *
19 * This function is called as a last chance to get the value of a property before the
20 * {@link PropertyNotDefined} exception is thrown.
21 *
22 * @param Object $target Target object from which the property should be retrieved.
23 * @param string $property Name of the property.
24 * @param bool $success `true` if the value was successfully retrieved, `false` otherwise.
25 *
26 * @return mixed
27 */
28 function last_chance_get($target, $property, &$success)
29 {
30 # this code is needed to preserve arguments passed by reference
31
32 return call_user_func_array(__NAMESPACE__ . '\Helpers::last_chance_get', array($target, $property, &$success));
33 }
34
35 /**
36 * Sets the value of an object's property.
37 *
38 * This function is called as a last chance to set the value of a property before the
39 * property is created with the `public` visibility.
40 *
41 * @param Object $target Target object in which the property should be stored.
42 * @param string $property Name of the property.
43 * @param mixed $value Value of the property.
44 * @param bool $success `true` if the value was successfully stored, `false` otherwise.
45 *
46 * @return mixed
47 */
48 function last_chance_set($target, $property, $value, &$success)
49 {
50 # this code is needed to preserve arguments passed by reference
51
52 return call_user_func_array(__NAMESPACE__ . '\Helpers::last_chance_set', [ $target, $property, $value, &$success ]);
53 }
54
55 /**
56 * Patchable helpers.
57 */
58 class Helpers
59 {
60 static private $jumptable = [
61
62 'last_chance_get' => [ __CLASS__, 'last_chance_get' ],
63 'last_chance_set' => [ __CLASS__, 'last_chance_set' ]
64
65 ];
66
67 /**
68 * Calls the callback of a patchable function.
69 *
70 * @param string $name Name of the function.
71 * @param array $arguments Arguments.
72 *
73 * @return mixed
74 */
75 static public function __callstatic($name, array $arguments)
76 {
77 return call_user_func_array(self::$jumptable[$name], $arguments);
78 }
79
80 /**
81 * Patches a patchable function.
82 *
83 * @param string $name Name of the function.
84 * @param collable $callback Callback.
85 *
86 * @throws \RuntimeException is attempt to patch an undefined function.
87 */
88 static public function patch($name, $callback)
89 {
90 if (empty(self::$jumptable[$name]))
91 {
92 throw new \RuntimeException("Undefined patchable: $name.");
93 }
94
95 self::$jumptable[$name] = $callback;
96 }
97
98 /*
99 * Default implementations
100 */
101
102 static private function last_chance_get($target, $property, &$success=false)
103 {
104
105 }
106
107 static private function last_chance_set($target, $property, $value, &$success=false)
108 {
109
110 }
111 }