<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>SithTemplate: Plugins.php Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.2 -->
<div class="navigation" id="top">
<div class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul>
</div>
<div class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File List</span></a></li>
<li><a href="globals.html"><span>File Members</span></a></li>
</ul>
</div>
<div class="header">
<div class="headertitle">
<h1>Plugins.php</h1> </div>
</div>
<div class="contents">
<a href="_plugins_8php.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <?php
<a name="l00013"></a><a class="code" href="class_template_plugins.html">00013</a> <span class="keyword">class </span><a class="code" href="class_template_plugins.html" title="Handles discovery, registration and utilization of plugins.">TemplatePlugins</a> {
<a name="l00017"></a><a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007">00017</a> <span class="keyword">private</span> <a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a> = array();
<a name="l00021"></a><a class="code" href="class_template_plugins.html#a79bcf24c03de22788f74a9a1399a2d92">00021</a> <span class="keyword">private</span> <a class="code" href="class_template_plugins.html#a79bcf24c03de22788f74a9a1399a2d92" title="Elements registry.">$elements</a> = array();
<a name="l00025"></a><a class="code" href="class_template_plugins.html#af9aa580edb4c30ae32a330fe74b6fff4">00025</a> <span class="keyword">public</span> <a class="code" href="class_template_plugins.html#af9aa580edb4c30ae32a330fe74b6fff4" title="Plugins&#39; search paths.">$searchPaths</a> = array();
<a name="l00026"></a>00026
<a name="l00032"></a><a class="code" href="class_template_plugins.html#a35839e04a5701ee2b7efdc5ce9c06501">00032</a> <span class="keyword">public</span> function <a class="code" href="class_template_plugins.html#a35839e04a5701ee2b7efdc5ce9c06501" title="Constructor.">__construct</a>(array $builtins) {
<a name="l00033"></a>00033 $this->elements = $builtins;
<a name="l00034"></a>00034 }
<a name="l00035"></a>00035
<a name="l00047"></a><a class="code" href="class_template_plugins.html#acad3fb00a013432da30dc682905384b0">00047</a> <span class="keyword">public</span> function <a class="code" href="class_template_plugins.html#acad3fb00a013432da30dc682905384b0" title="Load single plugin.">load</a>(<a class="code" href="class_template_compiler_ex.html" title="Primary compiler driver.">TemplateCompilerEx</a> $compiler, $node, $plugin, $pluginFile = null, $noThrow = <span class="keyword">false</span>) {
<a name="l00048"></a>00048 <span class="comment">// if already loaded, then abort</span>
<a name="l00049"></a>00049 <span class="keywordflow">if</span> (isset($this->plugins[$plugin])) <span class="keywordflow">return</span>;
<a name="l00050"></a>00050
<a name="l00051"></a>00051 <span class="comment">// sanity check</span>
<a name="l00052"></a>00052 <a class="code" href="class_template_utils.html#a9ded2c07232134c25d3cf9300822e7d8" title="Checks whether element is allowed.">TemplateUtils::checkIfAllowed</a>($compiler, <span class="stringliteral">'plugin'</span>, $plugin, $node);
<a name="l00053"></a>00053 <span class="keywordflow">if</span> (!$pluginFile) $pluginFile = $this-><a class="code" href="class_template_plugins.html#a1a64cc16940f3fa5ff0a711a803bbe25" title="Looks for plugin file on all search paths.">findPlugin</a>($plugin);
<a name="l00054"></a>00054 $className = <span class="stringliteral">'Template'</span>.$plugin.<span class="stringliteral">'Plugin'</span>;
<a name="l00055"></a>00055
<a name="l00056"></a>00056 <span class="keywordflow">if</span> (
<a name="l00057"></a>00057 $pluginFile === <span class="keyword">false</span> || (!include_once $pluginFile) ||
<a name="l00058"></a>00058 !class_exists($className) || !<a class="code" href="class_template_utils.html#a1282a8a4e4a86cd2af8aef16f73434da" title="Check whether class implements given interface.">TemplateUtils::doesImplement</a>($className, <span class="stringliteral">'ITemplatePlugin'</span>)
<a name="l00059"></a>00059 ) {
<a name="l00060"></a>00060 <span class="keywordflow">if</span> ($noThrow) <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00061"></a>00061
<a name="l00062"></a>00062 <span class="keywordflow">throw</span> <span class="keyword">new</span> <a class="code" href="class_template_error.html" title="Main and currently the only exception type thrown by SithTemplate internals.">TemplateError</a>(
<a name="l00063"></a>00063 <span class="stringliteral">'Could not load plugin: "'</span>.$plugin.<span class="stringliteral">'". Either file or main class does not exists, or is invalid'</span>,
<a name="l00064"></a>00064 <a class="code" href="class_template_error.html#a694d041c1d63438166372fb6db18af7b" title="An invalid plugin error.">TemplateError::E_INVALID_PLUGIN</a>
<a name="l00065"></a>00065 );
<a name="l00066"></a>00066 }
<a name="l00067"></a>00067
<a name="l00068"></a>00068 <span class="comment">// create instance and register handlers</span>
<a name="l00069"></a>00069 $pluginObj = <span class="keyword">new</span> $className;
<a name="l00070"></a>00070 $handlers = $pluginObj->providedHandlers();
<a name="l00071"></a>00071 <span class="keywordflow">if</span> (!isset($handlers[<span class="stringliteral">'tags'</span>])) $handlers[<span class="stringliteral">'tags'</span>] = array();
<a name="l00072"></a>00072 <span class="keywordflow">if</span> (!isset($handlers[<span class="stringliteral">'filters'</span>])) $handlers[<span class="stringliteral">'filters'</span>] = array();
<a name="l00073"></a>00073 <span class="keywordflow">if</span> (!isset($handlers[<span class="stringliteral">'hooks'</span>])) $handlers[<span class="stringliteral">'hooks'</span>] = array();
<a name="l00074"></a>00074
<a name="l00075"></a>00075 $this-><span class="keyword">register</span>($plugin, $pluginObj, <span class="stringliteral">'tag'</span>, $handlers[<span class="stringliteral">'tags'</span>]);
<a name="l00076"></a>00076 $this-><span class="keyword">register</span>($plugin, $pluginObj, <span class="stringliteral">'filter'</span>, $handlers[<span class="stringliteral">'filters'</span>]);
<a name="l00077"></a>00077 <span class="comment">// too special case after all</span>
<a name="l00078"></a>00078 $this-><a class="code" href="class_template_plugins.html#aadc5f1a6e0ee4961413dd6f5b091619f" title="Register given hooks.">registerHooks</a>($plugin, $pluginObj, $handlers[<span class="stringliteral">'hooks'</span>]);
<a name="l00079"></a>00079
<a name="l00080"></a>00080 $this->plugins[$plugin] = $pluginObj;
<a name="l00081"></a>00081 }
<a name="l00082"></a>00082
<a name="l00090"></a><a class="code" href="class_template_plugins.html#a0bea8d628f5b57b8bf8c930980ab8896">00090</a> <span class="keyword">public</span> function <a class="code" href="class_template_plugins.html#a0bea8d628f5b57b8bf8c930980ab8896" title="Load multiple plugins.">loadMultiple</a>(<a class="code" href="class_template_compiler_ex.html" title="Primary compiler driver.">TemplateCompilerEx</a> $compiler, $node, <a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a>) {
<a name="l00091"></a>00091 <span class="keywordflow">foreach</span> ($this-><a class="code" href="class_template_plugins.html#a562e9a9f92fa59024c38398cc6fefc3e" title="Looks for multiple plugins&#39; files on all search paths.">findPlugins</a>(<a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a>) as $plugin => $pluginFile) {
<a name="l00092"></a>00092 $this-><a class="code" href="class_template_plugins.html#acad3fb00a013432da30dc682905384b0" title="Load single plugin.">load</a>($compiler, $node, $plugin, $pluginFile);
<a name="l00093"></a>00093 }
<a name="l00094"></a>00094 }
<a name="l00095"></a>00095
<a name="l00104"></a><a class="code" href="class_template_plugins.html#a9fdc46282fea5afe6850469897e9a0dd">00104</a> <span class="keyword">public</span> function <a class="code" href="class_template_plugins.html#a9fdc46282fea5afe6850469897e9a0dd" title="Check whether given element is registered.">known</a>($type, $name) {
<a name="l00105"></a>00105 <span class="keywordflow">if</span> ($type == <span class="stringliteral">'plugin'</span>) <span class="keywordflow">return</span> isset($this->plugins[$name]);
<a name="l00106"></a>00106 <span class="keywordflow">return</span> isset($this->elements[$type.<span class="charliteral">'s'</span>][$name]);
<a name="l00107"></a>00107 }
<a name="l00108"></a>00108
<a name="l00117"></a><a class="code" href="class_template_plugins.html#ac2bccff1c1d49f63a0f66efc118220a7">00117</a> <span class="keyword">public</span> function &<span class="keyword">get</span>($type, $name) {
<a name="l00118"></a>00118 <span class="keywordflow">return</span> $this->elements[$type.<span class="charliteral">'s'</span>][$name];
<a name="l00119"></a>00119 }
<a name="l00120"></a>00120
<a name="l00128"></a><a class="code" href="class_template_plugins.html#a1a64cc16940f3fa5ff0a711a803bbe25">00128</a> <span class="keyword">private</span> function <a class="code" href="class_template_plugins.html#a1a64cc16940f3fa5ff0a711a803bbe25" title="Looks for plugin file on all search paths.">findPlugin</a>($plugin) {
<a name="l00129"></a>00129 $found = <span class="keyword">false</span>;
<a name="l00130"></a>00130
<a name="l00131"></a>00131 <span class="keywordflow">foreach</span> ($this->searchPaths as &$path) {
<a name="l00132"></a>00132 <span class="keywordflow">if</span> (file_exists($path.$plugin.<span class="stringliteral">'.plugin.php'</span>)) {
<a name="l00133"></a>00133 $found = $path.$plugin.<span class="stringliteral">'.plugin.php'</span>;
<a name="l00134"></a>00134 <span class="keywordflow">break</span>;
<a name="l00135"></a>00135 }
<a name="l00136"></a>00136 }
<a name="l00137"></a>00137
<a name="l00138"></a>00138 <span class="keywordflow">return</span> $found;
<a name="l00139"></a>00139 }
<a name="l00140"></a>00140
<a name="l00148"></a><a class="code" href="class_template_plugins.html#a562e9a9f92fa59024c38398cc6fefc3e">00148</a> <span class="keyword">private</span> function <a class="code" href="class_template_plugins.html#a562e9a9f92fa59024c38398cc6fefc3e" title="Looks for multiple plugins&#39; files on all search paths.">findPlugins</a>(<a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a>) {
<a name="l00149"></a>00149 $found = array();
<a name="l00150"></a>00150
<a name="l00151"></a>00151 <span class="keywordflow">foreach</span> ($this->searchPaths as &$path) {
<a name="l00152"></a>00152 <span class="keywordflow">if</span> (is_bool(<a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a>)) {
<a name="l00153"></a>00153 <span class="keywordflow">foreach</span> (glob($path.<span class="stringliteral">'*.plugin.php'</span>) as $file) {
<a name="l00154"></a>00154 $plugin = mb_substr(pathinfo($file, PATHINFO_BASENAME), 0, -11);
<a name="l00155"></a>00155 $found[$plugin] = $file;
<a name="l00156"></a>00156 }
<a name="l00157"></a>00157 } <span class="keywordflow">else</span> {
<a name="l00158"></a>00158 <span class="keywordflow">foreach</span> (<a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a> as &$plugin) {
<a name="l00159"></a>00159 <span class="keywordflow">if</span> (isset($found[$plugin])) <span class="keywordflow">continue</span>;
<a name="l00160"></a>00160
<a name="l00161"></a>00161 <span class="keywordflow">if</span> (file_exists($path.$plugin.<span class="stringliteral">'.plugin.php'</span>)) {
<a name="l00162"></a>00162 $found[$plugin] = $path.$plugin.<span class="stringliteral">'.plugin.php'</span>;
<a name="l00163"></a>00163 }
<a name="l00164"></a>00164 }
<a name="l00165"></a>00165 }
<a name="l00166"></a>00166 }
<a name="l00167"></a>00167
<a name="l00168"></a>00168
<a name="l00169"></a>00169 <span class="keywordflow">if</span> (is_array(<a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a>) && ($notFound = array_diff(<a class="code" href="class_template_plugins.html#a36adfd02aa0e82327f89485df90b3007" title="Already loaded plugins.">$plugins</a>, array_keys($found)))) {
<a name="l00170"></a>00170 <span class="keywordflow">throw</span> <span class="keyword">new</span> <a class="code" href="class_template_error.html" title="Main and currently the only exception type thrown by SithTemplate internals.">TemplateError</a>(
<a name="l00171"></a>00171 <span class="stringliteral">'Several of requested plugins have not been found: "'</span>.implode(<span class="stringliteral">', '</span>, $notFound).<span class="charliteral">'"'</span>,
<a name="l00172"></a>00172 <a class="code" href="class_template_error.html#adcc81c7e44a611c01e633f0c7f88a89d" title="An unknown plugin error.">TemplateError::E_UNKNOWN_PLUGIN</a>
<a name="l00173"></a>00173 );
<a name="l00174"></a>00174 }
<a name="l00175"></a>00175 <span class="keywordflow">return</span> $found;
<a name="l00176"></a>00176 }
<a name="l00177"></a>00177
<a name="l00186"></a><a class="code" href="class_template_plugins.html#a30612b19259f49e4525d608cca00936f">00186</a> <span class="keyword">private</span> function <span class="keyword">register</span>($plugin, <a class="code" href="interface_i_template_plugin.html" title="Interface required for all plugins.">ITemplatePlugin</a> $pluginObj, $type, array &$handlers) {
<a name="l00187"></a>00187 <span class="keywordflow">foreach</span> ($handlers as $element => &$elementInfo) {
<a name="l00188"></a>00188 <span class="keywordflow">if</span> (isset($this->elements[$type.<span class="charliteral">'s'</span>][$element])) {
<a name="l00189"></a>00189 <span class="keywordflow">throw</span> <span class="keyword">new</span> <a class="code" href="class_template_error.html" title="Main and currently the only exception type thrown by SithTemplate internals.">TemplateError</a>(
<a name="l00190"></a>00190 <span class="stringliteral">'Element collision while loading plugin "'</span>.$plugin.<span class="stringliteral">'" - '</span>.$type.<span class="stringliteral">' "'</span>.$element.<span class="stringliteral">'" already exists'</span>,
<a name="l00191"></a>00191 <a class="code" href="class_template_error.html#a197d3675b19ddab587d765167ba142d2" title="An invalid handler error.">TemplateError::E_INVALID_HANDLER</a>
<a name="l00192"></a>00192 );
<a name="l00193"></a>00193 }
<a name="l00194"></a>00194
<a name="l00195"></a>00195 $elementInfo[<span class="stringliteral">'plugin'</span>] = $plugin;
<a name="l00196"></a>00196 <span class="keywordflow">if</span> (!is_array($elementInfo[<span class="stringliteral">'handler'</span>]) && !isset($elementInfo[<span class="stringliteral">'standalone'</span>])) {
<a name="l00197"></a>00197 $elementInfo[<span class="stringliteral">'handler'</span>] = array($pluginObj, $elementInfo[<span class="stringliteral">'handler'</span>]);
<a name="l00198"></a>00198 }
<a name="l00199"></a>00199
<a name="l00200"></a>00200 $this->elements[$type.<span class="charliteral">'s'</span>][$element] = $elementInfo;
<a name="l00201"></a>00201 }
<a name="l00202"></a>00202 }
<a name="l00203"></a>00203
<a name="l00211"></a><a class="code" href="class_template_plugins.html#aadc5f1a6e0ee4961413dd6f5b091619f">00211</a> <span class="keyword">private</span> function <a class="code" href="class_template_plugins.html#aadc5f1a6e0ee4961413dd6f5b091619f" title="Register given hooks.">registerHooks</a>($plugin, <a class="code" href="interface_i_template_plugin.html" title="Interface required for all plugins.">ITemplatePlugin</a> $pluginObj, array &$hooks) {
<a name="l00212"></a>00212 $allHooks = &$this->elements[<span class="stringliteral">'hooks'</span>];
<a name="l00213"></a>00213 <span class="keywordflow">foreach</span> ($hooks as $hook => &$handlers) {
<a name="l00214"></a>00214 <span class="keywordflow">if</span> (!isset($allHooks[$hook])) $allHooks[$hook] = array();
<a name="l00215"></a>00215
<a name="l00216"></a>00216 <span class="keywordflow">foreach</span> ($handlers as &$handler) {
<a name="l00217"></a>00217 $handler[<span class="stringliteral">'plugin'</span>] = $plugin;
<a name="l00218"></a>00218
<a name="l00219"></a>00219 <span class="keywordflow">if</span> (!is_array($handler[<span class="stringliteral">'handler'</span>]) && !isset($handler[<span class="stringliteral">'standalone'</span>])) {
<a name="l00220"></a>00220 $handler[<span class="stringliteral">'handler'</span>] = array($pluginObj, $handler[<span class="stringliteral">'handler'</span>]);
<a name="l00221"></a>00221 }
<a name="l00222"></a>00222
<a name="l00223"></a>00223 $allHooks[$hook][] = $handler;
<a name="l00224"></a>00224 }
<a name="l00225"></a>00225 }
<a name="l00226"></a>00226 }
<a name="l00227"></a>00227 }
</pre></div></div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Fri Jan 14 2011 20:08:37 for SithTemplate by 
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.2 </small></address>
</body>
</html>