summaryrefslogtreecommitdiff
path: root/src/Runtime/CaptureRuntime.php
blob: 3f37c59fcc54691057654bd2a3002de60441487d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

namespace Smarty\Runtime;
use Smarty\Template;

/**
 * Runtime Extension Capture
 *


 * @author     Uwe Tews
 */
class CaptureRuntime {

	/**
	 * Stack of capture parameter
	 *
	 * @var array
	 */
	private $captureStack = [];

	/**
	 * Current open capture sections
	 *
	 * @var int
	 */
	private $captureCount = 0;

	/**
	 * Count stack
	 *
	 * @var int[]
	 */
	private $countStack = [];

	/**
	 * Named buffer
	 *
	 * @var string[]
	 */
	private $namedBuffer = [];

	/**
	 * Open capture section
	 *
	 * @param \Smarty\Template $_template
	 * @param string $buffer capture name
	 * @param string $assign variable name
	 * @param string $append variable name
	 */
	public function open(Template $_template, $buffer, $assign, $append) {

		$this->registerCallbacks($_template);

		$this->captureStack[] = [
			$buffer,
			$assign,
			$append,
		];
		$this->captureCount++;
		ob_start();
	}

	/**
	 * Register callbacks in template class
	 *
	 * @param \Smarty\Template $_template
	 */
	private function registerCallbacks(Template $_template) {

		foreach ($_template->startRenderCallbacks as $callback) {
			if (is_array($callback) && get_class($callback[0]) == self::class) {
				// already registered
				return;
			}
		}

		$_template->startRenderCallbacks[] = [
			$this,
			'startRender',
		];
		$_template->endRenderCallbacks[] = [
			$this,
			'endRender',
		];
		$this->startRender($_template);
	}

	/**
	 * Start render callback
	 *
	 * @param \Smarty\Template $_template
	 */
	public function startRender(Template $_template) {
		$this->countStack[] = $this->captureCount;
		$this->captureCount = 0;
	}

	/**
	 * Close capture section
	 *
	 * @param \Smarty\Template $_template
	 *
	 * @throws \Smarty\Exception
	 */
	public function close(Template $_template) {
		if ($this->captureCount) {
			[$buffer, $assign, $append] = array_pop($this->captureStack);
			$this->captureCount--;
			if (isset($assign)) {
				$_template->assign($assign, ob_get_contents());
			}
			if (isset($append)) {
				$_template->append($append, ob_get_contents());
			}
			$this->namedBuffer[$buffer] = ob_get_clean();
		} else {
			$this->error($_template);
		}
	}

	/**
	 * Error exception on not matching {capture}{/capture}
	 *
	 * @param \Smarty\Template $_template
	 *
	 * @throws \Smarty\Exception
	 */
	public function error(Template $_template) {
		throw new \Smarty\Exception("Not matching {capture}{/capture} in '{$_template->template_resource}'");
	}

	/**
	 * Return content of named capture buffer by key or as array
	 *
	 * @param \Smarty\Template $_template
	 * @param string|null $name
	 *
	 * @return string|string[]|null
	 */
	public function getBuffer(Template $_template, $name = null) {
		if (isset($name)) {
			return $this->namedBuffer[$name] ?? null;
		} else {
			return $this->namedBuffer;
		}
	}

	/**
	 * End render callback
	 *
	 * @param \Smarty\Template $_template
	 *
	 * @throws \Smarty\Exception
	 */
	public function endRender(Template $_template) {
		if ($this->captureCount) {
			$this->error($_template);
		} else {
			$this->captureCount = array_pop($this->countStack);
		}
	}
}