1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 20, 2016 - 9:12:11 PM |
5
|
|
|
* Contact [email protected] |
6
|
|
|
* @copyright 2016 Marko Kungla - https://github.com/mkungla |
7
|
|
|
* @license The MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* @category AframeVR |
10
|
|
|
* @package aframe-php |
11
|
|
|
* |
12
|
|
|
* Lang PHP (php version >= 7) |
13
|
|
|
* Encoding UTF-8 |
14
|
|
|
* File Mixin.php |
15
|
|
|
* Code format PSR-2 and 12 |
16
|
|
|
* @link https://github.com/mkungla/aframe-php |
17
|
|
|
^ @issues https://github.com/mkungla/aframe-php/issues |
18
|
|
|
* ******************************************************************** |
19
|
|
|
* Contributors: |
20
|
|
|
* @author Marko Kungla <[email protected]> |
21
|
|
|
* ******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
* @formatter:on */ |
24
|
|
|
namespace AframeVR\Core\Assets; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Core\Exceptions\BadComponentCallException; |
27
|
|
|
use \AframeVR\Interfaces\Core\Assets\MixinInterface; |
28
|
|
|
use \AframeVR\Core\Helpers\AssetsAbstract; |
29
|
|
|
use \Closure; |
30
|
|
|
|
31
|
|
|
final class Mixin extends AssetsAbstract implements MixinInterface |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Array of used components |
36
|
|
|
* |
37
|
|
|
* @var array $components |
38
|
|
|
*/ |
39
|
|
|
protected $components = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Load component for this entity |
43
|
|
|
* |
44
|
|
|
* @param string $component_name |
45
|
|
|
* @throws \AframeVR\Core\Exceptions\BadComponentCallException |
46
|
|
|
* @return object|null |
47
|
|
|
*/ |
48
|
4 |
View Code Duplication |
public function component(string $component_name) |
|
|
|
|
49
|
|
|
{ |
50
|
4 |
|
$component_name = strtolower($component_name); |
51
|
|
|
|
52
|
|
|
/* Does this mixin already have this component loaded */ |
53
|
4 |
|
if (! array_key_exists($component_name, $this->components)) { |
54
|
4 |
|
$component = sprintf('\AframeVR\Core\Components\%s\%sComponent', ucfirst($component_name), ucfirst($component_name)); |
|
|
|
|
55
|
|
|
/* Does called component exist */ |
56
|
4 |
|
if (class_exists($component)) { |
57
|
3 |
|
$this->components[$component_name] = $this->componentClosure(); |
58
|
|
|
} else { |
59
|
1 |
|
throw new BadComponentCallException($component_name); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
return $this->components[$component_name] ?? null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Handle mixin components |
68
|
|
|
* |
69
|
|
|
* Since we might need to customize these to have |
70
|
|
|
* custom components loaded as $this->methosd aswell therefore |
71
|
|
|
* we have these placeholder magic methods here |
72
|
|
|
* |
73
|
|
|
* @param string $component_name |
74
|
|
|
* @param array $args |
75
|
|
|
*/ |
76
|
4 |
View Code Duplication |
public function __call(string $component_name, array $args) |
|
|
|
|
77
|
|
|
{ |
78
|
4 |
|
if (! method_exists($this, $component_name)) { |
79
|
4 |
|
$this->{$component_name} = Closure::bind(function () use ($component_name) { |
80
|
4 |
|
return $this->component($component_name); |
81
|
4 |
|
}, $this, get_class()); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
return call_user_func($this->{$component_name}, $args); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create Closure to mock compnent to be aplied to entity using this mixin |
89
|
|
|
* |
90
|
|
|
* @return object |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
protected function componentClosure() |
93
|
|
|
{ |
94
|
|
|
return new class() { |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Record all menthod / args to be called when mixin is used on some entity |
98
|
|
|
* |
99
|
|
|
* @var array |
100
|
|
|
*/ |
101
|
|
|
protected $component_methods = array(); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Call passes all calls to no existing methods to self::methodProvider |
105
|
|
|
* |
106
|
|
|
* @param string $method |
107
|
|
|
* @param array $args |
108
|
|
|
* @throws InvalidComponentMethodException |
109
|
|
|
* @return object |
|
|
|
|
110
|
|
|
*/ |
111
|
3 |
|
public function __call(string $method, $args) |
112
|
|
|
{ |
113
|
3 |
|
$this->component_methods[$method] = $args; |
114
|
3 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
}; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.