1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nip\View; |
5
|
|
|
|
6
|
|
|
use ArrayAccess; |
7
|
|
|
use Nip\View\Extensions\Helpers\HasHelpersTrait; |
8
|
|
|
use Nip\View\ViewFinder\HasViewFinder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class View |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class View implements ViewInterface, ArrayAccess |
15
|
|
|
{ |
16
|
|
|
use Traits\CanRenderTrait; |
17
|
|
|
use Traits\HasDataTrait; |
18
|
|
|
use Traits\HasExtensionsTrait; |
19
|
|
|
use HasHelpersTrait; |
20
|
|
|
use Traits\HasMethodsTrait; |
21
|
|
|
use Traits\HasPathsTrait; |
22
|
|
|
use Traits\HasRequestTrait; |
23
|
|
|
use Traits\MethodsOverloadingTrait; |
24
|
|
|
|
25
|
|
|
use HasViewFinder; |
26
|
|
|
|
27
|
|
|
protected $helpers = []; |
28
|
|
|
protected $blocks = []; |
29
|
11 |
|
|
30
|
|
|
public function __construct() |
31
|
11 |
|
{ |
32
|
11 |
|
$this->addMethodsPipelineStage(); |
33
|
11 |
|
$this->addHelpersExtension(); |
34
|
11 |
|
$this->initFinder(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $name |
39
|
|
|
* @param $block |
40
|
|
|
*/ |
41
|
|
|
public function setBlock($name, $block) |
42
|
|
|
{ |
43
|
|
|
$this->blocks[$name] = $block; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $name |
48
|
|
|
* @param $default |
49
|
|
|
* @return string|null |
50
|
|
|
*/ |
51
|
|
|
public function getBlock($name, $default = null) |
52
|
|
|
{ |
53
|
|
|
return $this->blocks[$name] ?? $default; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $block |
58
|
|
|
*/ |
59
|
|
|
public function render($block = 'default') |
60
|
|
|
{ |
61
|
|
|
if (!empty($this->blocks[$block])) { |
62
|
|
|
$this->load("/" . $this->blocks[$block]); |
63
|
|
|
} else { |
64
|
|
|
trigger_error("No $block block", E_USER_ERROR); |
65
|
4 |
|
} |
66
|
|
|
} |
67
|
4 |
|
|
68
|
|
|
/** |
69
|
|
|
* Builds path for including |
70
|
|
|
* If $view starts with / the path will be relative to the root of the views folder. |
71
|
|
|
* Otherwise to caller file location. |
72
|
|
|
* |
73
|
|
|
* @param string $view |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function buildPath($view) |
77
|
|
|
{ |
78
|
|
|
return $this->getFinder()->find($view); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $block |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function isBlock($block = 'default') |
86
|
|
|
{ |
87
|
|
|
return empty($this->blocks[$block]) ? false : true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Assigns variables in bulk in the current scope |
92
|
|
|
* |
93
|
|
|
* @param array $array |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function assign($array = []) |
97
|
|
|
{ |
98
|
|
|
foreach ($array as $key => $value) { |
99
|
|
|
if (is_string($key)) { |
100
|
|
|
$this->set($key, $value); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|