]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_runtime_capture.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_runtime_capture.php
1 <?php
2
3 /**
4  * Runtime Extension Capture
5  *
6  * @package    Smarty
7  * @subpackage PluginsInternal
8  * @author     Uwe Tews
9  */
10 class Smarty_Internal_Runtime_Capture
11 {
12     /**
13      * Flag that this instance  will not be cached
14      *
15      * @var bool
16      */
17     public $isPrivateExtension = true;
18
19     /**
20      * Stack of capture parameter
21      *
22      * @var array
23      */
24     private $captureStack = array();
25
26     /**
27      * Current open capture sections
28      *
29      * @var int
30      */
31     private $captureCount = 0;
32
33     /**
34      * Count stack
35      *
36      * @var int[]
37      */
38     private $countStack = array();
39
40     /**
41      * Named buffer
42      *
43      * @var string[]
44      */
45     private $namedBuffer = array();
46
47     /**
48      * Flag if callbacks are registered
49      *
50      * @var bool
51      */
52     private $isRegistered = false;
53
54     /**
55      * Open capture section
56      *
57      * @param \Smarty_Internal_Template $_template
58      * @param string                    $buffer capture name
59      * @param string                    $assign variable name
60      * @param string                    $append variable name
61      */
62     public function open(Smarty_Internal_Template $_template, $buffer, $assign, $append)
63     {
64         if (!$this->isRegistered) {
65             $this->register($_template);
66         }
67         $this->captureStack[] = array($buffer, $assign, $append);
68         $this->captureCount ++;
69         ob_start();
70     }
71
72     /**
73      * Register callbacks in template class
74      *
75      * @param \Smarty_Internal_Template $_template
76      */
77     private function register(Smarty_Internal_Template $_template)
78     {
79         $_template->startRenderCallbacks[] = array($this, 'startRender');
80         $_template->endRenderCallbacks[] = array($this, 'endRender');
81         $this->startRender($_template);
82         $this->isRegistered = true;
83     }
84
85     /**
86      * Start render callback
87      *
88      * @param \Smarty_Internal_Template $_template
89      */
90     public function startRender(Smarty_Internal_Template $_template)
91     {
92         $this->countStack[] = $this->captureCount;
93         $this->captureCount = 0;
94     }
95
96     /**
97      * Close capture section
98      *
99      * @param \Smarty_Internal_Template $_template
100      *
101      * @throws \SmartyException
102      */
103     public function close(Smarty_Internal_Template $_template)
104     {
105         if ($this->captureCount) {
106             list($buffer, $assign, $append) = array_pop($this->captureStack);
107             $this->captureCount --;
108             if (isset($assign)) {
109                 $_template->assign($assign, ob_get_contents());
110             }
111             if (isset($append)) {
112                 $_template->append($append, ob_get_contents());
113             }
114             $this->namedBuffer[ $buffer ] = ob_get_clean();
115         } else {
116             $this->error($_template);
117         }
118     }
119
120     /**
121      * Error exception on not matching {capture}{/capture}
122      *
123      * @param \Smarty_Internal_Template $_template
124      *
125      * @throws \SmartyException
126      */
127     public function error(Smarty_Internal_Template $_template)
128     {
129         throw new SmartyException("Not matching {capture}{/capture} in \"{$_template->template_resource}\"");
130     }
131
132     /**
133      * Return content of named capture buffer
134      *
135      * @param \Smarty_Internal_Template $_template
136      * @param                           $name
137      *
138      * @return null
139      */
140     public function getBuffer(Smarty_Internal_Template $_template, $name)
141     {
142         return isset($this->namedBuffer[ $name ]) ? $this->namedBuffer[ $name ] : null;
143     }
144
145     /**
146      * End render callback
147      *
148      * @param \Smarty_Internal_Template $_template
149      *
150      * @throws \SmartyException
151      */
152     public function endRender(Smarty_Internal_Template $_template)
153     {
154         if ($this->captureCount) {
155             $this->error($_template);
156         } else {
157             $this->captureCount = array_pop($this->countStack);
158         }
159     }
160
161 }