1: <?php
2:
3: namespace Deimos\Flow\Traits;
4:
5: use Deimos\Flow\Configure;
6: use Deimos\Flow\Flow;
7:
8: trait Block
9: {
10:
11: /**
12: * @var Configure
13: */
14: protected $configure;
15:
16: /**
17: * @var array
18: */
19: protected $blocks = [];
20:
21: /**
22: * @var string
23: */
24: protected $lastName;
25:
26: /**
27: * @var string
28: */
29: protected $type;
30:
31: /**
32: * @var string
33: */
34: protected $selectView;
35:
36: /**
37: * @var bool
38: */
39: protected $isStarted;
40:
41: /**
42: * @param Flow $flow
43: * @param string $name
44: * @param string $type
45: */
46: public function start($flow, $name, $type = 'inner')
47: {
48: $this->isStarted = true;
49: $this->lastName = $name;
50: $this->selectView = $flow->selectView();
51: $this->type = $type;
52:
53: if (!isset($this->blocks[$this->lastName]))
54: {
55: $this->blocks[$this->lastName] = '';
56: }
57:
58: ob_start();
59: }
60:
61: /**
62: * @throws \InvalidArgumentException
63: */
64: public function end()
65: {
66: if (!$this->isStarted)
67: {
68: throw new \InvalidArgumentException('Block not started!');
69: }
70:
71: $this->isStarted = false;
72:
73: $result = ob_get_clean();
74:
75: if ($this->type === 'inner')
76: {
77: if (
78: empty($this->blocks[$this->lastName]) ||
79: empty($this->configure->getExtendsAll($this->selectView))
80: )
81: {
82: // default value
83: $this->blocks[$this->lastName] = $result;
84: }
85: }
86: else
87: {
88:
89: if ($this->type === 'append')
90: {
91: $this->blocks[$this->lastName] .= $result;
92: }
93:
94: if ($this->type === 'prepend')
95: {
96: $this->blocks[$this->lastName]
97: = $result . $this->blocks[$this->lastName];
98: }
99:
100: }
101:
102: $this->display();
103:
104: }
105:
106: protected function display()
107: {
108: if (!empty($this->configure->getExtendsAll($this->selectView)))
109: {
110: echo $this->getBlock($this->lastName);
111:
112: return;
113: }
114: }
115:
116: /**
117: * @param $name
118: *
119: * @return mixed
120: */
121: public function getBlock($name)
122: {
123: return $this->blocks[$name];
124: }
125:
126: }