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\Routing;
13
14 /**
15 * Contextualize a pathname.
16 *
17 * @param string $pathname
18 *
19 * @return string
20 */
21 function contextualize($pathname)
22 {
23 return Helpers::contextualize($pathname);
24 }
25
26 /**
27 * Decontextualize a pathname.
28 *
29 * @param string $pathname
30 *
31 * @return string
32 */
33 function decontextualize($pathname)
34 {
35 return Helpers::decontextualize($pathname);
36 }
37
38 /**
39 * Patchable helpers.
40 */
41 class Helpers
42 {
43 static private $jumptable = array
44 (
45 'contextualize' => array(__CLASS__, 'contextualize'),
46 'decontextualize' => array(__CLASS__, 'decontextualize')
47 );
48
49 /**
50 * Calls the callback of a patchable function.
51 *
52 * @param string $name Name of the function.
53 * @param array $arguments Arguments.
54 *
55 * @return mixed
56 */
57 static public function __callstatic($name, array $arguments)
58 {
59 return call_user_func_array(self::$jumptable[$name], $arguments);
60 }
61
62 /**
63 * Patches a patchable function.
64 *
65 * @param string $name Name of the function.
66 * @param collable $callback Callback.
67 *
68 * @throws \RuntimeException is attempt to patch an undefined function.
69 */
70 static public function patch($name, $callback)
71 {
72 if (empty(self::$jumptable[$name]))
73 {
74 throw new \RuntimeException("Undefined patchable: $name.");
75 }
76
77 self::$jumptable[$name] = $callback;
78 }
79
80 /*
81 * Default implementations
82 */
83
84 static private function contextualize($pathname)
85 {
86 return $pathname;
87 }
88
89 static private function decontextualize($pathname)
90 {
91 return $pathname;
92 }
93 }