1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
4
|
|
|
|
5
|
|
|
class Renderer |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Singleton The reference to *Singleton* instance of this class |
9
|
|
|
*/ |
10
|
|
|
private static $instance; |
11
|
|
|
|
12
|
|
|
private $registered_components = array(); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Returns the *Singleton* instance of this class. |
16
|
|
|
* |
17
|
|
|
* @return Singleton The *Singleton* instance. |
18
|
|
|
*/ |
19
|
|
|
public static function get_instance() |
20
|
|
|
{ |
21
|
|
|
if( null === static::$instance ) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
static::$instance = new static(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
return static::$instance; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function render_component( $type, array $props = array() ) |
29
|
|
|
{ |
30
|
|
|
$component = $this->create_component( $type, $props ); |
|
|
|
|
31
|
|
|
return $component->render(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Register a custom component class. If the given component name is similar |
37
|
|
|
* to the name of one of the core components, it will override it. If a |
38
|
|
|
* custom component with a similar name has already been registered, an |
39
|
|
|
* exception will be thrown. |
40
|
|
|
* |
41
|
|
|
* @param string $type |
42
|
|
|
* @param string $class_name |
43
|
|
|
* @throws \RuntimeException |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function register_component( $type, $class_name ) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
if( !in_array($type, $this->registered_components) ) |
48
|
|
|
{ |
49
|
|
|
$this->registered_components[$type] = $class_name; |
50
|
|
|
} |
51
|
|
|
else throw new \RuntimeException("A component of type '$type' has already been registered."); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @param type $type |
57
|
|
|
* @param type $props |
58
|
|
|
* @return type |
59
|
|
|
*/ |
60
|
|
|
private function create_component( $type, $props ) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$component = $this->create_registered_component ($type, $props ); |
64
|
|
|
} catch (\RuntimeException $ex) { |
65
|
|
|
$component = $this->create_core_component ($type, $props ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $component; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @param type $type |
74
|
|
|
* @param type $props |
75
|
|
|
* @throws \RuntimeException |
76
|
|
|
*/ |
77
|
|
|
private function create_core_component( $type, $props ) |
78
|
|
|
{ |
79
|
|
|
$file_name = __DIR__."/components/$type/controller.php"; |
80
|
|
|
$class_name = 'Amarkal\\UI\\Component_'.$type; |
81
|
|
|
|
82
|
|
|
// Load one of the core components |
83
|
|
|
if(!class_exists($class_name)) |
84
|
|
|
{ |
85
|
|
|
if( file_exists( $file_name ) ) |
86
|
|
|
{ |
87
|
|
|
require_once $file_name; |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
|
|
throw new \RuntimeException("A component of type '$type' does not exist."); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new $class_name($props); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
* @param type $type |
101
|
|
|
* @param type $props |
102
|
|
|
* @throws \RuntimeException |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
private function create_registered_component( $type, $props ) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if(in_array($type, $this->registered_components)) |
107
|
|
|
{ |
108
|
|
|
$class_name = $this->registered_components[$type]; |
109
|
|
|
return new $class_name($props); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new \RuntimeException("A component of type '$type' has not been registered."); |
113
|
|
|
} |
114
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: