@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * compiles the provided string down to php code. |
29 | 29 | * |
30 | - * @param string $templateStr the template to compile |
|
30 | + * @param string $template the template to compile |
|
31 | 31 | * |
32 | 32 | * @return string a compiled php code string |
33 | 33 | */ |
@@ -39,6 +39,7 @@ discard block |
||
39 | 39 | * @see Dwoo_Core::addPlugin |
40 | 40 | * |
41 | 41 | * @param array $customPlugins an array of custom plugins |
42 | + * @return void |
|
42 | 43 | */ |
43 | 44 | public function setCustomPlugins(array $customPlugins); |
44 | 45 | |
@@ -49,6 +50,7 @@ discard block |
||
49 | 50 | * set it on the Dwoo object as it will be passed onto the compiler automatically |
50 | 51 | * |
51 | 52 | * @param Dwoo_Security_Policy $policy the security policy object |
53 | + * @return void |
|
52 | 54 | */ |
53 | 55 | public function setSecurityPolicy(Dwoo_Security_Policy $policy = null); |
54 | 56 | } |
@@ -24,31 +24,31 @@ |
||
24 | 24 | */ |
25 | 25 | interface Dwoo_ICompiler |
26 | 26 | { |
27 | - /** |
|
28 | - * compiles the provided string down to php code. |
|
29 | - * |
|
30 | - * @param string $templateStr the template to compile |
|
31 | - * |
|
32 | - * @return string a compiled php code string |
|
33 | - */ |
|
34 | - public function compile(Dwoo_Core $dwoo, Dwoo_ITemplate $template); |
|
27 | + /** |
|
28 | + * compiles the provided string down to php code. |
|
29 | + * |
|
30 | + * @param string $templateStr the template to compile |
|
31 | + * |
|
32 | + * @return string a compiled php code string |
|
33 | + */ |
|
34 | + public function compile(Dwoo_Core $dwoo, Dwoo_ITemplate $template); |
|
35 | 35 | |
36 | - /** |
|
37 | - * adds the custom plugins loaded into Dwoo to the compiler so it can load them. |
|
38 | - * |
|
39 | - * @see Dwoo_Core::addPlugin |
|
40 | - * |
|
41 | - * @param array $customPlugins an array of custom plugins |
|
42 | - */ |
|
43 | - public function setCustomPlugins(array $customPlugins); |
|
36 | + /** |
|
37 | + * adds the custom plugins loaded into Dwoo to the compiler so it can load them. |
|
38 | + * |
|
39 | + * @see Dwoo_Core::addPlugin |
|
40 | + * |
|
41 | + * @param array $customPlugins an array of custom plugins |
|
42 | + */ |
|
43 | + public function setCustomPlugins(array $customPlugins); |
|
44 | 44 | |
45 | - /** |
|
46 | - * sets the security policy object to enforce some php security settings. |
|
47 | - * |
|
48 | - * use this if untrusted persons can modify templates, |
|
49 | - * set it on the Dwoo object as it will be passed onto the compiler automatically |
|
50 | - * |
|
51 | - * @param Dwoo_Security_Policy $policy the security policy object |
|
52 | - */ |
|
53 | - public function setSecurityPolicy(Dwoo_Security_Policy $policy = null); |
|
45 | + /** |
|
46 | + * sets the security policy object to enforce some php security settings. |
|
47 | + * |
|
48 | + * use this if untrusted persons can modify templates, |
|
49 | + * set it on the Dwoo object as it will be passed onto the compiler automatically |
|
50 | + * |
|
51 | + * @param Dwoo_Security_Policy $policy the security policy object |
|
52 | + */ |
|
53 | + public function setSecurityPolicy(Dwoo_Security_Policy $policy = null); |
|
54 | 54 | } |
@@ -47,6 +47,9 @@ |
||
47 | 47 | |
48 | 48 | protected $corePluginDir; |
49 | 49 | |
50 | + /** |
|
51 | + * @param string $cacheDir |
|
52 | + */ |
|
50 | 53 | public function __construct($cacheDir) |
51 | 54 | { |
52 | 55 | $this->corePluginDir = DWOO_DIRECTORY.'plugins'; |
@@ -19,143 +19,143 @@ |
||
19 | 19 | */ |
20 | 20 | class Dwoo_Loader implements Dwoo_ILoader |
21 | 21 | { |
22 | - /** |
|
23 | - * stores the plugin directories. |
|
24 | - * |
|
25 | - * @see addDirectory |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $paths = array(); |
|
22 | + /** |
|
23 | + * stores the plugin directories. |
|
24 | + * |
|
25 | + * @see addDirectory |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $paths = array(); |
|
30 | 30 | |
31 | - /** |
|
32 | - * stores the plugins names/paths relationships |
|
33 | - * don't edit this on your own, use addDirectory. |
|
34 | - * |
|
35 | - * @see addDirectory |
|
36 | - * |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - protected $classPath = array(); |
|
31 | + /** |
|
32 | + * stores the plugins names/paths relationships |
|
33 | + * don't edit this on your own, use addDirectory. |
|
34 | + * |
|
35 | + * @see addDirectory |
|
36 | + * |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + protected $classPath = array(); |
|
40 | 40 | |
41 | - /** |
|
42 | - * path where class paths cache files are written. |
|
43 | - * |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $cacheDir; |
|
41 | + /** |
|
42 | + * path where class paths cache files are written. |
|
43 | + * |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $cacheDir; |
|
47 | 47 | |
48 | - protected $corePluginDir; |
|
48 | + protected $corePluginDir; |
|
49 | 49 | |
50 | - public function __construct($cacheDir) |
|
51 | - { |
|
52 | - $this->corePluginDir = DWOO_DIRECTORY.'plugins'; |
|
53 | - $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
|
50 | + public function __construct($cacheDir) |
|
51 | + { |
|
52 | + $this->corePluginDir = DWOO_DIRECTORY.'plugins'; |
|
53 | + $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
|
54 | 54 | |
55 | - // include class paths or rebuild paths if the cache file isn't there |
|
56 | - $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'; |
|
57 | - if (file_exists($cacheFile)) { |
|
58 | - $classpath = file_get_contents($cacheFile); |
|
59 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
60 | - } else { |
|
61 | - $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); |
|
62 | - } |
|
63 | - } |
|
55 | + // include class paths or rebuild paths if the cache file isn't there |
|
56 | + $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'; |
|
57 | + if (file_exists($cacheFile)) { |
|
58 | + $classpath = file_get_contents($cacheFile); |
|
59 | + $this->classPath = unserialize($classpath) + $this->classPath; |
|
60 | + } else { |
|
61 | + $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); |
|
62 | + } |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * rebuilds class paths, scans the given directory recursively and saves all paths in the given file. |
|
67 | - * |
|
68 | - * @param string $path the plugin path to scan |
|
69 | - * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten |
|
70 | - * |
|
71 | - * @throws Dwoo_Exception |
|
72 | - */ |
|
73 | - protected function rebuildClassPathCache($path, $cacheFile) |
|
74 | - { |
|
75 | - if ($cacheFile !== false) { |
|
76 | - $tmp = $this->classPath; |
|
77 | - $this->classPath = array(); |
|
78 | - } |
|
65 | + /** |
|
66 | + * rebuilds class paths, scans the given directory recursively and saves all paths in the given file. |
|
67 | + * |
|
68 | + * @param string $path the plugin path to scan |
|
69 | + * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten |
|
70 | + * |
|
71 | + * @throws Dwoo_Exception |
|
72 | + */ |
|
73 | + protected function rebuildClassPathCache($path, $cacheFile) |
|
74 | + { |
|
75 | + if ($cacheFile !== false) { |
|
76 | + $tmp = $this->classPath; |
|
77 | + $this->classPath = array(); |
|
78 | + } |
|
79 | 79 | |
80 | - // iterates over all files/folders |
|
81 | - $list = glob(rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'*'); |
|
82 | - if (is_array($list)) { |
|
83 | - foreach ($list as $f) { |
|
84 | - if (is_dir($f)) { |
|
85 | - $this->rebuildClassPathCache($f, false); |
|
86 | - } else { |
|
87 | - $this->classPath[str_replace(array('function.', 'block.', 'modifier.', 'outputfilter.', 'filter.', 'prefilter.', 'postfilter.', 'pre.', 'post.', 'output.', 'shared.', 'helper.'), '', basename($f, '.php'))] = $f; |
|
88 | - } |
|
89 | - } |
|
90 | - } |
|
80 | + // iterates over all files/folders |
|
81 | + $list = glob(rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'*'); |
|
82 | + if (is_array($list)) { |
|
83 | + foreach ($list as $f) { |
|
84 | + if (is_dir($f)) { |
|
85 | + $this->rebuildClassPathCache($f, false); |
|
86 | + } else { |
|
87 | + $this->classPath[str_replace(array('function.', 'block.', 'modifier.', 'outputfilter.', 'filter.', 'prefilter.', 'postfilter.', 'pre.', 'post.', 'output.', 'shared.', 'helper.'), '', basename($f, '.php'))] = $f; |
|
88 | + } |
|
89 | + } |
|
90 | + } |
|
91 | 91 | |
92 | - // save in file if it's the first call (not recursed) |
|
93 | - if ($cacheFile !== false) { |
|
94 | - if (!file_put_contents($cacheFile, serialize($this->classPath))) { |
|
95 | - throw new Dwoo_Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); |
|
96 | - } |
|
97 | - $this->classPath += $tmp; |
|
98 | - } |
|
99 | - } |
|
92 | + // save in file if it's the first call (not recursed) |
|
93 | + if ($cacheFile !== false) { |
|
94 | + if (!file_put_contents($cacheFile, serialize($this->classPath))) { |
|
95 | + throw new Dwoo_Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); |
|
96 | + } |
|
97 | + $this->classPath += $tmp; |
|
98 | + } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * loads a plugin file. |
|
103 | - * |
|
104 | - * @param string $class the plugin name, without the Dwoo_Plugin_ prefix |
|
105 | - * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it has just been added, defaults to true |
|
106 | - * |
|
107 | - * @throws Dwoo_Exception |
|
108 | - */ |
|
109 | - public function loadPlugin($class, $forceRehash = true) |
|
110 | - { |
|
111 | - // An unknown class was requested (maybe newly added) or the |
|
112 | - // include failed so we rebuild the cache. include() will fail |
|
113 | - // with an uncatchable error if the file doesn't exist, which |
|
114 | - // usually means that the cache is stale and must be rebuilt, |
|
115 | - // so we check for that before trying to include() the plugin. |
|
116 | - if (!isset($this->classPath[$class]) || |
|
117 | - !is_readable($this->classPath[$class]) || |
|
118 | - !(include $this->classPath[$class])) { |
|
119 | - if ($forceRehash) { |
|
120 | - $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'); |
|
121 | - foreach ($this->paths as $path => $file) { |
|
122 | - $this->rebuildClassPathCache($path, $file); |
|
123 | - } |
|
124 | - if (isset($this->classPath[$class])) { |
|
125 | - include $this->classPath[$class]; |
|
126 | - } |
|
127 | - throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
128 | - } |
|
129 | - throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
130 | - } |
|
131 | - } |
|
101 | + /** |
|
102 | + * loads a plugin file. |
|
103 | + * |
|
104 | + * @param string $class the plugin name, without the Dwoo_Plugin_ prefix |
|
105 | + * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it has just been added, defaults to true |
|
106 | + * |
|
107 | + * @throws Dwoo_Exception |
|
108 | + */ |
|
109 | + public function loadPlugin($class, $forceRehash = true) |
|
110 | + { |
|
111 | + // An unknown class was requested (maybe newly added) or the |
|
112 | + // include failed so we rebuild the cache. include() will fail |
|
113 | + // with an uncatchable error if the file doesn't exist, which |
|
114 | + // usually means that the cache is stale and must be rebuilt, |
|
115 | + // so we check for that before trying to include() the plugin. |
|
116 | + if (!isset($this->classPath[$class]) || |
|
117 | + !is_readable($this->classPath[$class]) || |
|
118 | + !(include $this->classPath[$class])) { |
|
119 | + if ($forceRehash) { |
|
120 | + $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'); |
|
121 | + foreach ($this->paths as $path => $file) { |
|
122 | + $this->rebuildClassPathCache($path, $file); |
|
123 | + } |
|
124 | + if (isset($this->classPath[$class])) { |
|
125 | + include $this->classPath[$class]; |
|
126 | + } |
|
127 | + throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
128 | + } |
|
129 | + throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
130 | + } |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * adds a plugin directory, the plugins found in the new plugin directory |
|
135 | - * will take precedence over the other directories (including the default |
|
136 | - * dwoo plugin directory), you can use this for example to override plugins |
|
137 | - * in a specific directory for a specific application while keeping all your |
|
138 | - * usual plugins in the same place for all applications. |
|
139 | - * TOCOM don't forget that php functions overrides are not rehashed so you |
|
140 | - * need to clear the classpath caches by hand when adding those. |
|
141 | - * |
|
142 | - * @param string $pluginDirectory the plugin path to scan |
|
143 | - * |
|
144 | - * @throws Dwoo_Exception |
|
145 | - */ |
|
146 | - public function addDirectory($pluginDirectory) |
|
147 | - { |
|
148 | - $pluginDir = realpath($pluginDirectory); |
|
149 | - if (!$pluginDir) { |
|
150 | - throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory); |
|
151 | - } |
|
152 | - $cacheFile = $this->cacheDir.'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo_Core::RELEASE_TAG.'.php'; |
|
153 | - $this->paths[$pluginDir] = $cacheFile; |
|
154 | - if (file_exists($cacheFile)) { |
|
155 | - $classpath = file_get_contents($cacheFile); |
|
156 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
157 | - } else { |
|
158 | - $this->rebuildClassPathCache($pluginDir, $cacheFile); |
|
159 | - } |
|
160 | - } |
|
133 | + /** |
|
134 | + * adds a plugin directory, the plugins found in the new plugin directory |
|
135 | + * will take precedence over the other directories (including the default |
|
136 | + * dwoo plugin directory), you can use this for example to override plugins |
|
137 | + * in a specific directory for a specific application while keeping all your |
|
138 | + * usual plugins in the same place for all applications. |
|
139 | + * TOCOM don't forget that php functions overrides are not rehashed so you |
|
140 | + * need to clear the classpath caches by hand when adding those. |
|
141 | + * |
|
142 | + * @param string $pluginDirectory the plugin path to scan |
|
143 | + * |
|
144 | + * @throws Dwoo_Exception |
|
145 | + */ |
|
146 | + public function addDirectory($pluginDirectory) |
|
147 | + { |
|
148 | + $pluginDir = realpath($pluginDirectory); |
|
149 | + if (!$pluginDir) { |
|
150 | + throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory); |
|
151 | + } |
|
152 | + $cacheFile = $this->cacheDir.'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo_Core::RELEASE_TAG.'.php'; |
|
153 | + $this->paths[$pluginDir] = $cacheFile; |
|
154 | + if (file_exists($cacheFile)) { |
|
155 | + $classpath = file_get_contents($cacheFile); |
|
156 | + $this->classPath = unserialize($classpath) + $this->classPath; |
|
157 | + } else { |
|
158 | + $this->rebuildClassPathCache($pluginDir, $cacheFile); |
|
159 | + } |
|
160 | + } |
|
161 | 161 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'; |
57 | 57 | if (file_exists($cacheFile)) { |
58 | 58 | $classpath = file_get_contents($cacheFile); |
59 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
59 | + $this->classPath = unserialize($classpath)+$this->classPath; |
|
60 | 60 | } else { |
61 | 61 | $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); |
62 | 62 | } |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | $this->paths[$pluginDir] = $cacheFile; |
154 | 154 | if (file_exists($cacheFile)) { |
155 | 155 | $classpath = file_get_contents($cacheFile); |
156 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
156 | + $this->classPath = unserialize($classpath)+$this->classPath; |
|
157 | 157 | } else { |
158 | 158 | $this->rebuildClassPathCache($pluginDir, $cacheFile); |
159 | 159 | } |
@@ -286,7 +286,7 @@ |
||
286 | 286 | * @param Dwoo_Core $dwoo the dwoo instance that requests it |
287 | 287 | * @param string $output the template output |
288 | 288 | * |
289 | - * @return mixed full path of the cached file or false upon failure |
|
289 | + * @return false|string full path of the cached file or false upon failure |
|
290 | 290 | */ |
291 | 291 | public function cache(Dwoo_Core $dwoo, $output) |
292 | 292 | { |
@@ -19,514 +19,514 @@ |
||
19 | 19 | */ |
20 | 20 | class Dwoo_Template_String implements Dwoo_ITemplate |
21 | 21 | { |
22 | - /** |
|
23 | - * template name. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $name; |
|
28 | - |
|
29 | - /** |
|
30 | - * template compilation id. |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $compileId; |
|
35 | - |
|
36 | - /** |
|
37 | - * template cache id, if not provided in the constructor, it is set to |
|
38 | - * the md4 hash of the request_uri. it is however highly recommended to |
|
39 | - * provide one that will fit your needs. |
|
40 | - * |
|
41 | - * in all cases, the compilation id is prepended to the cache id to separate |
|
42 | - * templates with similar cache ids from one another |
|
43 | - * |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $cacheId; |
|
47 | - |
|
48 | - /** |
|
49 | - * validity duration of the generated cache file (in seconds). |
|
50 | - * |
|
51 | - * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
52 | - * |
|
53 | - * @var int |
|
54 | - */ |
|
55 | - protected $cacheTime; |
|
56 | - |
|
57 | - /** |
|
58 | - * boolean flag that defines whether the compilation should be enforced (once) or |
|
59 | - * not use this if you have issues with the compiled templates not being updated |
|
60 | - * but if you do need this it's most likely that you should file a bug report. |
|
61 | - * |
|
62 | - * @var bool |
|
63 | - */ |
|
64 | - protected $compilationEnforced; |
|
65 | - |
|
66 | - /** |
|
67 | - * caches the results of the file checks to save some time when the same |
|
68 | - * templates is rendered several times. |
|
69 | - * |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - protected static $cache = array('cached' => array(), 'compiled' => array()); |
|
73 | - |
|
74 | - /** |
|
75 | - * holds the compiler that built this template. |
|
76 | - * |
|
77 | - * @var Dwoo_ICompiler |
|
78 | - */ |
|
79 | - protected $compiler; |
|
80 | - |
|
81 | - /** |
|
82 | - * chmod value for all files written (cached or compiled ones). |
|
83 | - * |
|
84 | - * set to null if you don't want any chmod operation to happen |
|
85 | - * |
|
86 | - * @var int |
|
87 | - */ |
|
88 | - protected $chmod = 0777; |
|
89 | - |
|
90 | - protected $template; |
|
91 | - |
|
92 | - /** |
|
93 | - * creates a template from a string. |
|
94 | - * |
|
95 | - * @param string $templateString the template to use |
|
96 | - * @param int $cacheTime duration of the cache validity for this template, |
|
97 | - * if null it defaults to the Dwoo instance that will |
|
98 | - * render this template, set to -1 for infinite cache or 0 to disable |
|
99 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
100 | - * makes this template's content unique, if null it defaults |
|
101 | - * to the current url |
|
102 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
103 | - * template from others, if null it defaults to the md4 hash of the template |
|
104 | - */ |
|
105 | - public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
106 | - { |
|
107 | - $this->template = $templateString; |
|
108 | - if (function_exists('hash')) { |
|
109 | - $this->name = hash('md4', $templateString); |
|
110 | - } else { |
|
111 | - $this->name = md5($templateString); |
|
112 | - } |
|
113 | - $this->cacheTime = $cacheTime; |
|
114 | - |
|
115 | - if ($compileId !== null) { |
|
116 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
117 | - } |
|
118 | - |
|
119 | - if ($cacheId !== null) { |
|
120 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * returns the cache duration for this template. |
|
126 | - * |
|
127 | - * defaults to null if it was not provided |
|
128 | - * |
|
129 | - * @return int|null |
|
130 | - */ |
|
131 | - public function getCacheTime() |
|
132 | - { |
|
133 | - return $this->cacheTime; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * sets the cache duration for this template. |
|
138 | - * |
|
139 | - * can be used to set it after the object is created if you did not provide |
|
140 | - * it in the constructor |
|
141 | - * |
|
142 | - * @param int $seconds duration of the cache validity for this template, if |
|
143 | - * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
144 | - * -1 = infinite cache |
|
145 | - */ |
|
146 | - public function setCacheTime($seconds = null) |
|
147 | - { |
|
148 | - $this->cacheTime = $seconds; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * returns the chmod value for all files written (cached or compiled ones). |
|
153 | - * |
|
154 | - * defaults to 0777 |
|
155 | - * |
|
156 | - * @return int|null |
|
157 | - */ |
|
158 | - public function getChmod() |
|
159 | - { |
|
160 | - return $this->chmod; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * set the chmod value for all files written (cached or compiled ones). |
|
165 | - * |
|
166 | - * set to null if you don't want to do any chmod() operation |
|
167 | - * |
|
168 | - * @param int $mask new bitmask to use for all files |
|
169 | - */ |
|
170 | - public function setChmod($mask = null) |
|
171 | - { |
|
172 | - $this->chmod = $mask; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * returns the template name. |
|
177 | - * |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - public function getName() |
|
181 | - { |
|
182 | - return $this->name; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * returns the resource name for this template class. |
|
187 | - * |
|
188 | - * @return string |
|
189 | - */ |
|
190 | - public function getResourceName() |
|
191 | - { |
|
192 | - return 'string'; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * returns the resource identifier for this template, false here as strings don't have identifiers. |
|
197 | - * |
|
198 | - * @return false |
|
199 | - */ |
|
200 | - public function getResourceIdentifier() |
|
201 | - { |
|
202 | - return false; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * returns the template source of this template. |
|
207 | - * |
|
208 | - * @return string |
|
209 | - */ |
|
210 | - public function getSource() |
|
211 | - { |
|
212 | - return $this->template; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * returns an unique value identifying the current version of this template, |
|
217 | - * in this case it's the md4 hash of the content. |
|
218 | - * |
|
219 | - * @return string |
|
220 | - */ |
|
221 | - public function getUid() |
|
222 | - { |
|
223 | - return $this->name; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * returns the compiler used by this template, if it was just compiled, or null. |
|
228 | - * |
|
229 | - * @return Dwoo_ICompiler |
|
230 | - */ |
|
231 | - public function getCompiler() |
|
232 | - { |
|
233 | - return $this->compiler; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * marks this template as compile-forced, which means it will be recompiled even if it |
|
238 | - * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
239 | - * it's only meant to be used in development (and the development of dwoo particularly). |
|
240 | - */ |
|
241 | - public function forceCompilation() |
|
242 | - { |
|
243 | - $this->compilationEnforced = true; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * returns the cached template output file name, true if it's cache-able but not cached |
|
248 | - * or false if it's not cached. |
|
249 | - * |
|
250 | - * @param Dwoo_Core $dwoo the dwoo instance that requests it |
|
251 | - * |
|
252 | - * @return string|bool |
|
253 | - */ |
|
254 | - public function getCachedTemplate(Dwoo_Core $dwoo) |
|
255 | - { |
|
256 | - if ($this->cacheTime !== null) { |
|
257 | - $cacheLength = $this->cacheTime; |
|
258 | - } else { |
|
259 | - $cacheLength = $dwoo->getCacheTime(); |
|
260 | - } |
|
261 | - |
|
262 | - // file is not cacheable |
|
263 | - if ($cacheLength == 0) { |
|
264 | - return false; |
|
265 | - } |
|
266 | - |
|
267 | - $cachedFile = $this->getCacheFilename($dwoo); |
|
268 | - |
|
269 | - if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
270 | - // already checked, return cache file |
|
271 | - return $cachedFile; |
|
272 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { |
|
273 | - // cache is still valid and can be loaded |
|
274 | - self::$cache['cached'][$this->cacheId] = true; |
|
275 | - |
|
276 | - return $cachedFile; |
|
277 | - } else { |
|
278 | - // file is cacheable |
|
279 | - return true; |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * caches the provided output into the cache file. |
|
285 | - * |
|
286 | - * @param Dwoo_Core $dwoo the dwoo instance that requests it |
|
287 | - * @param string $output the template output |
|
288 | - * |
|
289 | - * @return mixed full path of the cached file or false upon failure |
|
290 | - */ |
|
291 | - public function cache(Dwoo_Core $dwoo, $output) |
|
292 | - { |
|
293 | - $cacheDir = $dwoo->getCacheDir(); |
|
294 | - $cachedFile = $this->getCacheFilename($dwoo); |
|
295 | - |
|
296 | - // the code below is courtesy of Rasmus Schultz, |
|
297 | - // thanks for his help on avoiding concurency issues |
|
298 | - $temp = tempnam($cacheDir, 'temp'); |
|
299 | - if (!($file = @fopen($temp, 'wb'))) { |
|
300 | - $temp = $cacheDir.uniqid('temp'); |
|
301 | - if (!($file = @fopen($temp, 'wb'))) { |
|
302 | - trigger_error('Error writing temporary file \''.$temp.'\'', E_USER_WARNING); |
|
303 | - |
|
304 | - return false; |
|
305 | - } |
|
306 | - } |
|
307 | - |
|
308 | - fwrite($file, $output); |
|
309 | - fclose($file); |
|
310 | - |
|
311 | - $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
312 | - if (!@rename($temp, $cachedFile)) { |
|
313 | - @unlink($cachedFile); |
|
314 | - @rename($temp, $cachedFile); |
|
315 | - } |
|
316 | - |
|
317 | - if ($this->chmod !== null) { |
|
318 | - chmod($cachedFile, $this->chmod); |
|
319 | - } |
|
320 | - |
|
321 | - self::$cache['cached'][$this->cacheId] = true; |
|
322 | - |
|
323 | - return $cachedFile; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * clears the cached template if it's older than the given time. |
|
328 | - * |
|
329 | - * @param Dwoo_Core $dwoo the dwoo instance that was used to cache that template |
|
330 | - * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
331 | - * |
|
332 | - * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
333 | - */ |
|
334 | - public function clearCache(Dwoo_Core $dwoo, $olderThan = -1) |
|
335 | - { |
|
336 | - $cachedFile = $this->getCacheFilename($dwoo); |
|
337 | - |
|
338 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * returns the compiled template file name. |
|
343 | - * |
|
344 | - * @param Dwoo_Core $dwoo the dwoo instance that requests it |
|
345 | - * @param Dwoo_ICompiler $compiler the compiler that must be used |
|
346 | - * |
|
347 | - * @return string |
|
348 | - */ |
|
349 | - public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null) |
|
350 | - { |
|
351 | - $compiledFile = $this->getCompiledFilename($dwoo); |
|
352 | - |
|
353 | - if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
354 | - // already checked, return compiled file |
|
355 | - } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
356 | - // template is compiled |
|
357 | - self::$cache['compiled'][$this->compileId] = true; |
|
358 | - } else { |
|
359 | - // compiles the template |
|
360 | - $this->compilationEnforced = false; |
|
361 | - |
|
362 | - if ($compiler === null) { |
|
363 | - $compiler = $dwoo->getDefaultCompilerFactory($this->getResourceName()); |
|
364 | - |
|
365 | - if ($compiler === null || $compiler === array('Dwoo_Compiler', 'compilerFactory')) { |
|
366 | - $compiler = Dwoo_Compiler::compilerFactory(); |
|
367 | - } else { |
|
368 | - $compiler = call_user_func($compiler); |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - $this->compiler = $compiler; |
|
373 | - |
|
374 | - $compiler->setCustomPlugins($dwoo->getCustomPlugins()); |
|
375 | - $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); |
|
376 | - $this->makeDirectory(dirname($compiledFile), $dwoo->getCompileDir()); |
|
377 | - file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); |
|
378 | - if ($this->chmod !== null) { |
|
379 | - chmod($compiledFile, $this->chmod); |
|
380 | - } |
|
381 | - |
|
382 | - if (extension_loaded('Zend OPcache')) { |
|
383 | - opcache_invalidate($compiledFile); |
|
384 | - } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
385 | - apc_delete_file($compiledFile); |
|
386 | - } |
|
387 | - |
|
388 | - self::$cache['compiled'][$this->compileId] = true; |
|
389 | - } |
|
390 | - |
|
391 | - return $compiledFile; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Checks if compiled file is valid (it exists). |
|
396 | - * |
|
397 | - * @param string $file |
|
398 | - * |
|
399 | - * @return bool True cache file existance |
|
400 | - */ |
|
401 | - protected function isValidCompiledFile($file) |
|
402 | - { |
|
403 | - return file_exists($file); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * returns a new template string object with the resource id being the template source code. |
|
408 | - * |
|
409 | - * @param Dwoo_Core $dwoo the dwoo instance requiring it |
|
410 | - * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
411 | - * @param int $cacheTime duration of the cache validity for this template, |
|
412 | - * if null it defaults to the Dwoo instance that will |
|
413 | - * render this template |
|
414 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
415 | - * makes this template's content unique, if null it defaults |
|
416 | - * to the current url |
|
417 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
418 | - * template from others, if null it defaults to the filename+bits of the path |
|
419 | - * @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through |
|
420 | - * an include, extends or any other plugin) |
|
421 | - * |
|
422 | - * @return Dwoo_Template_String |
|
423 | - */ |
|
424 | - public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) |
|
425 | - { |
|
426 | - return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * returns the full compiled file name and assigns a default value to it if |
|
431 | - * required. |
|
432 | - * |
|
433 | - * @param Dwoo_Core $dwoo the dwoo instance that requests the file name |
|
434 | - * |
|
435 | - * @return string the full path to the compiled file |
|
436 | - */ |
|
437 | - protected function getCompiledFilename(Dwoo_Core $dwoo) |
|
438 | - { |
|
439 | - // no compile id was provided, set default |
|
440 | - if ($this->compileId === null) { |
|
441 | - $this->compileId = $this->name; |
|
442 | - } |
|
443 | - |
|
444 | - return $dwoo->getCompileDir().$this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php'; |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * returns the full cached file name and assigns a default value to it if |
|
449 | - * required. |
|
450 | - * |
|
451 | - * @param Dwoo_Core $dwoo the dwoo instance that requests the file name |
|
452 | - * |
|
453 | - * @return string the full path to the cached file |
|
454 | - */ |
|
455 | - protected function getCacheFilename(Dwoo_Core $dwoo) |
|
456 | - { |
|
457 | - // no cache id provided, use request_uri as default |
|
458 | - if ($this->cacheId === null) { |
|
459 | - if (isset($_SERVER['REQUEST_URI']) === true) { |
|
460 | - $cacheId = $_SERVER['REQUEST_URI']; |
|
461 | - } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
462 | - $cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']); |
|
463 | - } else { |
|
464 | - $cacheId = ''; |
|
465 | - } |
|
466 | - // force compiled id generation |
|
467 | - $this->getCompiledFilename($dwoo); |
|
468 | - |
|
469 | - $this->cacheId = str_replace('../', '__', $this->compileId.strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
470 | - } |
|
471 | - |
|
472 | - return $dwoo->getCacheDir().$this->cacheId.'.html'; |
|
473 | - } |
|
474 | - |
|
475 | - /** |
|
476 | - * returns some php code that will check if this template has been modified or not. |
|
477 | - * |
|
478 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
479 | - * |
|
480 | - * @return string |
|
481 | - */ |
|
482 | - public function getIsModifiedCode() |
|
483 | - { |
|
484 | - return null; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * ensures the given path exists. |
|
489 | - * |
|
490 | - * @param string $path any path |
|
491 | - * @param string $baseDir the base directory where the directory is created |
|
492 | - * ($path must still contain the full path, $baseDir |
|
493 | - * is only used for unix permissions) |
|
494 | - * |
|
495 | - * @throws Exception |
|
496 | - */ |
|
497 | - protected function makeDirectory($path, $baseDir = null) |
|
498 | - { |
|
499 | - if (is_dir($path) === true) { |
|
500 | - return; |
|
501 | - } |
|
502 | - |
|
503 | - if ($this->chmod === null) { |
|
504 | - $chmod = 0777; |
|
505 | - } else { |
|
506 | - $chmod = $this->chmod; |
|
507 | - } |
|
508 | - |
|
509 | - $retries = 3; |
|
510 | - while ($retries--) { |
|
511 | - @mkdir($path, $chmod, true); |
|
512 | - if (is_dir($path)) { |
|
513 | - break; |
|
514 | - } |
|
515 | - usleep(20); |
|
516 | - } |
|
517 | - |
|
518 | - // enforce the correct mode for all directories created |
|
519 | - if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
520 | - $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
521 | - $folders = explode('/', trim($path, '/')); |
|
522 | - foreach ($folders as $folder) { |
|
523 | - $baseDir .= $folder.DIRECTORY_SEPARATOR; |
|
524 | - if (!chmod($baseDir, $chmod)) { |
|
525 | - throw new Exception('Unable to chmod '. |
|
526 | - "$baseDir to $chmod: ". |
|
527 | - print_r(error_get_last(), true)); |
|
528 | - } |
|
529 | - } |
|
530 | - } |
|
531 | - } |
|
22 | + /** |
|
23 | + * template name. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $name; |
|
28 | + |
|
29 | + /** |
|
30 | + * template compilation id. |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $compileId; |
|
35 | + |
|
36 | + /** |
|
37 | + * template cache id, if not provided in the constructor, it is set to |
|
38 | + * the md4 hash of the request_uri. it is however highly recommended to |
|
39 | + * provide one that will fit your needs. |
|
40 | + * |
|
41 | + * in all cases, the compilation id is prepended to the cache id to separate |
|
42 | + * templates with similar cache ids from one another |
|
43 | + * |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $cacheId; |
|
47 | + |
|
48 | + /** |
|
49 | + * validity duration of the generated cache file (in seconds). |
|
50 | + * |
|
51 | + * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
52 | + * |
|
53 | + * @var int |
|
54 | + */ |
|
55 | + protected $cacheTime; |
|
56 | + |
|
57 | + /** |
|
58 | + * boolean flag that defines whether the compilation should be enforced (once) or |
|
59 | + * not use this if you have issues with the compiled templates not being updated |
|
60 | + * but if you do need this it's most likely that you should file a bug report. |
|
61 | + * |
|
62 | + * @var bool |
|
63 | + */ |
|
64 | + protected $compilationEnforced; |
|
65 | + |
|
66 | + /** |
|
67 | + * caches the results of the file checks to save some time when the same |
|
68 | + * templates is rendered several times. |
|
69 | + * |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + protected static $cache = array('cached' => array(), 'compiled' => array()); |
|
73 | + |
|
74 | + /** |
|
75 | + * holds the compiler that built this template. |
|
76 | + * |
|
77 | + * @var Dwoo_ICompiler |
|
78 | + */ |
|
79 | + protected $compiler; |
|
80 | + |
|
81 | + /** |
|
82 | + * chmod value for all files written (cached or compiled ones). |
|
83 | + * |
|
84 | + * set to null if you don't want any chmod operation to happen |
|
85 | + * |
|
86 | + * @var int |
|
87 | + */ |
|
88 | + protected $chmod = 0777; |
|
89 | + |
|
90 | + protected $template; |
|
91 | + |
|
92 | + /** |
|
93 | + * creates a template from a string. |
|
94 | + * |
|
95 | + * @param string $templateString the template to use |
|
96 | + * @param int $cacheTime duration of the cache validity for this template, |
|
97 | + * if null it defaults to the Dwoo instance that will |
|
98 | + * render this template, set to -1 for infinite cache or 0 to disable |
|
99 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
100 | + * makes this template's content unique, if null it defaults |
|
101 | + * to the current url |
|
102 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
103 | + * template from others, if null it defaults to the md4 hash of the template |
|
104 | + */ |
|
105 | + public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
106 | + { |
|
107 | + $this->template = $templateString; |
|
108 | + if (function_exists('hash')) { |
|
109 | + $this->name = hash('md4', $templateString); |
|
110 | + } else { |
|
111 | + $this->name = md5($templateString); |
|
112 | + } |
|
113 | + $this->cacheTime = $cacheTime; |
|
114 | + |
|
115 | + if ($compileId !== null) { |
|
116 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
117 | + } |
|
118 | + |
|
119 | + if ($cacheId !== null) { |
|
120 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * returns the cache duration for this template. |
|
126 | + * |
|
127 | + * defaults to null if it was not provided |
|
128 | + * |
|
129 | + * @return int|null |
|
130 | + */ |
|
131 | + public function getCacheTime() |
|
132 | + { |
|
133 | + return $this->cacheTime; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * sets the cache duration for this template. |
|
138 | + * |
|
139 | + * can be used to set it after the object is created if you did not provide |
|
140 | + * it in the constructor |
|
141 | + * |
|
142 | + * @param int $seconds duration of the cache validity for this template, if |
|
143 | + * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
144 | + * -1 = infinite cache |
|
145 | + */ |
|
146 | + public function setCacheTime($seconds = null) |
|
147 | + { |
|
148 | + $this->cacheTime = $seconds; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * returns the chmod value for all files written (cached or compiled ones). |
|
153 | + * |
|
154 | + * defaults to 0777 |
|
155 | + * |
|
156 | + * @return int|null |
|
157 | + */ |
|
158 | + public function getChmod() |
|
159 | + { |
|
160 | + return $this->chmod; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * set the chmod value for all files written (cached or compiled ones). |
|
165 | + * |
|
166 | + * set to null if you don't want to do any chmod() operation |
|
167 | + * |
|
168 | + * @param int $mask new bitmask to use for all files |
|
169 | + */ |
|
170 | + public function setChmod($mask = null) |
|
171 | + { |
|
172 | + $this->chmod = $mask; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * returns the template name. |
|
177 | + * |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + public function getName() |
|
181 | + { |
|
182 | + return $this->name; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * returns the resource name for this template class. |
|
187 | + * |
|
188 | + * @return string |
|
189 | + */ |
|
190 | + public function getResourceName() |
|
191 | + { |
|
192 | + return 'string'; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * returns the resource identifier for this template, false here as strings don't have identifiers. |
|
197 | + * |
|
198 | + * @return false |
|
199 | + */ |
|
200 | + public function getResourceIdentifier() |
|
201 | + { |
|
202 | + return false; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * returns the template source of this template. |
|
207 | + * |
|
208 | + * @return string |
|
209 | + */ |
|
210 | + public function getSource() |
|
211 | + { |
|
212 | + return $this->template; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * returns an unique value identifying the current version of this template, |
|
217 | + * in this case it's the md4 hash of the content. |
|
218 | + * |
|
219 | + * @return string |
|
220 | + */ |
|
221 | + public function getUid() |
|
222 | + { |
|
223 | + return $this->name; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * returns the compiler used by this template, if it was just compiled, or null. |
|
228 | + * |
|
229 | + * @return Dwoo_ICompiler |
|
230 | + */ |
|
231 | + public function getCompiler() |
|
232 | + { |
|
233 | + return $this->compiler; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * marks this template as compile-forced, which means it will be recompiled even if it |
|
238 | + * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
239 | + * it's only meant to be used in development (and the development of dwoo particularly). |
|
240 | + */ |
|
241 | + public function forceCompilation() |
|
242 | + { |
|
243 | + $this->compilationEnforced = true; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * returns the cached template output file name, true if it's cache-able but not cached |
|
248 | + * or false if it's not cached. |
|
249 | + * |
|
250 | + * @param Dwoo_Core $dwoo the dwoo instance that requests it |
|
251 | + * |
|
252 | + * @return string|bool |
|
253 | + */ |
|
254 | + public function getCachedTemplate(Dwoo_Core $dwoo) |
|
255 | + { |
|
256 | + if ($this->cacheTime !== null) { |
|
257 | + $cacheLength = $this->cacheTime; |
|
258 | + } else { |
|
259 | + $cacheLength = $dwoo->getCacheTime(); |
|
260 | + } |
|
261 | + |
|
262 | + // file is not cacheable |
|
263 | + if ($cacheLength == 0) { |
|
264 | + return false; |
|
265 | + } |
|
266 | + |
|
267 | + $cachedFile = $this->getCacheFilename($dwoo); |
|
268 | + |
|
269 | + if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
270 | + // already checked, return cache file |
|
271 | + return $cachedFile; |
|
272 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { |
|
273 | + // cache is still valid and can be loaded |
|
274 | + self::$cache['cached'][$this->cacheId] = true; |
|
275 | + |
|
276 | + return $cachedFile; |
|
277 | + } else { |
|
278 | + // file is cacheable |
|
279 | + return true; |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * caches the provided output into the cache file. |
|
285 | + * |
|
286 | + * @param Dwoo_Core $dwoo the dwoo instance that requests it |
|
287 | + * @param string $output the template output |
|
288 | + * |
|
289 | + * @return mixed full path of the cached file or false upon failure |
|
290 | + */ |
|
291 | + public function cache(Dwoo_Core $dwoo, $output) |
|
292 | + { |
|
293 | + $cacheDir = $dwoo->getCacheDir(); |
|
294 | + $cachedFile = $this->getCacheFilename($dwoo); |
|
295 | + |
|
296 | + // the code below is courtesy of Rasmus Schultz, |
|
297 | + // thanks for his help on avoiding concurency issues |
|
298 | + $temp = tempnam($cacheDir, 'temp'); |
|
299 | + if (!($file = @fopen($temp, 'wb'))) { |
|
300 | + $temp = $cacheDir.uniqid('temp'); |
|
301 | + if (!($file = @fopen($temp, 'wb'))) { |
|
302 | + trigger_error('Error writing temporary file \''.$temp.'\'', E_USER_WARNING); |
|
303 | + |
|
304 | + return false; |
|
305 | + } |
|
306 | + } |
|
307 | + |
|
308 | + fwrite($file, $output); |
|
309 | + fclose($file); |
|
310 | + |
|
311 | + $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
312 | + if (!@rename($temp, $cachedFile)) { |
|
313 | + @unlink($cachedFile); |
|
314 | + @rename($temp, $cachedFile); |
|
315 | + } |
|
316 | + |
|
317 | + if ($this->chmod !== null) { |
|
318 | + chmod($cachedFile, $this->chmod); |
|
319 | + } |
|
320 | + |
|
321 | + self::$cache['cached'][$this->cacheId] = true; |
|
322 | + |
|
323 | + return $cachedFile; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * clears the cached template if it's older than the given time. |
|
328 | + * |
|
329 | + * @param Dwoo_Core $dwoo the dwoo instance that was used to cache that template |
|
330 | + * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
331 | + * |
|
332 | + * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
333 | + */ |
|
334 | + public function clearCache(Dwoo_Core $dwoo, $olderThan = -1) |
|
335 | + { |
|
336 | + $cachedFile = $this->getCacheFilename($dwoo); |
|
337 | + |
|
338 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * returns the compiled template file name. |
|
343 | + * |
|
344 | + * @param Dwoo_Core $dwoo the dwoo instance that requests it |
|
345 | + * @param Dwoo_ICompiler $compiler the compiler that must be used |
|
346 | + * |
|
347 | + * @return string |
|
348 | + */ |
|
349 | + public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null) |
|
350 | + { |
|
351 | + $compiledFile = $this->getCompiledFilename($dwoo); |
|
352 | + |
|
353 | + if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
354 | + // already checked, return compiled file |
|
355 | + } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
356 | + // template is compiled |
|
357 | + self::$cache['compiled'][$this->compileId] = true; |
|
358 | + } else { |
|
359 | + // compiles the template |
|
360 | + $this->compilationEnforced = false; |
|
361 | + |
|
362 | + if ($compiler === null) { |
|
363 | + $compiler = $dwoo->getDefaultCompilerFactory($this->getResourceName()); |
|
364 | + |
|
365 | + if ($compiler === null || $compiler === array('Dwoo_Compiler', 'compilerFactory')) { |
|
366 | + $compiler = Dwoo_Compiler::compilerFactory(); |
|
367 | + } else { |
|
368 | + $compiler = call_user_func($compiler); |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + $this->compiler = $compiler; |
|
373 | + |
|
374 | + $compiler->setCustomPlugins($dwoo->getCustomPlugins()); |
|
375 | + $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); |
|
376 | + $this->makeDirectory(dirname($compiledFile), $dwoo->getCompileDir()); |
|
377 | + file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); |
|
378 | + if ($this->chmod !== null) { |
|
379 | + chmod($compiledFile, $this->chmod); |
|
380 | + } |
|
381 | + |
|
382 | + if (extension_loaded('Zend OPcache')) { |
|
383 | + opcache_invalidate($compiledFile); |
|
384 | + } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
385 | + apc_delete_file($compiledFile); |
|
386 | + } |
|
387 | + |
|
388 | + self::$cache['compiled'][$this->compileId] = true; |
|
389 | + } |
|
390 | + |
|
391 | + return $compiledFile; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Checks if compiled file is valid (it exists). |
|
396 | + * |
|
397 | + * @param string $file |
|
398 | + * |
|
399 | + * @return bool True cache file existance |
|
400 | + */ |
|
401 | + protected function isValidCompiledFile($file) |
|
402 | + { |
|
403 | + return file_exists($file); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * returns a new template string object with the resource id being the template source code. |
|
408 | + * |
|
409 | + * @param Dwoo_Core $dwoo the dwoo instance requiring it |
|
410 | + * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
411 | + * @param int $cacheTime duration of the cache validity for this template, |
|
412 | + * if null it defaults to the Dwoo instance that will |
|
413 | + * render this template |
|
414 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
415 | + * makes this template's content unique, if null it defaults |
|
416 | + * to the current url |
|
417 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
418 | + * template from others, if null it defaults to the filename+bits of the path |
|
419 | + * @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through |
|
420 | + * an include, extends or any other plugin) |
|
421 | + * |
|
422 | + * @return Dwoo_Template_String |
|
423 | + */ |
|
424 | + public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) |
|
425 | + { |
|
426 | + return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * returns the full compiled file name and assigns a default value to it if |
|
431 | + * required. |
|
432 | + * |
|
433 | + * @param Dwoo_Core $dwoo the dwoo instance that requests the file name |
|
434 | + * |
|
435 | + * @return string the full path to the compiled file |
|
436 | + */ |
|
437 | + protected function getCompiledFilename(Dwoo_Core $dwoo) |
|
438 | + { |
|
439 | + // no compile id was provided, set default |
|
440 | + if ($this->compileId === null) { |
|
441 | + $this->compileId = $this->name; |
|
442 | + } |
|
443 | + |
|
444 | + return $dwoo->getCompileDir().$this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php'; |
|
445 | + } |
|
446 | + |
|
447 | + /** |
|
448 | + * returns the full cached file name and assigns a default value to it if |
|
449 | + * required. |
|
450 | + * |
|
451 | + * @param Dwoo_Core $dwoo the dwoo instance that requests the file name |
|
452 | + * |
|
453 | + * @return string the full path to the cached file |
|
454 | + */ |
|
455 | + protected function getCacheFilename(Dwoo_Core $dwoo) |
|
456 | + { |
|
457 | + // no cache id provided, use request_uri as default |
|
458 | + if ($this->cacheId === null) { |
|
459 | + if (isset($_SERVER['REQUEST_URI']) === true) { |
|
460 | + $cacheId = $_SERVER['REQUEST_URI']; |
|
461 | + } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
462 | + $cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']); |
|
463 | + } else { |
|
464 | + $cacheId = ''; |
|
465 | + } |
|
466 | + // force compiled id generation |
|
467 | + $this->getCompiledFilename($dwoo); |
|
468 | + |
|
469 | + $this->cacheId = str_replace('../', '__', $this->compileId.strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
470 | + } |
|
471 | + |
|
472 | + return $dwoo->getCacheDir().$this->cacheId.'.html'; |
|
473 | + } |
|
474 | + |
|
475 | + /** |
|
476 | + * returns some php code that will check if this template has been modified or not. |
|
477 | + * |
|
478 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
479 | + * |
|
480 | + * @return string |
|
481 | + */ |
|
482 | + public function getIsModifiedCode() |
|
483 | + { |
|
484 | + return null; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * ensures the given path exists. |
|
489 | + * |
|
490 | + * @param string $path any path |
|
491 | + * @param string $baseDir the base directory where the directory is created |
|
492 | + * ($path must still contain the full path, $baseDir |
|
493 | + * is only used for unix permissions) |
|
494 | + * |
|
495 | + * @throws Exception |
|
496 | + */ |
|
497 | + protected function makeDirectory($path, $baseDir = null) |
|
498 | + { |
|
499 | + if (is_dir($path) === true) { |
|
500 | + return; |
|
501 | + } |
|
502 | + |
|
503 | + if ($this->chmod === null) { |
|
504 | + $chmod = 0777; |
|
505 | + } else { |
|
506 | + $chmod = $this->chmod; |
|
507 | + } |
|
508 | + |
|
509 | + $retries = 3; |
|
510 | + while ($retries--) { |
|
511 | + @mkdir($path, $chmod, true); |
|
512 | + if (is_dir($path)) { |
|
513 | + break; |
|
514 | + } |
|
515 | + usleep(20); |
|
516 | + } |
|
517 | + |
|
518 | + // enforce the correct mode for all directories created |
|
519 | + if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
520 | + $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
521 | + $folders = explode('/', trim($path, '/')); |
|
522 | + foreach ($folders as $folder) { |
|
523 | + $baseDir .= $folder.DIRECTORY_SEPARATOR; |
|
524 | + if (!chmod($baseDir, $chmod)) { |
|
525 | + throw new Exception('Unable to chmod '. |
|
526 | + "$baseDir to $chmod: ". |
|
527 | + print_r(error_get_last(), true)); |
|
528 | + } |
|
529 | + } |
|
530 | + } |
|
531 | + } |
|
532 | 532 | } |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
270 | 270 | // already checked, return cache file |
271 | 271 | return $cachedFile; |
272 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { |
|
272 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME']-$cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { |
|
273 | 273 | // cache is still valid and can be loaded |
274 | 274 | self::$cache['cached'][$this->cacheId] = true; |
275 | 275 | |
@@ -335,7 +335,7 @@ discard block |
||
335 | 335 | { |
336 | 336 | $cachedFile = $this->getCacheFilename($dwoo); |
337 | 337 | |
338 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
338 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time()-$olderThan) && unlink($cachedFile)); |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | /** |
@@ -57,6 +57,10 @@ |
||
57 | 57 | return $output; |
58 | 58 | } |
59 | 59 | |
60 | + /** |
|
61 | + * @param string $dynamicId |
|
62 | + * @param string $compiledFile |
|
63 | + */ |
|
60 | 64 | public static function unescape($output, $dynamicId, $compiledFile) |
61 | 65 | { |
62 | 66 | $output = preg_replace_callback('/<dwoo:dynamic_('.$dynamicId.')>(.+?)<\/dwoo:dynamic_'.$dynamicId.'>/s', array('self', 'unescapePhp'), $output, -1, $count); |
@@ -19,57 +19,57 @@ |
||
19 | 19 | */ |
20 | 20 | class Dwoo_Plugin_dynamic extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block |
21 | 21 | { |
22 | - public function init() |
|
23 | - { |
|
24 | - } |
|
22 | + public function init() |
|
23 | + { |
|
24 | + } |
|
25 | 25 | |
26 | - public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) |
|
27 | - { |
|
28 | - return ''; |
|
29 | - } |
|
26 | + public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) |
|
27 | + { |
|
28 | + return ''; |
|
29 | + } |
|
30 | 30 | |
31 | - public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) |
|
32 | - { |
|
33 | - try { |
|
34 | - $compiler->findBlock('dynamic'); |
|
31 | + public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) |
|
32 | + { |
|
33 | + try { |
|
34 | + $compiler->findBlock('dynamic'); |
|
35 | 35 | |
36 | - return $content; |
|
37 | - } catch (Dwoo_Compilation_Exception $e) { |
|
38 | - } |
|
39 | - $output = Dwoo_Compiler::PHP_OPEN. |
|
40 | - 'if($doCache) {'."\n\t". |
|
41 | - 'echo \'<dwoo:dynamic_\'.$dynamicId.\'>'. |
|
42 | - str_replace('\'', '\\\'', $content). |
|
43 | - '</dwoo:dynamic_\'.$dynamicId.\'>\';'. |
|
44 | - "\n} else {\n\t"; |
|
45 | - if (substr($content, 0, strlen(Dwoo_Compiler::PHP_OPEN)) == Dwoo_Compiler::PHP_OPEN) { |
|
46 | - $output .= substr($content, strlen(Dwoo_Compiler::PHP_OPEN)); |
|
47 | - } else { |
|
48 | - $output .= Dwoo_Compiler::PHP_CLOSE.$content; |
|
49 | - } |
|
50 | - if (substr($output, -strlen(Dwoo_Compiler::PHP_CLOSE)) == Dwoo_Compiler::PHP_CLOSE) { |
|
51 | - $output = substr($output, 0, -strlen(Dwoo_Compiler::PHP_CLOSE)); |
|
52 | - } else { |
|
53 | - $output .= Dwoo_Compiler::PHP_OPEN; |
|
54 | - } |
|
55 | - $output .= "\n}".Dwoo_Compiler::PHP_CLOSE; |
|
36 | + return $content; |
|
37 | + } catch (Dwoo_Compilation_Exception $e) { |
|
38 | + } |
|
39 | + $output = Dwoo_Compiler::PHP_OPEN. |
|
40 | + 'if($doCache) {'."\n\t". |
|
41 | + 'echo \'<dwoo:dynamic_\'.$dynamicId.\'>'. |
|
42 | + str_replace('\'', '\\\'', $content). |
|
43 | + '</dwoo:dynamic_\'.$dynamicId.\'>\';'. |
|
44 | + "\n} else {\n\t"; |
|
45 | + if (substr($content, 0, strlen(Dwoo_Compiler::PHP_OPEN)) == Dwoo_Compiler::PHP_OPEN) { |
|
46 | + $output .= substr($content, strlen(Dwoo_Compiler::PHP_OPEN)); |
|
47 | + } else { |
|
48 | + $output .= Dwoo_Compiler::PHP_CLOSE.$content; |
|
49 | + } |
|
50 | + if (substr($output, -strlen(Dwoo_Compiler::PHP_CLOSE)) == Dwoo_Compiler::PHP_CLOSE) { |
|
51 | + $output = substr($output, 0, -strlen(Dwoo_Compiler::PHP_CLOSE)); |
|
52 | + } else { |
|
53 | + $output .= Dwoo_Compiler::PHP_OPEN; |
|
54 | + } |
|
55 | + $output .= "\n}".Dwoo_Compiler::PHP_CLOSE; |
|
56 | 56 | |
57 | - return $output; |
|
58 | - } |
|
57 | + return $output; |
|
58 | + } |
|
59 | 59 | |
60 | - public static function unescape($output, $dynamicId, $compiledFile) |
|
61 | - { |
|
62 | - $output = preg_replace_callback('/<dwoo:dynamic_('.$dynamicId.')>(.+?)<\/dwoo:dynamic_'.$dynamicId.'>/s', array('self', 'unescapePhp'), $output, -1, $count); |
|
63 | - // re-add the includes on top of the file |
|
64 | - if ($count && preg_match('#/\* template head \*/(.+?)/\* end template head \*/#s', file_get_contents($compiledFile), $m)) { |
|
65 | - $output = '<?php '.$m[1].' ?>'.$output; |
|
66 | - } |
|
60 | + public static function unescape($output, $dynamicId, $compiledFile) |
|
61 | + { |
|
62 | + $output = preg_replace_callback('/<dwoo:dynamic_('.$dynamicId.')>(.+?)<\/dwoo:dynamic_'.$dynamicId.'>/s', array('self', 'unescapePhp'), $output, -1, $count); |
|
63 | + // re-add the includes on top of the file |
|
64 | + if ($count && preg_match('#/\* template head \*/(.+?)/\* end template head \*/#s', file_get_contents($compiledFile), $m)) { |
|
65 | + $output = '<?php '.$m[1].' ?>'.$output; |
|
66 | + } |
|
67 | 67 | |
68 | - return $output; |
|
69 | - } |
|
68 | + return $output; |
|
69 | + } |
|
70 | 70 | |
71 | - public static function unescapePhp($match) |
|
72 | - { |
|
73 | - return preg_replace('{<\?php /\*'.$match[1].'\*/ echo \'(.+?)\'; \?>}s', '$1', $match[2]); |
|
74 | - } |
|
71 | + public static function unescapePhp($match) |
|
72 | + { |
|
73 | + return preg_replace('{<\?php /\*'.$match[1].'\*/ echo \'(.+?)\'; \?>}s', '$1', $match[2]); |
|
74 | + } |
|
75 | 75 | } |
@@ -19,10 +19,10 @@ |
||
19 | 19 | */ |
20 | 20 | interface Dwoo_IDataProvider |
21 | 21 | { |
22 | - /** |
|
23 | - * returns the data as an associative array that will be used in the template. |
|
24 | - * |
|
25 | - * @return array |
|
26 | - */ |
|
27 | - public function getData(); |
|
22 | + /** |
|
23 | + * returns the data as an associative array that will be used in the template. |
|
24 | + * |
|
25 | + * @return array |
|
26 | + */ |
|
27 | + public function getData(); |
|
28 | 28 | } |
@@ -18,30 +18,30 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class Dwoo_Filter |
20 | 20 | { |
21 | - /** |
|
22 | - * The dwoo instance that runs this filter. |
|
23 | - * |
|
24 | - * @var Dwoo |
|
25 | - */ |
|
26 | - protected $dwoo; |
|
21 | + /** |
|
22 | + * The dwoo instance that runs this filter. |
|
23 | + * |
|
24 | + * @var Dwoo |
|
25 | + */ |
|
26 | + protected $dwoo; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor, if you override it, call parent::__construct($dwoo); or assign |
|
30 | - * the dwoo instance yourself if you need it. |
|
31 | - * |
|
32 | - * @param Dwoo_Core $dwoo the dwoo instance that runs this plugin |
|
33 | - */ |
|
34 | - public function __construct(Dwoo_Core $dwoo) |
|
35 | - { |
|
36 | - $this->dwoo = $dwoo; |
|
37 | - } |
|
28 | + /** |
|
29 | + * Constructor, if you override it, call parent::__construct($dwoo); or assign |
|
30 | + * the dwoo instance yourself if you need it. |
|
31 | + * |
|
32 | + * @param Dwoo_Core $dwoo the dwoo instance that runs this plugin |
|
33 | + */ |
|
34 | + public function __construct(Dwoo_Core $dwoo) |
|
35 | + { |
|
36 | + $this->dwoo = $dwoo; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Processes the input and returns it filtered. |
|
41 | - * |
|
42 | - * @param string $input the template to process |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - abstract public function process($input); |
|
39 | + /** |
|
40 | + * Processes the input and returns it filtered. |
|
41 | + * |
|
42 | + * @param string $input the template to process |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + abstract public function process($input); |
|
47 | 47 | } |
@@ -19,24 +19,24 @@ |
||
19 | 19 | */ |
20 | 20 | class Dwoo_Adapters_ZendFramework_Dwoo extends Dwoo_Core |
21 | 21 | { |
22 | - /** |
|
23 | - * Redirects all unknown properties to plugin proxy |
|
24 | - * to support $this->viewVariable from within templates. |
|
25 | - * |
|
26 | - * @param string $name Property name |
|
27 | - * |
|
28 | - * @return mixed |
|
29 | - */ |
|
30 | - public function __get($name) |
|
31 | - { |
|
32 | - if (isset($this->getPluginProxy()->view->$name)) { |
|
33 | - return $this->getPluginProxy()->view->$name; |
|
34 | - } |
|
35 | - $trace = debug_backtrace(); |
|
36 | - trigger_error('Undefined property via __get(): '.$name. |
|
37 | - ' in '.$trace[0]['file']. |
|
38 | - ' on line '.$trace[0]['line'], E_USER_NOTICE); |
|
22 | + /** |
|
23 | + * Redirects all unknown properties to plugin proxy |
|
24 | + * to support $this->viewVariable from within templates. |
|
25 | + * |
|
26 | + * @param string $name Property name |
|
27 | + * |
|
28 | + * @return mixed |
|
29 | + */ |
|
30 | + public function __get($name) |
|
31 | + { |
|
32 | + if (isset($this->getPluginProxy()->view->$name)) { |
|
33 | + return $this->getPluginProxy()->view->$name; |
|
34 | + } |
|
35 | + $trace = debug_backtrace(); |
|
36 | + trigger_error('Undefined property via __get(): '.$name. |
|
37 | + ' in '.$trace[0]['file']. |
|
38 | + ' on line '.$trace[0]['line'], E_USER_NOTICE); |
|
39 | 39 | |
40 | - return null; |
|
41 | - } |
|
40 | + return null; |
|
41 | + } |
|
42 | 42 | } |
@@ -48,157 +48,157 @@ |
||
48 | 48 | */ |
49 | 49 | class DwooRenderer extends AgaviRenderer implements AgaviIReusableRenderer |
50 | 50 | { |
51 | - /** |
|
52 | - * @constant string The directory inside the cache dir where templates will |
|
53 | - * be stored in compiled form. |
|
54 | - */ |
|
55 | - const COMPILE_DIR = 'templates'; |
|
56 | - |
|
57 | - /** |
|
58 | - * @constant string The subdirectory inside the compile dir where templates |
|
59 | - * will be stored in compiled form. |
|
60 | - */ |
|
61 | - const COMPILE_SUBDIR = 'dwoo'; |
|
62 | - |
|
63 | - /** |
|
64 | - * @constant string The directory inside the cache dir where cached content |
|
65 | - * will be stored. |
|
66 | - */ |
|
67 | - const CACHE_DIR = 'dwoo'; |
|
68 | - |
|
69 | - /** |
|
70 | - * @var Dwoo Dwoo template engine |
|
71 | - */ |
|
72 | - protected $dwoo = null; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var string A string with the default template file extension, |
|
76 | - * including the dot |
|
77 | - */ |
|
78 | - protected $defaultExtension = '.html'; |
|
79 | - |
|
80 | - /** |
|
81 | - * stores the (optional) plugin directories to add to the Dwoo_Loader. |
|
82 | - */ |
|
83 | - protected $plugin_dir = null; |
|
84 | - |
|
85 | - /** |
|
86 | - * Pre-serialization callback. |
|
87 | - * |
|
88 | - * Excludes the Dwoo instance to prevent excessive serialization load. |
|
89 | - */ |
|
90 | - public function __sleep() |
|
91 | - { |
|
92 | - $keys = parent::__sleep(); |
|
93 | - unset($keys[array_search('dwoo', $keys)]); |
|
94 | - |
|
95 | - return $keys; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Initialize this Renderer. |
|
100 | - * |
|
101 | - * @param AgaviContext The current application context |
|
102 | - * @param array An associative array of initialization parameters |
|
103 | - */ |
|
104 | - public function initialize(AgaviContext $context, array $parameters = array()) |
|
105 | - { |
|
106 | - parent::initialize($context, $parameters); |
|
107 | - |
|
108 | - $this->plugin_dir = $this->getParameter('plugin_dir', $this->plugin_dir); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * provides a custom compiler to the dwoo renderer with optional settings |
|
113 | - * you can set in the agavi output_types.xml config file. |
|
114 | - * |
|
115 | - * @return Dwoo_Compiler |
|
116 | - */ |
|
117 | - public function compilerFactory() |
|
118 | - { |
|
119 | - $compiler = Dwoo_Compiler::compilerFactory(); |
|
120 | - $compiler->setAutoEscape((bool) $this->getParameter('auto_escape', false)); |
|
121 | - |
|
122 | - return $compiler; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Grab a cleaned up dwoo instance. |
|
127 | - * |
|
128 | - * @return Dwoo A Dwoo instance |
|
129 | - */ |
|
130 | - protected function getEngine() |
|
131 | - { |
|
132 | - if ($this->dwoo) { |
|
133 | - return $this->dwoo; |
|
134 | - } |
|
135 | - |
|
136 | - // this triggers Agavi autoload |
|
137 | - if (!class_exists('Dwoo')) { |
|
138 | - if (file_exists(dirname(__FILE__).'/../../../dwooAutoload.php')) { |
|
139 | - // file was dropped with the entire dwoo package |
|
140 | - require dirname(__FILE__).'/../../../dwooAutoload.php'; |
|
141 | - } else { |
|
142 | - // assume the dwoo package is in the include path |
|
143 | - require 'dwooAutoload.php'; |
|
144 | - } |
|
145 | - } |
|
146 | - |
|
147 | - $parentMode = fileperms(AgaviConfig::get('core.cache_dir')); |
|
148 | - |
|
149 | - $compileDir = AgaviConfig::get('core.cache_dir').DIRECTORY_SEPARATOR.self::COMPILE_DIR.DIRECTORY_SEPARATOR.self::COMPILE_SUBDIR; |
|
150 | - AgaviToolkit::mkdir($compileDir, $parentMode, true); |
|
151 | - |
|
152 | - $cacheDir = AgaviConfig::get('core.cache_dir').DIRECTORY_SEPARATOR.self::CACHE_DIR; |
|
153 | - AgaviToolkit::mkdir($cacheDir, $parentMode, true); |
|
154 | - |
|
155 | - $this->dwoo = new Dwoo_Core($compileDir, $cacheDir); |
|
156 | - |
|
157 | - if (!empty($this->plugin_dir)) { |
|
158 | - foreach ((array) $this->plugin_dir as $dir) { |
|
159 | - $this->dwoo->getLoader()->addDirectory($dir); |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - $this->dwoo->setDefaultCompilerFactory('file', array($this, 'compilerFactory')); |
|
164 | - |
|
165 | - return $this->dwoo; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Render the presentation and return the result. |
|
170 | - * |
|
171 | - * @param AgaviTemplateLayer The template layer to render |
|
172 | - * @param array The template variables |
|
173 | - * @param array The slots |
|
174 | - * @param array Associative array of additional assigns |
|
175 | - * |
|
176 | - * @return string A rendered result |
|
177 | - */ |
|
178 | - public function render(AgaviTemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array()) |
|
179 | - { |
|
180 | - $engine = $this->getEngine(); |
|
181 | - |
|
182 | - $data = array(); |
|
183 | - if ($this->extractVars) { |
|
184 | - $data = $attributes; |
|
185 | - } else { |
|
186 | - $data[$this->varName] = &$attributes; |
|
187 | - } |
|
188 | - |
|
189 | - $data[$this->slotsVarName] = &$slots; |
|
190 | - |
|
191 | - foreach ($this->assigns as $key => $getter) { |
|
192 | - $data[$key] = $this->getContext()->$getter(); |
|
193 | - } |
|
194 | - |
|
195 | - foreach ($moreAssigns as $key => &$value) { |
|
196 | - if (isset($this->moreAssignNames[$key])) { |
|
197 | - $key = $this->moreAssignNames[$key]; |
|
198 | - } |
|
199 | - $data[$key] = &$value; |
|
200 | - } |
|
201 | - |
|
202 | - return $engine->get($layer->getResourceStreamIdentifier(), $data); |
|
203 | - } |
|
51 | + /** |
|
52 | + * @constant string The directory inside the cache dir where templates will |
|
53 | + * be stored in compiled form. |
|
54 | + */ |
|
55 | + const COMPILE_DIR = 'templates'; |
|
56 | + |
|
57 | + /** |
|
58 | + * @constant string The subdirectory inside the compile dir where templates |
|
59 | + * will be stored in compiled form. |
|
60 | + */ |
|
61 | + const COMPILE_SUBDIR = 'dwoo'; |
|
62 | + |
|
63 | + /** |
|
64 | + * @constant string The directory inside the cache dir where cached content |
|
65 | + * will be stored. |
|
66 | + */ |
|
67 | + const CACHE_DIR = 'dwoo'; |
|
68 | + |
|
69 | + /** |
|
70 | + * @var Dwoo Dwoo template engine |
|
71 | + */ |
|
72 | + protected $dwoo = null; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var string A string with the default template file extension, |
|
76 | + * including the dot |
|
77 | + */ |
|
78 | + protected $defaultExtension = '.html'; |
|
79 | + |
|
80 | + /** |
|
81 | + * stores the (optional) plugin directories to add to the Dwoo_Loader. |
|
82 | + */ |
|
83 | + protected $plugin_dir = null; |
|
84 | + |
|
85 | + /** |
|
86 | + * Pre-serialization callback. |
|
87 | + * |
|
88 | + * Excludes the Dwoo instance to prevent excessive serialization load. |
|
89 | + */ |
|
90 | + public function __sleep() |
|
91 | + { |
|
92 | + $keys = parent::__sleep(); |
|
93 | + unset($keys[array_search('dwoo', $keys)]); |
|
94 | + |
|
95 | + return $keys; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Initialize this Renderer. |
|
100 | + * |
|
101 | + * @param AgaviContext The current application context |
|
102 | + * @param array An associative array of initialization parameters |
|
103 | + */ |
|
104 | + public function initialize(AgaviContext $context, array $parameters = array()) |
|
105 | + { |
|
106 | + parent::initialize($context, $parameters); |
|
107 | + |
|
108 | + $this->plugin_dir = $this->getParameter('plugin_dir', $this->plugin_dir); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * provides a custom compiler to the dwoo renderer with optional settings |
|
113 | + * you can set in the agavi output_types.xml config file. |
|
114 | + * |
|
115 | + * @return Dwoo_Compiler |
|
116 | + */ |
|
117 | + public function compilerFactory() |
|
118 | + { |
|
119 | + $compiler = Dwoo_Compiler::compilerFactory(); |
|
120 | + $compiler->setAutoEscape((bool) $this->getParameter('auto_escape', false)); |
|
121 | + |
|
122 | + return $compiler; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Grab a cleaned up dwoo instance. |
|
127 | + * |
|
128 | + * @return Dwoo A Dwoo instance |
|
129 | + */ |
|
130 | + protected function getEngine() |
|
131 | + { |
|
132 | + if ($this->dwoo) { |
|
133 | + return $this->dwoo; |
|
134 | + } |
|
135 | + |
|
136 | + // this triggers Agavi autoload |
|
137 | + if (!class_exists('Dwoo')) { |
|
138 | + if (file_exists(dirname(__FILE__).'/../../../dwooAutoload.php')) { |
|
139 | + // file was dropped with the entire dwoo package |
|
140 | + require dirname(__FILE__).'/../../../dwooAutoload.php'; |
|
141 | + } else { |
|
142 | + // assume the dwoo package is in the include path |
|
143 | + require 'dwooAutoload.php'; |
|
144 | + } |
|
145 | + } |
|
146 | + |
|
147 | + $parentMode = fileperms(AgaviConfig::get('core.cache_dir')); |
|
148 | + |
|
149 | + $compileDir = AgaviConfig::get('core.cache_dir').DIRECTORY_SEPARATOR.self::COMPILE_DIR.DIRECTORY_SEPARATOR.self::COMPILE_SUBDIR; |
|
150 | + AgaviToolkit::mkdir($compileDir, $parentMode, true); |
|
151 | + |
|
152 | + $cacheDir = AgaviConfig::get('core.cache_dir').DIRECTORY_SEPARATOR.self::CACHE_DIR; |
|
153 | + AgaviToolkit::mkdir($cacheDir, $parentMode, true); |
|
154 | + |
|
155 | + $this->dwoo = new Dwoo_Core($compileDir, $cacheDir); |
|
156 | + |
|
157 | + if (!empty($this->plugin_dir)) { |
|
158 | + foreach ((array) $this->plugin_dir as $dir) { |
|
159 | + $this->dwoo->getLoader()->addDirectory($dir); |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + $this->dwoo->setDefaultCompilerFactory('file', array($this, 'compilerFactory')); |
|
164 | + |
|
165 | + return $this->dwoo; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Render the presentation and return the result. |
|
170 | + * |
|
171 | + * @param AgaviTemplateLayer The template layer to render |
|
172 | + * @param array The template variables |
|
173 | + * @param array The slots |
|
174 | + * @param array Associative array of additional assigns |
|
175 | + * |
|
176 | + * @return string A rendered result |
|
177 | + */ |
|
178 | + public function render(AgaviTemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array()) |
|
179 | + { |
|
180 | + $engine = $this->getEngine(); |
|
181 | + |
|
182 | + $data = array(); |
|
183 | + if ($this->extractVars) { |
|
184 | + $data = $attributes; |
|
185 | + } else { |
|
186 | + $data[$this->varName] = &$attributes; |
|
187 | + } |
|
188 | + |
|
189 | + $data[$this->slotsVarName] = &$slots; |
|
190 | + |
|
191 | + foreach ($this->assigns as $key => $getter) { |
|
192 | + $data[$key] = $this->getContext()->$getter(); |
|
193 | + } |
|
194 | + |
|
195 | + foreach ($moreAssigns as $key => &$value) { |
|
196 | + if (isset($this->moreAssignNames[$key])) { |
|
197 | + $key = $this->moreAssignNames[$key]; |
|
198 | + } |
|
199 | + $data[$key] = &$value; |
|
200 | + } |
|
201 | + |
|
202 | + return $engine->get($layer->getResourceStreamIdentifier(), $data); |
|
203 | + } |
|
204 | 204 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | if (!defined('BASEPATH')) { |
3 | - exit('No direct script access allowed'); |
|
3 | + exit('No direct script access allowed'); |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | // The name of the directory where templates are located. |