|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\View\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Nip\View\Methods\MethodsCollection; |
|
7
|
|
|
use Nip\View\Methods\Pipeline\Stages\MethodCollectionStage; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait HasMethodsTrait |
|
11
|
|
|
* @package Nip\View\Traits |
|
12
|
|
|
*/ |
|
13
|
|
|
trait HasMethodsTrait |
|
14
|
|
|
{ |
|
15
|
|
|
protected $engineMethods = null; |
|
16
|
|
|
|
|
17
|
6 |
|
public function addMethodsPipelineStage() |
|
18
|
|
|
{ |
|
19
|
6 |
|
$this->addCallPipeline(new MethodCollectionStage()); |
|
|
|
|
|
|
20
|
6 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return MethodsCollection |
|
24
|
|
|
*/ |
|
25
|
6 |
|
public function getEngineMethods(): MethodsCollection |
|
26
|
|
|
{ |
|
27
|
6 |
|
if ($this->engineMethods === null) { |
|
28
|
6 |
|
$this->initEngineMethods(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
return $this->engineMethods; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param MethodsCollection $engineMethods |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function setEngineMethods(MethodsCollection $engineMethods): void |
|
38
|
|
|
{ |
|
39
|
6 |
|
$this->engineMethods = $engineMethods; |
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $name |
|
44
|
|
|
* @param Closure $callable |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function addMethod($name, Closure $callable) |
|
47
|
|
|
{ |
|
48
|
6 |
|
$this->getEngineMethods()->set($name, $callable); |
|
49
|
6 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Closure[] $methods |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addMethods(array $methods) |
|
55
|
|
|
{ |
|
56
|
|
|
foreach ($methods as $name => $closure) { |
|
57
|
|
|
$this->addMethod($name, $closure); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
protected function initEngineMethods() |
|
62
|
|
|
{ |
|
63
|
6 |
|
$methods = $this->newEngineMethods(); |
|
64
|
6 |
|
$this->setEngineMethods($methods); |
|
65
|
6 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return MethodsCollection |
|
69
|
|
|
*/ |
|
70
|
6 |
|
protected function newEngineMethods() |
|
71
|
|
|
{ |
|
72
|
6 |
|
return new MethodsCollection(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|