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 \AframeVR\Core\Helpers\MockComponent; |
30
|
|
|
use \Closure; |
31
|
|
|
|
32
|
|
|
final class Mixin extends AssetsAbstract implements MixinInterface |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Array of mocked components |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $mock_components = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Load component for this Mixin |
44
|
|
|
* |
45
|
|
|
* @param string $cmpnt_name |
46
|
|
|
* @throws \AframeVR\Core\Exceptions\BadComponentCallException |
47
|
|
|
* @return object|null |
48
|
|
|
*/ |
49
|
4 |
View Code Duplication |
public function component(string $cmpnt_name) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
/* Does this mixin already have this component loaded */ |
52
|
4 |
|
if (! array_key_exists($cmpnt_name, $this->mock_components)) { |
53
|
4 |
|
$cmpnt = sprintf( |
54
|
4 |
|
'\AframeVR\Core\Components\%s\%sComponent', |
55
|
|
|
ucfirst($cmpnt_name), |
56
|
|
|
ucfirst($cmpnt_name) |
57
|
|
|
); |
58
|
|
|
/* Does called component exist */ |
59
|
4 |
|
if (class_exists($cmpnt)) { |
60
|
|
|
/* Create Closure to mock compnent to be applied to entity using this mixin */ |
61
|
3 |
|
$this->mock_components[$cmpnt_name] = new MockComponent; |
62
|
|
|
} else { |
63
|
1 |
|
throw new BadComponentCallException($cmpnt); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
return $this->mock_components[$cmpnt_name] ?? null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Handle mixin components |
72
|
|
|
* |
73
|
|
|
* Since we might need to customize these to have |
74
|
|
|
* custom components loaded as $this->methosd aswell therefore |
75
|
|
|
* we have these placeholder magic methods here |
76
|
|
|
* |
77
|
|
|
* @param string $component_name |
78
|
|
|
* @param array $args |
79
|
|
|
*/ |
80
|
4 |
View Code Duplication |
public function __call(string $component_name, array $args) |
|
|
|
|
81
|
|
|
{ |
82
|
4 |
|
if (! method_exists($this, $component_name)) { |
83
|
4 |
|
$this->{$component_name} = Closure::bind(function () use ($component_name) { |
84
|
4 |
|
return $this->component($component_name); |
85
|
4 |
|
}, $this, get_class()); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
return call_user_func($this->{$component_name}, $args); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.