|
1
|
|
|
<?php |
|
2
|
|
|
namespace ntentan\honam\engines\php\helpers\form; |
|
3
|
|
|
|
|
4
|
|
|
use ntentan\honam\exceptions\HonamException; |
|
5
|
|
|
use ntentan\utils\Text; |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
trait ElementFactory { |
|
8
|
|
|
|
|
9
|
|
|
private $caller; |
|
10
|
|
|
|
|
11
|
|
|
public function findMethodClass($name) { |
|
12
|
|
|
if($this->caller === null) { |
|
13
|
|
|
return null; |
|
14
|
|
|
} else if(method_exists($this->caller, $name)) { |
|
15
|
|
|
return $this->caller; |
|
16
|
|
|
} else { |
|
17
|
|
|
return $this->caller->findMethodClass($name); |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function __call($name, $arguments) |
|
22
|
|
|
{ |
|
23
|
|
|
// Check if the method exists in the parent class which we call the caller. |
|
24
|
|
|
$methodClass = $this->findMethodClass($name); |
|
25
|
|
|
if($methodClass !== null) { |
|
26
|
|
|
return call_user_func_array([$methodClass, $name], $arguments); |
|
27
|
|
|
} |
|
28
|
|
|
if(str_starts_with($name, "open_")) { |
|
29
|
|
|
$containerCheck = true; |
|
30
|
|
|
$name = substr($name, 5); |
|
31
|
|
|
} else if (str_starts_with($name, "close_")) { |
|
32
|
|
|
return $this->popContainer(substr($name, 6)); |
|
|
|
|
|
|
33
|
|
|
} else { |
|
34
|
|
|
$containerCheck = false; |
|
35
|
|
|
} |
|
36
|
|
|
$elementClass = new \ReflectionClass(__NAMESPACE__ . "\\" . Text::ucamelize($name)); |
|
37
|
|
|
$element = $elementClass->newInstanceArgs($arguments == null ? array() : $arguments); |
|
38
|
|
|
$element->setTemplateRenderer($this->templateRenderer); |
|
39
|
|
|
$element->setCaller($this); |
|
40
|
|
|
|
|
41
|
|
|
if($containerCheck && is_a($element, __NAMESPACE__ . "\\Container")) { |
|
42
|
|
|
$this->pushContainer($name, $element); |
|
|
|
|
|
|
43
|
|
|
} else if ($containerCheck) { |
|
44
|
|
|
throw new HonamException("Element $name is not a container. Use the open_ prefix only when creating a container."); |
|
45
|
|
|
} else if (!$containerCheck) { |
|
|
|
|
|
|
46
|
|
|
$this->getActiveContainer()->add($element); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $element; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setCaller($caller) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->caller = $caller; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: