Helpers::__callStatic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
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
 * Patchable helpers.
16
 *
17
 * @method static string contextualize() contextualize($pathname)
18
 * @method static string decontextualize() decontextualize($pathname)
19
 * @method static string absolutize_url() absolutize_url($url)
20
 */
21
class Helpers
22
{
23
	static private $mapping = [
24
25
		'contextualize'   => [ __CLASS__, 'default_contextualize' ],
26
		'decontextualize' => [ __CLASS__, 'default_decontextualize' ],
27
		'absolutize_url'  => [ __CLASS__, 'default_absolutize_url' ]
28
29
	];
30
31
	/**
32
	 * Calls the callback of a patchable function.
33
	 *
34
	 * @param string $name Name of the function.
35
	 * @param array $arguments Arguments.
36
	 *
37
	 * @return mixed
38
	 */
39
	static public function __callStatic($name, array $arguments)
40
	{
41
		return call_user_func_array(self::$mapping[$name], $arguments);
42
	}
43
44
	/**
45
	 * Patches a patchable function.
46
	 *
47
	 * @param string $name Name of the function.
48
	 * @param callable $callback Callback.
49
	 *
50
	 * @throws \RuntimeException is attempt to patch an undefined function.
51
	 */
52
	// @codeCoverageIgnoreStart
53
	static public function patch($name, $callback)
54
	{
55
		if (empty(self::$mapping[$name]))
56
		{
57
			throw new \RuntimeException("Undefined patchable: $name.");
58
		}
59
60
		self::$mapping[$name] = $callback;
61
	}
62
	// @codeCoverageIgnoreEnd
63
64
	/*
65
	 * Default implementations
66
	 */
67
68
	static protected function default_contextualize($pathname)
69
	{
70
		return $pathname;
71
	}
72
73
	static protected function default_decontextualize($pathname)
74
	{
75
		return $pathname;
76
	}
77
78
	static protected function default_absolutize_url($url)
79
	{
80
		return 'http://' . $_SERVER['HTTP_HOST'] . $url;
81
	}
82
}
83