| 2 |
|
-[2008-04-] 0.4.0 (Happy Getty) |
| 3 |
|
-! BC Break: $dwoo->output() and get() have been swapped internally, but it |
| 4 |
|
- doesn't change anything for you unless you called output(*, *, *, true) |
| 5 |
|
- directly to emulate get(). This was done to reduce some overhead |
| 6 |
|
-+ Plugins: Added {extends} and {block} to handle template inheritance, read |
| 7 |
|
- more about it at http://wiki.dwoo.org/index.php/TemplateInheritance |
| 8 |
|
-+ Plugins: Added {loop} that combines {foreach} and {with}, see |
| 9 |
|
- http://wiki.dwoo.org/index.php/Block:loop for details |
| 10 |
|
-+ Plugins: Added {do} that executes whatever you feed it whitout echoing the |
| 11 |
|
- result, used internally for extends but you can use it if required |
| 12 |
|
-+ Syntax: Added shortcut for {$dwoo.const.*} using '%', for example you can use |
| 13 |
|
- {%FOO} instead of {$dwoo.const.FOO} |
| 14 |
|
-+ Syntax: When using named parameters, typing a parameter name without any |
| 15 |
|
- value is the same as typing param=true, for example {foo name="test" bar} and |
| 16 |
|
- {foo name="test" bar=true} are equals, can be useful for very complex plugins |
| 17 |
|
- with huge amounts of parameters. |
| 18 |
|
-+ Syntax: Added support for {$foo+=5}, {$foo="a"}, {$foo++} and {$foo--} |
| 19 |
|
-+ Syntax: Added shortcut for $dwoo.*, you can now use {$.foreach.foo} instead |
| 20 |
|
- of {$dwoo.foreach.foo} for example, applies to all $dwoo.* vars |
| 21 |
|
-+ API: Added getSource(), getUid() and getResourceIdentifier() to DwooITemplate |
| 22 |
|
-+ API: Added setSecurityPolicy() too DwooICompiler and modified the arguments |
| 23 |
|
- of its compile() method |
| 24 |
|
-+ API: Added a bunch of utility functions to DwooCompiler, allowing compiled |
| 25 |
|
- plugins to access more of the compiler internals |
| 26 |
|
-* Reworked Dwoo template rendering to avoid variable conflicts |
| 27 |
|
-* Fixed a classpath rebuilding bug that occured on some UNIX platforms due to |
| 28 |
|
- glob() inconsistencies |
| 29 |
|
-* Various performance improvements, including the removal of a lot of isset() |
| 30 |
|
- calls. Doing {$foo} if foo is undefined will now display a PHP warning, but |
| 31 |
|
- doing {foreach $foo}..{/foreach} will not however, that way you don't have |
| 32 |
|
- to do {if isset($foo)} before the foreach, but automated isset() calls don't |
| 33 |
|
- impact performance as much as they did before. |
| 34 |
|
-* API: DwooITemplate->clearCache now requires a Dwoo instance as its first arg, |
| 35 |
|
- should not affect you unless you built a custom template class from scratch |
| 36 |
|
-* {include} now uses the current resource if none is provided instead of using |
| 37 |
|
- file as it did before |
| 38 |
|
-* Dwoo uses include path instead of absolute includes |
| 39 |
|
-* Fixed a regression in the handling of custom class plugins |
| 40 |
|
-* Fixed a bug in DwooSecurityPolicy->getAllowedDirectories(), no security |
| 41 |
|
- issue though |
| 42 |
|
-* TestFest happened early for Dwoo, lots of new tests and more code covered |
| 43 |
|
- |
| 44 |
|
-[2008-04-09] 0.3.4 |
| 45 |
|
-! BC Break: DWOO_PATH constant changed to DWOO_DIRECTORY |
| 46 |
|
-! BC Break: Smarty's @ operator for modifiers is now reversed, for example |
| 47 |
|
- $array|reverse will reverse the items of that array while $array|@reverse |
| 48 |
|
- will reverse each item of the given array (as if you used array_map) |
| 49 |
|
-+ Syntax: Added support for method calls on objects i.e. {$foo->bar()} |
| 50 |
|
-+ Added support for smarty security features, see the DwooSecurityPolicy class |
| 51 |
|
- and $dwoo->setSecurityPolicy() |
| 52 |
|
-+ API: Added a DwooCompiler->setLooseOpeningHandling() method that, if set to |
| 53 |
|
- true, allows tags to contain spaces between the opening bracket and the |
| 54 |
|
- content. Turned off by default as it allows to compile files containing |
| 55 |
|
- css and javascript without the need to escape it through {literal} or \{ |
| 56 |
|
-+ Added DWOO_CACHE_DIRECTORY and DWOO_COMPILE_DIRECTORY constants that you can |
| 57 |
|
- set before including Dwoo.php to override the defaults (although |
| 58 |
|
- Dwoo->setCacheDir/setCompileDir() still work to change that if required) |
| 59 |
|
-+ Added the DwooException class |
| 60 |
|
-+ Smarty: Added partial support for register_object(), unregister_object() and |
| 61 |
|
- get_registered_object(). All features can not be supported by the adapter |
| 62 |
|
- though so you might get compatibility warnings |
| 63 |
|
-* Fixed {elseif} bug that appeared when multiple elseif tags were used in a row |
| 64 |
|
-* Syntax: Improved simple math support to work within variable variables |
| 65 |
|
- (i.e. you can do {$array[$index+1]}) and within strings as well. To prevent |
| 66 |
|
- this enclose the variables in backticks (i.e. {"$foo/$bar"} will do the math |
| 67 |
|
- while {"`$foo`/$bar"} won't as $foo is properly delimited) |
| 68 |
|
-* Changed DwooLoader::addDirectory() so that it writes the class paths cache |
| 69 |
|
- into DWOO_COMPILE_DIRECTORY, that way you don't have to make your plugin |
| 70 |
|
- directory writable |
| 71 |
|
-* Made all the error triggering more consistent |
| 72 |
|
-* Changed automatic cacheId generation in DwooTemplateFile/String to be faster |
| 73 |
|
- |
| 74 |
|
-[2008-03-19] 0.3.3 |
| 75 |
|
-+ Syntax: Added support for $dwoo.const.CONSTANT and |
| 76 |
|
- $dwoo.const.Class::CONSTANT to read PHP constants from the template |
| 77 |
|
-+ Syntax: Added support for on/off/yes/no, that work as aliases for true/false |
| 78 |
|
-+ Syntax: Added the $dwoo.charset global variable |
| 79 |
|
-+ Plugins: Added {withelse} and made {with} compatible with {else} also |
| 80 |
|
-+ API: Added left/right delimiters customization, see DwooCompiler->setDelimiters() |
| 81 |
|
-+ API: Added DwooCompiler->triggerError() |
| 82 |
|
-+ API: Added Dwoo->clearCache() and DwooITemplate->clearCache() methods |
| 83 |
|
-+ Smarty: The smartyCompat prefilter converts {section} tags into {for} tags on the |
| 84 |
|
- fly, however it's not guaranteed to work with *all* section tags, let me know if |
| 85 |
|
- it breaks for you |
| 86 |
|
-* {with} now skips the entire block if it's variable doesn't exist, so by |
| 87 |
|
- itself it acts as if you would do {if $var}{with $var}{/with}{/if} |
| 88 |
|
-* Each resource has a compiler factory function assigned to it, allowing you to |
| 89 |
|
- easily provide a custom compiler without loading it on every page |
| 90 |
|
-* OutputFilters are now simply called Filters (they still use DwooFilter) |
| 91 |
|
-* Pre/PostFilters have become Pre/PostProcessors (they now use DwooProcessor) |
| 92 |
|
-* Compiler: Fixed parsing bug that prevented function names of 1character |
| 93 |
|
-* Compiler: Changed internal handling of variables to fix some errors being |
| 94 |
|
- thrown with specific cases |
| 95 |
|
-* Reorganized Dwoo/DwooCompiler and fully commented all the core classes |
| 96 |
|
- and interfaces |
| 97 |
|
- |
| 98 |
|
-[2008-03-09] 0.3.2 |
| 99 |
|
-+ Added access to superglobals through $dwoo.get.value, $dwoo.post.value, |
| 100 |
|
- etc. |
| 101 |
|
-+ Added outputFilters to Dwoo (use Dwoo->addOutputFilter and |
| 102 |
|
- Dwoo->removeOutputFilter) |
| 103 |
|
-+ Added preFilters and postFilters to DwooCompiler (use |
| 104 |
|
- DwooCompiler->addPreFilter, etc) |
| 105 |
|
-+ Added a html_format output filter that intends properly the html code, |
| 106 |
|
- use it only on full page templates |
| 107 |
|
-+ Plugins: Added {for} and {forelse} which allow to loop over an array or to |
| 108 |
|
- loop over a range of numbers |
| 109 |
|
-+ Plugins: Added {mailto}, {counter}, {eval}, {fetch} and {include} |
| 110 |
|
-+ Syntax : Enhanced support for implicit math operations, |
| 111 |
|
- {$var+$var2*var3+5} now works. Operations are executed from left to right |
| 112 |
|
- though, there is no operator priority. (i.e. 1+1*2 = (1+1)*2 = 4, not 3) |
| 113 |
|
-+ API: Added resources support through DwooITemplate implementations and |
| 114 |
|
- Dwoo->addResource() |
| 115 |
|
-+ API: Added Dwoo->getTemplate() to get the currently running template object |
| 116 |
|
-+ API: Added DwooCompiler::getInstance() to use only one compiler object when |
| 117 |
|
- rendering from the default compiler and to provide you with a singleton if |
| 118 |
|
- it's easier, however the class is not a singleton in the sense that it can |
| 119 |
|
- be instantiated separately |
| 120 |
|
-+ API: Added a factory method on DwooITemplate to support resources creation |
| 121 |
|
-+ Added a release tag so that all compiled templates are forced to recompile |
| 122 |
|
- after an update, however it is recommended to cleanup your "compiled" |
| 123 |
|
- directory now and then as each release uses new filenames |
| 124 |
|
-+ Added an abstract DwooFilter class that you can extend to build filters |
| 125 |
|
-* PHP function calls are now case insensitive |
| 126 |
|
-* Syntax: The compiler now parses expressions before modifiers, allowing for |
| 127 |
|
- {$var/2|number_format} for example |
| 128 |
|
-* DwooTemplateFile now extends DwooTemplateString instead of the other way |
| 129 |
|
- around as it was before |
| 130 |
|
-* {else} is now a general purpose plugin that can act as 'else' for foreach, |
| 131 |
|
- for and if/elseif, foreachelse is still available though |
| 132 |
|
- |
| 133 |
|
-[2008-03-05] 0.3.1 |
| 134 |
|
-+ Added {cycle} function |
| 135 |
|
-+ Syntax : Enabled support for associative arrays using |
| 136 |
|
- array(key="value", key2=5) for example, which you can assign or use in a |
| 137 |
|
- foreach directly |
| 138 |
|
-+ Syntax : Added support for {$var +-/*% X} (i.e. {$var + 4}), useful for |
| 139 |
|
- simple math operations without the math plugin |
| 140 |
|
-+ API : Added append/appendByRef to DwooData |
| 141 |
|
-+ Completely rebuilt DwooSmartyAdapter, it should "work" and fail silently if |
| 142 |
|
- you use a non supported function now, however you can set |
| 143 |
|
- $smarty->show_compat_errors=true; on it to receive notices about unsupported |
| 144 |
|
- features that you use |
| 145 |
|
-* Bug fixed in {literal} parsing |
| 146 |
|
-* Bug fixed in smarty functions handling |
| 147 |
|
-* API : Moved Plugin types constants to Dwoo so the compiler doesn't have to |
| 148 |
|
- be loaded unles really required |
| 149 |
|
-* API : Moved globals and var reinitialization in Dwoo into their own methods |
| 150 |
|
- so that child classes can easily add globals |
| 151 |
|
-* Some improvements in the compiler output |
| 152 |
|
-* Some changes in the cache handling of DwooTemplateFile |
| 153 |
|
-- Special thanks to Andrew Collins that found many of the bugs fixed in this |
| 154 |
|
- release |
| 155 |
|
- |
| 156 |
|
-[2008-03-02] 0.3.0 |
| 157 |
|
-+ Full template cache support |
| 158 |
|
-+ DwooTemplateString class to load templates from a string |
| 159 |
|
-+ Dwoo::VERSION constant |
| 160 |
|
-+ {dump} plugin to print out variables |
| 161 |
|
-+ Unit tests (with PHPUnit) covering 73% of the codebase right now, which |
| 162 |
|
- should help reducing regression bugs in the next versions. |
| 163 |
|
-+ Started commenting (with phpdocs) properly all the classes, should be |
| 164 |
|
- complete for 0.4.0 |
| 165 |
|
-* {capture} is now compilable and has a new boolean flag to append output into |
| 166 |
|
- the target variable instead of overwriting |
| 167 |
|
-* {foreach} supports direct input (instead of only variables), allowing |
| 168 |
|
- constructs like {foreach array(a,b,c) val}{$val}{/foreach} for example that |
| 169 |
|
- would output abc. |
| 170 |
|
-* pre/postProcessing functions in block plugins now receive an array of named |
| 171 |
|
- parameters instead of numbered |
| 172 |
|
-* Major refactoring of DwooTemplateFile and DwooCompiler |
| 173 |
|
-* Cleaned up members visibility in Dwoo/DwooCompiler |
| 174 |
|
-* Fixes in the compiler parsing and general variables handling |
| 175 |
|
-* Multiple bugfixes here and there thanks to the unit tests |
| 176 |
|
-* Optimized {foreach} a lot |
| 177 |
|
- |
| 178 |
|
-[2008-02-19] 0.2.1 |
| 179 |
|
-* Compiler fixes for argument parsing and handling of Smarty plugins |
| 180 |
|
- |
| 181 |
|
-[2008-02-14] 0.2.0 |
| 182 |
|
-+ Added support for plugins made for Smarty (that includes modifiers, |
| 183 |
|
- functions and blocks). Not thoroughly tested. |
| 184 |
|
-+ Major API changes in the way Dwoo must be run, it's now much more |
| 185 |
|
- flexible and should not change too much in the future. |
| 186 |
|
-+ Added support for custom plugins, filters should come in the next version |
| 187 |
|
- although the API to register them is already in. |
| 188 |
|
- |
| 189 |
|
-[2008-02-08] 0.1.0 |
|
2 |
+[2008-04-] 0.4.0 (Happy Getty) |
|
3 |
+! BC Break: $dwoo->output() and get() have been swapped internally, but it |
|
4 |
+ doesn't change anything for you unless you called output(*, *, *, true) |
|
5 |
+ directly to emulate get(). This was done to reduce some overhead |
|
6 |
+! BC Break: $dwoo->getTemplate() changed to $dwoo->templateFactory() and |
|
7 |
+ $dwoo->getCurrentTemplate() changed to $dwoo->getTemplate() for consistency |
|
8 |
+ among all classes and factory functions |
|
9 |
++ Plugins: Added {extends} and {block} to handle template inheritance, read |
|
10 |
+ more about it at http://wiki.dwoo.org/index.php/TemplateInheritance |
|
11 |
++ Plugins: Added {loop} that combines {foreach} and {with}, see |
|
12 |
+ http://wiki.dwoo.org/index.php/Block:loop for details |
|
13 |
++ Plugins: Added {do} that executes whatever you feed it whitout echoing the |
|
14 |
+ result, used internally for extends but you can use it if required |
|
15 |
++ Syntax: Added shortcut for {$dwoo.const.*} using '%', for example you can use |
|
16 |
+ {%FOO} instead of {$dwoo.const.FOO} |
|
17 |
++ Syntax: When using named parameters, typing a parameter name without any |
|
18 |
+ value is the same as typing param=true, for example {foo name="test" bar} and |
|
19 |
+ {foo name="test" bar=true} are equals, can be useful for very complex plugins |
|
20 |
+ with huge amounts of parameters. |
|
21 |
++ Syntax: Added support for {$foo+=5}, {$foo="a"}, {$foo++} and {$foo--} |
|
22 |
++ Syntax: Added shortcut for $dwoo.*, you can now use {$.foreach.foo} instead |
|
23 |
+ of {$dwoo.foreach.foo} for example, applies to all $dwoo.* vars |
|
24 |
++ API: Added getSource(), getUid() and getResourceIdentifier() to DwooITemplate |
|
25 |
++ API: Added setSecurityPolicy() too DwooICompiler and modified the arguments |
|
26 |
+ of its compile() method |
|
27 |
++ API: Added a bunch of utility functions to DwooCompiler, allowing compiled |
|
28 |
+ plugins to access more of the compiler internals |
|
29 |
+* Moved all files to Dwoo/Class.php excepted for the core Dwoo.php file |
|
30 |
+* Fixed a classpath rebuilding bug that occured on some UNIX platforms due to |
|
31 |
+ glob() returning false sometimes for empty folders |
|
32 |
+* Various performance improvements, including the removal of a lot of isset() |
|
33 |
+ calls. Doing {$foo} if foo is undefined will now display a PHP warning, but |
|
34 |
+ doing {foreach $foo}..{/foreach} will not however, that way you don't have |
|
35 |
+ to do {if isset($foo)} before the foreach, but automated isset() calls don't |
|
36 |
+ impact performance as much as they did before. |
|
37 |
+* API: DwooITemplate->clearCache now requires a Dwoo instance as its first arg, |
|
38 |
+ should not affect you unless you built a custom template class from scratch |
|
39 |
+* Reworked Dwoo template rendering to avoid variable conflicts with plugins |
|
40 |
+* {include} now uses the current resource if none is provided instead of using |
|
41 |
+ file as it did before |
|
42 |
+* Dwoo uses include path instead of absolute includes |
|
43 |
+* Fixed a regression in the handling of custom class plugins |
|
44 |
+* Fixed a bug in DwooSecurityPolicy->getAllowedDirectories(), no security |
|
45 |
+ issue though |
|
46 |
+* Changed all line endings to Unix (line feed only) and all spaces left have |
|
47 |
+ been converted to tabs (tabspace 4) |
|
48 |
+* TestFest happened early for Dwoo, lots of new tests and more code covered |
|
49 |
+ |
|
50 |
+[2008-04-09] 0.3.4 |
|
51 |
+! BC Break: DWOO_PATH constant changed to DWOO_DIRECTORY |
|
52 |
+! BC Break: Smarty's @ operator for modifiers is now reversed, for example |
|
53 |
+ $array|reverse will reverse the items of that array while $array|@reverse |
|
54 |
+ will reverse each item of the given array (as if you used array_map) |
|
55 |
++ Syntax: Added support for method calls on objects i.e. {$foo->bar()} |
|
56 |
++ Added support for smarty security features, see the DwooSecurityPolicy class |
|
57 |
+ and $dwoo->setSecurityPolicy() |
|
58 |
++ API: Added a DwooCompiler->setLooseOpeningHandling() method that, if set to |
|
59 |
+ true, allows tags to contain spaces between the opening bracket and the |
|
60 |
+ content. Turned off by default as it allows to compile files containing |
|
61 |
+ css and javascript without the need to escape it through {literal} or \{ |
|
62 |
++ Added DWOO_CACHE_DIRECTORY and DWOO_COMPILE_DIRECTORY constants that you can |
|
63 |
+ set before including Dwoo.php to override the defaults (although |
|
64 |
+ Dwoo->setCacheDir/setCompileDir() still work to change that if required) |
|
65 |
++ Added the DwooException class |
|
66 |
++ Smarty: Added partial support for register_object(), unregister_object() and |
|
67 |
+ get_registered_object(). All features can not be supported by the adapter |
|
68 |
+ though so you might get compatibility warnings |
|
69 |
+* Fixed {elseif} bug that appeared when multiple elseif tags were used in a row |
|
70 |
+* Syntax: Improved simple math support to work within variable variables |
|
71 |
+ (i.e. you can do {$array[$index+1]}) and within strings as well. To prevent |
|
72 |
+ this enclose the variables in backticks (i.e. {"$foo/$bar"} will do the math |
|
73 |
+ while {"`$foo`/$bar"} won't as $foo is properly delimited) |
|
74 |
+* Changed DwooLoader::addDirectory() so that it writes the class paths cache |
|
75 |
+ into DWOO_COMPILE_DIRECTORY, that way you don't have to make your plugin |
|
76 |
+ directory writable |
|
77 |
+* Made all the error triggering more consistent |
|
78 |
+* Changed automatic cacheId generation in DwooTemplateFile/String to be faster |
|
79 |
+ |
|
80 |
+[2008-03-19] 0.3.3 |
|
81 |
++ Syntax: Added support for $dwoo.const.CONSTANT and |
|
82 |
+ $dwoo.const.Class::CONSTANT to read PHP constants from the template |
|
83 |
++ Syntax: Added support for on/off/yes/no, that work as aliases for true/false |
|
84 |
++ Syntax: Added the $dwoo.charset global variable |
|
85 |
++ Plugins: Added {withelse} and made {with} compatible with {else} also |
|
86 |
++ API: Added left/right delimiters customization, see DwooCompiler->setDelimiters() |
|
87 |
++ API: Added DwooCompiler->triggerError() |
|
88 |
++ API: Added Dwoo->clearCache() and DwooITemplate->clearCache() methods |
|
89 |
++ Smarty: The smartyCompat prefilter converts {section} tags into {for} tags on the |
|
90 |
+ fly, however it's not guaranteed to work with *all* section tags, let me know if |
|
91 |
+ it breaks for you |
|
92 |
+* {with} now skips the entire block if it's variable doesn't exist, so by |
|
93 |
+ itself it acts as if you would do {if $var}{with $var}{/with}{/if} |
|
94 |
+* Each resource has a compiler factory function assigned to it, allowing you to |
|
95 |
+ easily provide a custom compiler without loading it on every page |
|
96 |
+* OutputFilters are now simply called Filters (they still use DwooFilter) |
|
97 |
+* Pre/PostFilters have become Pre/PostProcessors (they now use DwooProcessor) |
|
98 |
+* Compiler: Fixed parsing bug that prevented function names of 1character |
|
99 |
+* Compiler: Changed internal handling of variables to fix some errors being |
|
100 |
+ thrown with specific cases |
|
101 |
+* Reorganized Dwoo/DwooCompiler and fully commented all the core classes |
|
102 |
+ and interfaces |
|
103 |
+ |
|
104 |
+[2008-03-09] 0.3.2 |
|
105 |
++ Added access to superglobals through $dwoo.get.value, $dwoo.post.value, |
|
106 |
+ etc. |
|
107 |
++ Added outputFilters to Dwoo (use Dwoo->addOutputFilter and |
|
108 |
+ Dwoo->removeOutputFilter) |
|
109 |
++ Added preFilters and postFilters to DwooCompiler (use |
|
110 |
+ DwooCompiler->addPreFilter, etc) |
|
111 |
++ Added a html_format output filter that intends properly the html code, |
|
112 |
+ use it only on full page templates |
|
113 |
++ Plugins: Added {for} and {forelse} which allow to loop over an array or to |
|
114 |
+ loop over a range of numbers |
|
115 |
++ Plugins: Added {mailto}, {counter}, {eval}, {fetch} and {include} |
|
116 |
++ Syntax : Enhanced support for implicit math operations, |
|
117 |
+ {$var+$var2*var3+5} now works. Operations are executed from left to right |
|
118 |
+ though, there is no operator priority. (i.e. 1+1*2 = (1+1)*2 = 4, not 3) |
|
119 |
++ API: Added resources support through DwooITemplate implementations and |
|
120 |
+ Dwoo->addResource() |
|
121 |
++ API: Added Dwoo->getTemplate() to get the currently running template object |
|
122 |
++ API: Added DwooCompiler::getInstance() to use only one compiler object when |
|
123 |
+ rendering from the default compiler and to provide you with a singleton if |
|
124 |
+ it's easier, however the class is not a singleton in the sense that it can |
|
125 |
+ be instantiated separately |
|
126 |
++ API: Added a factory method on DwooITemplate to support resources creation |
|
127 |
++ Added a release tag so that all compiled templates are forced to recompile |
|
128 |
+ after an update, however it is recommended to cleanup your "compiled" |
|
129 |
+ directory now and then as each release uses new filenames |
|
130 |
++ Added an abstract DwooFilter class that you can extend to build filters |
|
131 |
+* PHP function calls are now case insensitive |
|
132 |
+* Syntax: The compiler now parses expressions before modifiers, allowing for |
|
133 |
+ {$var/2|number_format} for example |
|
134 |
+* DwooTemplateFile now extends DwooTemplateString instead of the other way |
|
135 |
+ around as it was before |
|
136 |
+* {else} is now a general purpose plugin that can act as 'else' for foreach, |
|
137 |
+ for and if/elseif, foreachelse is still available though |
|
138 |
+ |
|
139 |
+[2008-03-05] 0.3.1 |
|
140 |
++ Added {cycle} function |
|
141 |
++ Syntax : Enabled support for associative arrays using |
|
142 |
+ array(key="value", key2=5) for example, which you can assign or use in a |
|
143 |
+ foreach directly |
|
144 |
++ Syntax : Added support for {$var +-/*% X} (i.e. {$var + 4}), useful for |
|
145 |
+ simple math operations without the math plugin |
|
146 |
++ API : Added append/appendByRef to DwooData |
|
147 |
++ Completely rebuilt DwooSmartyAdapter, it should "work" and fail silently if |
|
148 |
+ you use a non supported function now, however you can set |
|
149 |
+ $smarty->show_compat_errors=true; on it to receive notices about unsupported |
|
150 |
+ features that you use |
|
151 |
+* Bug fixed in {literal} parsing |
|
152 |
+* Bug fixed in smarty functions handling |
|
153 |
+* API : Moved Plugin types constants to Dwoo so the compiler doesn't have to |
|
154 |
+ be loaded unles really required |
|
155 |
+* API : Moved globals and var reinitialization in Dwoo into their own methods |
|
156 |
+ so that child classes can easily add globals |
|
157 |
+* Some improvements in the compiler output |
|
158 |
+* Some changes in the cache handling of DwooTemplateFile |
|
159 |
+- Special thanks to Andrew Collins that found many of the bugs fixed in this |
|
160 |
+ release |
|
161 |
+ |
|
162 |
+[2008-03-02] 0.3.0 |
|
163 |
++ Full template cache support |
|
164 |
++ DwooTemplateString class to load templates from a string |
|
165 |
++ Dwoo::VERSION constant |
|
166 |
++ {dump} plugin to print out variables |
|
167 |
++ Unit tests (with PHPUnit) covering 73% of the codebase right now, which |
|
168 |
+ should help reducing regression bugs in the next versions. |
|
169 |
++ Started commenting (with phpdocs) properly all the classes, should be |
|
170 |
+ complete for 0.4.0 |
|
171 |
+* {capture} is now compilable and has a new boolean flag to append output into |
|
172 |
+ the target variable instead of overwriting |
|
173 |
+* {foreach} supports direct input (instead of only variables), allowing |
|
174 |
+ constructs like {foreach array(a,b,c) val}{$val}{/foreach} for example that |
|
175 |
+ would output abc. |
|
176 |
+* pre/postProcessing functions in block plugins now receive an array of named |
|
177 |
+ parameters instead of numbered |
|
178 |
+* Major refactoring of DwooTemplateFile and DwooCompiler |
|
179 |
+* Cleaned up members visibility in Dwoo/DwooCompiler |
|
180 |
+* Fixes in the compiler parsing and general variables handling |
|
181 |
+* Multiple bugfixes here and there thanks to the unit tests |
|
182 |
+* Optimized {foreach} a lot |
|
183 |
+ |
|
184 |
+[2008-02-19] 0.2.1 |
|
185 |
+* Compiler fixes for argument parsing and handling of Smarty plugins |
|
186 |
+ |
|
187 |
+[2008-02-14] 0.2.0 |
|
188 |
++ Added support for plugins made for Smarty (that includes modifiers, |
|
189 |
+ functions and blocks). Not thoroughly tested. |
|
190 |
++ Major API changes in the way Dwoo must be run, it's now much more |
|
191 |
+ flexible and should not change too much in the future. |
|
192 |
++ Added support for custom plugins, filters should come in the next version |
|
193 |
+ although the API to register them is already in. |
|
194 |
+ |
|
195 |
+[2008-02-08] 0.1.0 |
| 190 |
196 |
Initial release |
| 2 |
|
-<?php |
| 3 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
| 4 |
4 |
set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__)); |
| 5 |
|
-// TODO move those in Dwoo/Interface, ... |
| 6 |
|
-include 'DwooInterfaces.php'; |
| 7 |
|
-include 'DwooPlugin.php'; |
| 8 |
|
-include 'DwooTemplateString.php'; |
| 9 |
|
-include 'DwooTemplateFile.php'; |
| 10 |
|
-include 'DwooData.php'; |
| 11 |
|
- |
| 12 |
|
-define('DWOO_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR); |
| 13 |
|
-if(defined('DWOO_CACHE_DIRECTORY') === false) |
| 14 |
|
- define('DWOO_CACHE_DIRECTORY', DWOO_DIRECTORY.'cache'.DIRECTORY_SEPARATOR); |
| 15 |
|
-if(defined('DWOO_COMPILE_DIRECTORY') === false) |
| 16 |
|
- define('DWOO_COMPILE_DIRECTORY', DWOO_DIRECTORY.'compiled'.DIRECTORY_SEPARATOR); |
| 17 |
|
-if(is_writable(DWOO_CACHE_DIRECTORY) === false) |
| 18 |
|
- throw new DwooException('Dwoo cache directory must be writable, either chmod "'.DWOO_CACHE_DIRECTORY.'" to make it writable or define DWOO_CACHE_DIRECTORY to a writable directory before including Dwoo.php'); |
| 19 |
|
-if(is_writable(DWOO_COMPILE_DIRECTORY) === false) |
| 20 |
|
- throw new DwooException('Dwoo compile directory must be writable, either chmod "'.DWOO_COMPILE_DIRECTORY.'" to make it writable or define DWOO_COMPILE_DIRECTORY to a writable directory before including Dwoo.php'); |
| 21 |
|
- |
| 22 |
|
-// include class paths or rebuild paths if the cache file isn't there |
| 23 |
|
-if((file_exists(DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php') && include DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php') === false) |
| 24 |
|
- DwooLoader::rebuildClassPathCache(DWOO_DIRECTORY.'plugins', DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php'); |
| 25 |
|
- |
| 26 |
|
-DwooLoader::loadPlugin('topLevelBlock'); |
| 27 |
|
- |
| 28 |
|
-/** |
| 29 |
|
- * main dwoo class, allows communication between the compiler, template and data classes |
| 30 |
|
- * |
| 31 |
|
- * requirements : |
| 32 |
|
- * php 5.2.0 or above |
| 33 |
|
- * php's mbstring extension for some plugins |
| 34 |
|
- * php's hash extension to use DwooTemplateString class |
| 35 |
|
- * |
| 36 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 37 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 38 |
|
- * |
| 39 |
|
- * This file is released under the LGPL |
| 40 |
|
- * "GNU Lesser General Public License" |
| 41 |
|
- * More information can be found here: |
| 42 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 43 |
|
- * |
| 44 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 45 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 46 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 47 |
|
- * @link http://dwoo.org/ |
| 48 |
|
- * @version 0.3.4 |
| 49 |
|
- * @date 2008-04-09 |
| 50 |
|
- * @package Dwoo |
| 51 |
|
- */ |
| 52 |
|
-class Dwoo |
| 53 |
|
-{ |
| 54 |
|
- /** |
| 55 |
|
- * current version number |
| 56 |
|
- * |
| 57 |
|
- * @var string |
| 58 |
|
- */ |
| 59 |
|
- const VERSION = "0.3.4"; |
| 60 |
|
- |
| 61 |
|
- /** |
| 62 |
|
- * unique number of this dwoo release |
| 63 |
|
- * |
| 64 |
|
- * this can be used by templates classes to check whether the compiled template |
| 65 |
|
- * has been compiled before this release or not, so that old templates are |
| 66 |
|
- * recompiled automatically when Dwoo is updated |
| 67 |
|
- */ |
| 68 |
|
- const RELEASE_TAG = 8; |
| 69 |
|
- |
| 70 |
|
- /**#@+ |
| 71 |
|
- * constants that represents all plugin types |
| 72 |
|
- * |
| 73 |
|
- * these are bitwise-operation-safe values to allow multiple types |
| 74 |
|
- * on a single plugin |
| 75 |
|
- * |
| 76 |
|
- * @var int |
| 77 |
|
- */ |
| 78 |
|
- const CLASS_PLUGIN = 1; |
| 79 |
|
- const FUNC_PLUGIN = 2; |
| 80 |
|
- const NATIVE_PLUGIN = 4; |
| 81 |
|
- const BLOCK_PLUGIN = 8; |
| 82 |
|
- const COMPILABLE_PLUGIN = 16; |
| 83 |
|
- const CUSTOM_PLUGIN = 32; |
| 84 |
|
- const SMARTY_MODIFIER = 64; |
| 85 |
|
- const SMARTY_BLOCK = 128; |
| 86 |
|
- const SMARTY_FUNCTION = 256; |
| 87 |
|
- /**#@-*/ |
| 88 |
|
- |
| 89 |
|
- /** |
| 90 |
|
- * character set of the template, used by string manipulation plugins |
| 91 |
|
- * |
| 92 |
|
- * it must be lowercase, but setCharset() will take care of that |
| 93 |
|
- * |
| 94 |
|
- * @see setCharset |
| 95 |
|
- * @see getCharset |
| 96 |
|
- * @var string |
| 97 |
|
- */ |
| 98 |
|
- protected $charset = 'utf-8'; |
| 99 |
|
- |
| 100 |
|
- /** |
| 101 |
|
- * global variables that are accessible through $dwoo.* in the templates |
| 102 |
|
- * |
| 103 |
|
- * default values include: |
| 104 |
|
- * |
| 105 |
|
- * $dwoo.version - current version number |
| 106 |
|
- * $dwoo.ad - a Powered by Dwoo link pointing to dwoo.org |
| 107 |
|
- * $dwoo.now - the current time |
|
5 |
+ |
|
6 |
+include 'Dwoo/Interfaces.php'; |
|
7 |
+include 'Dwoo/Plugin.php'; |
|
8 |
+include 'Dwoo/Template/String.php'; |
|
9 |
+include 'Dwoo/Template/File.php'; |
|
10 |
+include 'Dwoo/Data.php'; |
|
11 |
+ |
|
12 |
+define('DWOO_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR); |
|
13 |
+if(defined('DWOO_CACHE_DIRECTORY') === false) |
|
14 |
+ define('DWOO_CACHE_DIRECTORY', DWOO_DIRECTORY.'cache'.DIRECTORY_SEPARATOR); |
|
15 |
+if(defined('DWOO_COMPILE_DIRECTORY') === false) |
|
16 |
+ define('DWOO_COMPILE_DIRECTORY', DWOO_DIRECTORY.'compiled'.DIRECTORY_SEPARATOR); |
|
17 |
+if(is_writable(DWOO_CACHE_DIRECTORY) === false) |
|
18 |
+ throw new DwooException('Dwoo cache directory must be writable, either chmod "'.DWOO_CACHE_DIRECTORY.'" to make it writable or define DWOO_CACHE_DIRECTORY to a writable directory before including Dwoo.php'); |
|
19 |
+if(is_writable(DWOO_COMPILE_DIRECTORY) === false) |
|
20 |
+ throw new DwooException('Dwoo compile directory must be writable, either chmod "'.DWOO_COMPILE_DIRECTORY.'" to make it writable or define DWOO_COMPILE_DIRECTORY to a writable directory before including Dwoo.php'); |
|
21 |
+ |
|
22 |
+// include class paths or rebuild paths if the cache file isn't there |
|
23 |
+if((file_exists(DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php') && include DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php') === false) |
|
24 |
+ DwooLoader::rebuildClassPathCache(DWOO_DIRECTORY.'plugins', DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php'); |
|
25 |
+ |
|
26 |
+DwooLoader::loadPlugin('topLevelBlock'); |
|
27 |
+ |
|
28 |
+/** |
|
29 |
+ * main dwoo class, allows communication between the compiler, template and data classes |
|
30 |
+ * |
|
31 |
+ * requirements : |
|
32 |
+ * php 5.2.0 or above |
|
33 |
+ * php's mbstring extension for some plugins |
|
34 |
+ * php's hash extension to use DwooTemplateString class |
|
35 |
+ * |
|
36 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
37 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
38 |
+ * |
|
39 |
+ * This file is released under the LGPL |
|
40 |
+ * "GNU Lesser General Public License" |
|
41 |
+ * More information can be found here: |
|
42 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
43 |
+ * |
|
44 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
45 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
46 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
47 |
+ * @link http://dwoo.org/ |
|
48 |
+ * @version 0.3.4 |
|
49 |
+ * @date 2008-04-09 |
|
50 |
+ * @package Dwoo |
|
51 |
+ */ |
|
52 |
+class Dwoo |
|
53 |
+{ |
|
54 |
+ /** |
|
55 |
+ * current version number |
|
56 |
+ * |
|
57 |
+ * @var string |
|
58 |
+ */ |
|
59 |
+ const VERSION = "0.3.4"; |
|
60 |
+ |
|
61 |
+ /** |
|
62 |
+ * unique number of this dwoo release |
|
63 |
+ * |
|
64 |
+ * this can be used by templates classes to check whether the compiled template |
|
65 |
+ * has been compiled before this release or not, so that old templates are |
|
66 |
+ * recompiled automatically when Dwoo is updated |
|
67 |
+ */ |
|
68 |
+ const RELEASE_TAG = 8; |
|
69 |
+ |
|
70 |
+ /**#@+ |
|
71 |
+ * constants that represents all plugin types |
|
72 |
+ * |
|
73 |
+ * these are bitwise-operation-safe values to allow multiple types |
|
74 |
+ * on a single plugin |
|
75 |
+ * |
|
76 |
+ * @var int |
|
77 |
+ */ |
|
78 |
+ const CLASS_PLUGIN = 1; |
|
79 |
+ const FUNC_PLUGIN = 2; |
|
80 |
+ const NATIVE_PLUGIN = 4; |
|
81 |
+ const BLOCK_PLUGIN = 8; |
|
82 |
+ const COMPILABLE_PLUGIN = 16; |
|
83 |
+ const CUSTOM_PLUGIN = 32; |
|
84 |
+ const SMARTY_MODIFIER = 64; |
|
85 |
+ const SMARTY_BLOCK = 128; |
|
86 |
+ const SMARTY_FUNCTION = 256; |
|
87 |
+ /**#@-*/ |
|
88 |
+ |
|
89 |
+ /** |
|
90 |
+ * character set of the template, used by string manipulation plugins |
|
91 |
+ * |
|
92 |
+ * it must be lowercase, but setCharset() will take care of that |
|
93 |
+ * |
|
94 |
+ * @see setCharset |
|
95 |
+ * @see getCharset |
|
96 |
+ * @var string |
|
97 |
+ */ |
|
98 |
+ protected $charset = 'utf-8'; |
|
99 |
+ |
|
100 |
+ /** |
|
101 |
+ * global variables that are accessible through $dwoo.* in the templates |
|
102 |
+ * |
|
103 |
+ * default values include: |
|
104 |
+ * |
|
105 |
+ * $dwoo.version - current version number |
|
106 |
+ * $dwoo.ad - a Powered by Dwoo link pointing to dwoo.org |
|
107 |
+ * $dwoo.now - the current time |
| 108 |
108 |
* $dwoo.template - the current template filename |
| 109 |
|
- * $dwoo.charset - the character set used by the template |
| 110 |
|
- * |
| 111 |
|
- * on top of that, foreach and other plugins can store special values in there, |
| 112 |
|
- * see their documentation for more details. |
| 113 |
|
- * |
| 114 |
|
- * @var array |
| 115 |
|
- */ |
| 116 |
|
- protected $globals; |
| 117 |
|
- |
| 118 |
|
- /** |
| 119 |
|
- * directory where the compiled templates are stored |
| 120 |
|
- * |
| 121 |
|
- * defaults to DWOO_COMPILEDIR (= DWOO_DIRECTORY/compiled by default) |
| 122 |
|
- * |
| 123 |
|
- * @var string |
| 124 |
|
- */ |
| 125 |
|
- protected $compileDir; |
| 126 |
|
- |
| 127 |
|
- /** |
| 128 |
|
- * directory where the cached templates are stored |
| 129 |
|
- * |
| 130 |
|
- * defaults to DWOO_CACHEDIR (= DWOO_DIRECTORY/cache by default) |
| 131 |
|
- * |
| 132 |
|
- * @var string |
| 133 |
|
- */ |
| 134 |
|
- protected $cacheDir; |
| 135 |
|
- |
| 136 |
|
- /** |
|
109 |
+ * $dwoo.charset - the character set used by the template |
|
110 |
+ * |
|
111 |
+ * on top of that, foreach and other plugins can store special values in there, |
|
112 |
+ * see their documentation for more details. |
|
113 |
+ * |
|
114 |
+ * @var array |
|
115 |
+ */ |
|
116 |
+ protected $globals; |
|
117 |
+ |
|
118 |
+ /** |
|
119 |
+ * directory where the compiled templates are stored |
|
120 |
+ * |
|
121 |
+ * defaults to DWOO_COMPILEDIR (= DWOO_DIRECTORY/compiled by default) |
|
122 |
+ * |
|
123 |
+ * @var string |
|
124 |
+ */ |
|
125 |
+ protected $compileDir; |
|
126 |
+ |
|
127 |
+ /** |
|
128 |
+ * directory where the cached templates are stored |
|
129 |
+ * |
|
130 |
+ * defaults to DWOO_CACHEDIR (= DWOO_DIRECTORY/cache by default) |
|
131 |
+ * |
|
132 |
+ * @var string |
|
133 |
+ */ |
|
134 |
+ protected $cacheDir; |
|
135 |
+ |
|
136 |
+ /** |
| 137 |
137 |
* defines how long (in seconds) the cached files must remain valid |
| 138 |
138 |
* |
| 139 |
|
- * can be overriden on a per-template basis |
| 140 |
|
- * |
| 141 |
|
- * -1 = never delete |
| 142 |
|
- * 0 = disabled |
| 143 |
|
- * >0 = duration in seconds |
| 144 |
|
- * |
| 145 |
|
- * @var int |
| 146 |
|
- */ |
| 147 |
|
- protected $cacheTime = 0; |
| 148 |
|
- |
| 149 |
|
- /** |
| 150 |
|
- * security policy object |
| 151 |
|
- * |
| 152 |
|
- * @var DwooSecurityPolicy |
| 153 |
|
- */ |
| 154 |
|
- protected $securityPolicy = null; |
| 155 |
|
- |
| 156 |
|
- /** |
| 157 |
|
- * stores the custom plugins callbacks |
| 158 |
|
- * |
| 159 |
|
- * @see addPlugin |
| 160 |
|
- * @see removePlugin |
| 161 |
|
- * @var array |
| 162 |
|
- */ |
| 163 |
|
- protected $plugins = array(); |
| 164 |
|
- |
| 165 |
|
- /** |
| 166 |
|
- * stores the filter callbacks |
| 167 |
|
- * |
| 168 |
|
- * @see addFilter |
| 169 |
|
- * @see removeFilter |
| 170 |
|
- * @var array |
| 171 |
|
- */ |
| 172 |
|
- protected $filters = array(); |
| 173 |
|
- |
| 174 |
|
- /** |
|
139 |
+ * can be overriden on a per-template basis |
|
140 |
+ * |
|
141 |
+ * -1 = never delete |
|
142 |
+ * 0 = disabled |
|
143 |
+ * >0 = duration in seconds |
|
144 |
+ * |
|
145 |
+ * @var int |
|
146 |
+ */ |
|
147 |
+ protected $cacheTime = 0; |
|
148 |
+ |
|
149 |
+ /** |
|
150 |
+ * security policy object |
|
151 |
+ * |
|
152 |
+ * @var DwooSecurityPolicy |
|
153 |
+ */ |
|
154 |
+ protected $securityPolicy = null; |
|
155 |
+ |
|
156 |
+ /** |
|
157 |
+ * stores the custom plugins callbacks |
|
158 |
+ * |
|
159 |
+ * @see addPlugin |
|
160 |
+ * @see removePlugin |
|
161 |
+ * @var array |
|
162 |
+ */ |
|
163 |
+ protected $plugins = array(); |
|
164 |
+ |
|
165 |
+ /** |
|
166 |
+ * stores the filter callbacks |
|
167 |
+ * |
|
168 |
+ * @see addFilter |
|
169 |
+ * @see removeFilter |
|
170 |
+ * @var array |
|
171 |
+ */ |
|
172 |
+ protected $filters = array(); |
|
173 |
+ |
|
174 |
+ /** |
| 175 |
175 |
* stores the resource types and associated |
| 176 |
|
- * classes / compiler classes |
| 177 |
|
- * |
| 178 |
|
- * @var array |
| 179 |
|
- */ |
| 180 |
|
- protected $resources = array |
| 181 |
|
- ( |
|
176 |
+ * classes / compiler classes |
|
177 |
+ * |
|
178 |
+ * @var array |
|
179 |
+ */ |
|
180 |
+ protected $resources = array |
|
181 |
+ ( |
| 182 |
182 |
'file' => array |
| 183 |
183 |
( |
| 184 |
184 |
'class' => 'DwooTemplateFile', |
| 188 |
188 |
( |
| 189 |
189 |
'class' => 'DwooTemplateString', |
| 190 |
190 |
'compiler' => null |
| 191 |
|
- ) |
| 192 |
|
- ); |
| 193 |
|
- |
| 194 |
|
- /** |
| 195 |
|
- * currently rendered template, set to null when not-rendering |
| 196 |
|
- * |
| 197 |
|
- * @var DwooITemplate |
| 198 |
|
- */ |
| 199 |
|
- protected $template = null; |
| 200 |
|
- |
| 201 |
|
- /** |
| 202 |
|
- * stores the instances of the class plugins during template runtime |
| 203 |
|
- * |
| 204 |
|
- * @var array |
| 205 |
|
- */ |
| 206 |
|
- protected $runtimePlugins; |
| 207 |
|
- |
| 208 |
|
- /** |
| 209 |
|
- * stores the data during template runtime |
| 210 |
|
- * |
| 211 |
|
- * @var array |
| 212 |
|
- */ |
| 213 |
|
- protected $data; |
| 214 |
|
- |
| 215 |
|
- /** |
| 216 |
|
- * stores the current scope during template runtime |
| 217 |
|
- * |
| 218 |
|
- * @var mixed |
| 219 |
|
- */ |
| 220 |
|
- protected $scope; |
| 221 |
|
- |
| 222 |
|
- /** |
| 223 |
|
- * stores the scope tree during template runtime |
| 224 |
|
- * |
| 225 |
|
- * @var array |
| 226 |
|
- */ |
| 227 |
|
- protected $scopeTree; |
| 228 |
|
- |
| 229 |
|
- /** |
| 230 |
|
- * stores the block plugins stack during template runtime |
| 231 |
|
- * |
| 232 |
|
- * @var array |
| 233 |
|
- */ |
| 234 |
|
- protected $stack; |
| 235 |
|
- |
| 236 |
|
- /** |
| 237 |
|
- * stores the current block plugin at the top of the stack during template runtime |
| 238 |
|
- * |
| 239 |
|
- * @var DwooBlockPlugin |
| 240 |
|
- */ |
| 241 |
|
- protected $curBlock; |
| 242 |
|
- |
| 243 |
|
- /** |
| 244 |
|
- * stores the output buffer during template runtime |
| 245 |
|
- * |
| 246 |
|
- * @var string |
| 247 |
|
- */ |
| 248 |
|
- protected $buffer; |
| 249 |
|
- |
| 250 |
|
- /** |
| 251 |
|
- * constructor, sets the cache and compile dir to the default values |
| 252 |
|
- */ |
| 253 |
|
- public function __construct() |
| 254 |
|
- { |
| 255 |
|
- $this->cacheDir = DWOO_CACHE_DIRECTORY.DIRECTORY_SEPARATOR; |
| 256 |
|
- $this->compileDir = DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR; |
| 257 |
|
- } |
| 258 |
|
- |
| 259 |
|
- /** |
| 260 |
|
- * resets some runtime variables to allow a cloned object to be used to render sub-templates |
| 261 |
|
- */ |
| 262 |
|
- public function __clone() |
| 263 |
|
- { |
| 264 |
|
- $this->template = null; |
| 265 |
|
- unset($this->scope); |
| 266 |
|
- unset($this->data); |
| 267 |
|
- } |
| 268 |
|
- |
| 269 |
|
- /** |
| 270 |
|
- * outputs the template instead of returning it, this is basically a shortcut for get(*, *, *, true) |
| 271 |
|
- * @see get |
| 272 |
|
- */ |
| 273 |
|
- public function output($tpl, $data = array(), DwooICompiler $compiler = null) |
| 274 |
|
- { |
| 275 |
|
- return $this->get($tpl, $data, $compiler, true); |
| 276 |
|
- } |
| 277 |
|
- |
| 278 |
|
- /** |
| 279 |
|
- * returns the given template rendered using the provided data and optional compiler |
| 280 |
|
- * |
| 281 |
|
- * @param mixed $tpl template, can either be a DwooITemplate object (i.e. DwooTemplateFile), a valid path to a template, or |
| 282 |
|
- * a template as a string it is recommended to provide a DwooITemplate as it will probably make things faster, |
| 283 |
|
- * especially if you render a template multiple times |
| 284 |
|
- * @param mixed $data the data to use, can either be a DwooIDataProvider object (i.e. DwooData) or an associative array. if you're |
| 285 |
|
- * rendering the template from cache, it can be left null |
| 286 |
|
- * @param DwooICompiler $compiler the compiler that must be used to compile the template, if left empty a default |
| 287 |
|
- * DwooCompiler will be used. |
| 288 |
|
- * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true) |
| 289 |
|
- * @return string nothing or the template output if $output is true |
| 290 |
|
- */ |
| 291 |
|
- public function get($_tpl, $data = array(), $_compiler = null, $_output = false) |
| 292 |
|
- { |
| 293 |
|
- // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one |
| 294 |
|
- if($this->template instanceof DwooITemplate) |
| 295 |
|
- { |
| 296 |
|
- $proxy = clone $this; |
| 297 |
|
- return $proxy->get($_tpl, $data, $_compiler, $_output); |
| 298 |
|
- } |
| 299 |
|
- |
| 300 |
|
- // auto-create template if required |
| 301 |
|
- if($_tpl instanceof DwooITemplate) |
| 302 |
|
- {} |
| 303 |
|
- elseif(is_string($_tpl) && file_exists($_tpl)) |
| 304 |
|
- $_tpl = new DwooTemplateFile($_tpl); |
| 305 |
|
- elseif(is_string($_tpl)) |
| 306 |
|
- $_tpl = new DwooTemplateString($_tpl); |
| 307 |
|
- else |
| 308 |
|
- throw new DwooException('Dwoo->get/Dwoo->output\'s first argument must be a DwooITemplate (i.e. DwooTemplateFile) or a valid path to a template file', E_USER_NOTICE); |
| 309 |
|
- |
| 310 |
|
- // save the current template, enters render mode at the same time |
| 311 |
|
- // if another rendering is requested it will be proxied to a new Dwoo instance |
| 312 |
|
- $this->template = $_tpl; |
| 313 |
|
- |
| 314 |
|
- // load data |
| 315 |
|
- if($data instanceof DwooIDataProvider) |
| 316 |
|
- $this->data = $data->getData(); |
| 317 |
|
- elseif(is_array($data)) |
| 318 |
|
- $this->data = $data; |
| 319 |
|
- else |
| 320 |
|
- throw new DwooException('Dwoo->get/Dwoo->output\'s data argument must be a DwooIDataProvider object (i.e. DwooData) or an associative array', E_USER_NOTICE); |
| 321 |
|
- |
| 322 |
|
- $this->initGlobals($_tpl); |
| 323 |
|
- $this->initRuntimeVars($_tpl); |
| 324 |
|
- |
| 325 |
|
- // try to get cached template |
| 326 |
|
- $file = $_tpl->getCachedTemplate($this); |
| 327 |
|
- $doCache = $file === true; |
| 328 |
|
- $cacheLoaded = $doCache === false && is_string($file); |
| 329 |
|
- |
| 330 |
|
- // cache is present, run it |
| 331 |
|
- if($cacheLoaded === true) |
| 332 |
|
- { |
| 333 |
|
- if($_output === true) |
| 334 |
|
- { |
| 335 |
|
- readfile($file); |
| 336 |
|
- $this->template = null; |
| 337 |
|
- } |
| 338 |
|
- else |
| 339 |
|
- { |
| 340 |
|
- //ob_start(); |
| 341 |
|
- $this->template = null; |
| 342 |
|
- return file_get_contents($file); |
| 343 |
|
- //return ob_get_clean(); |
| 344 |
|
- } |
| 345 |
|
- } |
| 346 |
|
- // no cache present |
| 347 |
|
- else |
| 348 |
|
- { |
| 349 |
|
- // render template |
| 350 |
|
- $out = include $_tpl->getCompiledTemplate($this, $_compiler); |
| 351 |
|
- |
| 352 |
|
- // template returned false so it needs to be recompiled |
| 353 |
|
- if($out === false) |
| 354 |
|
- { |
| 355 |
|
- $_tpl->forceCompilation(); |
| 356 |
|
- $out = include $_tpl->getCompiledTemplate($this, $_compiler); |
| 357 |
|
- } |
| 358 |
|
- |
| 359 |
|
- // process filters |
| 360 |
|
- foreach($this->filters as $filter) |
| 361 |
|
- { |
| 362 |
|
- if(is_array($filter) && $filter[0] instanceof DwooFilter) |
| 363 |
|
- $out = call_user_func($filter, $out); |
| 364 |
|
- else |
| 365 |
|
- $out = call_user_func($filter, $this, $out); |
| 366 |
|
- } |
| 367 |
|
- |
| 368 |
|
- // exit render mode |
| 369 |
|
- $this->template = null; |
| 370 |
|
- |
| 371 |
|
- // building cache |
| 372 |
|
- if($doCache) |
| 373 |
|
- { |
| 374 |
|
- $_tpl->cache($this, $out); |
| 375 |
|
- if($_output === true) |
| 376 |
|
- echo $out; |
| 377 |
|
- else |
| 378 |
|
- return $out; |
| 379 |
|
- } |
| 380 |
|
- // no need to build cache |
| 381 |
|
- else |
| 382 |
|
- { |
| 383 |
|
- if($_output === true) |
| 384 |
|
- echo $out; |
| 385 |
|
- else |
| 386 |
|
- return $out; |
| 387 |
|
- } |
| 388 |
|
- } |
| 389 |
|
- } |
|
191 |
+ ) |
|
192 |
+ ); |
|
193 |
+ |
|
194 |
+ /** |
|
195 |
+ * currently rendered template, set to null when not-rendering |
|
196 |
+ * |
|
197 |
+ * @var DwooITemplate |
|
198 |
+ */ |
|
199 |
+ protected $template = null; |
|
200 |
+ |
|
201 |
+ /** |
|
202 |
+ * stores the instances of the class plugins during template runtime |
|
203 |
+ * |
|
204 |
+ * @var array |
|
205 |
+ */ |
|
206 |
+ protected $runtimePlugins; |
|
207 |
+ |
|
208 |
+ /** |
|
209 |
+ * stores the data during template runtime |
|
210 |
+ * |
|
211 |
+ * @var array |
|
212 |
+ */ |
|
213 |
+ protected $data; |
|
214 |
+ |
|
215 |
+ /** |
|
216 |
+ * stores the current scope during template runtime |
|
217 |
+ * |
|
218 |
+ * @var mixed |
|
219 |
+ */ |
|
220 |
+ protected $scope; |
|
221 |
+ |
|
222 |
+ /** |
|
223 |
+ * stores the scope tree during template runtime |
|
224 |
+ * |
|
225 |
+ * @var array |
|
226 |
+ */ |
|
227 |
+ protected $scopeTree; |
|
228 |
+ |
|
229 |
+ /** |
|
230 |
+ * stores the block plugins stack during template runtime |
|
231 |
+ * |
|
232 |
+ * @var array |
|
233 |
+ */ |
|
234 |
+ protected $stack; |
|
235 |
+ |
|
236 |
+ /** |
|
237 |
+ * stores the current block plugin at the top of the stack during template runtime |
|
238 |
+ * |
|
239 |
+ * @var DwooBlockPlugin |
|
240 |
+ */ |
|
241 |
+ protected $curBlock; |
|
242 |
+ |
|
243 |
+ /** |
|
244 |
+ * stores the output buffer during template runtime |
|
245 |
+ * |
|
246 |
+ * @var string |
|
247 |
+ */ |
|
248 |
+ protected $buffer; |
|
249 |
+ |
|
250 |
+ /** |
|
251 |
+ * constructor, sets the cache and compile dir to the default values |
|
252 |
+ */ |
|
253 |
+ public function __construct() |
|
254 |
+ { |
|
255 |
+ $this->cacheDir = DWOO_CACHE_DIRECTORY.DIRECTORY_SEPARATOR; |
|
256 |
+ $this->compileDir = DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR; |
|
257 |
+ } |
|
258 |
+ |
|
259 |
+ /** |
|
260 |
+ * resets some runtime variables to allow a cloned object to be used to render sub-templates |
|
261 |
+ */ |
|
262 |
+ public function __clone() |
|
263 |
+ { |
|
264 |
+ $this->template = null; |
|
265 |
+ unset($this->scope); |
|
266 |
+ unset($this->data); |
|
267 |
+ } |
|
268 |
+ |
|
269 |
+ /** |
|
270 |
+ * outputs the template instead of returning it, this is basically a shortcut for get(*, *, *, true) |
|
271 |
+ * @see get |
|
272 |
+ */ |
|
273 |
+ public function output($tpl, $data = array(), DwooICompiler $compiler = null) |
|
274 |
+ { |
|
275 |
+ return $this->get($tpl, $data, $compiler, true); |
|
276 |
+ } |
|
277 |
+ |
|
278 |
+ /** |
|
279 |
+ * returns the given template rendered using the provided data and optional compiler |
|
280 |
+ * |
|
281 |
+ * @param mixed $tpl template, can either be a DwooITemplate object (i.e. DwooTemplateFile), a valid path to a template, or |
|
282 |
+ * a template as a string it is recommended to provide a DwooITemplate as it will probably make things faster, |
|
283 |
+ * especially if you render a template multiple times |
|
284 |
+ * @param mixed $data the data to use, can either be a DwooIDataProvider object (i.e. DwooData) or an associative array. if you're |
|
285 |
+ * rendering the template from cache, it can be left null |
|
286 |
+ * @param DwooICompiler $compiler the compiler that must be used to compile the template, if left empty a default |
|
287 |
+ * DwooCompiler will be used. |
|
288 |
+ * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true) |
|
289 |
+ * @return string nothing or the template output if $output is true |
|
290 |
+ */ |
|
291 |
+ public function get($_tpl, $data = array(), $_compiler = null, $_output = false) |
|
292 |
+ { |
|
293 |
+ // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one |
|
294 |
+ if($this->template instanceof DwooITemplate) |
|
295 |
+ { |
|
296 |
+ $proxy = clone $this; |
|
297 |
+ return $proxy->get($_tpl, $data, $_compiler, $_output); |
|
298 |
+ } |
|
299 |
+ |
|
300 |
+ // auto-create template if required |
|
301 |
+ if($_tpl instanceof DwooITemplate) |
|
302 |
+ {} |
|
303 |
+ elseif(is_string($_tpl) && file_exists($_tpl)) |
|
304 |
+ $_tpl = new DwooTemplateFile($_tpl); |
|
305 |
+ elseif(is_string($_tpl)) |
|
306 |
+ $_tpl = new DwooTemplateString($_tpl); |
|
307 |
+ else |
|
308 |
+ throw new DwooException('Dwoo->get/Dwoo->output\'s first argument must be a DwooITemplate (i.e. DwooTemplateFile) or a valid path to a template file', E_USER_NOTICE); |
|
309 |
+ |
|
310 |
+ // save the current template, enters render mode at the same time |
|
311 |
+ // if another rendering is requested it will be proxied to a new Dwoo instance |
|
312 |
+ $this->template = $_tpl; |
|
313 |
+ |
|
314 |
+ // load data |
|
315 |
+ if($data instanceof DwooIDataProvider) |
|
316 |
+ $this->data = $data->getData(); |
|
317 |
+ elseif(is_array($data)) |
|
318 |
+ $this->data = $data; |
|
319 |
+ else |
|
320 |
+ throw new DwooException('Dwoo->get/Dwoo->output\'s data argument must be a DwooIDataProvider object (i.e. DwooData) or an associative array', E_USER_NOTICE); |
|
321 |
+ |
|
322 |
+ $this->initGlobals($_tpl); |
|
323 |
+ $this->initRuntimeVars($_tpl); |
|
324 |
+ |
|
325 |
+ // try to get cached template |
|
326 |
+ $file = $_tpl->getCachedTemplate($this); |
|
327 |
+ $doCache = $file === true; |
|
328 |
+ $cacheLoaded = $doCache === false && is_string($file); |
|
329 |
+ |
|
330 |
+ // cache is present, run it |
|
331 |
+ if($cacheLoaded === true) |
|
332 |
+ { |
|
333 |
+ if($_output === true) |
|
334 |
+ { |
|
335 |
+ readfile($file); |
|
336 |
+ $this->template = null; |
|
337 |
+ } |
|
338 |
+ else |
|
339 |
+ { |
|
340 |
+ //ob_start(); |
|
341 |
+ $this->template = null; |
|
342 |
+ return file_get_contents($file); |
|
343 |
+ //return ob_get_clean(); |
|
344 |
+ } |
|
345 |
+ } |
|
346 |
+ // no cache present |
|
347 |
+ else |
|
348 |
+ { |
|
349 |
+ // render template |
|
350 |
+ $out = include $_tpl->getCompiledTemplate($this, $_compiler); |
|
351 |
+ |
|
352 |
+ // template returned false so it needs to be recompiled |
|
353 |
+ if($out === false) |
|
354 |
+ { |
|
355 |
+ $_tpl->forceCompilation(); |
|
356 |
+ $out = include $_tpl->getCompiledTemplate($this, $_compiler); |
|
357 |
+ } |
|
358 |
+ |
|
359 |
+ // process filters |
|
360 |
+ foreach($this->filters as $filter) |
|
361 |
+ { |
|
362 |
+ if(is_array($filter) && $filter[0] instanceof DwooFilter) |
|
363 |
+ $out = call_user_func($filter, $out); |
|
364 |
+ else |
|
365 |
+ $out = call_user_func($filter, $this, $out); |
|
366 |
+ } |
|
367 |
+ |
|
368 |
+ // exit render mode |
|
369 |
+ $this->template = null; |
|
370 |
+ |
|
371 |
+ // building cache |
|
372 |
+ if($doCache) |
|
373 |
+ { |
|
374 |
+ $_tpl->cache($this, $out); |
|
375 |
+ if($_output === true) |
|
376 |
+ echo $out; |
|
377 |
+ else |
|
378 |
+ return $out; |
|
379 |
+ } |
|
380 |
+ // no need to build cache |
|
381 |
+ else |
|
382 |
+ { |
|
383 |
+ if($_output === true) |
|
384 |
+ echo $out; |
|
385 |
+ else |
|
386 |
+ return $out; |
|
387 |
+ } |
|
388 |
+ } |
|
389 |
+ } |
| 390 |
390 |
|
| 391 |
391 |
/** |
| 392 |
392 |
* re-initializes the globals array before each template run |
| 432 |
432 |
* @param callback $callback the plugin callback, either a function name, |
| 433 |
433 |
* a class name or an array containing an object |
| 434 |
434 |
* or class name and a method name |
| 435 |
|
- */ |
| 436 |
|
- public function addPlugin($name, $callback) |
| 437 |
|
- { |
| 438 |
|
- if(is_array($callback)) |
| 439 |
|
- { |
| 440 |
|
- if(is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'DwooBlockPlugin')) |
| 441 |
|
- $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); |
| 442 |
|
- else |
| 443 |
|
- $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); |
| 444 |
|
- } |
| 445 |
|
- elseif(class_exists($callback, false)) |
| 446 |
|
- { |
| 447 |
|
- if(is_subclass_of($callback, 'DwooBlockPlugin')) |
| 448 |
|
- $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN, 'callback'=>$callback, 'class'=>$callback); |
| 449 |
|
- else |
| 450 |
|
- $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); |
| 451 |
|
- } |
| 452 |
|
- elseif(function_exists($callback)) |
| 453 |
|
- { |
| 454 |
|
- $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN, 'callback'=>$callback); |
| 455 |
|
- } |
| 456 |
|
- else |
| 457 |
|
- { |
| 458 |
|
- throw new DwooException('Callback could not be processed correctly, please check that the function/class you used exists'); |
| 459 |
|
- } |
| 460 |
|
- } |
|
435 |
+ */ |
|
436 |
+ public function addPlugin($name, $callback) |
|
437 |
+ { |
|
438 |
+ if(is_array($callback)) |
|
439 |
+ { |
|
440 |
+ if(is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'DwooBlockPlugin')) |
|
441 |
+ $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); |
|
442 |
+ else |
|
443 |
+ $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); |
|
444 |
+ } |
|
445 |
+ elseif(class_exists($callback, false)) |
|
446 |
+ { |
|
447 |
+ if(is_subclass_of($callback, 'DwooBlockPlugin')) |
|
448 |
+ $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN, 'callback'=>$callback, 'class'=>$callback); |
|
449 |
+ else |
|
450 |
+ $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); |
|
451 |
+ } |
|
452 |
+ elseif(function_exists($callback)) |
|
453 |
+ { |
|
454 |
+ $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN, 'callback'=>$callback); |
|
455 |
+ } |
|
456 |
+ else |
|
457 |
+ { |
|
458 |
+ throw new DwooException('Callback could not be processed correctly, please check that the function/class you used exists'); |
|
459 |
+ } |
|
460 |
+ } |
| 461 |
461 |
|
| 462 |
462 |
/** |
| 463 |
463 |
* removes a custom plugin |
| 464 |
464 |
* |
| 465 |
465 |
* @param string $name the plugin name |
| 466 |
|
- */ |
| 467 |
|
- public function removePlugin($name) |
| 468 |
|
- { |
| 469 |
|
- if(isset($this->plugins[$name])) |
| 470 |
|
- unset($this->plugins[$name]); |
| 471 |
|
- } |
|
466 |
+ */ |
|
467 |
+ public function removePlugin($name) |
|
468 |
+ { |
|
469 |
+ if(isset($this->plugins[$name])) |
|
470 |
+ unset($this->plugins[$name]); |
|
471 |
+ } |
| 472 |
472 |
|
| 473 |
473 |
/** |
| 474 |
474 |
* adds a filter to this Dwoo instance, it will be used to filter the output of all the templates rendered by this instance |
| 475 |
475 |
* |
| 476 |
476 |
* @param mixed $callback a callback or a filter name if it is autoloaded from a plugin directory |
| 477 |
477 |
* @param bool $autoload if true, the first parameter must be a filter name from one of the plugin directories |
| 478 |
|
- */ |
| 479 |
|
- public function addFilter($callback, $autoload = false) |
| 480 |
|
- { |
| 481 |
|
- if($autoload) |
| 482 |
|
- { |
| 483 |
|
- $name = str_replace('DwooFilter_', '', $callback); |
| 484 |
|
- $class = 'DwooFilter_'.$name; |
| 485 |
|
- |
| 486 |
|
- if(!class_exists($class, false) && !function_exists($class)) |
| 487 |
|
- DwooLoader::loadPlugin($name); |
| 488 |
|
- |
| 489 |
|
- if(class_exists($class, false)) |
| 490 |
|
- $callback = array(new $class($this), 'process'); |
| 491 |
|
- elseif(function_exists($class)) |
| 492 |
|
- $callback = $class; |
| 493 |
|
- else |
| 494 |
|
- $this->triggerError('Wrong filter name, when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "DwooFilter_name"', E_USER_ERROR); |
| 495 |
|
- |
| 496 |
|
- $this->filters[] = $callback; |
| 497 |
|
- } |
| 498 |
|
- else |
| 499 |
|
- { |
| 500 |
|
- $this->filters[] = $callback; |
| 501 |
|
- } |
| 502 |
|
- } |
|
478 |
+ */ |
|
479 |
+ public function addFilter($callback, $autoload = false) |
|
480 |
+ { |
|
481 |
+ if($autoload) |
|
482 |
+ { |
|
483 |
+ $name = str_replace('DwooFilter_', '', $callback); |
|
484 |
+ $class = 'DwooFilter_'.$name; |
|
485 |
+ |
|
486 |
+ if(!class_exists($class, false) && !function_exists($class)) |
|
487 |
+ DwooLoader::loadPlugin($name); |
|
488 |
+ |
|
489 |
+ if(class_exists($class, false)) |
|
490 |
+ $callback = array(new $class($this), 'process'); |
|
491 |
+ elseif(function_exists($class)) |
|
492 |
+ $callback = $class; |
|
493 |
+ else |
|
494 |
+ $this->triggerError('Wrong filter name, when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "DwooFilter_name"', E_USER_ERROR); |
|
495 |
+ |
|
496 |
+ $this->filters[] = $callback; |
|
497 |
+ } |
|
498 |
+ else |
|
499 |
+ { |
|
500 |
+ $this->filters[] = $callback; |
|
501 |
+ } |
|
502 |
+ } |
| 503 |
503 |
|
| 504 |
504 |
/** |
| 505 |
505 |
* removes a filter |
| 506 |
506 |
* |
| 507 |
507 |
* @param mixed $callback callback or filter name if it was autoloaded |
| 508 |
|
- */ |
| 509 |
|
- public function removeFilter($callback) |
| 510 |
|
- { |
| 511 |
|
- if(($index = array_search($callback, $this->filters, true)) !== false) |
| 512 |
|
- unset($this->filters[$index]); |
| 513 |
|
- elseif(($index = array_search('DwooFilter_'.str_replace('DwooFilter_', '', $callback), $this->filters, true)) !== false) |
| 514 |
|
- unset($this->filters[$index]); |
| 515 |
|
- else |
| 516 |
|
- { |
| 517 |
|
- $class = 'DwooFilter_' . str_replace('DwooFilter_', '', $callback); |
| 518 |
|
- foreach($this->filters as $index=>$filter) |
| 519 |
|
- { |
| 520 |
|
- if(is_array($filter) && $filter[0] instanceof $class) |
| 521 |
|
- { |
| 522 |
|
- unset($this->filters[$index]); |
| 523 |
|
- break; |
| 524 |
|
- } |
| 525 |
|
- } |
| 526 |
|
- } |
| 527 |
|
- } |
|
508 |
+ */ |
|
509 |
+ public function removeFilter($callback) |
|
510 |
+ { |
|
511 |
+ if(($index = array_search($callback, $this->filters, true)) !== false) |
|
512 |
+ unset($this->filters[$index]); |
|
513 |
+ elseif(($index = array_search('DwooFilter_'.str_replace('DwooFilter_', '', $callback), $this->filters, true)) !== false) |
|
514 |
+ unset($this->filters[$index]); |
|
515 |
+ else |
|
516 |
+ { |
|
517 |
+ $class = 'DwooFilter_' . str_replace('DwooFilter_', '', $callback); |
|
518 |
+ foreach($this->filters as $index=>$filter) |
|
519 |
+ { |
|
520 |
+ if(is_array($filter) && $filter[0] instanceof $class) |
|
521 |
+ { |
|
522 |
+ unset($this->filters[$index]); |
|
523 |
+ break; |
|
524 |
+ } |
|
525 |
+ } |
|
526 |
+ } |
|
527 |
+ } |
| 528 |
528 |
|
| 529 |
529 |
/** |
| 530 |
530 |
* adds a resource or overrides a default one |
| 1462 |
1462 |
$this->setScope($scope); |
| 1463 |
1463 |
return $prev; |
| 1464 |
1464 |
} |
| 1465 |
|
-} |
| 1466 |
|
- |
| 1467 |
|
-/** |
| 1468 |
|
- * handles plugin loading and caching of plugins names/paths relationships |
| 1469 |
|
- * |
| 1470 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 1471 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 1472 |
|
- * |
| 1473 |
|
- * This file is released under the LGPL |
| 1474 |
|
- * "GNU Lesser General Public License" |
| 1475 |
|
- * More information can be found here: |
| 1476 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 1477 |
|
- * |
| 1478 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 1479 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 1480 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 1481 |
|
- * @link http://dwoo.org/ |
| 1482 |
|
- * @version 0.3.4 |
| 1483 |
|
- * @date 2008-04-09 |
| 1484 |
|
- * @package Dwoo |
| 1485 |
|
- */ |
| 1486 |
|
-class DwooLoader |
| 1487 |
|
-{ |
| 1488 |
|
- /** |
| 1489 |
|
- * stores the plugin directories |
| 1490 |
|
- * |
| 1491 |
|
- * @see addDirectory |
| 1492 |
|
- * @var array |
| 1493 |
|
- */ |
| 1494 |
|
- protected static $paths = array(); |
| 1495 |
|
- |
| 1496 |
|
- /** |
| 1497 |
|
- * stores the plugins names/paths relationships |
| 1498 |
|
- * don't edit this on your own, use addDirectory |
| 1499 |
|
- * |
| 1500 |
|
- * @see addDirectory |
| 1501 |
|
- * @var array |
| 1502 |
|
- * @access protected |
| 1503 |
|
- */ |
| 1504 |
|
- public static $classpath = array(); |
| 1505 |
|
- |
| 1506 |
|
- /** |
| 1507 |
|
- * rebuilds class paths, scans the given directory recursively and saves all paths in the given file |
| 1508 |
|
- * |
| 1509 |
|
- * @param string $path the plugin path to scan |
| 1510 |
|
- * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten |
| 1511 |
|
- * @access protected |
| 1512 |
|
- */ |
| 1513 |
|
- public static function rebuildClassPathCache($path, $cacheFile) |
| 1514 |
|
- { |
| 1515 |
|
- if($cacheFile!==false) |
| 1516 |
|
- { |
| 1517 |
|
- $tmp = self::$classpath; |
| 1518 |
|
- self::$classpath = array(); |
| 1519 |
|
- } |
| 1520 |
|
- |
| 1521 |
|
- // iterates over all files/folders |
| 1522 |
|
- $list = glob($path.DIRECTORY_SEPARATOR.'*'); |
| 1523 |
|
- if(is_array($list)) |
| 1524 |
|
- foreach($list as $f) |
| 1525 |
|
- { |
| 1526 |
|
- if(is_dir($f)) |
| 1527 |
|
- self::rebuildClassPathCache($f, false); |
| 1528 |
|
- elseif(basename($f) !== 'classpath.cache') |
| 1529 |
|
- self::$classpath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f,'.php'))] = $f; |
| 1530 |
|
- } |
| 1531 |
|
- |
| 1532 |
|
- // save in file if it's the first call (not recursed) |
| 1533 |
|
- if($cacheFile!==false) |
| 1534 |
|
- { |
| 1535 |
|
- if(!file_put_contents($cacheFile, '<?php DwooLoader::$classpath = '.var_export(self::$classpath, true).' + DwooLoader::$classpath; ?>')) |
| 1536 |
|
- throw new DwooException('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)'); |
| 1537 |
|
- self::$classpath += $tmp; |
| 1538 |
|
- } |
| 1539 |
|
- } |
| 1540 |
|
- |
| 1541 |
|
- /** |
| 1542 |
|
- * loads a plugin file |
| 1543 |
|
- * |
| 1544 |
|
- * @param string $class the plugin name, without the DwooPlugin_ prefix |
| 1545 |
|
- */ |
| 1546 |
|
- public static function loadPlugin($class) |
| 1547 |
|
- { |
| 1548 |
|
- // a new class was added or the include failed so we rebuild the cache |
| 1549 |
|
- if(!isset(self::$classpath[$class]) || !include self::$classpath[$class]) |
| 1550 |
|
- { |
| 1551 |
|
- self::rebuildClassPathCache(DWOO_DIRECTORY . 'plugins', DWOO_COMPILE_DIRECTORY . DIRECTORY_SEPARATOR . 'classpath.cache.php'); |
| 1552 |
|
- foreach(self::$paths as $path) |
| 1553 |
|
- self::rebuildClassPathCache($path[0], $path[1]); |
| 1554 |
|
- if(isset(self::$classpath[$class])) |
| 1555 |
|
- include self::$classpath[$class]; |
| 1556 |
|
- else |
| 1557 |
|
- throw new DwooException('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
| 1558 |
|
- } |
| 1559 |
|
- } |
| 1560 |
|
- |
| 1561 |
|
- /** |
| 1562 |
|
- * adds a plugin directory |
| 1563 |
|
- * |
| 1564 |
|
- * @param string $pluginDir the plugin path to scan |
| 1565 |
|
- */ |
| 1566 |
|
- public static function addDirectory($pluginDir) |
| 1567 |
|
- { |
| 1568 |
|
- $cacheFile = DWOO_COMPILE_DIRECTORY . DIRECTORY_SEPARATOR . 'classpath-'.substr(strtr($pluginDir, ':/\\.', '----'), strlen($pluginDir) > 80 ? -80 : 0).'.cache.php'; |
| 1569 |
|
- self::$paths[] = array($pluginDir, $cacheFile); |
| 1570 |
|
- if(file_exists($cacheFile)) |
| 1571 |
|
- include $cacheFile; |
| 1572 |
|
- else |
| 1573 |
|
- DwooLoader::rebuildClassPathCache($pluginDir, $cacheFile); |
| 1574 |
|
- } |
| 1575 |
|
-} |
| 1576 |
|
- |
| 1577 |
|
-/** |
| 1578 |
|
- * main dwoo exception class |
| 1579 |
|
- * |
| 1580 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 1581 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 1582 |
|
- * |
| 1583 |
|
- * This file is released under the LGPL |
| 1584 |
|
- * "GNU Lesser General Public License" |
| 1585 |
|
- * More information can be found here: |
| 1586 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 1587 |
|
- * |
| 1588 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 1589 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 1590 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 1591 |
|
- * @link http://dwoo.org/ |
| 1592 |
|
- * @version 0.3.4 |
| 1593 |
|
- * @date 2008-04-09 |
| 1594 |
|
- * @package Dwoo |
| 1595 |
|
- */ |
| 1596 |
|
-class DwooException extends Exception |
| 1597 |
|
-{ |
| 1598 |
|
-} |
| 1599 |
|
- |
| 1600 |
|
-/** |
| 1601 |
|
- * represents the security settings of a dwoo instance, it can be passed around to different dwoo instances |
| 1602 |
|
- * |
| 1603 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 1604 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 1605 |
|
- * |
| 1606 |
|
- * This file is released under the LGPL |
| 1607 |
|
- * "GNU Lesser General Public License" |
| 1608 |
|
- * More information can be found here: |
| 1609 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 1610 |
|
- * |
| 1611 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 1612 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 1613 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 1614 |
|
- * @link http://dwoo.org/ |
| 1615 |
|
- * @version 0.3.4 |
| 1616 |
|
- * @date 2008-04-09 |
| 1617 |
|
- * @package Dwoo |
| 1618 |
|
- */ |
| 1619 |
|
-class DwooSecurityPolicy |
| 1620 |
|
-{ |
| 1621 |
|
- /**#@+ |
| 1622 |
|
- * php handling constants, defaults to PHP_REMOVE |
| 1623 |
|
- * |
| 1624 |
|
- * PHP_REMOVE : remove all <?php ?> (+ short tags if your short tags option is on) from the input template |
| 1625 |
|
- * PHP_ALLOW : leave them as they are |
| 1626 |
|
- * PHP_ENCODE : run htmlentities over them |
| 1627 |
|
- * |
| 1628 |
|
- * @var int |
| 1629 |
|
- */ |
| 1630 |
|
- const PHP_ENCODE = 1; |
| 1631 |
|
- const PHP_REMOVE = 2; |
| 1632 |
|
- const PHP_ALLOW = 3; |
| 1633 |
|
- /**#@-*/ |
| 1634 |
|
- |
| 1635 |
|
- /**#@+ |
| 1636 |
|
- * constant handling constants, defaults to CONST_DISALLOW |
| 1637 |
|
- * |
| 1638 |
|
- * CONST_DISALLOW : throw an error if {$dwoo.const.*} is used in the template |
| 1639 |
|
- * CONST_ALLOW : allow {$dwoo.const.*} calls |
| 1640 |
|
- */ |
| 1641 |
|
- const CONST_DISALLOW = false; |
| 1642 |
|
- const CONST_ALLOW = true; |
| 1643 |
|
- /**#@-*/ |
| 1644 |
|
- |
| 1645 |
|
- /** |
| 1646 |
|
- * php functions that are allowed to be used within the template |
| 1647 |
|
- * |
| 1648 |
|
- * @var array |
| 1649 |
|
- */ |
| 1650 |
|
- protected $allowedPhpFunctions = array |
| 1651 |
|
- ( |
| 1652 |
|
- 'str_repeat', 'count', 'number_format', 'htmlentities', 'htmlspecialchars', |
| 1653 |
|
- 'long2ip', 'strlen', 'list', 'empty', 'count', 'sizeof', 'in_array', 'is_array', |
| 1654 |
|
- ); |
| 1655 |
|
- |
| 1656 |
|
- /** |
| 1657 |
|
- * paths that are safe to use with include or other file-access plugins |
| 1658 |
|
- * |
| 1659 |
|
- * @var array |
| 1660 |
|
- */ |
| 1661 |
|
- protected $allowedDirectories = array(); |
| 1662 |
|
- |
| 1663 |
|
- /** |
| 1664 |
|
- * stores the php handling level |
| 1665 |
|
- * |
| 1666 |
|
- * defaults to DwooSecurityPolicy::PHP_REMOVE |
| 1667 |
|
- * |
| 1668 |
|
- * @var int |
| 1669 |
|
- */ |
| 1670 |
|
- protected $phpHandling = self::PHP_REMOVE; |
| 1671 |
|
- |
| 1672 |
|
- /** |
| 1673 |
|
- * stores the constant handling level |
| 1674 |
|
- * |
| 1675 |
|
- * defaults to DwooSecurityPolicy::CONST_DISALLOW |
| 1676 |
|
- * |
| 1677 |
|
- * @var bool |
| 1678 |
|
- */ |
| 1679 |
|
- protected $constHandling = self::CONST_DISALLOW; |
| 1680 |
|
- |
| 1681 |
|
- /** |
| 1682 |
|
- * adds a php function to the allowed list |
| 1683 |
|
- * |
| 1684 |
|
- * @param mixed $func function name or array of function names |
| 1685 |
|
- */ |
| 1686 |
|
- public function allowPhpFunction($func) |
| 1687 |
|
- { |
| 1688 |
|
- if(is_array($func)) |
| 1689 |
|
- foreach($func as $fname) |
| 1690 |
|
- $this->allowedPhpFunctions[strtolower($fname)] = true; |
| 1691 |
|
- else |
| 1692 |
|
- $this->allowedPhpFunctions[strtolower($func)] = true; |
| 1693 |
|
- } |
| 1694 |
|
- |
| 1695 |
|
- /** |
| 1696 |
|
- * removes a php function from the allowed list |
| 1697 |
|
- * |
| 1698 |
|
- * @param mixed $func function name or array of function names |
| 1699 |
|
- */ |
| 1700 |
|
- public function disallowPhpFunction($func) |
| 1701 |
|
- { |
| 1702 |
|
- if(is_array($func)) |
| 1703 |
|
- foreach($func as $fname) |
| 1704 |
|
- unset($this->allowedPhpFunctions[strtolower($fname)]); |
| 1705 |
|
- else |
| 1706 |
|
- unset($this->allowedPhpFunctions[strtolower($func)]); |
| 1707 |
|
- } |
| 1708 |
|
- |
| 1709 |
|
- /** |
| 1710 |
|
- * returns the list of php functions allowed to run, note that the function names |
| 1711 |
|
- * are stored in the array keys and not values |
| 1712 |
|
- * |
| 1713 |
|
- * @return array |
| 1714 |
|
- */ |
| 1715 |
|
- public function getAllowedPhpFunctions() |
| 1716 |
|
- { |
| 1717 |
|
- return $this->allowedPhpFunctions; |
| 1718 |
|
- } |
| 1719 |
|
- |
| 1720 |
|
- /** |
| 1721 |
|
- * adds a directory to the safelist for includes and other file-access plugins |
| 1722 |
|
- * |
| 1723 |
|
- * @param mixed $path a path name or an array of paths |
| 1724 |
|
- */ |
| 1725 |
|
- public function allowDirectory($path) |
| 1726 |
|
- { |
| 1727 |
|
- if(is_array($path)) |
| 1728 |
|
- foreach($path as $dir) |
| 1729 |
|
- $this->allowedDirectories[realpath($dir)] = true; |
| 1730 |
|
- else |
| 1731 |
|
- $this->allowedDirectories[realpath($path)] = true; |
| 1732 |
|
- } |
| 1733 |
|
- |
| 1734 |
|
- /** |
| 1735 |
|
- * removes a directory from the safelist |
| 1736 |
|
- * |
| 1737 |
|
- * @param mixed $path a path name or an array of paths |
| 1738 |
|
- */ |
| 1739 |
|
- public function disallowDirectory($path) |
| 1740 |
|
- { |
| 1741 |
|
- if(is_array($path)) |
| 1742 |
|
- foreach($path as $dir) |
| 1743 |
|
- unset($this->allowedDirectories[realpath($dir)]); |
| 1744 |
|
- else |
| 1745 |
|
- unset($this->allowedDirectories[realpath($path)]); |
| 1746 |
|
- } |
| 1747 |
|
- |
| 1748 |
|
- /** |
| 1749 |
|
- * returns the list of safe paths, note that the paths are stored in the array |
| 1750 |
|
- * keys and not values |
| 1751 |
|
- * |
| 1752 |
|
- * @return array |
| 1753 |
|
- */ |
| 1754 |
|
- public function getAllowedDirectories() |
| 1755 |
|
- { |
| 1756 |
|
- return $this->allowedDirectories; |
| 1757 |
|
- } |
| 1758 |
|
- |
| 1759 |
|
- /** |
| 1760 |
|
- * sets the php handling level, defaults to REMOVE |
| 1761 |
|
- * |
| 1762 |
|
- * @param int $level one of the DwooSecurityPolicy::PHP_* constants |
| 1763 |
|
- */ |
| 1764 |
|
- public function setPhpHandling($level = self::PHP_REMOVE) |
| 1765 |
|
- { |
| 1766 |
|
- $this->phpHandling = $level; |
| 1767 |
|
- } |
| 1768 |
|
- |
| 1769 |
|
- /** |
| 1770 |
|
- * returns the php handling level |
| 1771 |
|
- * |
| 1772 |
|
- * @return int the current level, one of the DwooSecurityPolicy::PHP_* constants |
| 1773 |
|
- */ |
| 1774 |
|
- public function getPhpHandling() |
| 1775 |
|
- { |
| 1776 |
|
- return $this->phpHandling; |
| 1777 |
|
- } |
| 1778 |
|
- |
| 1779 |
|
- /** |
| 1780 |
|
- * sets the constant handling level, defaults to CONST_DISALLOW |
| 1781 |
|
- * |
| 1782 |
|
- * @param bool $level one of the DwooSecurityPolicy::CONST_* constants |
| 1783 |
|
- */ |
| 1784 |
|
- public function setConstantHandling($level = self::CONST_DISALLOW) |
| 1785 |
|
- { |
| 1786 |
|
- $this->constHandling = $level; |
| 1787 |
|
- } |
| 1788 |
|
- |
| 1789 |
|
- /** |
| 1790 |
|
- * returns the constant handling level |
| 1791 |
|
- * |
| 1792 |
|
- * @return bool the current level, one of the DwooSecurityPolicy::CONST_* constants |
| 1793 |
|
- */ |
| 1794 |
|
- public function getConstantHandling() |
| 1795 |
|
- { |
| 1796 |
|
- return $this->constHandling; |
| 1797 |
|
- } |
| 1798 |
|
-} |
| 1799 |
|
- |
|
1465 |
+} |
|
1466 |
+ |
|
1467 |
+/** |
|
1468 |
+ * handles plugin loading and caching of plugins names/paths relationships |
|
1469 |
+ * |
|
1470 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
1471 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
1472 |
+ * |
|
1473 |
+ * This file is released under the LGPL |
|
1474 |
+ * "GNU Lesser General Public License" |
|
1475 |
+ * More information can be found here: |
|
1476 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
1477 |
+ * |
|
1478 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
1479 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
1480 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
1481 |
+ * @link http://dwoo.org/ |
|
1482 |
+ * @version 0.3.4 |
|
1483 |
+ * @date 2008-04-09 |
|
1484 |
+ * @package Dwoo |
|
1485 |
+ */ |
|
1486 |
+class DwooLoader |
|
1487 |
+{ |
|
1488 |
+ /** |
|
1489 |
+ * stores the plugin directories |
|
1490 |
+ * |
|
1491 |
+ * @see addDirectory |
|
1492 |
+ * @var array |
|
1493 |
+ */ |
|
1494 |
+ protected static $paths = array(); |
|
1495 |
+ |
|
1496 |
+ /** |
|
1497 |
+ * stores the plugins names/paths relationships |
|
1498 |
+ * don't edit this on your own, use addDirectory |
|
1499 |
+ * |
|
1500 |
+ * @see addDirectory |
|
1501 |
+ * @var array |
|
1502 |
+ * @access protected |
|
1503 |
+ */ |
|
1504 |
+ public static $classpath = array(); |
|
1505 |
+ |
|
1506 |
+ /** |
|
1507 |
+ * rebuilds class paths, scans the given directory recursively and saves all paths in the given file |
|
1508 |
+ * |
|
1509 |
+ * @param string $path the plugin path to scan |
|
1510 |
+ * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten |
|
1511 |
+ * @access protected |
|
1512 |
+ */ |
|
1513 |
+ public static function rebuildClassPathCache($path, $cacheFile) |
|
1514 |
+ { |
|
1515 |
+ if($cacheFile!==false) |
|
1516 |
+ { |
|
1517 |
+ $tmp = self::$classpath; |
|
1518 |
+ self::$classpath = array(); |
|
1519 |
+ } |
|
1520 |
+ |
|
1521 |
+ // iterates over all files/folders |
|
1522 |
+ $list = glob($path.DIRECTORY_SEPARATOR.'*'); |
|
1523 |
+ if(is_array($list)) |
|
1524 |
+ foreach($list as $f) |
|
1525 |
+ { |
|
1526 |
+ if(is_dir($f)) |
|
1527 |
+ self::rebuildClassPathCache($f, false); |
|
1528 |
+ elseif(basename($f) !== 'classpath.cache') |
|
1529 |
+ self::$classpath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f,'.php'))] = $f; |
|
1530 |
+ } |
|
1531 |
+ |
|
1532 |
+ // save in file if it's the first call (not recursed) |
|
1533 |
+ if($cacheFile!==false) |
|
1534 |
+ { |
|
1535 |
+ if(!file_put_contents($cacheFile, '<?php DwooLoader::$classpath = '.var_export(self::$classpath, true).' + DwooLoader::$classpath; ?>')) |
|
1536 |
+ throw new DwooException('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)'); |
|
1537 |
+ self::$classpath += $tmp; |
|
1538 |
+ } |
|
1539 |
+ } |
|
1540 |
+ |
|
1541 |
+ /** |
|
1542 |
+ * loads a plugin file |
|
1543 |
+ * |
|
1544 |
+ * @param string $class the plugin name, without the DwooPlugin_ prefix |
|
1545 |
+ */ |
|
1546 |
+ public static function loadPlugin($class) |
|
1547 |
+ { |
|
1548 |
+ // a new class was added or the include failed so we rebuild the cache |
|
1549 |
+ if(!isset(self::$classpath[$class]) || !include self::$classpath[$class]) |
|
1550 |
+ { |
|
1551 |
+ self::rebuildClassPathCache(DWOO_DIRECTORY . 'plugins', DWOO_COMPILE_DIRECTORY . DIRECTORY_SEPARATOR . 'classpath.cache.php'); |
|
1552 |
+ foreach(self::$paths as $path) |
|
1553 |
+ self::rebuildClassPathCache($path[0], $path[1]); |
|
1554 |
+ if(isset(self::$classpath[$class])) |
|
1555 |
+ include self::$classpath[$class]; |
|
1556 |
+ else |
|
1557 |
+ throw new DwooException('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
1558 |
+ } |
|
1559 |
+ } |
|
1560 |
+ |
|
1561 |
+ /** |
|
1562 |
+ * adds a plugin directory |
|
1563 |
+ * |
|
1564 |
+ * @param string $pluginDir the plugin path to scan |
|
1565 |
+ */ |
|
1566 |
+ public static function addDirectory($pluginDir) |
|
1567 |
+ { |
|
1568 |
+ $cacheFile = DWOO_COMPILE_DIRECTORY . DIRECTORY_SEPARATOR . 'classpath-'.substr(strtr($pluginDir, ':/\\.', '----'), strlen($pluginDir) > 80 ? -80 : 0).'.cache.php'; |
|
1569 |
+ self::$paths[] = array($pluginDir, $cacheFile); |
|
1570 |
+ if(file_exists($cacheFile)) |
|
1571 |
+ include $cacheFile; |
|
1572 |
+ else |
|
1573 |
+ DwooLoader::rebuildClassPathCache($pluginDir, $cacheFile); |
|
1574 |
+ } |
|
1575 |
+} |
|
1576 |
+ |
|
1577 |
+/** |
|
1578 |
+ * main dwoo exception class |
|
1579 |
+ * |
|
1580 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
1581 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
1582 |
+ * |
|
1583 |
+ * This file is released under the LGPL |
|
1584 |
+ * "GNU Lesser General Public License" |
|
1585 |
+ * More information can be found here: |
|
1586 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
1587 |
+ * |
|
1588 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
1589 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
1590 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
1591 |
+ * @link http://dwoo.org/ |
|
1592 |
+ * @version 0.3.4 |
|
1593 |
+ * @date 2008-04-09 |
|
1594 |
+ * @package Dwoo |
|
1595 |
+ */ |
|
1596 |
+class DwooException extends Exception |
|
1597 |
+{ |
|
1598 |
+} |
|
1599 |
+ |
|
1600 |
+/** |
|
1601 |
+ * represents the security settings of a dwoo instance, it can be passed around to different dwoo instances |
|
1602 |
+ * |
|
1603 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
1604 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
1605 |
+ * |
|
1606 |
+ * This file is released under the LGPL |
|
1607 |
+ * "GNU Lesser General Public License" |
|
1608 |
+ * More information can be found here: |
|
1609 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
1610 |
+ * |
|
1611 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
1612 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
1613 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
1614 |
+ * @link http://dwoo.org/ |
|
1615 |
+ * @version 0.3.4 |
|
1616 |
+ * @date 2008-04-09 |
|
1617 |
+ * @package Dwoo |
|
1618 |
+ */ |
|
1619 |
+class DwooSecurityPolicy |
|
1620 |
+{ |
|
1621 |
+ /**#@+ |
|
1622 |
+ * php handling constants, defaults to PHP_REMOVE |
|
1623 |
+ * |
|
1624 |
+ * PHP_REMOVE : remove all <?php ?> (+ short tags if your short tags option is on) from the input template |
|
1625 |
+ * PHP_ALLOW : leave them as they are |
|
1626 |
+ * PHP_ENCODE : run htmlentities over them |
|
1627 |
+ * |
|
1628 |
+ * @var int |
|
1629 |
+ */ |
|
1630 |
+ const PHP_ENCODE = 1; |
|
1631 |
+ const PHP_REMOVE = 2; |
|
1632 |
+ const PHP_ALLOW = 3; |
|
1633 |
+ /**#@-*/ |
|
1634 |
+ |
|
1635 |
+ /**#@+ |
|
1636 |
+ * constant handling constants, defaults to CONST_DISALLOW |
|
1637 |
+ * |
|
1638 |
+ * CONST_DISALLOW : throw an error if {$dwoo.const.*} is used in the template |
|
1639 |
+ * CONST_ALLOW : allow {$dwoo.const.*} calls |
|
1640 |
+ */ |
|
1641 |
+ const CONST_DISALLOW = false; |
|
1642 |
+ const CONST_ALLOW = true; |
|
1643 |
+ /**#@-*/ |
|
1644 |
+ |
|
1645 |
+ /** |
|
1646 |
+ * php functions that are allowed to be used within the template |
|
1647 |
+ * |
|
1648 |
+ * @var array |
|
1649 |
+ */ |
|
1650 |
+ protected $allowedPhpFunctions = array |
|
1651 |
+ ( |
|
1652 |
+ 'str_repeat', 'count', 'number_format', 'htmlentities', 'htmlspecialchars', |
|
1653 |
+ 'long2ip', 'strlen', 'list', 'empty', 'count', 'sizeof', 'in_array', 'is_array', |
|
1654 |
+ ); |
|
1655 |
+ |
|
1656 |
+ /** |
|
1657 |
+ * paths that are safe to use with include or other file-access plugins |
|
1658 |
+ * |
|
1659 |
+ * @var array |
|
1660 |
+ */ |
|
1661 |
+ protected $allowedDirectories = array(); |
|
1662 |
+ |
|
1663 |
+ /** |
|
1664 |
+ * stores the php handling level |
|
1665 |
+ * |
|
1666 |
+ * defaults to DwooSecurityPolicy::PHP_REMOVE |
|
1667 |
+ * |
|
1668 |
+ * @var int |
|
1669 |
+ */ |
|
1670 |
+ protected $phpHandling = self::PHP_REMOVE; |
|
1671 |
+ |
|
1672 |
+ /** |
|
1673 |
+ * stores the constant handling level |
|
1674 |
+ * |
|
1675 |
+ * defaults to DwooSecurityPolicy::CONST_DISALLOW |
|
1676 |
+ * |
|
1677 |
+ * @var bool |
|
1678 |
+ */ |
|
1679 |
+ protected $constHandling = self::CONST_DISALLOW; |
|
1680 |
+ |
|
1681 |
+ /** |
|
1682 |
+ * adds a php function to the allowed list |
|
1683 |
+ * |
|
1684 |
+ * @param mixed $func function name or array of function names |
|
1685 |
+ */ |
|
1686 |
+ public function allowPhpFunction($func) |
|
1687 |
+ { |
|
1688 |
+ if(is_array($func)) |
|
1689 |
+ foreach($func as $fname) |
|
1690 |
+ $this->allowedPhpFunctions[strtolower($fname)] = true; |
|
1691 |
+ else |
|
1692 |
+ $this->allowedPhpFunctions[strtolower($func)] = true; |
|
1693 |
+ } |
|
1694 |
+ |
|
1695 |
+ /** |
|
1696 |
+ * removes a php function from the allowed list |
|
1697 |
+ * |
|
1698 |
+ * @param mixed $func function name or array of function names |
|
1699 |
+ */ |
|
1700 |
+ public function disallowPhpFunction($func) |
|
1701 |
+ { |
|
1702 |
+ if(is_array($func)) |
|
1703 |
+ foreach($func as $fname) |
|
1704 |
+ unset($this->allowedPhpFunctions[strtolower($fname)]); |
|
1705 |
+ else |
|
1706 |
+ unset($this->allowedPhpFunctions[strtolower($func)]); |
|
1707 |
+ } |
|
1708 |
+ |
|
1709 |
+ /** |
|
1710 |
+ * returns the list of php functions allowed to run, note that the function names |
|
1711 |
+ * are stored in the array keys and not values |
|
1712 |
+ * |
|
1713 |
+ * @return array |
|
1714 |
+ */ |
|
1715 |
+ public function getAllowedPhpFunctions() |
|
1716 |
+ { |
|
1717 |
+ return $this->allowedPhpFunctions; |
|
1718 |
+ } |
|
1719 |
+ |
|
1720 |
+ /** |
|
1721 |
+ * adds a directory to the safelist for includes and other file-access plugins |
|
1722 |
+ * |
|
1723 |
+ * @param mixed $path a path name or an array of paths |
|
1724 |
+ */ |
|
1725 |
+ public function allowDirectory($path) |
|
1726 |
+ { |
|
1727 |
+ if(is_array($path)) |
|
1728 |
+ foreach($path as $dir) |
|
1729 |
+ $this->allowedDirectories[realpath($dir)] = true; |
|
1730 |
+ else |
|
1731 |
+ $this->allowedDirectories[realpath($path)] = true; |
|
1732 |
+ } |
|
1733 |
+ |
|
1734 |
+ /** |
|
1735 |
+ * removes a directory from the safelist |
|
1736 |
+ * |
|
1737 |
+ * @param mixed $path a path name or an array of paths |
|
1738 |
+ */ |
|
1739 |
+ public function disallowDirectory($path) |
|
1740 |
+ { |
|
1741 |
+ if(is_array($path)) |
|
1742 |
+ foreach($path as $dir) |
|
1743 |
+ unset($this->allowedDirectories[realpath($dir)]); |
|
1744 |
+ else |
|
1745 |
+ unset($this->allowedDirectories[realpath($path)]); |
|
1746 |
+ } |
|
1747 |
+ |
|
1748 |
+ /** |
|
1749 |
+ * returns the list of safe paths, note that the paths are stored in the array |
|
1750 |
+ * keys and not values |
|
1751 |
+ * |
|
1752 |
+ * @return array |
|
1753 |
+ */ |
|
1754 |
+ public function getAllowedDirectories() |
|
1755 |
+ { |
|
1756 |
+ return $this->allowedDirectories; |
|
1757 |
+ } |
|
1758 |
+ |
|
1759 |
+ /** |
|
1760 |
+ * sets the php handling level, defaults to REMOVE |
|
1761 |
+ * |
|
1762 |
+ * @param int $level one of the DwooSecurityPolicy::PHP_* constants |
|
1763 |
+ */ |
|
1764 |
+ public function setPhpHandling($level = self::PHP_REMOVE) |
|
1765 |
+ { |
|
1766 |
+ $this->phpHandling = $level; |
|
1767 |
+ } |
|
1768 |
+ |
|
1769 |
+ /** |
|
1770 |
+ * returns the php handling level |
|
1771 |
+ * |
|
1772 |
+ * @return int the current level, one of the DwooSecurityPolicy::PHP_* constants |
|
1773 |
+ */ |
|
1774 |
+ public function getPhpHandling() |
|
1775 |
+ { |
|
1776 |
+ return $this->phpHandling; |
|
1777 |
+ } |
|
1778 |
+ |
|
1779 |
+ /** |
|
1780 |
+ * sets the constant handling level, defaults to CONST_DISALLOW |
|
1781 |
+ * |
|
1782 |
+ * @param bool $level one of the DwooSecurityPolicy::CONST_* constants |
|
1783 |
+ */ |
|
1784 |
+ public function setConstantHandling($level = self::CONST_DISALLOW) |
|
1785 |
+ { |
|
1786 |
+ $this->constHandling = $level; |
|
1787 |
+ } |
|
1788 |
+ |
|
1789 |
+ /** |
|
1790 |
+ * returns the constant handling level |
|
1791 |
+ * |
|
1792 |
+ * @return bool the current level, one of the DwooSecurityPolicy::CONST_* constants |
|
1793 |
+ */ |
|
1794 |
+ public function getConstantHandling() |
|
1795 |
+ { |
|
1796 |
+ return $this->constHandling; |
|
1797 |
+ } |
|
1798 |
+} |
|
1799 |
+ |
| 1800 |
1800 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * default dwoo compiler class, compiles dwoo templates into php |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-class DwooCompiler implements DwooICompiler |
| 24 |
|
-{ |
| 25 |
|
- /** |
| 26 |
|
- * constant that represents a php opening tag |
| 27 |
|
- * |
| 28 |
|
- * use it in case it needs to be adjusted |
| 29 |
|
- * |
| 30 |
|
- * @var string |
| 31 |
|
- */ |
| 32 |
|
- const PHP_OPEN = "<?php "; |
| 33 |
|
- |
| 34 |
|
- /** |
| 35 |
|
- * constant that represents a php closing tag |
| 36 |
|
- * |
| 37 |
|
- * use it in case it needs to be adjusted |
| 38 |
|
- * |
| 39 |
|
- * @var string |
| 40 |
|
- */ |
| 41 |
|
- const PHP_CLOSE = "?>"; |
| 42 |
|
- |
| 43 |
|
- /** |
| 44 |
|
- * boolean flag to enable or disable debugging output |
| 45 |
|
- * |
| 46 |
|
- * @var bool |
| 47 |
|
- */ |
| 48 |
|
- public $debug = false; |
| 49 |
|
- |
| 50 |
|
- /** |
| 51 |
|
- * left script delimiter |
| 52 |
|
- * |
| 53 |
|
- * @var string |
| 54 |
|
- */ |
| 55 |
|
- protected $ld = '{'; |
| 56 |
|
- |
| 57 |
|
- /** |
| 58 |
|
- * left script delimiter with escaped regex meta characters |
| 59 |
|
- * |
| 60 |
|
- * @var string |
| 61 |
|
- */ |
| 62 |
|
- protected $ldr = '\\{'; |
| 63 |
|
- |
| 64 |
|
- /** |
| 65 |
|
- * right script delimiter |
| 66 |
|
- * |
| 67 |
|
- * @var string |
| 68 |
|
- */ |
| 69 |
|
- protected $rd = '}'; |
| 70 |
|
- |
| 71 |
|
- /** |
| 72 |
|
- * right script delimiter with escaped regex meta characters |
| 73 |
|
- * |
| 74 |
|
- * @var string |
| 75 |
|
- */ |
| 76 |
|
- protected $rdr = '\\}'; |
| 77 |
|
- |
| 78 |
|
- /** |
| 79 |
|
- * defines whether opening and closing tags can contain spaces before valid data or not |
| 80 |
|
- * |
| 81 |
|
- * turn to true if you want to be sloppy with the syntax, but when set to false it allows |
| 82 |
|
- * to skip javascript and css tags as long as they are in the form "{ something", which is |
| 83 |
|
- * nice. default is false. |
| 84 |
|
- */ |
| 85 |
|
- protected $allowLooseOpenings = false; |
| 86 |
|
- |
| 87 |
|
- /** |
| 88 |
|
- * security policy object |
| 89 |
|
- * |
| 90 |
|
- * @var DwooSecurityPolicy |
| 91 |
|
- */ |
| 92 |
|
- protected $securityPolicy; |
| 93 |
|
- |
| 94 |
|
- /** |
| 95 |
|
- * storage for parse errors/warnings |
| 96 |
|
- * |
| 97 |
|
- * will be deprecated when proper exceptions are added |
| 98 |
|
- * |
| 99 |
|
- * @var array |
| 100 |
|
- */ |
| 101 |
|
- protected $errors = array(); |
| 102 |
|
- |
| 103 |
|
- /** |
| 104 |
|
- * stores the custom plugins registered with this compiler |
| 105 |
|
- * |
| 106 |
|
- * @var array |
| 107 |
|
- */ |
| 108 |
|
- protected $customPlugins = array(); |
| 109 |
|
- |
| 110 |
|
- /** |
| 111 |
|
- * stores the pre- and post-processors callbacks |
| 112 |
|
- * |
| 113 |
|
- * @var array |
| 114 |
|
- */ |
| 115 |
|
- protected $processors = array('pre'=>array(), 'post'=>array()); |
| 116 |
|
- |
| 117 |
|
- /** |
| 118 |
|
- * stores a list of plugins that are used in the currently compiled |
| 119 |
|
- * template, and that are not compilable. these plugins will be loaded |
| 120 |
|
- * during the template's runtime if required. |
| 121 |
|
- * |
| 122 |
|
- * it is a 1D array formatted as key:pluginName value:pluginType |
| 123 |
|
- * |
| 124 |
|
- * @var array |
| 125 |
|
- */ |
| 126 |
|
- protected $usedPlugins; |
| 127 |
|
- |
| 128 |
|
- /** |
| 129 |
|
- * stores the template undergoing compilation |
| 130 |
|
- * |
| 131 |
|
- * @var string |
| 132 |
|
- */ |
| 133 |
|
- protected $template; |
| 134 |
|
- |
| 135 |
|
- /** |
| 136 |
|
- * stores the current pointer position inside the template |
| 137 |
|
- * |
| 138 |
|
- * @var int |
| 139 |
|
- */ |
| 140 |
|
- protected $pointer; |
| 141 |
|
- |
| 142 |
|
- /** |
| 143 |
|
- * stores the data within which the scope moves |
| 144 |
|
- * |
| 145 |
|
- * @var array |
| 146 |
|
- */ |
| 147 |
|
- protected $data; |
| 148 |
|
- |
| 149 |
|
- /** |
| 150 |
|
- * variable scope of the compiler, set to null if |
| 151 |
|
- * it can not be resolved to a static string (i.e. if some |
| 152 |
|
- * plugin defines a new scope based on a variable array key) |
| 153 |
|
- * |
| 154 |
|
- * @var mixed |
| 155 |
|
- */ |
| 156 |
|
- protected $scope; |
| 157 |
|
- |
| 158 |
|
- /** |
| 159 |
|
- * variable scope tree, that allows to rebuild the current |
| 160 |
|
- * scope if required, i.e. when going to a parent level |
| 161 |
|
- * |
| 162 |
|
- * @var array |
| 163 |
|
- */ |
| 164 |
|
- protected $scopeTree; |
| 165 |
|
- |
| 166 |
|
- /** |
| 167 |
|
- * block plugins stack, accessible through some methods |
| 168 |
|
- * |
| 169 |
|
- * @see findBlock |
| 170 |
|
- * @see getCurrentBlock |
| 171 |
|
- * @see addBlock |
| 172 |
|
- * @see addCustomBlock |
| 173 |
|
- * @see injectBlock |
| 174 |
|
- * @see removeBlock |
| 175 |
|
- * @see removeTopBlock |
| 176 |
|
- * |
| 177 |
|
- * @var array |
| 178 |
|
- */ |
| 179 |
|
- protected $stack = array(); |
| 180 |
|
- |
| 181 |
|
- /** |
| 182 |
|
- * current block at the top of the block plugins stack, |
| 183 |
|
- * accessible through getCurrentBlock |
| 184 |
|
- * |
| 185 |
|
- * @see getCurrentBlock |
| 186 |
|
- * |
| 187 |
|
- * @var DwooBlockPlugin |
| 188 |
|
- */ |
| 189 |
|
- protected $curBlock; |
| 190 |
|
- |
| 191 |
|
- /** |
| 192 |
|
- * holds an instance of this class, used by getInstance when you don't |
| 193 |
|
- * provide a custom compiler in order to save resources |
| 194 |
|
- * |
| 195 |
|
- * @var DwooCompiler |
| 196 |
|
- */ |
| 197 |
|
- protected static $instance; |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * default dwoo compiler class, compiles dwoo templates into php |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+class DwooCompiler implements DwooICompiler |
|
24 |
+{ |
|
25 |
+ /** |
|
26 |
+ * constant that represents a php opening tag |
|
27 |
+ * |
|
28 |
+ * use it in case it needs to be adjusted |
|
29 |
+ * |
|
30 |
+ * @var string |
|
31 |
+ */ |
|
32 |
+ const PHP_OPEN = "<?php "; |
|
33 |
+ |
|
34 |
+ /** |
|
35 |
+ * constant that represents a php closing tag |
|
36 |
+ * |
|
37 |
+ * use it in case it needs to be adjusted |
|
38 |
+ * |
|
39 |
+ * @var string |
|
40 |
+ */ |
|
41 |
+ const PHP_CLOSE = "?>"; |
|
42 |
+ |
|
43 |
+ /** |
|
44 |
+ * boolean flag to enable or disable debugging output |
|
45 |
+ * |
|
46 |
+ * @var bool |
|
47 |
+ */ |
|
48 |
+ public $debug = false; |
|
49 |
+ |
|
50 |
+ /** |
|
51 |
+ * left script delimiter |
|
52 |
+ * |
|
53 |
+ * @var string |
|
54 |
+ */ |
|
55 |
+ protected $ld = '{'; |
|
56 |
+ |
|
57 |
+ /** |
|
58 |
+ * left script delimiter with escaped regex meta characters |
|
59 |
+ * |
|
60 |
+ * @var string |
|
61 |
+ */ |
|
62 |
+ protected $ldr = '\\{'; |
|
63 |
+ |
|
64 |
+ /** |
|
65 |
+ * right script delimiter |
|
66 |
+ * |
|
67 |
+ * @var string |
|
68 |
+ */ |
|
69 |
+ protected $rd = '}'; |
|
70 |
+ |
|
71 |
+ /** |
|
72 |
+ * right script delimiter with escaped regex meta characters |
|
73 |
+ * |
|
74 |
+ * @var string |
|
75 |
+ */ |
|
76 |
+ protected $rdr = '\\}'; |
|
77 |
+ |
|
78 |
+ /** |
|
79 |
+ * defines whether opening and closing tags can contain spaces before valid data or not |
|
80 |
+ * |
|
81 |
+ * turn to true if you want to be sloppy with the syntax, but when set to false it allows |
|
82 |
+ * to skip javascript and css tags as long as they are in the form "{ something", which is |
|
83 |
+ * nice. default is false. |
|
84 |
+ */ |
|
85 |
+ protected $allowLooseOpenings = false; |
|
86 |
+ |
|
87 |
+ /** |
|
88 |
+ * security policy object |
|
89 |
+ * |
|
90 |
+ * @var DwooSecurityPolicy |
|
91 |
+ */ |
|
92 |
+ protected $securityPolicy; |
|
93 |
+ |
|
94 |
+ /** |
|
95 |
+ * storage for parse errors/warnings |
|
96 |
+ * |
|
97 |
+ * will be deprecated when proper exceptions are added |
|
98 |
+ * |
|
99 |
+ * @var array |
|
100 |
+ */ |
|
101 |
+ protected $errors = array(); |
|
102 |
+ |
|
103 |
+ /** |
|
104 |
+ * stores the custom plugins registered with this compiler |
|
105 |
+ * |
|
106 |
+ * @var array |
|
107 |
+ */ |
|
108 |
+ protected $customPlugins = array(); |
|
109 |
+ |
|
110 |
+ /** |
|
111 |
+ * stores the pre- and post-processors callbacks |
|
112 |
+ * |
|
113 |
+ * @var array |
|
114 |
+ */ |
|
115 |
+ protected $processors = array('pre'=>array(), 'post'=>array()); |
|
116 |
+ |
|
117 |
+ /** |
|
118 |
+ * stores a list of plugins that are used in the currently compiled |
|
119 |
+ * template, and that are not compilable. these plugins will be loaded |
|
120 |
+ * during the template's runtime if required. |
|
121 |
+ * |
|
122 |
+ * it is a 1D array formatted as key:pluginName value:pluginType |
|
123 |
+ * |
|
124 |
+ * @var array |
|
125 |
+ */ |
|
126 |
+ protected $usedPlugins; |
|
127 |
+ |
|
128 |
+ /** |
|
129 |
+ * stores the template undergoing compilation |
|
130 |
+ * |
|
131 |
+ * @var string |
|
132 |
+ */ |
|
133 |
+ protected $template; |
|
134 |
+ |
|
135 |
+ /** |
|
136 |
+ * stores the current pointer position inside the template |
|
137 |
+ * |
|
138 |
+ * @var int |
|
139 |
+ */ |
|
140 |
+ protected $pointer; |
|
141 |
+ |
|
142 |
+ /** |
|
143 |
+ * stores the data within which the scope moves |
|
144 |
+ * |
|
145 |
+ * @var array |
|
146 |
+ */ |
|
147 |
+ protected $data; |
|
148 |
+ |
|
149 |
+ /** |
|
150 |
+ * variable scope of the compiler, set to null if |
|
151 |
+ * it can not be resolved to a static string (i.e. if some |
|
152 |
+ * plugin defines a new scope based on a variable array key) |
|
153 |
+ * |
|
154 |
+ * @var mixed |
|
155 |
+ */ |
|
156 |
+ protected $scope; |
|
157 |
+ |
|
158 |
+ /** |
|
159 |
+ * variable scope tree, that allows to rebuild the current |
|
160 |
+ * scope if required, i.e. when going to a parent level |
|
161 |
+ * |
|
162 |
+ * @var array |
|
163 |
+ */ |
|
164 |
+ protected $scopeTree; |
|
165 |
+ |
|
166 |
+ /** |
|
167 |
+ * block plugins stack, accessible through some methods |
|
168 |
+ * |
|
169 |
+ * @see findBlock |
|
170 |
+ * @see getCurrentBlock |
|
171 |
+ * @see addBlock |
|
172 |
+ * @see addCustomBlock |
|
173 |
+ * @see injectBlock |
|
174 |
+ * @see removeBlock |
|
175 |
+ * @see removeTopBlock |
|
176 |
+ * |
|
177 |
+ * @var array |
|
178 |
+ */ |
|
179 |
+ protected $stack = array(); |
|
180 |
+ |
|
181 |
+ /** |
|
182 |
+ * current block at the top of the block plugins stack, |
|
183 |
+ * accessible through getCurrentBlock |
|
184 |
+ * |
|
185 |
+ * @see getCurrentBlock |
|
186 |
+ * |
|
187 |
+ * @var DwooBlockPlugin |
|
188 |
+ */ |
|
189 |
+ protected $curBlock; |
|
190 |
+ |
|
191 |
+ /** |
|
192 |
+ * holds an instance of this class, used by getInstance when you don't |
|
193 |
+ * provide a custom compiler in order to save resources |
|
194 |
+ * |
|
195 |
+ * @var DwooCompiler |
|
196 |
+ */ |
|
197 |
+ protected static $instance; |
| 198 |
198 |
|
| 199 |
199 |
/** |
| 200 |
200 |
* sets the delimiters to use in the templates |
| 313 |
313 |
* @param mixed $callback either a valid callback to the postprocessor or a simple name if the autoload is set to true |
| 314 |
314 |
* @param bool $autoload if set to true, the postprocessor is auto-loaded from one of the plugin directories, else you must provide a valid callback |
| 315 |
315 |
*/ |
| 316 |
|
- public function addPostProcessor($callback, $autoload = false) |
| 317 |
|
- { |
| 318 |
|
- if($autoload) |
| 319 |
|
- { |
| 320 |
|
- $name = str_replace('DwooProcessor_', '', $callback); |
| 321 |
|
- $class = 'DwooProcessor_'.$name; |
| 322 |
|
- |
| 323 |
|
- if(!class_exists($class, false) && !function_exists($class)) |
| 324 |
|
- DwooLoader::loadPlugin($name); |
| 325 |
|
- |
| 326 |
|
- if(class_exists($class, false)) |
| 327 |
|
- $callback = array(new $class($this), 'process'); |
| 328 |
|
- elseif(function_exists($class)) |
| 329 |
|
- $callback = $class; |
| 330 |
|
- else |
| 331 |
|
- $this->triggerError('Wrong post-processor name, when using autoload the processor must be in one of your plugin dir as "name.php" containg a class or function named "DwooProcessor_name"', E_USER_ERROR); |
| 332 |
|
- |
| 333 |
|
- $this->processors['post'][] = $callback; |
| 334 |
|
- } |
| 335 |
|
- else |
| 336 |
|
- { |
| 337 |
|
- $this->processors['post'][] = $callback; |
| 338 |
|
- } |
| 339 |
|
- } |
| 340 |
|
- |
|
316 |
+ public function addPostProcessor($callback, $autoload = false) |
|
317 |
+ { |
|
318 |
+ if($autoload) |
|
319 |
+ { |
|
320 |
+ $name = str_replace('DwooProcessor_', '', $callback); |
|
321 |
+ $class = 'DwooProcessor_'.$name; |
|
322 |
+ |
|
323 |
+ if(!class_exists($class, false) && !function_exists($class)) |
|
324 |
+ DwooLoader::loadPlugin($name); |
|
325 |
+ |
|
326 |
+ if(class_exists($class, false)) |
|
327 |
+ $callback = array(new $class($this), 'process'); |
|
328 |
+ elseif(function_exists($class)) |
|
329 |
+ $callback = $class; |
|
330 |
+ else |
|
331 |
+ $this->triggerError('Wrong post-processor name, when using autoload the processor must be in one of your plugin dir as "name.php" containg a class or function named "DwooProcessor_name"', E_USER_ERROR); |
|
332 |
+ |
|
333 |
+ $this->processors['post'][] = $callback; |
|
334 |
+ } |
|
335 |
+ else |
|
336 |
+ { |
|
337 |
+ $this->processors['post'][] = $callback; |
|
338 |
+ } |
|
339 |
+ } |
|
340 |
+ |
| 341 |
341 |
/** |
| 342 |
342 |
* removes a postprocessor from the compiler |
| 343 |
343 |
* |
| 344 |
344 |
* @param mixed $callback either a valid callback to the postprocessor or a simple name if it was autoloaded |
| 345 |
345 |
*/ |
| 346 |
|
- public function removePostProcessor($callback) |
| 347 |
|
- { |
| 348 |
|
- if(($index = array_search($callback, $this->processors['post'], true)) !== false) |
| 349 |
|
- unset($this->processors['post'][$index]); |
| 350 |
|
- elseif(($index = array_search('DwooProcessor_'.str_replace('DwooProcessor_', '', $callback), $this->processors['post'], true)) !== false) |
| 351 |
|
- unset($this->processors['post'][$index]); |
| 352 |
|
- else |
| 353 |
|
- { |
| 354 |
|
- $class = 'DwooProcessor_' . str_replace('DwooProcessor_', '', $callback); |
| 355 |
|
- foreach($this->processors['post'] as $index=>$proc) |
| 356 |
|
- { |
| 357 |
|
- if(is_array($proc) && $proc[0] instanceof $class) |
| 358 |
|
- { |
| 359 |
|
- unset($this->processors['post'][$index]); |
| 360 |
|
- break; |
| 361 |
|
- } |
| 362 |
|
- } |
| 363 |
|
- } |
| 364 |
|
- } |
| 365 |
|
- |
| 366 |
|
- /** |
| 367 |
|
- * adds the custom plugins loaded into Dwoo to the compiler so it can load them |
| 368 |
|
- * |
| 369 |
|
- * @see Dwoo::addPlugin |
| 370 |
|
- * @param array $customPlugins an array of custom plugins |
| 371 |
|
- */ |
| 372 |
|
- public function setCustomPlugins(array $customPlugins) |
| 373 |
|
- { |
| 374 |
|
- $this->customPlugins = $customPlugins; |
| 375 |
|
- } |
| 376 |
|
- |
| 377 |
|
- /** |
| 378 |
|
- * sets the security policy object to enforce some php security settings |
| 379 |
|
- * |
| 380 |
|
- * use this if untrusted persons can modify templates, |
| 381 |
|
- * set it on the Dwoo object as it will be passed onto the compiler automatically |
| 382 |
|
- * |
| 383 |
|
- * @param DwooSecurityPolicy $policy the security policy object |
| 384 |
|
- */ |
| 385 |
|
- public function setSecurityPolicy(DwooSecurityPolicy $policy = null) |
| 386 |
|
- { |
| 387 |
|
- $this->securityPolicy = $policy; |
| 388 |
|
- } |
| 389 |
|
- |
| 390 |
|
- /** |
| 391 |
|
- * returns the current security policy object or null by default |
| 392 |
|
- * |
| 393 |
|
- * @return DwooSecurityPolicy|null the security policy object if any |
| 394 |
|
- */ |
| 395 |
|
- public function getSecurityPolicy() |
| 396 |
|
- { |
| 397 |
|
- return $this->securityPolicy; |
| 398 |
|
- } |
| 399 |
|
- |
| 400 |
|
- /** |
| 401 |
|
- * sets the pointer position |
| 402 |
|
- * |
| 403 |
|
- * @param int $position the new pointer position |
| 404 |
|
- * @param bool $isOffset if set to true, the position acts as an offset and not an absolute position |
| 405 |
|
- */ |
| 406 |
|
- public function setPointer($position, $isOffset = false) |
| 407 |
|
- { |
| 408 |
|
- if($isOffset) |
| 409 |
|
- $this->pointer += $position; |
| 410 |
|
- else |
| 411 |
|
- $this->pointer = $position; |
| 412 |
|
- } |
| 413 |
|
- |
| 414 |
|
- /** |
| 415 |
|
- * returns the current pointer position, only available during compilation of a template |
| 416 |
|
- * |
| 417 |
|
- * @return int |
| 418 |
|
- */ |
| 419 |
|
- public function getPointer() |
| 420 |
|
- { |
| 421 |
|
- return $this->pointer; |
| 422 |
|
- } |
| 423 |
|
- |
| 424 |
|
- /** |
| 425 |
|
- * returns the dwoo object that initiated this template compilation, only available during compilation of a template |
| 426 |
|
- * |
| 427 |
|
- * @return Dwoo |
| 428 |
|
- */ |
| 429 |
|
- public function getDwoo() |
| 430 |
|
- { |
| 431 |
|
- return $this->dwoo; |
| 432 |
|
- } |
| 433 |
|
- |
| 434 |
|
- /** |
| 435 |
|
- * overwrites the template that is being compiled |
| 436 |
|
- * |
| 437 |
|
- * @param string $newSource the template source that must replace the current one |
| 438 |
|
- * @param bool $fromPointer if set to true, only the source from the current pointer position is replaced |
| 439 |
|
- * @return string the template or partial template |
| 440 |
|
- */ |
| 441 |
|
- public function setTemplateSource($newSource, $fromPointer = false) |
| 442 |
|
- { |
| 443 |
|
- if($fromPointer === true) |
| 444 |
|
- $this->templateSource = substr($this->templateSource, 0, $this->pointer) . $newSource; |
| 445 |
|
- else |
| 446 |
|
- $this->templateSource = $newSource; |
| 447 |
|
- } |
| 448 |
|
- |
| 449 |
|
- /** |
| 450 |
|
- * returns the template that is being compiled |
| 451 |
|
- * |
| 452 |
|
- * @param bool $fromPointer if set to true, only the source from the current pointer position is returned |
| 453 |
|
- * @return string the template or partial template |
| 454 |
|
- */ |
| 455 |
|
- public function getTemplateSource($fromPointer = false) |
| 456 |
|
- { |
| 457 |
|
- if($fromPointer) |
| 458 |
|
- return substr($this->templateSource, $this->pointer); |
| 459 |
|
- else |
| 460 |
|
- return $this->templateSource; |
| 461 |
|
- } |
| 462 |
|
- |
| 463 |
|
- /** |
| 464 |
|
- * compiles the provided string down to php code |
| 465 |
|
- * |
| 466 |
|
- * @param string $tpl the template to compile |
| 467 |
|
- * @return string a compiled php string |
| 468 |
|
- */ |
| 469 |
|
- public function compile(Dwoo $dwoo, DwooITemplate $template) |
|
346 |
+ public function removePostProcessor($callback) |
|
347 |
+ { |
|
348 |
+ if(($index = array_search($callback, $this->processors['post'], true)) !== false) |
|
349 |
+ unset($this->processors['post'][$index]); |
|
350 |
+ elseif(($index = array_search('DwooProcessor_'.str_replace('DwooProcessor_', '', $callback), $this->processors['post'], true)) !== false) |
|
351 |
+ unset($this->processors['post'][$index]); |
|
352 |
+ else |
|
353 |
+ { |
|
354 |
+ $class = 'DwooProcessor_' . str_replace('DwooProcessor_', '', $callback); |
|
355 |
+ foreach($this->processors['post'] as $index=>$proc) |
|
356 |
+ { |
|
357 |
+ if(is_array($proc) && $proc[0] instanceof $class) |
|
358 |
+ { |
|
359 |
+ unset($this->processors['post'][$index]); |
|
360 |
+ break; |
|
361 |
+ } |
|
362 |
+ } |
|
363 |
+ } |
|
364 |
+ } |
|
365 |
+ |
|
366 |
+ /** |
|
367 |
+ * adds the custom plugins loaded into Dwoo to the compiler so it can load them |
|
368 |
+ * |
|
369 |
+ * @see Dwoo::addPlugin |
|
370 |
+ * @param array $customPlugins an array of custom plugins |
|
371 |
+ */ |
|
372 |
+ public function setCustomPlugins(array $customPlugins) |
|
373 |
+ { |
|
374 |
+ $this->customPlugins = $customPlugins; |
|
375 |
+ } |
|
376 |
+ |
|
377 |
+ /** |
|
378 |
+ * sets the security policy object to enforce some php security settings |
|
379 |
+ * |
|
380 |
+ * use this if untrusted persons can modify templates, |
|
381 |
+ * set it on the Dwoo object as it will be passed onto the compiler automatically |
|
382 |
+ * |
|
383 |
+ * @param DwooSecurityPolicy $policy the security policy object |
|
384 |
+ */ |
|
385 |
+ public function setSecurityPolicy(DwooSecurityPolicy $policy = null) |
|
386 |
+ { |
|
387 |
+ $this->securityPolicy = $policy; |
|
388 |
+ } |
|
389 |
+ |
|
390 |
+ /** |
|
391 |
+ * returns the current security policy object or null by default |
|
392 |
+ * |
|
393 |
+ * @return DwooSecurityPolicy|null the security policy object if any |
|
394 |
+ */ |
|
395 |
+ public function getSecurityPolicy() |
|
396 |
+ { |
|
397 |
+ return $this->securityPolicy; |
|
398 |
+ } |
|
399 |
+ |
|
400 |
+ /** |
|
401 |
+ * sets the pointer position |
|
402 |
+ * |
|
403 |
+ * @param int $position the new pointer position |
|
404 |
+ * @param bool $isOffset if set to true, the position acts as an offset and not an absolute position |
|
405 |
+ */ |
|
406 |
+ public function setPointer($position, $isOffset = false) |
|
407 |
+ { |
|
408 |
+ if($isOffset) |
|
409 |
+ $this->pointer += $position; |
|
410 |
+ else |
|
411 |
+ $this->pointer = $position; |
|
412 |
+ } |
|
413 |
+ |
|
414 |
+ /** |
|
415 |
+ * returns the current pointer position, only available during compilation of a template |
|
416 |
+ * |
|
417 |
+ * @return int |
|
418 |
+ */ |
|
419 |
+ public function getPointer() |
|
420 |
+ { |
|
421 |
+ return $this->pointer; |
|
422 |
+ } |
|
423 |
+ |
|
424 |
+ /** |
|
425 |
+ * returns the dwoo object that initiated this template compilation, only available during compilation of a template |
|
426 |
+ * |
|
427 |
+ * @return Dwoo |
|
428 |
+ */ |
|
429 |
+ public function getDwoo() |
|
430 |
+ { |
|
431 |
+ return $this->dwoo; |
|
432 |
+ } |
|
433 |
+ |
|
434 |
+ /** |
|
435 |
+ * overwrites the template that is being compiled |
|
436 |
+ * |
|
437 |
+ * @param string $newSource the template source that must replace the current one |
|
438 |
+ * @param bool $fromPointer if set to true, only the source from the current pointer position is replaced |
|
439 |
+ * @return string the template or partial template |
|
440 |
+ */ |
|
441 |
+ public function setTemplateSource($newSource, $fromPointer = false) |
|
442 |
+ { |
|
443 |
+ if($fromPointer === true) |
|
444 |
+ $this->templateSource = substr($this->templateSource, 0, $this->pointer) . $newSource; |
|
445 |
+ else |
|
446 |
+ $this->templateSource = $newSource; |
|
447 |
+ } |
|
448 |
+ |
|
449 |
+ /** |
|
450 |
+ * returns the template that is being compiled |
|
451 |
+ * |
|
452 |
+ * @param bool $fromPointer if set to true, only the source from the current pointer position is returned |
|
453 |
+ * @return string the template or partial template |
|
454 |
+ */ |
|
455 |
+ public function getTemplateSource($fromPointer = false) |
|
456 |
+ { |
|
457 |
+ if($fromPointer) |
|
458 |
+ return substr($this->templateSource, $this->pointer); |
|
459 |
+ else |
|
460 |
+ return $this->templateSource; |
|
461 |
+ } |
|
462 |
+ |
|
463 |
+ /** |
|
464 |
+ * compiles the provided string down to php code |
|
465 |
+ * |
|
466 |
+ * @param string $tpl the template to compile |
|
467 |
+ * @return string a compiled php string |
|
468 |
+ */ |
|
469 |
+ public function compile(Dwoo $dwoo, DwooITemplate $template) |
| 470 |
470 |
{ |
| 471 |
471 |
// init vars |
| 472 |
|
- $tpl = $template->getSource(); |
| 473 |
|
- $ptr = 0; |
| 474 |
|
- $this->dwoo = $dwoo; |
| 475 |
|
- $this->template = $template; |
| 476 |
|
- $this->templateSource =& $tpl; |
| 477 |
|
- $this->pointer =& $ptr; |
| 478 |
|
- |
| 479 |
|
- if($this->debug) echo 'PROCESSING PREPROCESSORS<br>'; |
| 480 |
|
- |
| 481 |
|
- // runs preprocessors |
| 482 |
|
- foreach($this->processors['pre'] as $preProc) |
| 483 |
|
- { |
| 484 |
|
- if(is_array($preProc) && $preProc[0] instanceof DwooProcessor) |
| 485 |
|
- $tpl = call_user_func($preProc, $tpl); |
| 486 |
|
- else |
| 487 |
|
- $tpl = call_user_func($preProc, $this, $tpl); |
| 488 |
|
- } |
| 489 |
|
- unset($preProc); |
| 490 |
|
- |
| 491 |
|
- if($this->debug) echo '<pre>'.print_r(htmlentities($tpl), true).'<hr>'; |
| 492 |
|
- |
| 493 |
|
- // strips comments |
| 494 |
|
- if(strstr($tpl, $this->ld.'*') !== false) |
| 495 |
|
- $tpl = preg_replace('{'.$this->ldr.'\*.*?\*'.$this->rdr.'}s', '', $tpl); |
| 496 |
|
- |
| 497 |
|
- // strips php tags if required by the security policy |
| 498 |
|
- if($this->securityPolicy !== null) |
| 499 |
|
- { |
| 500 |
|
- $search = array('{<\?php.*?\?>}'); |
| 501 |
|
- if(ini_get('short_open_tags')) |
| 502 |
|
- $search = array('{<\?.*?\?>}', '{<%.*?%>}'); |
| 503 |
|
- switch($this->securityPolicy->getPhpHandling()) |
| 504 |
|
- { |
| 505 |
|
- case DwooSecurityPolicy::PHP_ALLOW: |
| 506 |
|
- break; |
| 507 |
|
- case DwooSecurityPolicy::PHP_ENCODE: |
| 508 |
|
- $tpl = preg_replace_callback($search, array($this, 'phpTagEncodingHelper'), $tpl); |
| 509 |
|
- break; |
| 510 |
|
- case DwooSecurityPolicy::PHP_REMOVE: |
| 511 |
|
- $tpl = preg_replace($search, '', $tpl); |
| 512 |
|
- } |
| 513 |
|
- } |
| 514 |
|
- |
| 515 |
|
- // handles the built-in strip function |
| 516 |
|
- if(($pos = strpos($tpl, $this->ld.'strip'.$this->rd)) !== false && substr($tpl, $pos-1, 1) !== '\\') |
| 517 |
|
- $tpl = preg_replace_callback('{'.$this->ldr.'strip'.$this->rdr.'(.+?)'.$this->ldr.'/strip'.$this->rdr.'}s', array($this, 'stripPreprocessorHelper'), $tpl); |
| 518 |
|
- |
| 519 |
|
- while(true) |
| 520 |
|
- { |
| 521 |
|
- // if pointer is at the beginning, reset everything, that allows a plugin to externally reset the compiler if everything must be reparsed |
| 522 |
|
- if($ptr===0) |
| 523 |
|
- { |
| 524 |
|
- // resets variables |
| 525 |
|
- $this->usedPlugins = array('topLevelBlock' => Dwoo::BLOCK_PLUGIN); |
| 526 |
|
- $this->data = array(); |
| 527 |
|
- $this->scope =& $this->data; |
| 528 |
|
- $this->scopeTree = array(); |
| 529 |
|
- $this->stack = array(); |
| 530 |
|
- // add top level block |
| 531 |
|
- $compiled = $this->addBlock('topLevelBlock', array(), 0); |
| 532 |
|
- } |
| 533 |
|
- |
| 534 |
|
- $pos = strpos($tpl, $this->ld, $ptr); |
| 535 |
|
- |
| 536 |
|
- if($pos === false) |
| 537 |
|
- { |
| 538 |
|
- $compiled .= substr($tpl, $ptr); |
| 539 |
|
- break; |
| 540 |
|
- } |
| 541 |
|
- elseif(substr($tpl, $pos-1, 1) === '\\' && substr($tpl, $pos-2, 1) !== '\\') |
| 542 |
|
- { |
| 543 |
|
- $compiled .= substr($tpl, $ptr, $pos-$ptr-1).$this->ld; |
| 544 |
|
- $ptr = $pos+strlen($this->ld); |
| 545 |
|
- } |
| 546 |
|
- elseif(strpos($tpl, $this->ld.'literal'.$this->rd, $pos) === $pos) |
| 547 |
|
- { |
| 548 |
|
- $endpos = strpos($tpl, $this->ld.'/literal'.$this->rd, $pos); |
| 549 |
|
- $compiled .= substr($tpl, $ptr, $pos-$ptr); |
| 550 |
|
- $compiled .= substr($tpl, $pos + strlen($this->ld.'literal'.$this->rd), $endpos-$pos-strlen($this->ld.'literal'.$this->rd)); |
| 551 |
|
- $ptr = $endpos+strlen($this->ld.'/literal'.$this->rd); |
| 552 |
|
- } |
| 553 |
|
- else |
| 554 |
|
- { |
| 555 |
|
- if(substr($tpl, $pos-2, 1) === '\\' && substr($tpl, $pos-1, 1) === '\\') |
| 556 |
|
- { |
| 557 |
|
- $compiled .= substr($tpl, $ptr, $pos-$ptr-1); |
| 558 |
|
- $ptr = $pos; |
| 559 |
|
- } |
| 560 |
|
- |
| 561 |
|
- $compiled .= substr($tpl, $ptr, $pos-$ptr); |
| 562 |
|
- |
| 563 |
|
- $endpos = strpos($tpl, $this->rd, $pos); |
| 564 |
|
- |
| 565 |
|
- if($endpos===false) |
| 566 |
|
- $this->triggerError('A template tag was not closed, started with <em>'.substr($tpl, $pos, 100).'</em>', E_USER_ERROR); |
| 567 |
|
- |
| 568 |
|
- while(substr($tpl, $endpos-1, 1) === '\\') |
| 569 |
|
- { |
| 570 |
|
- $tpl = substr_replace($tpl, $this->rd, $endpos-1, 1+strlen($this->rd)); |
| 571 |
|
- $endpos = strpos($tpl, $this->rd, $endpos); |
| 572 |
|
- } |
| 573 |
|
- |
| 574 |
|
- $pos += strlen($this->ld); |
| 575 |
|
- if($this->allowLooseOpenings) |
| 576 |
|
- { |
| 577 |
|
- while(substr($tpl, $pos, 1) === ' ') |
| 578 |
|
- $pos+=1; |
| 579 |
|
- } |
| 580 |
|
- else |
| 581 |
|
- { |
| 582 |
|
- if(substr($tpl, $pos, 1) === ' ' || substr($tpl, $pos, 1) === "\r" || substr($tpl, $pos, 1) === "\n") |
| 583 |
|
- { |
| 584 |
|
- $ptr = $pos; |
| 585 |
|
- $compiled .= $this->ld; |
| 586 |
|
- continue; |
| 587 |
|
- } |
| 588 |
|
- } |
| 589 |
|
- |
| 590 |
|
- $ptr = $endpos+strlen($this->rd); |
| 591 |
|
- |
| 592 |
|
- if(substr($tpl, $pos, 1)==='/') |
| 593 |
|
- $compiled .= $this->removeBlock(substr($tpl, $pos+1, $endpos-$pos-1)); |
| 594 |
|
- else |
| 595 |
|
- $compiled .= $this->parse($tpl, $pos, $endpos, false, 'root'); |
| 596 |
|
- |
| 597 |
|
- // adds additional line breaks between php closing and opening tags because the php parser removes those if there is just a single line break |
| 598 |
|
- if(substr($compiled, -2) === '?>' && preg_match('{^(([\r\n])([\r\n]?))}', substr($tpl, $ptr, 3), $m)) |
| 599 |
|
- { |
| 600 |
|
- if($m[3] === '') |
| 601 |
|
- { |
| 602 |
|
- $ptr+=1; |
| 603 |
|
- $compiled .= $m[1].$m[1]; |
| 604 |
|
- } |
| 605 |
|
- else |
| 606 |
|
- { |
| 607 |
|
- $ptr+=2; |
| 608 |
|
- $compiled .= $m[1]."\n"; |
| 609 |
|
- } |
| 610 |
|
- } |
| 611 |
|
- } |
| 612 |
|
- } |
| 613 |
|
- |
| 614 |
|
- $compiled .= $this->removeBlock('topLevelBlock'); |
| 615 |
|
- |
| 616 |
|
- if($this->debug) echo 'PROCESSING POSTPROCESSORS<br>'; |
| 617 |
|
- |
| 618 |
|
- foreach($this->processors['post'] as $postProc) |
| 619 |
|
- { |
| 620 |
|
- if(is_array($postProc) && $postProc[0] instanceof DwooProcessor) |
| 621 |
|
- $compiled = call_user_func($postProc, $compiled); |
| 622 |
|
- else |
| 623 |
|
- $compiled = call_user_func($postProc, $this, $compiled); |
| 624 |
|
- } |
| 625 |
|
- unset($postProc); |
| 626 |
|
- |
| 627 |
|
- if($this->debug) echo 'COMPILATION COMPLETE : MEM USAGE : '.memory_get_usage().'<br>'; |
| 628 |
|
- |
| 629 |
|
- $output = "<?php\n"; |
| 630 |
|
- // remove topLevelBlock |
| 631 |
|
- array_shift($this->usedPlugins); |
| 632 |
|
- // build plugin preloader |
| 633 |
|
- foreach($this->usedPlugins as $plugin=>$type) |
| 634 |
|
- { |
| 635 |
|
- if($type & Dwoo::CUSTOM_PLUGIN) continue; |
| 636 |
|
- switch($type) |
| 637 |
|
- { |
| 638 |
|
- case Dwoo::BLOCK_PLUGIN: |
| 639 |
|
- case Dwoo::CLASS_PLUGIN: |
| 640 |
|
- $output .= "if(class_exists('DwooPlugin_$plugin', false)===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
| 641 |
|
- break; |
| 642 |
|
- case Dwoo::FUNC_PLUGIN: |
| 643 |
|
- $output .= "if(function_exists('DwooPlugin_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
| 644 |
|
- break; |
| 645 |
|
- case Dwoo::SMARTY_MODIFIER: |
| 646 |
|
- $output .= "if(function_exists('smarty_modifier_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
| 647 |
|
- break; |
| 648 |
|
- case Dwoo::SMARTY_FUNCTION: |
| 649 |
|
- $output .= "if(function_exists('smarty_function_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
| 650 |
|
- break; |
| 651 |
|
- case Dwoo::SMARTY_BLOCK: |
| 652 |
|
- $output .= "if(function_exists('smarty_block_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
| 653 |
|
- break; |
| 654 |
|
- default: |
| 655 |
|
- $this->triggerError('Type error for '.$plugin.' with type'.$type, E_USER_ERROR); |
| 656 |
|
- } |
| 657 |
|
- } |
| 658 |
|
- |
| 659 |
|
- $output .= $compiled."\n?>"; |
| 660 |
|
- |
| 661 |
|
- $output = str_replace(self::PHP_CLOSE . self::PHP_OPEN, "\n", $output); |
| 662 |
|
- |
| 663 |
|
- if($this->debug) { |
| 664 |
|
- echo '<hr><pre>'; |
| 665 |
|
- $lines = preg_split('{\r\n|\n}', htmlentities($output)); |
| 666 |
|
- foreach($lines as $i=>$line) |
| 667 |
|
- echo ($i+1).'. '.$line."\r\n"; |
| 668 |
|
- } |
| 669 |
|
- if($this->debug) echo '<hr></pre></pre>'; |
| 670 |
|
- |
| 671 |
|
- if(!empty($this->errors)) |
| 672 |
|
- print_r($this->errors); |
| 673 |
|
- |
| 674 |
|
- $this->template = $this->dwoo = null; |
| 675 |
|
- $tpl = null; |
| 676 |
|
- |
| 677 |
|
- return $output; |
| 678 |
|
- } |
| 679 |
|
- |
|
472 |
+ $tpl = $template->getSource(); |
|
473 |
+ $ptr = 0; |
|
474 |
+ $this->dwoo = $dwoo; |
|
475 |
+ $this->template = $template; |
|
476 |
+ $this->templateSource =& $tpl; |
|
477 |
+ $this->pointer =& $ptr; |
|
478 |
+ |
|
479 |
+ if($this->debug) echo 'PROCESSING PREPROCESSORS<br>'; |
|
480 |
+ |
|
481 |
+ // runs preprocessors |
|
482 |
+ foreach($this->processors['pre'] as $preProc) |
|
483 |
+ { |
|
484 |
+ if(is_array($preProc) && $preProc[0] instanceof DwooProcessor) |
|
485 |
+ $tpl = call_user_func($preProc, $tpl); |
|
486 |
+ else |
|
487 |
+ $tpl = call_user_func($preProc, $this, $tpl); |
|
488 |
+ } |
|
489 |
+ unset($preProc); |
|
490 |
+ |
|
491 |
+ if($this->debug) echo '<pre>'.print_r(htmlentities($tpl), true).'<hr>'; |
|
492 |
+ |
|
493 |
+ // strips comments |
|
494 |
+ if(strstr($tpl, $this->ld.'*') !== false) |
|
495 |
+ $tpl = preg_replace('{'.$this->ldr.'\*.*?\*'.$this->rdr.'}s', '', $tpl); |
|
496 |
+ |
|
497 |
+ // strips php tags if required by the security policy |
|
498 |
+ if($this->securityPolicy !== null) |
|
499 |
+ { |
|
500 |
+ $search = array('{<\?php.*?\?>}'); |
|
501 |
+ if(ini_get('short_open_tags')) |
|
502 |
+ $search = array('{<\?.*?\?>}', '{<%.*?%>}'); |
|
503 |
+ switch($this->securityPolicy->getPhpHandling()) |
|
504 |
+ { |
|
505 |
+ case DwooSecurityPolicy::PHP_ALLOW: |
|
506 |
+ break; |
|
507 |
+ case DwooSecurityPolicy::PHP_ENCODE: |
|
508 |
+ $tpl = preg_replace_callback($search, array($this, 'phpTagEncodingHelper'), $tpl); |
|
509 |
+ break; |
|
510 |
+ case DwooSecurityPolicy::PHP_REMOVE: |
|
511 |
+ $tpl = preg_replace($search, '', $tpl); |
|
512 |
+ } |
|
513 |
+ } |
|
514 |
+ |
|
515 |
+ // handles the built-in strip function |
|
516 |
+ if(($pos = strpos($tpl, $this->ld.'strip'.$this->rd)) !== false && substr($tpl, $pos-1, 1) !== '\\') |
|
517 |
+ $tpl = preg_replace_callback('{'.$this->ldr.'strip'.$this->rdr.'(.+?)'.$this->ldr.'/strip'.$this->rdr.'}s', array($this, 'stripPreprocessorHelper'), $tpl); |
|
518 |
+ |
|
519 |
+ while(true) |
|
520 |
+ { |
|
521 |
+ // if pointer is at the beginning, reset everything, that allows a plugin to externally reset the compiler if everything must be reparsed |
|
522 |
+ if($ptr===0) |
|
523 |
+ { |
|
524 |
+ // resets variables |
|
525 |
+ $this->usedPlugins = array('topLevelBlock' => Dwoo::BLOCK_PLUGIN); |
|
526 |
+ $this->data = array(); |
|
527 |
+ $this->scope =& $this->data; |
|
528 |
+ $this->scopeTree = array(); |
|
529 |
+ $this->stack = array(); |
|
530 |
+ // add top level block |
|
531 |
+ $compiled = $this->addBlock('topLevelBlock', array(), 0); |
|
532 |
+ } |
|
533 |
+ |
|
534 |
+ $pos = strpos($tpl, $this->ld, $ptr); |
|
535 |
+ |
|
536 |
+ if($pos === false) |
|
537 |
+ { |
|
538 |
+ $compiled .= substr($tpl, $ptr); |
|
539 |
+ break; |
|
540 |
+ } |
|
541 |
+ elseif(substr($tpl, $pos-1, 1) === '\\' && substr($tpl, $pos-2, 1) !== '\\') |
|
542 |
+ { |
|
543 |
+ $compiled .= substr($tpl, $ptr, $pos-$ptr-1).$this->ld; |
|
544 |
+ $ptr = $pos+strlen($this->ld); |
|
545 |
+ } |
|
546 |
+ elseif(strpos($tpl, $this->ld.'literal'.$this->rd, $pos) === $pos) |
|
547 |
+ { |
|
548 |
+ $endpos = strpos($tpl, $this->ld.'/literal'.$this->rd, $pos); |
|
549 |
+ $compiled .= substr($tpl, $ptr, $pos-$ptr); |
|
550 |
+ $compiled .= substr($tpl, $pos + strlen($this->ld.'literal'.$this->rd), $endpos-$pos-strlen($this->ld.'literal'.$this->rd)); |
|
551 |
+ $ptr = $endpos+strlen($this->ld.'/literal'.$this->rd); |
|
552 |
+ } |
|
553 |
+ else |
|
554 |
+ { |
|
555 |
+ if(substr($tpl, $pos-2, 1) === '\\' && substr($tpl, $pos-1, 1) === '\\') |
|
556 |
+ { |
|
557 |
+ $compiled .= substr($tpl, $ptr, $pos-$ptr-1); |
|
558 |
+ $ptr = $pos; |
|
559 |
+ } |
|
560 |
+ |
|
561 |
+ $compiled .= substr($tpl, $ptr, $pos-$ptr); |
|
562 |
+ |
|
563 |
+ $endpos = strpos($tpl, $this->rd, $pos); |
|
564 |
+ |
|
565 |
+ if($endpos===false) |
|
566 |
+ $this->triggerError('A template tag was not closed, started with <em>'.substr($tpl, $pos, 100).'</em>', E_USER_ERROR); |
|
567 |
+ |
|
568 |
+ while(substr($tpl, $endpos-1, 1) === '\\') |
|
569 |
+ { |
|
570 |
+ $tpl = substr_replace($tpl, $this->rd, $endpos-1, 1+strlen($this->rd)); |
|
571 |
+ $endpos = strpos($tpl, $this->rd, $endpos); |
|
572 |
+ } |
|
573 |
+ |
|
574 |
+ $pos += strlen($this->ld); |
|
575 |
+ if($this->allowLooseOpenings) |
|
576 |
+ { |
|
577 |
+ while(substr($tpl, $pos, 1) === ' ') |
|
578 |
+ $pos+=1; |
|
579 |
+ } |
|
580 |
+ else |
|
581 |
+ { |
|
582 |
+ if(substr($tpl, $pos, 1) === ' ' || substr($tpl, $pos, 1) === "\r" || substr($tpl, $pos, 1) === "\n") |
|
583 |
+ { |
|
584 |
+ $ptr = $pos; |
|
585 |
+ $compiled .= $this->ld; |
|
586 |
+ continue; |
|
587 |
+ } |
|
588 |
+ } |
|
589 |
+ |
|
590 |
+ $ptr = $endpos+strlen($this->rd); |
|
591 |
+ |
|
592 |
+ if(substr($tpl, $pos, 1)==='/') |
|
593 |
+ $compiled .= $this->removeBlock(substr($tpl, $pos+1, $endpos-$pos-1)); |
|
594 |
+ else |
|
595 |
+ $compiled .= $this->parse($tpl, $pos, $endpos, false, 'root'); |
|
596 |
+ |
|
597 |
+ // adds additional line breaks between php closing and opening tags because the php parser removes those if there is just a single line break |
|
598 |
+ if(substr($compiled, -2) === '?>' && preg_match('{^(([\r\n])([\r\n]?))}', substr($tpl, $ptr, 3), $m)) |
|
599 |
+ { |
|
600 |
+ if($m[3] === '') |
|
601 |
+ { |
|
602 |
+ $ptr+=1; |
|
603 |
+ $compiled .= $m[1].$m[1]; |
|
604 |
+ } |
|
605 |
+ else |
|
606 |
+ { |
|
607 |
+ $ptr+=2; |
|
608 |
+ $compiled .= $m[1]."\n"; |
|
609 |
+ } |
|
610 |
+ } |
|
611 |
+ } |
|
612 |
+ } |
|
613 |
+ |
|
614 |
+ $compiled .= $this->removeBlock('topLevelBlock'); |
|
615 |
+ |
|
616 |
+ if($this->debug) echo 'PROCESSING POSTPROCESSORS<br>'; |
|
617 |
+ |
|
618 |
+ foreach($this->processors['post'] as $postProc) |
|
619 |
+ { |
|
620 |
+ if(is_array($postProc) && $postProc[0] instanceof DwooProcessor) |
|
621 |
+ $compiled = call_user_func($postProc, $compiled); |
|
622 |
+ else |
|
623 |
+ $compiled = call_user_func($postProc, $this, $compiled); |
|
624 |
+ } |
|
625 |
+ unset($postProc); |
|
626 |
+ |
|
627 |
+ if($this->debug) echo 'COMPILATION COMPLETE : MEM USAGE : '.memory_get_usage().'<br>'; |
|
628 |
+ |
|
629 |
+ $output = "<?php\n"; |
|
630 |
+ // remove topLevelBlock |
|
631 |
+ array_shift($this->usedPlugins); |
|
632 |
+ // build plugin preloader |
|
633 |
+ foreach($this->usedPlugins as $plugin=>$type) |
|
634 |
+ { |
|
635 |
+ if($type & Dwoo::CUSTOM_PLUGIN) continue; |
|
636 |
+ switch($type) |
|
637 |
+ { |
|
638 |
+ case Dwoo::BLOCK_PLUGIN: |
|
639 |
+ case Dwoo::CLASS_PLUGIN: |
|
640 |
+ $output .= "if(class_exists('DwooPlugin_$plugin', false)===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
|
641 |
+ break; |
|
642 |
+ case Dwoo::FUNC_PLUGIN: |
|
643 |
+ $output .= "if(function_exists('DwooPlugin_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
|
644 |
+ break; |
|
645 |
+ case Dwoo::SMARTY_MODIFIER: |
|
646 |
+ $output .= "if(function_exists('smarty_modifier_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
|
647 |
+ break; |
|
648 |
+ case Dwoo::SMARTY_FUNCTION: |
|
649 |
+ $output .= "if(function_exists('smarty_function_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
|
650 |
+ break; |
|
651 |
+ case Dwoo::SMARTY_BLOCK: |
|
652 |
+ $output .= "if(function_exists('smarty_block_$plugin')===false)\n\tDwooLoader::loadPlugin('$plugin');\n"; |
|
653 |
+ break; |
|
654 |
+ default: |
|
655 |
+ $this->triggerError('Type error for '.$plugin.' with type'.$type, E_USER_ERROR); |
|
656 |
+ } |
|
657 |
+ } |
|
658 |
+ |
|
659 |
+ $output .= $compiled."\n?>"; |
|
660 |
+ |
|
661 |
+ $output = str_replace(self::PHP_CLOSE . self::PHP_OPEN, "\n", $output); |
|
662 |
+ |
|
663 |
+ if($this->debug) { |
|
664 |
+ echo '<hr><pre>'; |
|
665 |
+ $lines = preg_split('{\r\n|\n}', htmlentities($output)); |
|
666 |
+ foreach($lines as $i=>$line) |
|
667 |
+ echo ($i+1).'. '.$line."\r\n"; |
|
668 |
+ } |
|
669 |
+ if($this->debug) echo '<hr></pre></pre>'; |
|
670 |
+ |
|
671 |
+ if(!empty($this->errors)) |
|
672 |
+ print_r($this->errors); |
|
673 |
+ |
|
674 |
+ $this->template = $this->dwoo = null; |
|
675 |
+ $tpl = null; |
|
676 |
+ |
|
677 |
+ return $output; |
|
678 |
+ } |
|
679 |
+ |
| 680 |
680 |
/** |
| 681 |
681 |
* sets the scope |
| 682 |
682 |
* |
| 1004 |
1011 |
* @param int $to the ending offset of the parsed area |
| 1005 |
1012 |
* @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default |
| 1006 |
1013 |
* @param string $curBlock the current parser-block being processed |
| 1007 |
|
- * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default |
|
1014 |
+ * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default |
| 1008 |
1015 |
* @return string parsed values |
| 1009 |
|
- */ |
| 1010 |
|
- protected function parseFunction($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
| 1011 |
|
- { |
| 1012 |
|
- $cmdstr = substr($in, $from, $to-$from); |
| 1013 |
|
- |
| 1014 |
|
- if($this->debug) echo 'FUNC FOUND<br />'; |
| 1015 |
|
- |
| 1016 |
|
- $paramsep = ''; |
| 1017 |
|
- if(is_array($parsingParams) || $curBlock != 'root') |
| 1018 |
|
- $ppos1 = false; |
| 1019 |
|
- else |
| 1020 |
|
- $ppos1 = strpos($cmdstr, ' '); |
| 1021 |
|
- $ppos2 = strpos($cmdstr, '('); |
| 1022 |
|
- |
| 1023 |
|
- if($ppos1 !== false && $ppos2 !== false) |
| 1024 |
|
- { |
| 1025 |
|
- $paramspos = min($ppos1, $ppos2); |
| 1026 |
|
- if($paramspos === $ppos2) |
| 1027 |
|
- $paramsep = ')'; |
| 1028 |
|
- } |
| 1029 |
|
- elseif($ppos1 !== false) |
| 1030 |
|
- $paramspos = $ppos1; |
| 1031 |
|
- else |
| 1032 |
|
- { |
| 1033 |
|
- $paramspos = $ppos2; |
| 1034 |
|
- $paramsep = ')'; |
| 1035 |
|
- } |
| 1036 |
|
- |
| 1037 |
|
- $state = 0; |
| 1038 |
|
- |
| 1039 |
|
- if($paramspos === false) |
| 1040 |
|
- { |
| 1041 |
|
- if(strpos($cmdstr,' ')) |
| 1042 |
|
- $func = substr($cmdstr, 0, strpos($cmdstr,' ')); |
| 1043 |
|
- else |
| 1044 |
|
- $func = $cmdstr; |
| 1045 |
|
- $params = array(); |
| 1046 |
|
- |
| 1047 |
|
- if($curBlock === 'namedparam' || $curBlock === 'modifier' || $curBlock === 'function' || $curBlock === 'condition') /* smarty supports unquoted strings so they're supported for named param (smarty syntax), |
| 1048 |
|
- any [a-z0-9_]+ without parenthesis/arguments after it is considered to be a string */ |
| 1049 |
|
- { |
| 1050 |
|
- return $this->parseOthers($in, $from, $to, $parsingParams, $curBlock, $pointer); |
| 1051 |
|
- } |
| 1052 |
|
- } |
| 1053 |
|
- else |
| 1054 |
|
- { |
| 1055 |
|
- $func = substr($cmdstr, 0, $paramspos); |
| 1056 |
|
- $paramstr = substr($cmdstr, $paramspos+1); |
| 1057 |
|
- if(substr($paramstr, -1, 1) === $paramsep) |
| 1058 |
|
- $paramstr = substr($paramstr, 0, -1); |
| 1059 |
|
- |
| 1060 |
|
- if(strlen($paramstr)===0) |
| 1061 |
|
- { |
| 1062 |
|
- $params = array(); |
| 1063 |
|
- $paramstr = ''; |
| 1064 |
|
- } |
| 1065 |
|
- else |
| 1066 |
|
- { |
| 1067 |
|
- $ptr = 0; |
| 1068 |
|
- $params = array(); |
| 1069 |
|
- while($ptr < strlen($paramstr)) |
| 1070 |
|
- { |
| 1071 |
|
- while(true) |
| 1072 |
|
- { |
| 1073 |
|
- if($ptr >= strlen($paramstr)) |
| 1074 |
|
- break 2; |
| 1075 |
|
- |
| 1076 |
|
- if($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr] === ')') |
| 1077 |
|
- { |
| 1078 |
|
- if($this->debug) echo 'PARAM PARSING ENDED, ")" FOUND, POINTER AT '.$ptr.'<br/>'; |
| 1079 |
|
- break 2; |
| 1080 |
|
- } |
| 1081 |
|
- |
| 1082 |
|
- if(($paramstr[$ptr] === ' ' || $paramstr[$ptr] === ',')) |
| 1083 |
|
- $ptr++; |
| 1084 |
|
- else |
| 1085 |
|
- break; |
| 1086 |
|
- } |
| 1087 |
|
- |
| 1088 |
|
- if($this->debug) echo 'FUNC START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
| 1089 |
|
- |
| 1090 |
|
- if($func === 'if' || $func === 'elseif') |
| 1091 |
|
- $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'condition', $ptr); |
| 1092 |
|
- else |
| 1093 |
|
- $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'function', $ptr); |
| 1094 |
|
- |
| 1095 |
|
- if($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.'<br/>'; |
| 1096 |
|
- } |
| 1097 |
|
- $paramstr = substr($paramstr, 0, $ptr); |
| 1098 |
|
- $state = 0; |
| 1099 |
|
- foreach($params as $k=>$p) |
| 1100 |
|
- { |
| 1101 |
|
- if(is_array($p) && is_array($p[1])) |
| 1102 |
|
- $state |= 2; |
| 1103 |
|
- else |
| 1104 |
|
- { |
| 1105 |
|
- if($state === 2 && preg_match('#^(["\'])(.+?)\1$#', $p[0], $m)) |
| 1106 |
|
- $params[$k] = array($m[2], array('true', 'true')); |
| 1107 |
|
- else |
| 1108 |
|
- $state |= 1; |
| 1109 |
|
- } |
| 1110 |
|
- } |
| 1111 |
|
- if($state === 3) |
| 1112 |
|
- $this->triggerError('Function calls can not have both named and un-named parameters', E_USER_ERROR); |
| 1113 |
|
- } |
| 1114 |
|
- } |
| 1115 |
|
- |
| 1116 |
|
- if($pointer !== null) |
| 1117 |
|
- { |
| 1118 |
|
- $pointer += (isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : 0) + strlen($func); |
| 1119 |
|
- if($this->debug) echo 'FUNC ADDS '.((isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : 0) + strlen($func)).' TO POINTER<br/>'; |
| 1120 |
|
- } |
| 1121 |
|
- |
| 1122 |
|
- if($curBlock === 'method' || $func === 'do') |
| 1123 |
|
- { |
| 1124 |
|
- $pluginType = Dwoo::NATIVE_PLUGIN; |
| 1125 |
|
- } |
| 1126 |
|
- else |
| 1127 |
|
- { |
| 1128 |
|
- $pluginType = $this->getPluginType($func); |
| 1129 |
|
- |
| 1130 |
|
- // add block |
| 1131 |
|
- if($pluginType & Dwoo::BLOCK_PLUGIN) |
| 1132 |
|
- { |
| 1133 |
|
- if($curBlock !== 'root' || is_array($parsingParams)) |
| 1134 |
|
- $this->triggerError('Block plugins can not be used as other plugin\'s arguments', E_USER_ERROR); |
| 1135 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 1136 |
|
- return $this->addCustomBlock($func, $params, $state); |
| 1137 |
|
- else |
| 1138 |
|
- return $this->addBlock($func, $params, $state); |
| 1139 |
|
- } |
| 1140 |
|
- elseif($pluginType & Dwoo::SMARTY_BLOCK) |
| 1141 |
|
- { |
| 1142 |
|
- if($curBlock !== 'root' || is_array($parsingParams)) |
| 1143 |
|
- $this->triggerError('Block plugins can not be used as other plugin\'s arguments', E_USER_ERROR); |
| 1144 |
|
- |
| 1145 |
|
- if($state===2) |
| 1146 |
|
- { |
| 1147 |
|
- array_unshift($params, array('__functype', array($pluginType, $pluginType))); |
| 1148 |
|
- array_unshift($params, array('__funcname', array($func, $func))); |
| 1149 |
|
- } |
| 1150 |
|
- else |
| 1151 |
|
- { |
| 1152 |
|
- array_unshift($params, array($pluginType, $pluginType)); |
| 1153 |
|
- array_unshift($params, array($func, $func)); |
| 1154 |
|
- } |
| 1155 |
|
- |
| 1156 |
|
- return $this->addBlock('smartyinterface', $params, $state); |
| 1157 |
|
- } |
| 1158 |
|
- } |
| 1159 |
|
- |
| 1160 |
|
- if($pluginType & Dwoo::NATIVE_PLUGIN || $pluginType & Dwoo::SMARTY_FUNCTION || $pluginType & Dwoo::SMARTY_BLOCK) |
| 1161 |
|
- { |
| 1162 |
|
- $params = $this->mapParams($params, null, $state); |
| 1163 |
|
- } |
| 1164 |
|
- elseif($pluginType & Dwoo::CLASS_PLUGIN) |
| 1165 |
|
- { |
| 1166 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 1167 |
|
- $params = $this->mapParams($params, array($this->customPlugins[$func]['class'], $this->customPlugins[$func]['function']), $state); |
| 1168 |
|
- else |
| 1169 |
|
- $params = $this->mapParams($params, array('DwooPlugin_'.$func, ($pluginType & Dwoo::COMPILABLE_PLUGIN) ? 'compile' : 'process'), $state); |
| 1170 |
|
- } |
| 1171 |
|
- elseif($pluginType & Dwoo::FUNC_PLUGIN) |
| 1172 |
|
- { |
| 1173 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 1174 |
|
- $params = $this->mapParams($params, $this->customPlugins[$func]['callback'], $state); |
| 1175 |
|
- else |
| 1176 |
|
- $params = $this->mapParams($params, 'DwooPlugin_'.$func.(($pluginType & Dwoo::COMPILABLE_PLUGIN) ? '_compile' : ''), $state); |
| 1177 |
|
- } |
| 1178 |
|
- elseif($pluginType & Dwoo::SMARTY_MODIFIER) |
| 1179 |
|
- { |
| 1180 |
|
- $output = 'smarty_modifier_'.$func.'('.implode(', ', $params).')'; |
| 1181 |
|
- } |
| 1182 |
|
- |
| 1183 |
|
- // keep php-syntax-safe values for non-block plugins |
| 1184 |
|
- foreach($params as &$p) |
| 1185 |
|
- $p = $p[0]; |
| 1186 |
|
- if($pluginType & Dwoo::NATIVE_PLUGIN) |
| 1187 |
|
- { |
| 1188 |
|
- if($func === 'do') |
| 1189 |
|
- { |
| 1190 |
|
- if(isset($params['*'])) |
| 1191 |
|
- $output = implode(';', $params['*']).';'; |
| 1192 |
|
- else |
| 1193 |
|
- $output = ''; |
| 1194 |
|
- |
| 1195 |
|
- if(is_array($parsingParams) || $curBlock !== 'root') |
| 1196 |
|
- return $this->triggerError('Do can not be used inside another function or block', E_USER_ERROR); |
| 1197 |
|
- else |
| 1198 |
|
- return self::PHP_OPEN.$output.self::PHP_CLOSE; |
| 1199 |
|
- } |
| 1200 |
|
- else |
| 1201 |
|
- { |
| 1202 |
|
- if(isset($params['*'])) |
| 1203 |
|
- $output = $func.'('.implode(', ', $params['*']).')'; |
| 1204 |
|
- else |
| 1205 |
|
- $output = $func.'()'; |
| 1206 |
|
- } |
| 1207 |
|
- } |
| 1208 |
|
- elseif($pluginType & Dwoo::FUNC_PLUGIN) |
| 1209 |
|
- { |
| 1210 |
|
- if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
| 1211 |
|
- { |
| 1212 |
|
- $funcCompiler = 'DwooPlugin_'.$func.'_compile'; |
| 1213 |
|
- array_unshift($params, $this); |
| 1214 |
|
- $output = call_user_func_array($funcCompiler, $params); |
| 1215 |
|
- } |
| 1216 |
|
- else |
| 1217 |
|
- { |
| 1218 |
|
- array_unshift($params, '$this'); |
| 1219 |
|
- $params = $this->implode_r($params); |
| 1220 |
|
- |
| 1221 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 1222 |
|
- { |
| 1223 |
|
- $callback = $this->customPlugins[$func]['callback']; |
| 1224 |
|
- $output = 'call_user_func(\''.$callback.'\', '.$params.')'; |
| 1225 |
|
- } |
| 1226 |
|
- else |
| 1227 |
|
- $output = 'DwooPlugin_'.$func.'('.$params.')'; |
| 1228 |
|
- } |
| 1229 |
|
- } |
| 1230 |
|
- elseif($pluginType & Dwoo::CLASS_PLUGIN) |
| 1231 |
|
- { |
| 1232 |
|
- if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
| 1233 |
|
- { |
| 1234 |
|
- $funcCompiler = array('DwooPlugin_'.$func, 'compile'); |
| 1235 |
|
- array_unshift($params, $this); |
| 1236 |
|
- $output = call_user_func_array($funcCompiler, $params); |
| 1237 |
|
- } |
| 1238 |
|
- else |
| 1239 |
|
- { |
| 1240 |
|
- $params = $this->implode_r($params); |
| 1241 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 1242 |
|
- { |
| 1243 |
|
- $callback = $this->customPlugins[$func]['callback']; |
| 1244 |
|
- if(!is_array($callback)) |
| 1245 |
|
- { |
| 1246 |
|
- if(($ref = new ReflectionMethod($callback, 'process')) && $ref->isStatic()) |
| 1247 |
|
- $output = 'call_user_func(array(\''.$callback.'\', \'process\'), '.$params.')'; |
| 1248 |
|
- else |
| 1249 |
|
- $output = 'call_user_func(array($this->getObjectPlugin(\''.$callback.'\'), \'process\'), '.$params.')'; |
| 1250 |
|
- } |
| 1251 |
|
- elseif(is_object($callback[0])) |
| 1252 |
|
- $output = 'call_user_func(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), '.$params.')'; |
| 1253 |
|
- elseif(($ref = new ReflectionMethod($callback[0], $callback[1])) && $ref->isStatic()) |
| 1254 |
|
- $output = 'call_user_func(array(\''.$callback[0].'\', \''.$callback[1].'\'), '.$params.')'; |
| 1255 |
|
- else |
| 1256 |
|
- $output = 'call_user_func(array($this->getObjectPlugin(\''.$callback[0].'\'), \''.$callback[1].'\'), '.$params.')'; |
| 1257 |
|
- } |
| 1258 |
|
- else |
| 1259 |
|
- $output = '$this->classCall(\''.$func.'\', array('.$params.'))'; |
| 1260 |
|
- } |
| 1261 |
|
- } |
| 1262 |
|
- elseif($pluginType & Dwoo::SMARTY_FUNCTION) |
| 1263 |
|
- { |
| 1264 |
|
- $params = $this->implode_r($params['*'], true); |
| 1265 |
|
- |
| 1266 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 1267 |
|
- { |
| 1268 |
|
- $callback = $this->customPlugins[$func]['callback']; |
| 1269 |
|
- if(is_array($callback)) |
| 1270 |
|
- { |
| 1271 |
|
- if(is_object($callback[0])) |
| 1272 |
|
- $output = 'call_user_func_array(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array(array('.$params.'), $this))'; |
| 1273 |
|
- else |
| 1274 |
|
- $output = 'call_user_func_array(array(\''.$callback[0].'\', \''.$callback[1].'\'), array(array('.$params.'), $this))'; |
| 1275 |
|
- } |
| 1276 |
|
- else |
| 1277 |
|
- $output = $callback.'(array('.$params.'), $this)'; |
| 1278 |
|
- } |
| 1279 |
|
- else |
| 1280 |
|
- $output = 'smarty_function_'.$func.'(array('.$params.'), $this)'; |
| 1281 |
|
- } |
| 1282 |
|
- |
| 1283 |
|
- if(is_array($parsingParams)) |
| 1284 |
|
- { |
| 1285 |
|
- $parsingParams[] = array($output, $output); |
| 1286 |
|
- return $parsingParams; |
| 1287 |
|
- } |
| 1288 |
|
- elseif($curBlock === 'namedparam') |
| 1289 |
|
- return array($output, $output); |
| 1290 |
|
- elseif($curBlock === 'method') |
| 1291 |
|
- return $output; |
| 1292 |
|
- else |
| 1293 |
|
- return self::PHP_OPEN.'echo '.$output.';'.self::PHP_CLOSE; |
| 1294 |
|
- } |
|
1016 |
+ */ |
|
1017 |
+ protected function parseFunction($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
|
1018 |
+ { |
|
1019 |
+ $cmdstr = substr($in, $from, $to-$from); |
|
1020 |
+ |
|
1021 |
+ if($this->debug) echo 'FUNC FOUND<br />'; |
|
1022 |
+ |
|
1023 |
+ $paramsep = ''; |
|
1024 |
+ if(is_array($parsingParams) || $curBlock != 'root') |
|
1025 |
+ $ppos1 = false; |
|
1026 |
+ else |
|
1027 |
+ $ppos1 = strpos($cmdstr, ' '); |
|
1028 |
+ $ppos2 = strpos($cmdstr, '('); |
|
1029 |
+ |
|
1030 |
+ if($ppos1 !== false && $ppos2 !== false) |
|
1031 |
+ { |
|
1032 |
+ $paramspos = min($ppos1, $ppos2); |
|
1033 |
+ if($paramspos === $ppos2) |
|
1034 |
+ $paramsep = ')'; |
|
1035 |
+ } |
|
1036 |
+ elseif($ppos1 !== false) |
|
1037 |
+ $paramspos = $ppos1; |
|
1038 |
+ else |
|
1039 |
+ { |
|
1040 |
+ $paramspos = $ppos2; |
|
1041 |
+ $paramsep = ')'; |
|
1042 |
+ } |
|
1043 |
+ |
|
1044 |
+ $state = 0; |
|
1045 |
+ |
|
1046 |
+ if($paramspos === false) |
|
1047 |
+ { |
|
1048 |
+ if(strpos($cmdstr,' ')) |
|
1049 |
+ $func = substr($cmdstr, 0, strpos($cmdstr,' ')); |
|
1050 |
+ else |
|
1051 |
+ $func = $cmdstr; |
|
1052 |
+ $params = array(); |
|
1053 |
+ |
|
1054 |
+ if($curBlock !== 'root') |
|
1055 |
+ { |
|
1056 |
+ return $this->parseOthers($in, $from, $to, $parsingParams, $curBlock, $pointer); |
|
1057 |
+ } |
|
1058 |
+ } |
|
1059 |
+ else |
|
1060 |
+ { |
|
1061 |
+ $func = substr($cmdstr, 0, $paramspos); |
|
1062 |
+ $paramstr = substr($cmdstr, $paramspos+1); |
|
1063 |
+ if(substr($paramstr, -1, 1) === $paramsep) |
|
1064 |
+ $paramstr = substr($paramstr, 0, -1); |
|
1065 |
+ |
|
1066 |
+ if(strlen($paramstr)===0) |
|
1067 |
+ { |
|
1068 |
+ $params = array(); |
|
1069 |
+ $paramstr = ''; |
|
1070 |
+ } |
|
1071 |
+ else |
|
1072 |
+ { |
|
1073 |
+ $ptr = 0; |
|
1074 |
+ $params = array(); |
|
1075 |
+ while($ptr < strlen($paramstr)) |
|
1076 |
+ { |
|
1077 |
+ while(true) |
|
1078 |
+ { |
|
1079 |
+ if($ptr >= strlen($paramstr)) |
|
1080 |
+ break 2; |
|
1081 |
+ |
|
1082 |
+ if($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr] === ')') |
|
1083 |
+ { |
|
1084 |
+ if($this->debug) echo 'PARAM PARSING ENDED, ")" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
1085 |
+ break 2; |
|
1086 |
+ } |
|
1087 |
+ |
|
1088 |
+ if(($paramstr[$ptr] === ' ' || $paramstr[$ptr] === ',')) |
|
1089 |
+ $ptr++; |
|
1090 |
+ else |
|
1091 |
+ break; |
|
1092 |
+ } |
|
1093 |
+ |
|
1094 |
+ if($this->debug) echo 'FUNC START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
|
1095 |
+ |
|
1096 |
+ if($func === 'if' || $func === 'elseif') |
|
1097 |
+ $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'condition', $ptr); |
|
1098 |
+ else |
|
1099 |
+ $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'function', $ptr); |
|
1100 |
+ |
|
1101 |
+ if($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.'<br/>'; |
|
1102 |
+ } |
|
1103 |
+ $paramstr = substr($paramstr, 0, $ptr); |
|
1104 |
+ $state = 0; |
|
1105 |
+ foreach($params as $k=>$p) |
|
1106 |
+ { |
|
1107 |
+ if(is_array($p) && is_array($p[1])) |
|
1108 |
+ $state |= 2; |
|
1109 |
+ else |
|
1110 |
+ { |
|
1111 |
+ if($state === 2 && preg_match('#^(["\'])(.+?)\1$#', $p[0], $m)) |
|
1112 |
+ $params[$k] = array($m[2], array('true', 'true')); |
|
1113 |
+ else |
|
1114 |
+ $state |= 1; |
|
1115 |
+ } |
|
1116 |
+ } |
|
1117 |
+ if($state === 3) |
|
1118 |
+ $this->triggerError('Function calls can not have both named and un-named parameters', E_USER_ERROR); |
|
1119 |
+ } |
|
1120 |
+ } |
|
1121 |
+ |
|
1122 |
+ if($pointer !== null) |
|
1123 |
+ { |
|
1124 |
+ $pointer += (isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : 0) + strlen($func); |
|
1125 |
+ if($this->debug) echo 'FUNC ADDS '.((isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : 0) + strlen($func)).' TO POINTER<br/>'; |
|
1126 |
+ } |
|
1127 |
+ |
|
1128 |
+ if($curBlock === 'method' || $func === 'do') |
|
1129 |
+ { |
|
1130 |
+ $pluginType = Dwoo::NATIVE_PLUGIN; |
|
1131 |
+ } |
|
1132 |
+ else |
|
1133 |
+ { |
|
1134 |
+ $pluginType = $this->getPluginType($func); |
|
1135 |
+ |
|
1136 |
+ // add block |
|
1137 |
+ if($pluginType & Dwoo::BLOCK_PLUGIN) |
|
1138 |
+ { |
|
1139 |
+ if($curBlock !== 'root' || is_array($parsingParams)) |
|
1140 |
+ $this->triggerError('Block plugins can not be used as other plugin\'s arguments', E_USER_ERROR); |
|
1141 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
1142 |
+ return $this->addCustomBlock($func, $params, $state); |
|
1143 |
+ else |
|
1144 |
+ return $this->addBlock($func, $params, $state); |
|
1145 |
+ } |
|
1146 |
+ elseif($pluginType & Dwoo::SMARTY_BLOCK) |
|
1147 |
+ { |
|
1148 |
+ if($curBlock !== 'root' || is_array($parsingParams)) |
|
1149 |
+ $this->triggerError('Block plugins can not be used as other plugin\'s arguments', E_USER_ERROR); |
|
1150 |
+ |
|
1151 |
+ if($state===2) |
|
1152 |
+ { |
|
1153 |
+ array_unshift($params, array('__functype', array($pluginType, $pluginType))); |
|
1154 |
+ array_unshift($params, array('__funcname', array($func, $func))); |
|
1155 |
+ } |
|
1156 |
+ else |
|
1157 |
+ { |
|
1158 |
+ array_unshift($params, array($pluginType, $pluginType)); |
|
1159 |
+ array_unshift($params, array($func, $func)); |
|
1160 |
+ } |
|
1161 |
+ |
|
1162 |
+ return $this->addBlock('smartyinterface', $params, $state); |
|
1163 |
+ } |
|
1164 |
+ } |
|
1165 |
+ |
|
1166 |
+ if($pluginType & Dwoo::NATIVE_PLUGIN || $pluginType & Dwoo::SMARTY_FUNCTION || $pluginType & Dwoo::SMARTY_BLOCK) |
|
1167 |
+ { |
|
1168 |
+ $params = $this->mapParams($params, null, $state); |
|
1169 |
+ } |
|
1170 |
+ elseif($pluginType & Dwoo::CLASS_PLUGIN) |
|
1171 |
+ { |
|
1172 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
1173 |
+ $params = $this->mapParams($params, array($this->customPlugins[$func]['class'], $this->customPlugins[$func]['function']), $state); |
|
1174 |
+ else |
|
1175 |
+ $params = $this->mapParams($params, array('DwooPlugin_'.$func, ($pluginType & Dwoo::COMPILABLE_PLUGIN) ? 'compile' : 'process'), $state); |
|
1176 |
+ } |
|
1177 |
+ elseif($pluginType & Dwoo::FUNC_PLUGIN) |
|
1178 |
+ { |
|
1179 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
1180 |
+ $params = $this->mapParams($params, $this->customPlugins[$func]['callback'], $state); |
|
1181 |
+ else |
|
1182 |
+ $params = $this->mapParams($params, 'DwooPlugin_'.$func.(($pluginType & Dwoo::COMPILABLE_PLUGIN) ? '_compile' : ''), $state); |
|
1183 |
+ } |
|
1184 |
+ elseif($pluginType & Dwoo::SMARTY_MODIFIER) |
|
1185 |
+ { |
|
1186 |
+ $output = 'smarty_modifier_'.$func.'('.implode(', ', $params).')'; |
|
1187 |
+ } |
|
1188 |
+ |
|
1189 |
+ // keep php-syntax-safe values for non-block plugins |
|
1190 |
+ foreach($params as &$p) |
|
1191 |
+ $p = $p[0]; |
|
1192 |
+ if($pluginType & Dwoo::NATIVE_PLUGIN) |
|
1193 |
+ { |
|
1194 |
+ if($func === 'do') |
|
1195 |
+ { |
|
1196 |
+ if(isset($params['*'])) |
|
1197 |
+ $output = implode(';', $params['*']).';'; |
|
1198 |
+ else |
|
1199 |
+ $output = ''; |
|
1200 |
+ |
|
1201 |
+ if(is_array($parsingParams) || $curBlock !== 'root') |
|
1202 |
+ return $this->triggerError('Do can not be used inside another function or block', E_USER_ERROR); |
|
1203 |
+ else |
|
1204 |
+ return self::PHP_OPEN.$output.self::PHP_CLOSE; |
|
1205 |
+ } |
|
1206 |
+ else |
|
1207 |
+ { |
|
1208 |
+ if(isset($params['*'])) |
|
1209 |
+ $output = $func.'('.implode(', ', $params['*']).')'; |
|
1210 |
+ else |
|
1211 |
+ $output = $func.'()'; |
|
1212 |
+ } |
|
1213 |
+ } |
|
1214 |
+ elseif($pluginType & Dwoo::FUNC_PLUGIN) |
|
1215 |
+ { |
|
1216 |
+ if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
|
1217 |
+ { |
|
1218 |
+ $funcCompiler = 'DwooPlugin_'.$func.'_compile'; |
|
1219 |
+ array_unshift($params, $this); |
|
1220 |
+ $output = call_user_func_array($funcCompiler, $params); |
|
1221 |
+ } |
|
1222 |
+ else |
|
1223 |
+ { |
|
1224 |
+ array_unshift($params, '$this'); |
|
1225 |
+ $params = $this->implode_r($params); |
|
1226 |
+ |
|
1227 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
1228 |
+ { |
|
1229 |
+ $callback = $this->customPlugins[$func]['callback']; |
|
1230 |
+ $output = 'call_user_func(\''.$callback.'\', '.$params.')'; |
|
1231 |
+ } |
|
1232 |
+ else |
|
1233 |
+ $output = 'DwooPlugin_'.$func.'('.$params.')'; |
|
1234 |
+ } |
|
1235 |
+ } |
|
1236 |
+ elseif($pluginType & Dwoo::CLASS_PLUGIN) |
|
1237 |
+ { |
|
1238 |
+ if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
|
1239 |
+ { |
|
1240 |
+ $funcCompiler = array('DwooPlugin_'.$func, 'compile'); |
|
1241 |
+ array_unshift($params, $this); |
|
1242 |
+ $output = call_user_func_array($funcCompiler, $params); |
|
1243 |
+ } |
|
1244 |
+ else |
|
1245 |
+ { |
|
1246 |
+ $params = $this->implode_r($params); |
|
1247 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
1248 |
+ { |
|
1249 |
+ $callback = $this->customPlugins[$func]['callback']; |
|
1250 |
+ if(!is_array($callback)) |
|
1251 |
+ { |
|
1252 |
+ if(($ref = new ReflectionMethod($callback, 'process')) && $ref->isStatic()) |
|
1253 |
+ $output = 'call_user_func(array(\''.$callback.'\', \'process\'), '.$params.')'; |
|
1254 |
+ else |
|
1255 |
+ $output = 'call_user_func(array($this->getObjectPlugin(\''.$callback.'\'), \'process\'), '.$params.')'; |
|
1256 |
+ } |
|
1257 |
+ elseif(is_object($callback[0])) |
|
1258 |
+ $output = 'call_user_func(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), '.$params.')'; |
|
1259 |
+ elseif(($ref = new ReflectionMethod($callback[0], $callback[1])) && $ref->isStatic()) |
|
1260 |
+ $output = 'call_user_func(array(\''.$callback[0].'\', \''.$callback[1].'\'), '.$params.')'; |
|
1261 |
+ else |
|
1262 |
+ $output = 'call_user_func(array($this->getObjectPlugin(\''.$callback[0].'\'), \''.$callback[1].'\'), '.$params.')'; |
|
1263 |
+ } |
|
1264 |
+ else |
|
1265 |
+ $output = '$this->classCall(\''.$func.'\', array('.$params.'))'; |
|
1266 |
+ } |
|
1267 |
+ } |
|
1268 |
+ elseif($pluginType & Dwoo::SMARTY_FUNCTION) |
|
1269 |
+ { |
|
1270 |
+ $params = $this->implode_r($params['*'], true); |
|
1271 |
+ |
|
1272 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
1273 |
+ { |
|
1274 |
+ $callback = $this->customPlugins[$func]['callback']; |
|
1275 |
+ if(is_array($callback)) |
|
1276 |
+ { |
|
1277 |
+ if(is_object($callback[0])) |
|
1278 |
+ $output = 'call_user_func_array(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array(array('.$params.'), $this))'; |
|
1279 |
+ else |
|
1280 |
+ $output = 'call_user_func_array(array(\''.$callback[0].'\', \''.$callback[1].'\'), array(array('.$params.'), $this))'; |
|
1281 |
+ } |
|
1282 |
+ else |
|
1283 |
+ $output = $callback.'(array('.$params.'), $this)'; |
|
1284 |
+ } |
|
1285 |
+ else |
|
1286 |
+ $output = 'smarty_function_'.$func.'(array('.$params.'), $this)'; |
|
1287 |
+ } |
|
1288 |
+ |
|
1289 |
+ if(is_array($parsingParams)) |
|
1290 |
+ { |
|
1291 |
+ $parsingParams[] = array($output, $output); |
|
1292 |
+ return $parsingParams; |
|
1293 |
+ } |
|
1294 |
+ elseif($curBlock === 'namedparam') |
|
1295 |
+ return array($output, $output); |
|
1296 |
+ else |
|
1297 |
+ return $output; |
|
1298 |
+ } |
| 1295 |
1299 |
|
| 1296 |
1300 |
/** |
| 1297 |
1301 |
* parses a string |
| 1303 |
1307 |
* @param string $curBlock the current parser-block being processed |
| 1304 |
1308 |
* @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default |
| 1305 |
1309 |
* @return string parsed values |
| 1306 |
|
- */ |
| 1307 |
|
- protected function parseString($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
| 1308 |
|
- { |
| 1309 |
|
- $substr = substr($in, $from, $to-$from); |
| 1310 |
|
- $first = $substr[0]; |
| 1311 |
|
- |
| 1312 |
|
- if($this->debug) echo 'STRING FOUND<br />'; |
| 1313 |
|
- $strend = false; |
| 1314 |
|
- $o = $from+1; |
| 1315 |
|
- while($strend===false) |
| 1316 |
|
- { |
| 1317 |
|
- $strend = strpos($in, $first, $o); |
| 1318 |
|
- if($strend === false) |
| 1319 |
|
- { |
| 1320 |
|
- $this->triggerError('Unfinished string in : <strong>'.substr($in, 0, $from).'<u>'.substr($in, $from, $to-$from).'</u>'.substr($in, $to).'</strong>', E_USER_ERROR); |
| 1321 |
|
- } |
| 1322 |
|
- if(substr($in, $strend-1, 1) === '\\') |
| 1323 |
|
- { |
| 1324 |
|
- $o = $strend+1; |
| 1325 |
|
- $strend = false; |
| 1326 |
|
- } |
| 1327 |
|
- } |
| 1328 |
|
- if($curBlock !== 'modifier' && substr($in, $strend+1, 1)==='|') |
| 1329 |
|
- { |
| 1330 |
|
- $strend = strpos($in, ' ', $strend+1); |
| 1331 |
|
- if($strend===false) |
| 1332 |
|
- $strend = strlen($in)-1; |
| 1333 |
|
- } |
| 1334 |
|
- $srcOutput = substr($in, $from, $strend+1-$from); |
| 1335 |
|
- |
| 1336 |
|
- if($pointer !== null) |
| 1337 |
|
- $pointer += strlen($srcOutput); |
| 1338 |
|
- |
| 1339 |
|
- $output = $this->replaceStringVars($srcOutput, $first); |
| 1340 |
|
- |
| 1341 |
|
- // handle modifiers |
| 1342 |
|
- if($curBlock !== 'modifier' && preg_match('#(.+?)((?:\|(?:@?[a-z0-9_]+(?::[^\s]*)*))+)#i', $output, $match)) |
| 1343 |
|
- { |
| 1344 |
|
- $modstr = $match[2]; |
| 1345 |
|
- $output = $match[1]; |
| 1346 |
|
- $strend += strlen($match[1]); |
| 1347 |
|
- |
| 1348 |
|
- if($curBlock === 'root' && substr($modstr, -1) === '}') |
| 1349 |
|
- $modstr = substr($modstr, 0, -1); |
| 1350 |
|
- $output = $this->replaceModifiers(array(null, null, $output, $modstr), 'string'); |
| 1351 |
|
- } |
| 1352 |
|
- |
| 1353 |
|
- if($curBlock !== 'namedparam' && $curBlock !== 'modifier' && $curBlock !== 'function' && $curBlock !== 'condition' && strlen(substr($in, 0, $to)) > $strend+1) |
| 1354 |
|
- $output .= $this->parse($in, $strend+1, $to, $parsingParams); |
| 1355 |
|
- |
| 1356 |
|
- if(is_array($parsingParams)) |
| 1357 |
|
- { |
| 1358 |
|
- $parsingParams[] = array($output, substr($srcOutput,1,-1)); |
| 1359 |
|
- return $parsingParams; |
| 1360 |
|
- } |
| 1361 |
|
- elseif($curBlock === 'namedparam') |
| 1362 |
|
- return array($output, substr($srcOutput,1,-1)); |
| 1363 |
|
- elseif($curBlock === 'root') |
| 1364 |
|
- return self::PHP_OPEN.'echo '.$output.';'.self::PHP_CLOSE; |
| 1365 |
|
- else |
| 1366 |
|
- return $output; |
| 1367 |
|
- } |
| 1368 |
|
- |
| 1369 |
|
- /** |
| 1370 |
|
- * parses a constant |
| 1371 |
|
- * |
| 1372 |
|
- * @param string $in the string within which we must parse something |
| 1373 |
|
- * @param int $from the starting offset of the parsed area |
| 1374 |
|
- * @param int $to the ending offset of the parsed area |
| 1375 |
|
- * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default |
| 1376 |
|
- * @param string $curBlock the current parser-block being processed |
| 1377 |
|
- * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default |
| 1378 |
|
- * @return string parsed values |
| 1379 |
|
- */ |
| 1380 |
|
- protected function parseConst($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
| 1381 |
|
- { |
| 1382 |
|
- $substr = substr($in, $from, $to-$from); |
| 1383 |
|
- |
| 1384 |
|
- if($this->debug) |
| 1385 |
|
- echo 'CONST FOUND : '.$substr.'<br />'; |
| 1386 |
|
- |
| 1387 |
|
- if(!preg_match('#^%([a-z0-9_:]+)#i', $substr, $m)) |
| 1388 |
|
- $this->triggerError('Invalid constant', E_USER_ERROR); |
| 1389 |
|
- |
| 1390 |
|
- $output = $this->parseConstKey($m[1], $curBlock); |
| 1391 |
|
- |
| 1392 |
|
- if(is_array($parsingParams)) |
| 1393 |
|
- { |
| 1394 |
|
- $parsingParams[] = array($output, $m[1]); |
| 1395 |
|
- return $parsingParams; |
| 1396 |
|
- } |
| 1397 |
|
- elseif($curBlock === 'namedparam') |
| 1398 |
|
- return array($output, $m[1]); |
| 1399 |
|
- elseif($curBlock === 'root') |
| 1400 |
|
- return self::PHP_OPEN.'echo '.$output.';'.self::PHP_CLOSE; |
| 1401 |
|
- else |
| 1402 |
|
- return $output; |
| 1403 |
|
- } |
| 1404 |
|
- |
| 1405 |
|
- /** |
| 1406 |
|
- * parses a constant |
| 1407 |
|
- * |
| 1408 |
|
- * @param string $key the constant to parse |
| 1409 |
|
- * @param string $curBlock the current parser-block being processed |
| 1410 |
|
- * @return string parsed constant |
| 1411 |
|
- */ |
| 1412 |
|
- protected function parseConstKey($key, $curBlock) |
| 1413 |
|
- { |
| 1414 |
|
- if($this->securityPolicy !== null && $this->securityPolicy->getConstantHandling() === DwooSecurityPolicy::CONST_DISALLOW) |
| 1415 |
|
- return 'null'; |
| 1416 |
|
- |
| 1417 |
|
- if($curBlock !== 'root') |
| 1418 |
|
- $output = '(defined("'.$key.'") ? '.$key.' : null)'; |
| 1419 |
|
- else |
| 1420 |
|
- $output = $key; |
| 1421 |
|
- |
| 1422 |
|
- return $output; |
| 1423 |
|
- } |
|
1310 |
+ */ |
|
1311 |
+ protected function parseString($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
|
1312 |
+ { |
|
1313 |
+ $substr = substr($in, $from, $to-$from); |
|
1314 |
+ $first = $substr[0]; |
|
1315 |
+ |
|
1316 |
+ if($this->debug) echo 'STRING FOUND<br />'; |
|
1317 |
+ $strend = false; |
|
1318 |
+ $o = $from+1; |
|
1319 |
+ while($strend===false) |
|
1320 |
+ { |
|
1321 |
+ $strend = strpos($in, $first, $o); |
|
1322 |
+ if($strend === false) |
|
1323 |
+ { |
|
1324 |
+ $this->triggerError('Unfinished string in : <strong>'.substr($in, 0, $from).'<u>'.substr($in, $from, $to-$from).'</u>'.substr($in, $to).'</strong>', E_USER_ERROR); |
|
1325 |
+ } |
|
1326 |
+ if(substr($in, $strend-1, 1) === '\\') |
|
1327 |
+ { |
|
1328 |
+ $o = $strend+1; |
|
1329 |
+ $strend = false; |
|
1330 |
+ } |
|
1331 |
+ } |
|
1332 |
+ if($curBlock !== 'modifier' && substr($in, $strend+1, 1)==='|') |
|
1333 |
+ { |
|
1334 |
+ $strend = strpos($in, ' ', $strend+1); |
|
1335 |
+ if($strend===false) |
|
1336 |
+ $strend = strlen($in)-1; |
|
1337 |
+ } |
|
1338 |
+ $srcOutput = substr($in, $from, $strend+1-$from); |
|
1339 |
+ |
|
1340 |
+ if($pointer !== null) |
|
1341 |
+ $pointer += strlen($srcOutput); |
|
1342 |
+ |
|
1343 |
+ $output = $this->replaceStringVars($srcOutput, $first); |
|
1344 |
+ |
|
1345 |
+ // handle modifiers |
|
1346 |
+ if($curBlock !== 'modifier' && preg_match('#(.+?)((?:\|(?:@?[a-z0-9_]+(?::[^\s]*)*))+)#i', $output, $match)) |
|
1347 |
+ { |
|
1348 |
+ $modstr = $match[2]; |
|
1349 |
+ $output = $match[1]; |
|
1350 |
+ $strend += strlen($match[1]); |
|
1351 |
+ |
|
1352 |
+ if($curBlock === 'root' && substr($modstr, -1) === '}') |
|
1353 |
+ $modstr = substr($modstr, 0, -1); |
|
1354 |
+ $output = $this->replaceModifiers(array(null, null, $output, $modstr), 'string'); |
|
1355 |
+ } |
|
1356 |
+ |
|
1357 |
+ if($curBlock !== 'namedparam' && $curBlock !== 'modifier' && $curBlock !== 'function' && $curBlock !== 'condition' && strlen(substr($in, 0, $to)) > $strend+1) |
|
1358 |
+ $output .= $this->parse($in, $strend+1, $to, $parsingParams); |
|
1359 |
+ |
|
1360 |
+ if(is_array($parsingParams)) |
|
1361 |
+ { |
|
1362 |
+ $parsingParams[] = array($output, substr($srcOutput,1,-1)); |
|
1363 |
+ return $parsingParams; |
|
1364 |
+ } |
|
1365 |
+ elseif($curBlock === 'namedparam') |
|
1366 |
+ return array($output, substr($srcOutput,1,-1)); |
|
1367 |
+ else |
|
1368 |
+ return $output; |
|
1369 |
+ } |
|
1370 |
+ |
|
1371 |
+ /** |
|
1372 |
+ * parses a constant |
|
1373 |
+ * |
|
1374 |
+ * @param string $in the string within which we must parse something |
|
1375 |
+ * @param int $from the starting offset of the parsed area |
|
1376 |
+ * @param int $to the ending offset of the parsed area |
|
1377 |
+ * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default |
|
1378 |
+ * @param string $curBlock the current parser-block being processed |
|
1379 |
+ * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default |
|
1380 |
+ * @return string parsed values |
|
1381 |
+ */ |
|
1382 |
+ protected function parseConst($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
|
1383 |
+ { |
|
1384 |
+ $substr = substr($in, $from, $to-$from); |
|
1385 |
+ |
|
1386 |
+ if($this->debug) |
|
1387 |
+ echo 'CONST FOUND : '.$substr.'<br />'; |
|
1388 |
+ |
|
1389 |
+ if(!preg_match('#^%([a-z0-9_:]+)#i', $substr, $m)) |
|
1390 |
+ $this->triggerError('Invalid constant', E_USER_ERROR); |
|
1391 |
+ |
|
1392 |
+ $output = $this->parseConstKey($m[1], $curBlock); |
|
1393 |
+ |
|
1394 |
+ if(is_array($parsingParams)) |
|
1395 |
+ { |
|
1396 |
+ $parsingParams[] = array($output, $m[1]); |
|
1397 |
+ return $parsingParams; |
|
1398 |
+ } |
|
1399 |
+ elseif($curBlock === 'namedparam') |
|
1400 |
+ return array($output, $m[1]); |
|
1401 |
+ else |
|
1402 |
+ return $output; |
|
1403 |
+ } |
|
1404 |
+ |
|
1405 |
+ /** |
|
1406 |
+ * parses a constant |
|
1407 |
+ * |
|
1408 |
+ * @param string $key the constant to parse |
|
1409 |
+ * @param string $curBlock the current parser-block being processed |
|
1410 |
+ * @return string parsed constant |
|
1411 |
+ */ |
|
1412 |
+ protected function parseConstKey($key, $curBlock) |
|
1413 |
+ { |
|
1414 |
+ if($this->securityPolicy !== null && $this->securityPolicy->getConstantHandling() === DwooSecurityPolicy::CONST_DISALLOW) |
|
1415 |
+ return 'null'; |
|
1416 |
+ |
|
1417 |
+ if($curBlock !== 'root') |
|
1418 |
+ $output = '(defined("'.$key.'") ? '.$key.' : null)'; |
|
1419 |
+ else |
|
1420 |
+ $output = $key; |
|
1421 |
+ |
|
1422 |
+ return $output; |
|
1423 |
+ } |
| 1424 |
1424 |
|
| 1425 |
1425 |
/** |
| 1426 |
1426 |
* parses a variable |
| 1432 |
1432 |
* @param string $curBlock the current parser-block being processed |
| 1433 |
1433 |
* @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default |
| 1434 |
1434 |
* @return string parsed values |
| 1435 |
|
- */ |
| 1436 |
|
- protected function parseVar($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
| 1437 |
|
- { |
| 1438 |
|
- $substr = substr($in, $from, $to-$from); |
| 1439 |
|
- |
| 1440 |
|
- if(preg_match('#(\$?\.?[a-z0-9_:]+(?:(?:(?:\.|->)(?:[a-z0-9_:]+|(?R))|\[(?:[a-z0-9_:]+|(?R))\]))*)' . // var key |
| 1441 |
|
- ($curBlock==='root' || $curBlock==='function' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='expression' ? '(\([^)]*?\)(?:->[a-z0-9_]+(?:\([^)]*?\))?)*)?' : '()') . // method call |
| 1442 |
|
- ($curBlock==='root' || $curBlock==='function' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='string' ? '((?:(?:[+/*%=-])(?:(?<!=)=?-?[$%][a-z0-9.[\]>_:-]+(?:\([^)]*\))?|(?<!=)=?-?[0-9.,]*|[+-]))*)':'()') . // simple math expressions |
| 1443 |
|
- ($curBlock!=='modifier'? '((?:\|(?:@?[a-z0-9_]+(?:(?::("|\').+?\5|:[^\s`"\']*))*))+)?':'(())') . // modifiers |
| 1444 |
|
- '#i', $substr, $match)) |
| 1445 |
|
- { |
| 1446 |
|
- $key = substr($match[1],1); |
| 1447 |
|
- |
| 1448 |
|
- $matchedLength = strlen($match[0]); |
| 1449 |
|
- $hasModifiers = isset($match[4]) && !empty($match[4]); |
| 1450 |
|
- $hasExpression = isset($match[3]) && !empty($match[3]); |
| 1451 |
|
- $hasMethodCall = isset($match[2]) && !empty($match[2]); |
| 1452 |
|
- |
| 1453 |
|
- if($hasMethodCall) |
| 1454 |
|
- { |
| 1455 |
|
- $key = substr($match[1], 1, strrpos($match[1], '->')-1); |
| 1456 |
|
- $methodCall = substr($match[1], strrpos($match[1], '->')) . $match[2]; |
| 1457 |
|
- } |
| 1458 |
|
- |
| 1459 |
|
- if($pointer !== null) |
| 1460 |
|
- $pointer += $matchedLength; |
| 1461 |
|
- |
| 1462 |
|
- // replace useless brackets by dot accessed vars |
| 1463 |
|
- $key = preg_replace('#\[([^\[.->]+)\]#', '.$1', $key); |
| 1464 |
|
- |
| 1465 |
|
- // prevent $foo->$bar calls because it doesn't seem worth the trouble |
| 1466 |
|
- if(strpos($key, '->$') !== false) |
| 1467 |
|
- $this->triggerError('You can not access an object\'s property using a variable name.', E_USER_ERROR); |
| 1468 |
|
- |
| 1469 |
|
- if($this->debug) |
| 1470 |
|
- { |
| 1471 |
|
- if($hasMethodCall) |
| 1472 |
|
- echo 'METHOD CALL FOUND : $'.$key.$methodCall.'<br />'; |
| 1473 |
|
- else |
| 1474 |
|
- echo 'VAR FOUND : $'.$key.'<br />'; |
| 1475 |
|
- } |
| 1476 |
|
- |
| 1477 |
|
- $key = str_replace('"','\\"',$key); |
| 1478 |
|
- |
| 1479 |
|
- $cnt=substr_count($key, '$'); |
| 1480 |
|
- if($cnt > 0) |
| 1481 |
|
- { |
| 1482 |
|
- $uid = 0; |
| 1483 |
|
- $parsed = array($uid => ''); |
| 1484 |
|
- $current =& $parsed; |
| 1485 |
|
- $curTxt =& $parsed[$uid++]; |
| 1486 |
|
- $tree = array(); |
| 1487 |
|
- $chars = str_split($key, 1); |
| 1488 |
|
- while(($char = array_shift($chars)) !== null) |
| 1489 |
|
- { |
| 1490 |
|
- if($char === '[') |
| 1491 |
|
- { |
| 1492 |
|
- $tree[] =& $current; |
| 1493 |
|
- $current[$uid] = array($uid+1 => ''); |
| 1494 |
|
- $current =& $current[$uid++]; |
| 1495 |
|
- $curTxt =& $current[$uid++]; |
| 1496 |
|
- } |
| 1497 |
|
- elseif($char === ']') |
| 1498 |
|
- { |
| 1499 |
|
- $current =& $tree[count($tree)-1]; |
| 1500 |
|
- array_pop($tree); |
| 1501 |
|
- if(current($chars) !== '[' && current($chars) !== false && current($chars) !== ']') |
| 1502 |
|
- { |
| 1503 |
|
- $current[$uid] = ''; |
| 1504 |
|
- $curTxt =& $current[$uid++]; |
| 1505 |
|
- } |
| 1506 |
|
- } |
| 1507 |
|
- else |
| 1508 |
|
- { |
| 1509 |
|
- $curTxt .= $char; |
| 1510 |
|
- } |
| 1511 |
|
- } |
| 1512 |
|
- unset($uid, $current, $curTxt, $tree, $chars); |
| 1513 |
|
- |
| 1514 |
|
- if($this->debug) echo 'RECURSIVE VAR REPLACEMENT : '.$key.'<br>'; |
| 1515 |
|
- |
| 1516 |
|
- $key = $this->flattenVarTree($parsed); |
| 1517 |
|
- |
| 1518 |
|
- if($this->debug) echo 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>'; |
| 1519 |
|
- |
| 1520 |
|
- $output = preg_replace('#(^""\.|""\.|\.""$|(\()""\.|\.""(\)))#', '$2$3', '$this->readVar("'.$key.'")'); |
| 1521 |
|
- } |
| 1522 |
|
- else |
| 1523 |
|
- { |
| 1524 |
|
- $output = $this->parseVarKey($key, $curBlock); |
| 1525 |
|
- } |
| 1526 |
|
- |
| 1527 |
|
- // methods |
| 1528 |
|
- if($hasMethodCall) |
| 1529 |
|
- { |
| 1530 |
|
- preg_match_all('{->([a-z0-9_]+)(\([^)]*\))?}i', $methodCall, $calls); |
| 1531 |
|
- foreach($calls[1] as $i=>$method) |
| 1532 |
|
- { |
| 1533 |
|
- $args = $calls[2][$i]; |
| 1534 |
|
- // property |
| 1535 |
|
- if($args === '') |
| 1536 |
|
- $output = '(property_exists($tmp = '.$output.', \''.$method.'\') ? $tmp->'.$method.' : null)'; |
| 1537 |
|
- // method |
| 1538 |
|
- else |
| 1539 |
|
- { |
| 1540 |
|
- if($args === '()') |
| 1541 |
|
- $parsedCall = '->'.$method.$args; |
| 1542 |
|
- else |
| 1543 |
|
- $parsedCall = '->'.$this->parseFunction($method.$args, 0, strlen($method.$args), false, 'method'); |
| 1544 |
|
- $output = '(is_object($tmp = '.$output.') ? (method_exists($tmp, \''.$method.'\') ? $tmp'.$parsedCall.' : $this->triggerError(\'Call to an undefined method : <em>\'.get_class($tmp).\'::'.$method.'()</em>\')) : $this->triggerError(\'Method <em>'.$method.'()</em> was called on a non-object (\'.var_export($tmp, true).\')\'))'; |
| 1545 |
|
- } |
| 1546 |
|
- } |
| 1547 |
|
- } |
| 1548 |
|
- |
| 1549 |
|
- // expressions |
| 1550 |
|
- if($hasExpression) |
| 1551 |
|
- { |
| 1552 |
|
- preg_match_all('#(?:([+/*%=-])(=?-?[%$][a-z0-9.[\]>_:-]+(?:\([^)]*\))?|=?-?[0-9.,]+|\1))#i', $match[3], $expMatch); |
| 1553 |
|
- |
| 1554 |
|
- foreach($expMatch[1] as $k=>$operator) |
| 1555 |
|
- { |
| 1556 |
|
- if(substr($expMatch[2][$k],0,1)==='=') |
| 1557 |
|
- { |
| 1558 |
|
- $assign = true; |
| 1559 |
|
- if($operator === '=') |
| 1560 |
|
- $this->triggerError('Invalid expression, <em>'.$substr.'</em>, can not use "==" in expressions', E_USER_ERROR); |
| 1561 |
|
- if($curBlock !== 'root') |
| 1562 |
|
- $this->triggerError('Invalid expression, <em>'.$substr.'</em>, "=" can only be used in pure expressions like {$foo+=3}, {$foo="bar"}', E_USER_ERROR); |
| 1563 |
|
- $operator .= '='; |
| 1564 |
|
- $expMatch[2][$k] = substr($expMatch[2][$k], 1); |
| 1565 |
|
- } |
| 1566 |
|
- |
| 1567 |
|
- if(substr($expMatch[2][$k],0,1)==='-' && strlen($expMatch[2][$k]) > 1) |
| 1568 |
|
- { |
| 1569 |
|
- $operator .= '-'; |
| 1570 |
|
- $expMatch[2][$k] = substr($expMatch[2][$k], 1); |
| 1571 |
|
- } |
| 1572 |
|
- if(($operator==='+'||$operator==='-') && $expMatch[2][$k]===$operator) |
| 1573 |
|
- { |
| 1574 |
|
- $output = '('.$output.$operator.$operator.')'; |
| 1575 |
|
- break; |
| 1576 |
|
- } |
| 1577 |
|
- elseif(substr($expMatch[2][$k], 0, 1) === '$') |
| 1578 |
|
- { |
| 1579 |
|
- $output = '('.$output.' '.$operator.' '.$this->parseVar($expMatch[2][$k], 0, strlen($expMatch[2][$k]), false, 'expression').')'; |
| 1580 |
|
- } |
| 1581 |
|
- elseif(substr($expMatch[2][$k], 0, 1) === '%') |
| 1582 |
|
- { |
| 1583 |
|
- $output = '('.$output.' '.$operator.' '.$this->parseConst($expMatch[2][$k], 0, strlen($expMatch[2][$k]), false, 'expression').')'; |
| 1584 |
|
- } |
| 1585 |
|
- elseif(!empty($expMatch[2][$k])) |
| 1586 |
|
- $output = '('.$output.' '.$operator.' '.str_replace(',', '.', $expMatch[2][$k]).')'; |
| 1587 |
|
- else |
| 1588 |
|
- $this->triggerError('Unfinished expression, <em>'.$substr.'</em>, missing var or number after math operator', E_USER_ERROR); |
| 1589 |
|
- } |
| 1590 |
|
- } |
| 1591 |
|
- // var assignment |
| 1592 |
|
- elseif($curBlock === 'root' && substr(trim(substr($substr, $matchedLength)), 0, 1) === '=') |
| 1593 |
|
- { |
| 1594 |
|
- $value = trim(substr(trim(substr($substr, $matchedLength)), 1)); |
| 1595 |
|
- |
| 1596 |
|
- $parts = array(); |
| 1597 |
|
- $parts = $this->parse($value, 0, strlen($value), $parts, 'condition'); |
| 1598 |
|
- |
| 1599 |
|
- // load if plugin |
| 1600 |
|
- try { |
| 1601 |
|
- $this->getPluginType('if'); |
| 1602 |
|
- } catch (DwooException $e) { |
| 1603 |
|
- $this->triggerError('Assignments require the "if" plugin to be accessible', E_USER_ERROR); |
| 1604 |
|
- } |
| 1605 |
|
- |
| 1606 |
|
- $parts = $this->mapParams($parts, array('DwooPlugin_if', 'init'), 1); |
| 1607 |
|
- $value = DwooPlugin_if::replaceKeywords($parts, $this); |
| 1608 |
|
- |
| 1609 |
|
- $output .= '='.implode(' ',$value); |
| 1610 |
|
- $assign = true; |
| 1611 |
|
- } |
| 1612 |
|
- |
| 1613 |
|
- // handle modifiers |
| 1614 |
|
- if($curBlock !== 'modifier' && $hasModifiers) |
| 1615 |
|
- { |
| 1616 |
|
- $output = $this->replaceModifiers(array(null, null, $output, $match[4]), 'var'); |
| 1617 |
|
- } |
| 1618 |
|
- |
| 1619 |
|
- if(is_array($parsingParams)) |
| 1620 |
|
- { |
| 1621 |
|
- $parsingParams[] = array($output, $key); |
| 1622 |
|
- return $parsingParams; |
| 1623 |
|
- } |
| 1624 |
|
- elseif($curBlock === 'namedparam') |
| 1625 |
|
- return array($output, $key); |
| 1626 |
|
- elseif($curBlock === 'string') |
| 1627 |
|
- return array($matchedLength, $output); |
| 1628 |
|
- elseif($curBlock === 'expression' || $curBlock === 'variable') |
| 1629 |
|
- return $output; |
| 1630 |
|
- elseif(substr($output, 0, strlen(self::PHP_OPEN)) === self::PHP_OPEN) |
| 1631 |
|
- return $output; |
| 1632 |
|
- elseif(isset($assign)) |
| 1633 |
|
- return self::PHP_OPEN.$output.';'.self::PHP_CLOSE; |
| 1634 |
|
- else |
| 1635 |
|
- return self::PHP_OPEN.'echo '.$output.';'.self::PHP_CLOSE; |
| 1636 |
|
- } |
| 1637 |
|
- else |
| 1638 |
|
- { |
| 1639 |
|
- if($curBlock === 'string') |
| 1640 |
|
- return array(0, ''); |
| 1641 |
|
- else |
| 1642 |
|
- { |
| 1643 |
|
- $this->triggerError('Invalid variable name <em>'.$substr.'</em>', E_USER_ERROR); |
| 1644 |
|
- } |
| 1645 |
|
- } |
| 1646 |
|
- } |
|
1435 |
+ */ |
|
1436 |
+ protected function parseVar($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
|
1437 |
+ { |
|
1438 |
+ $substr = substr($in, $from, $to-$from); |
|
1439 |
+ |
|
1440 |
+ if(preg_match('#(\$?\.?[a-z0-9_:]+(?:(?:(?:\.|->)(?:[a-z0-9_:]+|(?R))|\[(?:[a-z0-9_:]+|(?R))\]))*)' . // var key |
|
1441 |
+ ($curBlock==='root' || $curBlock==='function' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='expression' ? '(\([^)]*?\)(?:->[a-z0-9_]+(?:\([^)]*?\))?)*)?' : '()') . // method call |
|
1442 |
+ ($curBlock==='root' || $curBlock==='function' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='string' ? '((?:(?:[+/*%=-])(?:(?<!=)=?-?[$%][a-z0-9.[\]>_:-]+(?:\([^)]*\))?|(?<!=)=?-?[0-9.,]*|[+-]))*)':'()') . // simple math expressions |
|
1443 |
+ ($curBlock!=='modifier'? '((?:\|(?:@?[a-z0-9_]+(?:(?::("|\').+?\5|:[^\s`"\']*))*))+)?':'(())') . // modifiers |
|
1444 |
+ '#i', $substr, $match)) |
|
1445 |
+ { |
|
1446 |
+ $key = substr($match[1],1); |
|
1447 |
+ |
|
1448 |
+ $matchedLength = strlen($match[0]); |
|
1449 |
+ $hasModifiers = isset($match[4]) && !empty($match[4]); |
|
1450 |
+ $hasExpression = isset($match[3]) && !empty($match[3]); |
|
1451 |
+ $hasMethodCall = isset($match[2]) && !empty($match[2]); |
|
1452 |
+ |
|
1453 |
+ if($hasMethodCall) |
|
1454 |
+ { |
|
1455 |
+ $key = substr($match[1], 1, strrpos($match[1], '->')-1); |
|
1456 |
+ $methodCall = substr($match[1], strrpos($match[1], '->')) . $match[2]; |
|
1457 |
+ } |
|
1458 |
+ |
|
1459 |
+ if($pointer !== null) |
|
1460 |
+ $pointer += $matchedLength; |
|
1461 |
+ |
|
1462 |
+ // replace useless brackets by dot accessed vars |
|
1463 |
+ $key = preg_replace('#\[([^\[.->]+)\]#', '.$1', $key); |
|
1464 |
+ |
|
1465 |
+ // prevent $foo->$bar calls because it doesn't seem worth the trouble |
|
1466 |
+ if(strpos($key, '->$') !== false) |
|
1467 |
+ $this->triggerError('You can not access an object\'s property using a variable name.', E_USER_ERROR); |
|
1468 |
+ |
|
1469 |
+ if($this->debug) |
|
1470 |
+ { |
|
1471 |
+ if($hasMethodCall) |
|
1472 |
+ echo 'METHOD CALL FOUND : $'.$key.$methodCall.'<br />'; |
|
1473 |
+ else |
|
1474 |
+ echo 'VAR FOUND : $'.$key.'<br />'; |
|
1475 |
+ } |
|
1476 |
+ |
|
1477 |
+ $key = str_replace('"','\\"',$key); |
|
1478 |
+ |
|
1479 |
+ $cnt=substr_count($key, '$'); |
|
1480 |
+ if($cnt > 0) |
|
1481 |
+ { |
|
1482 |
+ $uid = 0; |
|
1483 |
+ $parsed = array($uid => ''); |
|
1484 |
+ $current =& $parsed; |
|
1485 |
+ $curTxt =& $parsed[$uid++]; |
|
1486 |
+ $tree = array(); |
|
1487 |
+ $chars = str_split($key, 1); |
|
1488 |
+ while(($char = array_shift($chars)) !== null) |
|
1489 |
+ { |
|
1490 |
+ if($char === '[') |
|
1491 |
+ { |
|
1492 |
+ $tree[] =& $current; |
|
1493 |
+ $current[$uid] = array($uid+1 => ''); |
|
1494 |
+ $current =& $current[$uid++]; |
|
1495 |
+ $curTxt =& $current[$uid++]; |
|
1496 |
+ } |
|
1497 |
+ elseif($char === ']') |
|
1498 |
+ { |
|
1499 |
+ $current =& $tree[count($tree)-1]; |
|
1500 |
+ array_pop($tree); |
|
1501 |
+ if(current($chars) !== '[' && current($chars) !== false && current($chars) !== ']') |
|
1502 |
+ { |
|
1503 |
+ $current[$uid] = ''; |
|
1504 |
+ $curTxt =& $current[$uid++]; |
|
1505 |
+ } |
|
1506 |
+ } |
|
1507 |
+ else |
|
1508 |
+ { |
|
1509 |
+ $curTxt .= $char; |
|
1510 |
+ } |
|
1511 |
+ } |
|
1512 |
+ unset($uid, $current, $curTxt, $tree, $chars); |
|
1513 |
+ |
|
1514 |
+ if($this->debug) echo 'RECURSIVE VAR REPLACEMENT : '.$key.'<br>'; |
|
1515 |
+ |
|
1516 |
+ $key = $this->flattenVarTree($parsed); |
|
1517 |
+ |
|
1518 |
+ if($this->debug) echo 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>'; |
|
1519 |
+ |
|
1520 |
+ $output = preg_replace('#(^""\.|""\.|\.""$|(\()""\.|\.""(\)))#', '$2$3', '$this->readVar("'.$key.'")'); |
|
1521 |
+ } |
|
1522 |
+ else |
|
1523 |
+ { |
|
1524 |
+ $output = $this->parseVarKey($key, $curBlock); |
|
1525 |
+ } |
|
1526 |
+ |
|
1527 |
+ // methods |
|
1528 |
+ if($hasMethodCall) |
|
1529 |
+ { |
|
1530 |
+ preg_match_all('{->([a-z0-9_]+)(\([^)]*\))?}i', $methodCall, $calls); |
|
1531 |
+ foreach($calls[1] as $i=>$method) |
|
1532 |
+ { |
|
1533 |
+ $args = $calls[2][$i]; |
|
1534 |
+ // property |
|
1535 |
+ if($args === '') |
|
1536 |
+ $output = '(property_exists($tmp = '.$output.', \''.$method.'\') ? $tmp->'.$method.' : null)'; |
|
1537 |
+ // method |
|
1538 |
+ else |
|
1539 |
+ { |
|
1540 |
+ if($args === '()') |
|
1541 |
+ $parsedCall = '->'.$method.$args; |
|
1542 |
+ else |
|
1543 |
+ $parsedCall = '->'.$this->parseFunction($method.$args, 0, strlen($method.$args), false, 'method'); |
|
1544 |
+ $output = '(is_object($tmp = '.$output.') ? (method_exists($tmp, \''.$method.'\') ? $tmp'.$parsedCall.' : $this->triggerError(\'Call to an undefined method : <em>\'.get_class($tmp).\'::'.$method.'()</em>\')) : $this->triggerError(\'Method <em>'.$method.'()</em> was called on a non-object (\'.var_export($tmp, true).\')\'))'; |
|
1545 |
+ } |
|
1546 |
+ } |
|
1547 |
+ } |
|
1548 |
+ |
|
1549 |
+ // expressions |
|
1550 |
+ if($hasExpression) |
|
1551 |
+ { |
|
1552 |
+ preg_match_all('#(?:([+/*%=-])(=?-?[%$][a-z0-9.[\]>_:-]+(?:\([^)]*\))?|=?-?[0-9.,]+|\1))#i', $match[3], $expMatch); |
|
1553 |
+ |
|
1554 |
+ foreach($expMatch[1] as $k=>$operator) |
|
1555 |
+ { |
|
1556 |
+ if(substr($expMatch[2][$k],0,1)==='=') |
|
1557 |
+ { |
|
1558 |
+ $assign = true; |
|
1559 |
+ if($operator === '=') |
|
1560 |
+ $this->triggerError('Invalid expression, <em>'.$substr.'</em>, can not use "==" in expressions', E_USER_ERROR); |
|
1561 |
+ if($curBlock !== 'root') |
|
1562 |
+ $this->triggerError('Invalid expression, <em>'.$substr.'</em>, "=" can only be used in pure expressions like {$foo+=3}, {$foo="bar"}', E_USER_ERROR); |
|
1563 |
+ $operator .= '='; |
|
1564 |
+ $expMatch[2][$k] = substr($expMatch[2][$k], 1); |
|
1565 |
+ } |
|
1566 |
+ |
|
1567 |
+ if(substr($expMatch[2][$k],0,1)==='-' && strlen($expMatch[2][$k]) > 1) |
|
1568 |
+ { |
|
1569 |
+ $operator .= '-'; |
|
1570 |
+ $expMatch[2][$k] = substr($expMatch[2][$k], 1); |
|
1571 |
+ } |
|
1572 |
+ if(($operator==='+'||$operator==='-') && $expMatch[2][$k]===$operator) |
|
1573 |
+ { |
|
1574 |
+ $output = '('.$output.$operator.$operator.')'; |
|
1575 |
+ break; |
|
1576 |
+ } |
|
1577 |
+ elseif(substr($expMatch[2][$k], 0, 1) === '$') |
|
1578 |
+ { |
|
1579 |
+ $output = '('.$output.' '.$operator.' '.$this->parseVar($expMatch[2][$k], 0, strlen($expMatch[2][$k]), false, 'expression').')'; |
|
1580 |
+ } |
|
1581 |
+ elseif(substr($expMatch[2][$k], 0, 1) === '%') |
|
1582 |
+ { |
|
1583 |
+ $output = '('.$output.' '.$operator.' '.$this->parseConst($expMatch[2][$k], 0, strlen($expMatch[2][$k]), false, 'expression').')'; |
|
1584 |
+ } |
|
1585 |
+ elseif(!empty($expMatch[2][$k])) |
|
1586 |
+ $output = '('.$output.' '.$operator.' '.str_replace(',', '.', $expMatch[2][$k]).')'; |
|
1587 |
+ else |
|
1588 |
+ $this->triggerError('Unfinished expression, <em>'.$substr.'</em>, missing var or number after math operator', E_USER_ERROR); |
|
1589 |
+ } |
|
1590 |
+ } |
|
1591 |
+ // var assignment |
|
1592 |
+ elseif($curBlock === 'root' && substr(trim(substr($substr, $matchedLength)), 0, 1) === '=') |
|
1593 |
+ { |
|
1594 |
+ $value = trim(substr(trim(substr($substr, $matchedLength)), 1)); |
|
1595 |
+ |
|
1596 |
+ $parts = array(); |
|
1597 |
+ $parts = $this->parse($value, 0, strlen($value), $parts, 'condition'); |
|
1598 |
+ |
|
1599 |
+ // load if plugin |
|
1600 |
+ try { |
|
1601 |
+ $this->getPluginType('if'); |
|
1602 |
+ } catch (DwooException $e) { |
|
1603 |
+ $this->triggerError('Assignments require the "if" plugin to be accessible', E_USER_ERROR); |
|
1604 |
+ } |
|
1605 |
+ |
|
1606 |
+ $parts = $this->mapParams($parts, array('DwooPlugin_if', 'init'), 1); |
|
1607 |
+ $value = DwooPlugin_if::replaceKeywords($parts, $this); |
|
1608 |
+ |
|
1609 |
+ $output .= '='.implode(' ',$value); |
|
1610 |
+ $assign = true; |
|
1611 |
+ } |
|
1612 |
+ |
|
1613 |
+ // handle modifiers |
|
1614 |
+ if($curBlock !== 'modifier' && $hasModifiers) |
|
1615 |
+ { |
|
1616 |
+ $output = $this->replaceModifiers(array(null, null, $output, $match[4]), 'var'); |
|
1617 |
+ } |
|
1618 |
+ |
|
1619 |
+ if(is_array($parsingParams)) |
|
1620 |
+ { |
|
1621 |
+ $parsingParams[] = array($output, $key); |
|
1622 |
+ return $parsingParams; |
|
1623 |
+ } |
|
1624 |
+ elseif($curBlock === 'namedparam') |
|
1625 |
+ return array($output, $key); |
|
1626 |
+ elseif($curBlock === 'string') |
|
1627 |
+ return array($matchedLength, $output); |
|
1628 |
+ elseif($curBlock === 'expression' || $curBlock === 'variable') |
|
1629 |
+ return $output; |
|
1630 |
+ elseif(isset($assign)) |
|
1631 |
+ return self::PHP_OPEN.$output.';'.self::PHP_CLOSE; |
|
1632 |
+ else |
|
1633 |
+ return $output; |
|
1634 |
+ } |
|
1635 |
+ else |
|
1636 |
+ { |
|
1637 |
+ if($curBlock === 'string') |
|
1638 |
+ return array(0, ''); |
|
1639 |
+ else |
|
1640 |
+ { |
|
1641 |
+ $this->triggerError('Invalid variable name <em>'.$substr.'</em>', E_USER_ERROR); |
|
1642 |
+ } |
|
1643 |
+ } |
|
1644 |
+ } |
| 1647 |
1645 |
|
| 1648 |
1646 |
/** |
| 1649 |
1647 |
* parses a constant variable (a variable that doesn't contain another variable) and preprocesses it to save runtime processing time |
| 1650 |
1648 |
* |
| 1651 |
|
- * @param string $key the variable to parse |
| 1652 |
|
- * @param string $curBlock the current parser-block being processed |
|
1649 |
+ * @param string $key the variable to parse |
|
1650 |
+ * @param string $curBlock the current parser-block being processed |
| 1653 |
1651 |
* @return string parsed variable |
| 1654 |
|
- */ |
| 1655 |
|
- protected function parseVarKey($key, $curBlock) |
| 1656 |
|
- { |
| 1657 |
|
- if(substr($key, 0, 1) === '.') |
| 1658 |
|
- $key = 'dwoo'.$key; |
| 1659 |
|
- if(preg_match('#dwoo\.(get|post|server|cookies|session|env|request)((?:\.[a-z0-9_-]+)+)#i', $key, $m)) |
| 1660 |
|
- { |
| 1661 |
|
- $global = strtoupper($m[1]); |
| 1662 |
|
- if($global === 'COOKIES') |
| 1663 |
|
- $global = 'COOKIE'; |
| 1664 |
|
- $key = '$_'.$global; |
| 1665 |
|
- foreach(explode('.', ltrim($m[2], '.')) as $part) |
| 1666 |
|
- $key .= '['.var_export($part, true).']'; |
| 1667 |
|
- if($curBlock === 'root') |
| 1668 |
|
- $output = $key; |
| 1669 |
|
- else |
| 1670 |
|
- $output = '(isset('.$key.')?'.$key.':null)'; |
| 1671 |
|
- } |
| 1672 |
|
- elseif(preg_match('#dwoo\.const\.([a-z0-9_:]+)#i', $key, $m)) |
| 1673 |
|
- { |
| 1674 |
|
- return $this->parseConstKey($m[1], $curBlock); |
| 1675 |
|
- } |
| 1676 |
|
- elseif($this->scope !== null) |
| 1677 |
|
- { |
| 1678 |
|
- if(strstr($key, '.') === false && strstr($key, '[') === false && strstr($key, '->') === false) |
| 1679 |
|
- { |
| 1680 |
|
- if($key === 'dwoo') |
| 1681 |
|
- { |
| 1682 |
|
- $output = '$this->globals'; |
| 1683 |
|
- } |
| 1684 |
|
- elseif($key === '_root') |
| 1685 |
|
- { |
| 1686 |
|
- $output = '$this->data'; |
| 1687 |
|
- } |
| 1688 |
|
- elseif($key === '_parent') |
| 1689 |
|
- { |
| 1690 |
|
- $output = '$this->readParentVar(1)'; |
| 1691 |
|
- } |
| 1692 |
|
- else |
| 1693 |
|
- { |
| 1694 |
|
- if($curBlock === 'root') |
| 1695 |
|
- $output = '$this->scope["'.$key.'"]'; |
| 1696 |
|
- else |
| 1697 |
|
- $output = '(isset($this->scope["'.$key.'"]) ? $this->scope["'.$key.'"] : null)'; |
| 1698 |
|
- } |
| 1699 |
|
- } |
| 1700 |
|
- else |
| 1701 |
|
- { |
| 1702 |
|
- preg_match_all('#(\[|->|\.)?([a-z0-9_]+)\]?#i', $key, $m); |
| 1703 |
|
- |
| 1704 |
|
- $i = $m[2][0]; |
| 1705 |
|
- if($i === '_parent') |
| 1706 |
|
- { |
| 1707 |
|
- $parentCnt = 0; |
| 1708 |
|
- |
| 1709 |
|
- while(true) |
| 1710 |
|
- { |
| 1711 |
|
- $parentCnt++; |
| 1712 |
|
- array_shift($m[2]); |
| 1713 |
|
- array_shift($m[1]); |
| 1714 |
|
- if(current($m[2]) === '_parent') |
| 1715 |
|
- continue; |
| 1716 |
|
- break; |
| 1717 |
|
- } |
| 1718 |
|
- |
| 1719 |
|
- $output = '$this->readParentVar('.$parentCnt.')'; |
| 1720 |
|
- } |
| 1721 |
|
- else |
| 1722 |
|
- { |
| 1723 |
|
- if($i === 'dwoo') |
| 1724 |
|
- { |
| 1725 |
|
- $output = '$this->globals'; |
| 1726 |
|
- array_shift($m[2]); |
| 1727 |
|
- array_shift($m[1]); |
| 1728 |
|
- } |
| 1729 |
|
- elseif($i === '_root') |
| 1730 |
|
- { |
| 1731 |
|
- $output = '$this->data'; |
| 1732 |
|
- array_shift($m[2]); |
| 1733 |
|
- array_shift($m[1]); |
| 1734 |
|
- } |
| 1735 |
|
- else |
| 1736 |
|
- { |
| 1737 |
|
- $output = '$this->scope'; |
| 1738 |
|
- } |
| 1739 |
|
- |
| 1740 |
|
- while(count($m[1]) && $m[1][0] !== '->') |
| 1741 |
|
- { |
| 1742 |
|
- $output .= '["'.$m[2][0].'"]'; |
| 1743 |
|
- array_shift($m[2]); |
| 1744 |
|
- array_shift($m[1]); |
| 1745 |
|
- } |
| 1746 |
|
- |
| 1747 |
|
- if($curBlock !== 'root') |
| 1748 |
|
- $output = '(isset('.$output.') ? '.$output.':null)'; |
| 1749 |
|
- } |
| 1750 |
|
- |
| 1751 |
|
- if(count($m[2])) |
| 1752 |
|
- { |
| 1753 |
|
- unset($m[0]); |
| 1754 |
|
- $output = '$this->readVarInto('.str_replace("\n", '', var_export($m, true)).', '.$output.')'; |
| 1755 |
|
- } |
| 1756 |
|
- } |
| 1757 |
|
- } |
| 1758 |
|
- else |
| 1759 |
|
- { |
| 1760 |
|
- preg_match_all('#(\[|->|\.)?([a-z0-9_]+)\]?#i', $key, $m); |
| 1761 |
|
- unset($m[0]); |
| 1762 |
|
- $output = '$this->readVar('.str_replace("\n", '', var_export($m, true)).')'; |
| 1763 |
|
- } |
| 1764 |
|
- |
| 1765 |
|
- return $output; |
| 1766 |
|
- } |
|
1652 |
+ */ |
|
1653 |
+ protected function parseVarKey($key, $curBlock) |
|
1654 |
+ { |
|
1655 |
+ if(substr($key, 0, 1) === '.') |
|
1656 |
+ $key = 'dwoo'.$key; |
|
1657 |
+ if(preg_match('#dwoo\.(get|post|server|cookies|session|env|request)((?:\.[a-z0-9_-]+)+)#i', $key, $m)) |
|
1658 |
+ { |
|
1659 |
+ $global = strtoupper($m[1]); |
|
1660 |
+ if($global === 'COOKIES') |
|
1661 |
+ $global = 'COOKIE'; |
|
1662 |
+ $key = '$_'.$global; |
|
1663 |
+ foreach(explode('.', ltrim($m[2], '.')) as $part) |
|
1664 |
+ $key .= '['.var_export($part, true).']'; |
|
1665 |
+ if($curBlock === 'root') |
|
1666 |
+ $output = $key; |
|
1667 |
+ else |
|
1668 |
+ $output = '(isset('.$key.')?'.$key.':null)'; |
|
1669 |
+ } |
|
1670 |
+ elseif(preg_match('#dwoo\.const\.([a-z0-9_:]+)#i', $key, $m)) |
|
1671 |
+ { |
|
1672 |
+ return $this->parseConstKey($m[1], $curBlock); |
|
1673 |
+ } |
|
1674 |
+ elseif($this->scope !== null) |
|
1675 |
+ { |
|
1676 |
+ if(strstr($key, '.') === false && strstr($key, '[') === false && strstr($key, '->') === false) |
|
1677 |
+ { |
|
1678 |
+ if($key === 'dwoo') |
|
1679 |
+ { |
|
1680 |
+ $output = '$this->globals'; |
|
1681 |
+ } |
|
1682 |
+ elseif($key === '_root') |
|
1683 |
+ { |
|
1684 |
+ $output = '$this->data'; |
|
1685 |
+ } |
|
1686 |
+ elseif($key === '_parent') |
|
1687 |
+ { |
|
1688 |
+ $output = '$this->readParentVar(1)'; |
|
1689 |
+ } |
|
1690 |
+ else |
|
1691 |
+ { |
|
1692 |
+ if($curBlock === 'root') |
|
1693 |
+ $output = '$this->scope["'.$key.'"]'; |
|
1694 |
+ else |
|
1695 |
+ $output = '(isset($this->scope["'.$key.'"]) ? $this->scope["'.$key.'"] : null)'; |
|
1696 |
+ } |
|
1697 |
+ } |
|
1698 |
+ else |
|
1699 |
+ { |
|
1700 |
+ preg_match_all('#(\[|->|\.)?([a-z0-9_]+)\]?#i', $key, $m); |
|
1701 |
+ |
|
1702 |
+ $i = $m[2][0]; |
|
1703 |
+ if($i === '_parent') |
|
1704 |
+ { |
|
1705 |
+ $parentCnt = 0; |
|
1706 |
+ |
|
1707 |
+ while(true) |
|
1708 |
+ { |
|
1709 |
+ $parentCnt++; |
|
1710 |
+ array_shift($m[2]); |
|
1711 |
+ array_shift($m[1]); |
|
1712 |
+ if(current($m[2]) === '_parent') |
|
1713 |
+ continue; |
|
1714 |
+ break; |
|
1715 |
+ } |
|
1716 |
+ |
|
1717 |
+ $output = '$this->readParentVar('.$parentCnt.')'; |
|
1718 |
+ } |
|
1719 |
+ else |
|
1720 |
+ { |
|
1721 |
+ if($i === 'dwoo') |
|
1722 |
+ { |
|
1723 |
+ $output = '$this->globals'; |
|
1724 |
+ array_shift($m[2]); |
|
1725 |
+ array_shift($m[1]); |
|
1726 |
+ } |
|
1727 |
+ elseif($i === '_root') |
|
1728 |
+ { |
|
1729 |
+ $output = '$this->data'; |
|
1730 |
+ array_shift($m[2]); |
|
1731 |
+ array_shift($m[1]); |
|
1732 |
+ } |
|
1733 |
+ else |
|
1734 |
+ { |
|
1735 |
+ $output = '$this->scope'; |
|
1736 |
+ } |
|
1737 |
+ |
|
1738 |
+ while(count($m[1]) && $m[1][0] !== '->') |
|
1739 |
+ { |
|
1740 |
+ $output .= '["'.$m[2][0].'"]'; |
|
1741 |
+ array_shift($m[2]); |
|
1742 |
+ array_shift($m[1]); |
|
1743 |
+ } |
|
1744 |
+ |
|
1745 |
+ if($curBlock !== 'root') |
|
1746 |
+ $output = '(isset('.$output.') ? '.$output.':null)'; |
|
1747 |
+ } |
|
1748 |
+ |
|
1749 |
+ if(count($m[2])) |
|
1750 |
+ { |
|
1751 |
+ unset($m[0]); |
|
1752 |
+ $output = '$this->readVarInto('.str_replace("\n", '', var_export($m, true)).', '.$output.')'; |
|
1753 |
+ } |
|
1754 |
+ } |
|
1755 |
+ } |
|
1756 |
+ else |
|
1757 |
+ { |
|
1758 |
+ preg_match_all('#(\[|->|\.)?([a-z0-9_]+)\]?#i', $key, $m); |
|
1759 |
+ unset($m[0]); |
|
1760 |
+ $output = '$this->readVar('.str_replace("\n", '', var_export($m, true)).')'; |
|
1761 |
+ } |
|
1762 |
+ |
|
1763 |
+ return $output; |
|
1764 |
+ } |
| 1767 |
1765 |
|
| 1768 |
1766 |
/** |
| 1769 |
1767 |
* flattens a variable tree, this helps in parsing very complex variables such as $var.foo[$foo.bar->baz].baz, |
| 1848 |
1846 |
* @param string $curBlock the current parser-block being processed |
| 1849 |
1847 |
* @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default |
| 1850 |
1848 |
* @return string parsed values |
| 1851 |
|
- */ |
| 1852 |
|
- protected function parseOthers($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
| 1853 |
|
- { |
| 1854 |
|
- $first = $in[$from]; |
| 1855 |
|
- $substr = substr($in, $from, $to-$from); |
| 1856 |
|
- |
| 1857 |
|
- $end = strlen($substr); |
| 1858 |
|
- |
| 1859 |
|
- if($curBlock === 'condition') |
| 1860 |
|
- { |
| 1861 |
|
- $breakChars = array('(', ')', ' ', '||', '&&', '|', '&', '>=', '<=', '===', '==', '=', '!==', '!=', '<<', '<', '>>', '>', '^', '~', ',', '+', '-', '*', '/', '%', '!'); |
| 1862 |
|
- } |
| 1863 |
|
- elseif($curBlock === 'modifier') |
| 1864 |
|
- $breakChars = array(' ', ',', ')', ':', '|'); |
| 1865 |
|
- else |
| 1866 |
|
- $breakChars = array(' ', ',', ')'); |
| 1867 |
|
- |
| 1868 |
|
- $breaker = false; |
| 1869 |
|
- while(list($k,$char) = each($breakChars)) |
| 1870 |
|
- { |
| 1871 |
|
- $test = strpos($substr,$char); |
| 1872 |
|
- if($test !== false && $test < $end) |
| 1873 |
|
- { |
| 1874 |
|
- $end = $test; |
| 1875 |
|
- $breaker = $k; |
| 1876 |
|
- } |
| 1877 |
|
- } |
| 1878 |
|
- |
| 1879 |
|
- if($curBlock === 'condition') |
| 1880 |
|
- { |
| 1881 |
|
- if($end === 0 && $breaker !== false) |
| 1882 |
|
- { |
| 1883 |
|
- $end = strlen($breakChars[$breaker]); |
| 1884 |
|
- } |
| 1885 |
|
- } |
| 1886 |
|
- |
| 1887 |
|
- if($end !== false) |
| 1888 |
|
- $substr = substr($substr, 0, $end); |
| 1889 |
|
- |
| 1890 |
|
- if($pointer !== null) |
| 1891 |
|
- $pointer += strlen($substr); |
| 1892 |
|
- |
| 1893 |
|
- $src = $substr; |
| 1894 |
|
- |
| 1895 |
|
- if(strtolower($substr) === 'false' || strtolower($substr) === 'no' || strtolower($substr) === 'off') |
| 1896 |
|
- { |
| 1897 |
|
- if($this->debug) echo 'BOOLEAN(FALSE) PARSED<br />'; |
| 1898 |
|
- $substr = 'false'; |
|
1849 |
+ */ |
|
1850 |
+ protected function parseOthers($in, $from, $to, $parsingParams = false, $curBlock='', &$pointer = null) |
|
1851 |
+ { |
|
1852 |
+ $first = $in[$from]; |
|
1853 |
+ $substr = substr($in, $from, $to-$from); |
|
1854 |
+ |
|
1855 |
+ $end = strlen($substr); |
|
1856 |
+ |
|
1857 |
+ if($curBlock === 'condition') |
|
1858 |
+ { |
|
1859 |
+ $breakChars = array('(', ')', ' ', '||', '&&', '|', '&', '>=', '<=', '===', '==', '=', '!==', '!=', '<<', '<', '>>', '>', '^', '~', ',', '+', '-', '*', '/', '%', '!'); |
|
1860 |
+ } |
|
1861 |
+ elseif($curBlock === 'modifier') |
|
1862 |
+ $breakChars = array(' ', ',', ')', ':', '|'); |
|
1863 |
+ else |
|
1864 |
+ $breakChars = array(' ', ',', ')'); |
|
1865 |
+ |
|
1866 |
+ $breaker = false; |
|
1867 |
+ while(list($k,$char) = each($breakChars)) |
|
1868 |
+ { |
|
1869 |
+ $test = strpos($substr,$char); |
|
1870 |
+ if($test !== false && $test < $end) |
|
1871 |
+ { |
|
1872 |
+ $end = $test; |
|
1873 |
+ $breaker = $k; |
|
1874 |
+ } |
|
1875 |
+ } |
|
1876 |
+ |
|
1877 |
+ if($curBlock === 'condition') |
|
1878 |
+ { |
|
1879 |
+ if($end === 0 && $breaker !== false) |
|
1880 |
+ { |
|
1881 |
+ $end = strlen($breakChars[$breaker]); |
|
1882 |
+ } |
|
1883 |
+ } |
|
1884 |
+ |
|
1885 |
+ if($end !== false) |
|
1886 |
+ $substr = substr($substr, 0, $end); |
|
1887 |
+ |
|
1888 |
+ if($pointer !== null) |
|
1889 |
+ $pointer += strlen($substr); |
|
1890 |
+ |
|
1891 |
+ $src = $substr; |
|
1892 |
+ |
|
1893 |
+ if(strtolower($substr) === 'false' || strtolower($substr) === 'no' || strtolower($substr) === 'off') |
|
1894 |
+ { |
|
1895 |
+ if($this->debug) echo 'BOOLEAN(FALSE) PARSED<br />'; |
|
1896 |
+ $substr = 'false'; |
| 1899 |
1897 |
} |
| 1900 |
1898 |
elseif(strtolower($substr) === 'true' || strtolower($substr) === 'yes' || strtolower($substr) === 'on') |
| 1901 |
1899 |
{ |
| 1902 |
1900 |
if($this->debug) echo 'BOOLEAN(TRUE) PARSED<br />'; |
| 1903 |
1901 |
$substr = 'true'; |
| 1904 |
|
- } |
| 1905 |
|
- elseif($substr === 'null' || $substr === 'NULL') |
| 1906 |
|
- { |
| 1907 |
|
- if($this->debug) echo 'NULL PARSED<br />'; |
| 1908 |
|
- $substr = 'null'; |
| 1909 |
|
- } |
| 1910 |
|
- elseif(is_numeric($substr)) |
| 1911 |
|
- { |
| 1912 |
|
- if($this->debug) echo 'NUMBER PARSED<br />'; |
| 1913 |
|
- $substr = (float) $substr; |
| 1914 |
|
- } |
| 1915 |
|
- elseif(preg_match('{^-?(\d+|\d*(\.\d+))\s*([/*%+-]\s*-?(\d+|\d*(\.\d+)))+$}', $substr)) |
| 1916 |
|
- { |
| 1917 |
|
- if($this->debug) echo 'SIMPLE MATH PARSED<br />'; |
| 1918 |
|
- $substr = '('.$substr.')'; |
| 1919 |
|
- } |
| 1920 |
|
- elseif($curBlock === 'condition' && array_search($substr, $breakChars, true) !== false) |
| 1921 |
|
- { |
| 1922 |
|
- if($this->debug) echo 'BREAKCHAR PARSED<br />'; |
| 1923 |
|
- $substr = '"'.$substr.'"'; |
| 1924 |
|
- } |
| 1925 |
|
- else |
| 1926 |
|
- { |
| 1927 |
|
- if($this->debug) echo 'BLABBER CASTED AS STRING<br />'; |
| 1928 |
|
- |
| 1929 |
|
- $substr = $this->replaceStringVars('"'.str_replace('"','\\"',$substr).'"', '"', $curBlock); |
| 1930 |
|
- } |
| 1931 |
|
- |
| 1932 |
|
- if(is_array($parsingParams)) |
| 1933 |
|
- { |
| 1934 |
|
- $parsingParams[] = array($substr, $src); |
| 1935 |
|
- return $parsingParams; |
| 1936 |
|
- } |
| 1937 |
|
- elseif($curBlock === 'namedparam') |
| 1938 |
|
- return array($substr, $src); |
| 1939 |
|
- } |
|
1902 |
+ } |
|
1903 |
+ elseif($substr === 'null' || $substr === 'NULL') |
|
1904 |
+ { |
|
1905 |
+ if($this->debug) echo 'NULL PARSED<br />'; |
|
1906 |
+ $substr = 'null'; |
|
1907 |
+ } |
|
1908 |
+ elseif(is_numeric($substr)) |
|
1909 |
+ { |
|
1910 |
+ if($this->debug) echo 'NUMBER PARSED<br />'; |
|
1911 |
+ $substr = (float) $substr; |
|
1912 |
+ } |
|
1913 |
+ elseif(preg_match('{^-?(\d+|\d*(\.\d+))\s*([/*%+-]\s*-?(\d+|\d*(\.\d+)))+$}', $substr)) |
|
1914 |
+ { |
|
1915 |
+ if($this->debug) echo 'SIMPLE MATH PARSED<br />'; |
|
1916 |
+ $substr = '('.$substr.')'; |
|
1917 |
+ } |
|
1918 |
+ elseif($curBlock === 'condition' && array_search($substr, $breakChars, true) !== false) |
|
1919 |
+ { |
|
1920 |
+ if($this->debug) echo 'BREAKCHAR PARSED<br />'; |
|
1921 |
+ $substr = '"'.$substr.'"'; |
|
1922 |
+ } |
|
1923 |
+ else |
|
1924 |
+ { |
|
1925 |
+ if($this->debug) echo 'BLABBER CASTED AS STRING<br />'; |
|
1926 |
+ |
|
1927 |
+ $substr = $this->replaceStringVars('"'.str_replace('"','\\"',$substr).'"', '"', $curBlock); |
|
1928 |
+ } |
|
1929 |
+ |
|
1930 |
+ if(is_array($parsingParams)) |
|
1931 |
+ { |
|
1932 |
+ $parsingParams[] = array($substr, $src); |
|
1933 |
+ return $parsingParams; |
|
1934 |
+ } |
|
1935 |
+ elseif($curBlock === 'namedparam') |
|
1936 |
+ return array($substr, $src); |
|
1937 |
+ else |
|
1938 |
+ throw new Exception('Something went wrong'); |
|
1939 |
+ } |
| 1940 |
1940 |
|
| 1941 |
1941 |
/** |
| 1942 |
1942 |
* replaces variables within a parsed string |
| 1990 |
1990 |
* @param array $m the regex matches that must be array(1=>"double or single quotes enclosing a string, when applicable", 2=>"the string or var", 3=>"the modifiers matched") |
| 1991 |
1991 |
* @param string $curBlock the current parser-block being processed |
| 1992 |
1992 |
* @return string the input enclosed with various function calls according to the modifiers found |
| 1993 |
|
- */ |
| 1994 |
|
- protected function replaceModifiers(array $m, $curBlock) |
|
1993 |
+ */ |
|
1994 |
+ protected function replaceModifiers(array $m, $curBlock) |
| 1995 |
1995 |
{ |
| 1996 |
|
- if($this->debug) echo 'PARSING MODIFIERS : '.$m[3].'<br />'; |
| 1997 |
|
- |
| 1998 |
|
- // remove first pipe |
| 1999 |
|
- $cmdstrsrc = substr($m[3],1); |
| 2000 |
|
- // remove last quote if present |
| 2001 |
|
- if(substr($cmdstrsrc,-1,1) === $m[1]) |
| 2002 |
|
- { |
| 2003 |
|
- $cmdstrsrc = substr($cmdstrsrc, 0, -1); |
| 2004 |
|
- $add = $m[1]; |
| 2005 |
|
- } |
| 2006 |
|
- |
| 2007 |
|
- $output = $m[2]; |
| 2008 |
|
- |
| 2009 |
|
- while(strlen($cmdstrsrc) > 0) |
| 2010 |
|
- { |
| 2011 |
|
- $cmdstr = $cmdstrsrc; |
| 2012 |
|
- $paramsep = ':'; |
| 2013 |
|
- $paramspos = strpos($cmdstr, $paramsep); |
| 2014 |
|
- $funcsep = strpos($cmdstr, '|'); |
| 2015 |
|
- if($funcsep !== false && ($paramspos === false || $paramspos > $funcsep)) |
| 2016 |
|
- { |
| 2017 |
|
- $paramspos = false; |
| 2018 |
|
- $cmdstr = substr($cmdstr, 0, $funcsep); |
| 2019 |
|
- } |
| 2020 |
|
- |
| 2021 |
|
- $state = 0; |
| 2022 |
|
- if($paramspos === false) |
| 2023 |
|
- { |
| 2024 |
|
- $func = $cmdstr; |
| 2025 |
|
- $cmdstrsrc = substr($cmdstrsrc, strlen($func)+1); |
| 2026 |
|
- $params = array(); |
| 2027 |
|
- } |
| 2028 |
|
- else |
| 2029 |
|
- { |
| 2030 |
|
- $func = substr($cmdstr, 0, $paramspos); |
| 2031 |
|
- $paramstr = substr($cmdstr, $paramspos+1); |
| 2032 |
|
- if(substr($paramstr, -1, 1) === $paramsep) |
| 2033 |
|
- $paramstr = substr($paramstr, 0, -1); |
| 2034 |
|
- |
| 2035 |
|
- $ptr = 0; |
| 2036 |
|
- $params = array(); |
| 2037 |
|
- while($ptr < strlen($paramstr)) |
| 2038 |
|
- { |
| 2039 |
|
- if($this->debug) echo 'MODIFIER START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
| 2040 |
|
- if($this->debug) echo $paramstr.'--'.$ptr.'--'.strlen($paramstr).'--modifier<br/>'; |
| 2041 |
|
- $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'modifier', $ptr); |
| 2042 |
|
- if($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.'<br/>'; |
| 2043 |
|
- |
| 2044 |
|
- if($ptr >= strlen($paramstr)) |
| 2045 |
|
- break; |
| 2046 |
|
- while(true) |
| 2047 |
|
- { |
| 2048 |
|
- if($paramstr[$ptr] === ' ' || $paramstr[$ptr] === '|') |
| 2049 |
|
- { |
| 2050 |
|
- if($this->debug) echo 'PARAM PARSING ENDED, " " or "|" FOUND, POINTER AT '.$ptr.'<br/>'; |
| 2051 |
|
- $ptr++; |
| 2052 |
|
- break 2; |
| 2053 |
|
- } |
| 2054 |
|
- if($ptr < strlen($paramstr) && $paramstr[$ptr] === ':') |
| 2055 |
|
- $ptr++; |
| 2056 |
|
- else |
| 2057 |
|
- break; |
| 2058 |
|
- } |
| 2059 |
|
- } |
| 2060 |
|
- $cmdstrsrc = substr($cmdstrsrc, strlen($func)+1+$ptr); |
| 2061 |
|
- $paramstr = substr($paramstr, 0, $ptr); |
| 2062 |
|
- foreach($params as $p) |
| 2063 |
|
- { |
| 2064 |
|
- if(is_array($p) && is_array($p[1])) |
| 2065 |
|
- $state |= 2; |
| 2066 |
|
- else |
| 2067 |
|
- { |
| 2068 |
|
- if($state === 2 && preg_match('#^(["\'])(.+?)\1$#', $p[0], $m)) |
| 2069 |
|
- $params[$k] = array($m[2], array('true', 'true')); |
| 2070 |
|
- else |
| 2071 |
|
- $state |= 1; |
| 2072 |
|
- } |
| 2073 |
|
- } |
| 2074 |
|
- if($state === 3) |
| 2075 |
|
- $this->errors[] = 'A function can not have named AND un-named parameters in : '.$cmdstr; |
| 2076 |
|
- } |
| 2077 |
|
- |
| 2078 |
|
- // check if we must use array_map with this plugin or not |
| 2079 |
|
- $mapped = false; |
| 2080 |
|
- if(substr($func, 0, 1) === '@') |
| 2081 |
|
- { |
| 2082 |
|
- $func = substr($func, 1); |
| 2083 |
|
- $mapped = true; |
| 2084 |
|
- } |
| 2085 |
|
- |
| 2086 |
|
- $pluginType = $this->getPluginType($func); |
| 2087 |
|
- |
| 2088 |
|
- if($state===2) |
| 2089 |
|
- array_unshift($params, array('value', array($output, $output))); |
| 2090 |
|
- else |
| 2091 |
|
- array_unshift($params, array($output, $output)); |
| 2092 |
|
- |
| 2093 |
|
- if($pluginType & Dwoo::NATIVE_PLUGIN) |
| 2094 |
|
- { |
| 2095 |
|
- $params = $this->mapParams($params, null, $state); |
| 2096 |
|
- |
| 2097 |
|
- $params = $params['*'][0]; |
| 2098 |
|
- |
| 2099 |
|
- $params = $this->implode_r($params); |
| 2100 |
|
- |
| 2101 |
|
- if($mapped) |
| 2102 |
|
- $output = '$this->arrayMap(\''.$func.'\', array('.$params.'))'; |
| 2103 |
|
- else |
| 2104 |
|
- $output = $func.'('.$params.')'; |
| 2105 |
|
- } |
| 2106 |
|
- elseif($pluginType & Dwoo::SMARTY_MODIFIER) |
| 2107 |
|
- { |
| 2108 |
|
- $params = $this->mapParams($params, null, $state); |
| 2109 |
|
- $params = $params['*'][0]; |
| 2110 |
|
- |
| 2111 |
|
- $params = $this->implode_r($params); |
| 2112 |
|
- |
| 2113 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 2114 |
|
- { |
| 2115 |
|
- $callback = $this->customPlugins[$func]['callback']; |
| 2116 |
|
- if(is_array($callback)) |
| 2117 |
|
- { |
| 2118 |
|
- if(is_object($callback[0])) |
| 2119 |
|
- $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array('.$params.'))'; |
| 2120 |
|
- else |
| 2121 |
|
- $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array(\''.$callback[0].'\', \''.$callback[1].'\'), array('.$params.'))'; |
| 2122 |
|
- } |
| 2123 |
|
- elseif($mapped) |
| 2124 |
|
- $output = '$this->arrayMap(\''.$callback.'\', array('.$params.'))'; |
| 2125 |
|
- else |
| 2126 |
|
- $output = $callback.'('.$params.')'; |
| 2127 |
|
- } |
| 2128 |
|
- elseif($mapped) |
| 2129 |
|
- $output = '$this->arrayMap(\'smarty_modifier_'.$func.'\', array('.$params.'))'; |
| 2130 |
|
- else |
| 2131 |
|
- $output = 'smarty_modifier_'.$func.'('.$params.')'; |
| 2132 |
|
- } |
| 2133 |
|
- else |
| 2134 |
|
- { |
| 2135 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 2136 |
|
- { |
| 2137 |
|
- $callback = $this->customPlugins[$func]['callback']; |
| 2138 |
|
- $pluginName = $callback; |
| 2139 |
|
- } |
| 2140 |
|
- else |
| 2141 |
|
- { |
| 2142 |
|
- $pluginName = 'DwooPlugin_'.$func; |
| 2143 |
|
- |
| 2144 |
|
- if($pluginType & Dwoo::CLASS_PLUGIN) |
| 2145 |
|
- { |
| 2146 |
|
- $callback = array($pluginName, ($pluginType & Dwoo::COMPILABLE_PLUGIN) ? 'compile' : 'process'); |
| 2147 |
|
- } |
| 2148 |
|
- else |
| 2149 |
|
- { |
| 2150 |
|
- $callback = $pluginName . (($pluginType & Dwoo::COMPILABLE_PLUGIN) ? '_compile' : ''); |
| 2151 |
|
- } |
| 2152 |
|
- } |
| 2153 |
|
- |
| 2154 |
|
- $params = $this->mapParams($params, $callback, $state); |
| 2155 |
|
- |
| 2156 |
|
- foreach($params as &$p) |
| 2157 |
|
- $p = $p[0]; |
| 2158 |
|
- |
| 2159 |
|
- if($pluginType & Dwoo::FUNC_PLUGIN) |
| 2160 |
|
- { |
| 2161 |
|
- if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
| 2162 |
|
- { |
| 2163 |
|
- if($mapped) |
| 2164 |
|
- $this->triggerError('The @ operator can not be used on compiled plugins.', E_USER_ERROR); |
| 2165 |
|
- $funcCompiler = 'DwooPlugin_'.$func.'_compile'; |
| 2166 |
|
- array_unshift($params, $this); |
| 2167 |
|
- $output = call_user_func_array($funcCompiler, $params); |
| 2168 |
|
- } |
| 2169 |
|
- else |
| 2170 |
|
- { |
| 2171 |
|
- array_unshift($params, '$this'); |
| 2172 |
|
- |
| 2173 |
|
- $params = $this->implode_r($params); |
| 2174 |
|
- if($mapped) |
| 2175 |
|
- $output = '$this->arrayMap(\''.$pluginName.'\', array('.$params.'))'; |
| 2176 |
|
- else |
| 2177 |
|
- $output = $pluginName.'('.$params.')'; |
| 2178 |
|
- } |
| 2179 |
|
- } |
| 2180 |
|
- else |
| 2181 |
|
- { |
| 2182 |
|
- if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
| 2183 |
|
- { |
| 2184 |
|
- if($mapped) |
| 2185 |
|
- $this->triggerError('The @ operator can not be used on compiled plugins.', E_USER_ERROR); |
| 2186 |
|
- $funcCompiler = array('DwooPlugin_'.$func, 'compile'); |
| 2187 |
|
- array_unshift($params, $this); |
| 2188 |
|
- $output = call_user_func_array($funcCompiler, $params); |
| 2189 |
|
- } |
| 2190 |
|
- else |
| 2191 |
|
- { |
| 2192 |
|
- $params = $this->implode_r($params); |
| 2193 |
|
- |
| 2194 |
|
- if($pluginType & Dwoo::CUSTOM_PLUGIN) |
| 2195 |
|
- { |
| 2196 |
|
- if(is_object($callback[0])) |
| 2197 |
|
- $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array('.$params.'))'; |
| 2198 |
|
- else |
| 2199 |
|
- $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array(\''.$callback[0].'\', \''.$callback[1].'\'), array('.$params.'))'; |
| 2200 |
|
- } |
| 2201 |
|
- elseif($mapped) |
| 2202 |
|
- $output = '$this->arrayMap(array($this->getObjectPlugin(\'DwooPlugin_'.$func.'\'), \'process\'), array('.$params.'))'; |
| 2203 |
|
- else |
| 2204 |
|
- $output = '$this->classCall(\''.$func.'\', array('.$params.'))'; |
| 2205 |
|
- } |
| 2206 |
|
- } |
| 2207 |
|
- } |
| 2208 |
|
- } |
| 2209 |
|
- |
| 2210 |
|
- if($curBlock === 'var' || $m[1] === null) |
| 2211 |
|
- { |
| 2212 |
|
- return $output; |
| 2213 |
|
- } |
| 2214 |
|
- elseif($curBlock === 'string' || $curBlock === 'root') |
| 2215 |
|
- return $m[1].'.'.$output.'.'.$m[1].(isset($add)?$add:null); |
| 2216 |
|
- } |
|
1996 |
+ if($this->debug) echo 'PARSING MODIFIERS : '.$m[3].'<br />'; |
|
1997 |
+ |
|
1998 |
+ // remove first pipe |
|
1999 |
+ $cmdstrsrc = substr($m[3],1); |
|
2000 |
+ // remove last quote if present |
|
2001 |
+ if(substr($cmdstrsrc,-1,1) === $m[1]) |
|
2002 |
+ { |
|
2003 |
+ $cmdstrsrc = substr($cmdstrsrc, 0, -1); |
|
2004 |
+ $add = $m[1]; |
|
2005 |
+ } |
|
2006 |
+ |
|
2007 |
+ $output = $m[2]; |
|
2008 |
+ |
|
2009 |
+ while(strlen($cmdstrsrc) > 0) |
|
2010 |
+ { |
|
2011 |
+ $cmdstr = $cmdstrsrc; |
|
2012 |
+ $paramsep = ':'; |
|
2013 |
+ $paramspos = strpos($cmdstr, $paramsep); |
|
2014 |
+ $funcsep = strpos($cmdstr, '|'); |
|
2015 |
+ if($funcsep !== false && ($paramspos === false || $paramspos > $funcsep)) |
|
2016 |
+ { |
|
2017 |
+ $paramspos = false; |
|
2018 |
+ $cmdstr = substr($cmdstr, 0, $funcsep); |
|
2019 |
+ } |
|
2020 |
+ |
|
2021 |
+ $state = 0; |
|
2022 |
+ if($paramspos === false) |
|
2023 |
+ { |
|
2024 |
+ $func = $cmdstr; |
|
2025 |
+ $cmdstrsrc = substr($cmdstrsrc, strlen($func)+1); |
|
2026 |
+ $params = array(); |
|
2027 |
+ } |
|
2028 |
+ else |
|
2029 |
+ { |
|
2030 |
+ $func = substr($cmdstr, 0, $paramspos); |
|
2031 |
+ $paramstr = substr($cmdstr, $paramspos+1); |
|
2032 |
+ if(substr($paramstr, -1, 1) === $paramsep) |
|
2033 |
+ $paramstr = substr($paramstr, 0, -1); |
|
2034 |
+ |
|
2035 |
+ $ptr = 0; |
|
2036 |
+ $params = array(); |
|
2037 |
+ while($ptr < strlen($paramstr)) |
|
2038 |
+ { |
|
2039 |
+ if($this->debug) echo 'MODIFIER START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
|
2040 |
+ if($this->debug) echo $paramstr.'--'.$ptr.'--'.strlen($paramstr).'--modifier<br/>'; |
|
2041 |
+ $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'modifier', $ptr); |
|
2042 |
+ if($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.'<br/>'; |
|
2043 |
+ |
|
2044 |
+ if($ptr >= strlen($paramstr)) |
|
2045 |
+ break; |
|
2046 |
+ while(true) |
|
2047 |
+ { |
|
2048 |
+ if($paramstr[$ptr] === ' ' || $paramstr[$ptr] === '|') |
|
2049 |
+ { |
|
2050 |
+ if($this->debug) echo 'PARAM PARSING ENDED, " " or "|" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
2051 |
+ $ptr++; |
|
2052 |
+ break 2; |
|
2053 |
+ } |
|
2054 |
+ if($ptr < strlen($paramstr) && $paramstr[$ptr] === ':') |
|
2055 |
+ $ptr++; |
|
2056 |
+ else |
|
2057 |
+ break; |
|
2058 |
+ } |
|
2059 |
+ } |
|
2060 |
+ $cmdstrsrc = substr($cmdstrsrc, strlen($func)+1+$ptr); |
|
2061 |
+ $paramstr = substr($paramstr, 0, $ptr); |
|
2062 |
+ foreach($params as $p) |
|
2063 |
+ { |
|
2064 |
+ if(is_array($p) && is_array($p[1])) |
|
2065 |
+ $state |= 2; |
|
2066 |
+ else |
|
2067 |
+ { |
|
2068 |
+ if($state === 2 && preg_match('#^(["\'])(.+?)\1$#', $p[0], $m)) |
|
2069 |
+ $params[$k] = array($m[2], array('true', 'true')); |
|
2070 |
+ else |
|
2071 |
+ $state |= 1; |
|
2072 |
+ } |
|
2073 |
+ } |
|
2074 |
+ if($state === 3) |
|
2075 |
+ $this->errors[] = 'A function can not have named AND un-named parameters in : '.$cmdstr; |
|
2076 |
+ } |
|
2077 |
+ |
|
2078 |
+ // check if we must use array_map with this plugin or not |
|
2079 |
+ $mapped = false; |
|
2080 |
+ if(substr($func, 0, 1) === '@') |
|
2081 |
+ { |
|
2082 |
+ $func = substr($func, 1); |
|
2083 |
+ $mapped = true; |
|
2084 |
+ } |
|
2085 |
+ |
|
2086 |
+ $pluginType = $this->getPluginType($func); |
|
2087 |
+ |
|
2088 |
+ if($state===2) |
|
2089 |
+ array_unshift($params, array('value', array($output, $output))); |
|
2090 |
+ else |
|
2091 |
+ array_unshift($params, array($output, $output)); |
|
2092 |
+ |
|
2093 |
+ if($pluginType & Dwoo::NATIVE_PLUGIN) |
|
2094 |
+ { |
|
2095 |
+ $params = $this->mapParams($params, null, $state); |
|
2096 |
+ |
|
2097 |
+ $params = $params['*'][0]; |
|
2098 |
+ |
|
2099 |
+ $params = $this->implode_r($params); |
|
2100 |
+ |
|
2101 |
+ if($mapped) |
|
2102 |
+ $output = '$this->arrayMap(\''.$func.'\', array('.$params.'))'; |
|
2103 |
+ else |
|
2104 |
+ $output = $func.'('.$params.')'; |
|
2105 |
+ } |
|
2106 |
+ elseif($pluginType & Dwoo::SMARTY_MODIFIER) |
|
2107 |
+ { |
|
2108 |
+ $params = $this->mapParams($params, null, $state); |
|
2109 |
+ $params = $params['*'][0]; |
|
2110 |
+ |
|
2111 |
+ $params = $this->implode_r($params); |
|
2112 |
+ |
|
2113 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
2114 |
+ { |
|
2115 |
+ $callback = $this->customPlugins[$func]['callback']; |
|
2116 |
+ if(is_array($callback)) |
|
2117 |
+ { |
|
2118 |
+ if(is_object($callback[0])) |
|
2119 |
+ $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array('.$params.'))'; |
|
2120 |
+ else |
|
2121 |
+ $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array(\''.$callback[0].'\', \''.$callback[1].'\'), array('.$params.'))'; |
|
2122 |
+ } |
|
2123 |
+ elseif($mapped) |
|
2124 |
+ $output = '$this->arrayMap(\''.$callback.'\', array('.$params.'))'; |
|
2125 |
+ else |
|
2126 |
+ $output = $callback.'('.$params.')'; |
|
2127 |
+ } |
|
2128 |
+ elseif($mapped) |
|
2129 |
+ $output = '$this->arrayMap(\'smarty_modifier_'.$func.'\', array('.$params.'))'; |
|
2130 |
+ else |
|
2131 |
+ $output = 'smarty_modifier_'.$func.'('.$params.')'; |
|
2132 |
+ } |
|
2133 |
+ else |
|
2134 |
+ { |
|
2135 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
2136 |
+ { |
|
2137 |
+ $callback = $this->customPlugins[$func]['callback']; |
|
2138 |
+ $pluginName = $callback; |
|
2139 |
+ } |
|
2140 |
+ else |
|
2141 |
+ { |
|
2142 |
+ $pluginName = 'DwooPlugin_'.$func; |
|
2143 |
+ |
|
2144 |
+ if($pluginType & Dwoo::CLASS_PLUGIN) |
|
2145 |
+ { |
|
2146 |
+ $callback = array($pluginName, ($pluginType & Dwoo::COMPILABLE_PLUGIN) ? 'compile' : 'process'); |
|
2147 |
+ } |
|
2148 |
+ else |
|
2149 |
+ { |
|
2150 |
+ $callback = $pluginName . (($pluginType & Dwoo::COMPILABLE_PLUGIN) ? '_compile' : ''); |
|
2151 |
+ } |
|
2152 |
+ } |
|
2153 |
+ |
|
2154 |
+ $params = $this->mapParams($params, $callback, $state); |
|
2155 |
+ |
|
2156 |
+ foreach($params as &$p) |
|
2157 |
+ $p = $p[0]; |
|
2158 |
+ |
|
2159 |
+ if($pluginType & Dwoo::FUNC_PLUGIN) |
|
2160 |
+ { |
|
2161 |
+ if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
|
2162 |
+ { |
|
2163 |
+ if($mapped) |
|
2164 |
+ $this->triggerError('The @ operator can not be used on compiled plugins.', E_USER_ERROR); |
|
2165 |
+ $funcCompiler = 'DwooPlugin_'.$func.'_compile'; |
|
2166 |
+ array_unshift($params, $this); |
|
2167 |
+ $output = call_user_func_array($funcCompiler, $params); |
|
2168 |
+ } |
|
2169 |
+ else |
|
2170 |
+ { |
|
2171 |
+ array_unshift($params, '$this'); |
|
2172 |
+ |
|
2173 |
+ $params = $this->implode_r($params); |
|
2174 |
+ if($mapped) |
|
2175 |
+ $output = '$this->arrayMap(\''.$pluginName.'\', array('.$params.'))'; |
|
2176 |
+ else |
|
2177 |
+ $output = $pluginName.'('.$params.')'; |
|
2178 |
+ } |
|
2179 |
+ } |
|
2180 |
+ else |
|
2181 |
+ { |
|
2182 |
+ if($pluginType & Dwoo::COMPILABLE_PLUGIN) |
|
2183 |
+ { |
|
2184 |
+ if($mapped) |
|
2185 |
+ $this->triggerError('The @ operator can not be used on compiled plugins.', E_USER_ERROR); |
|
2186 |
+ $funcCompiler = array('DwooPlugin_'.$func, 'compile'); |
|
2187 |
+ array_unshift($params, $this); |
|
2188 |
+ $output = call_user_func_array($funcCompiler, $params); |
|
2189 |
+ } |
|
2190 |
+ else |
|
2191 |
+ { |
|
2192 |
+ $params = $this->implode_r($params); |
|
2193 |
+ |
|
2194 |
+ if($pluginType & Dwoo::CUSTOM_PLUGIN) |
|
2195 |
+ { |
|
2196 |
+ if(is_object($callback[0])) |
|
2197 |
+ $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array('.$params.'))'; |
|
2198 |
+ else |
|
2199 |
+ $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array(\''.$callback[0].'\', \''.$callback[1].'\'), array('.$params.'))'; |
|
2200 |
+ } |
|
2201 |
+ elseif($mapped) |
|
2202 |
+ $output = '$this->arrayMap(array($this->getObjectPlugin(\'DwooPlugin_'.$func.'\'), \'process\'), array('.$params.'))'; |
|
2203 |
+ else |
|
2204 |
+ $output = '$this->classCall(\''.$func.'\', array('.$params.'))'; |
|
2205 |
+ } |
|
2206 |
+ } |
|
2207 |
+ } |
|
2208 |
+ } |
|
2209 |
+ |
|
2210 |
+ if($curBlock === 'var' || $m[1] === null) |
|
2211 |
+ { |
|
2212 |
+ return $output; |
|
2213 |
+ } |
|
2214 |
+ elseif($curBlock === 'string' || $curBlock === 'root') |
|
2215 |
+ return $m[1].'.'.$output.'.'.$m[1].(isset($add)?$add:null); |
|
2216 |
+ } |
| 2217 |
2217 |
|
| 2218 |
2218 |
/** |
| 2219 |
2219 |
* recursively implodes an array in a similar manner as var_export() does but with some tweaks |
| 2223 |
2223 |
* @param array $params the array to implode |
| 2224 |
2224 |
* @param bool $recursiveCall if set to true, the function outputs key names for the top level |
| 2225 |
2225 |
* @return string the imploded array |
| 2226 |
|
- */ |
| 2227 |
|
- protected function implode_r(array $params, $recursiveCall = false) |
| 2228 |
|
- { |
| 2229 |
|
- $out = ''; |
| 2230 |
|
- foreach($params as $k=>$p) |
| 2231 |
|
- { |
| 2232 |
|
- if(is_array($p)) |
| 2233 |
|
- { |
| 2234 |
|
- $out2 = 'array('; |
| 2235 |
|
- foreach($p as $k2=>$v) |
| 2236 |
|
- $out2 .= var_export($k2, true).' => '.(is_array($v) ? 'array('.$this->implode_r($v, true).')' : $v).', '; |
| 2237 |
|
- $p = rtrim($out2, ', ').')'; |
| 2238 |
|
- } |
| 2239 |
|
- if($recursiveCall) |
| 2240 |
|
- $out .= var_export($k, true).' => '.$p.', '; |
| 2241 |
|
- else |
| 2242 |
|
- $out .= $p.', '; |
| 2243 |
|
- } |
| 2244 |
|
- return rtrim($out, ', '); |
| 2245 |
|
- } |
|
2226 |
+ */ |
|
2227 |
+ protected function implode_r(array $params, $recursiveCall = false) |
|
2228 |
+ { |
|
2229 |
+ $out = ''; |
|
2230 |
+ foreach($params as $k=>$p) |
|
2231 |
+ { |
|
2232 |
+ if(is_array($p)) |
|
2233 |
+ { |
|
2234 |
+ $out2 = 'array('; |
|
2235 |
+ foreach($p as $k2=>$v) |
|
2236 |
+ $out2 .= var_export($k2, true).' => '.(is_array($v) ? 'array('.$this->implode_r($v, true).')' : $v).', '; |
|
2237 |
+ $p = rtrim($out2, ', ').')'; |
|
2238 |
+ } |
|
2239 |
+ if($recursiveCall) |
|
2240 |
+ $out .= var_export($k, true).' => '.$p.', '; |
|
2241 |
+ else |
|
2242 |
+ $out .= $p.', '; |
|
2243 |
+ } |
|
2244 |
+ return rtrim($out, ', '); |
|
2245 |
+ } |
| 2246 |
2246 |
|
| 2247 |
2247 |
/** |
| 2248 |
2248 |
* returns the plugin type of a plugin and adds it to the used plugins array if required |
| 2249 |
2249 |
* |
| 2250 |
2250 |
* @param string $name plugin name, as found in the template |
| 2251 |
2251 |
* @return int type as a multi bit flag composed of the Dwoo plugin types constants |
| 2252 |
|
- */ |
| 2253 |
|
- protected function getPluginType($name) |
| 2254 |
|
- { |
| 2255 |
|
- $pluginType = -1; |
| 2256 |
|
- |
| 2257 |
|
- if(($this->securityPolicy === null && function_exists($name)) || |
| 2258 |
|
- ($this->securityPolicy !== null && in_array(strtolower($name), $this->securityPolicy->getAllowedPhpFunctions()) !== false)) |
| 2259 |
|
- { |
| 2260 |
|
- $phpFunc = true; |
| 2261 |
|
- } |
| 2262 |
|
- |
| 2263 |
|
- while($pluginType <= 0) |
| 2264 |
|
- { |
| 2265 |
|
- if(isset($this->customPlugins[$name])) |
| 2266 |
|
- $pluginType = $this->customPlugins[$name]['type'] | Dwoo::CUSTOM_PLUGIN; |
| 2267 |
|
- elseif(class_exists('DwooPlugin_'.$name, false) !== false) |
| 2268 |
|
- { |
| 2269 |
|
- if(is_subclass_of('DwooPlugin_'.$name, 'DwooBlockPlugin')) |
| 2270 |
|
- $pluginType = Dwoo::BLOCK_PLUGIN; |
| 2271 |
|
- else |
| 2272 |
|
- $pluginType = Dwoo::CLASS_PLUGIN; |
| 2273 |
|
- $interfaces = class_implements('DwooPlugin_'.$name, false); |
| 2274 |
|
- if(in_array('DwooICompilable', $interfaces) !== false || in_array('DwooICompilableBlock', $interfaces) !== false) |
| 2275 |
|
- $pluginType |= Dwoo::COMPILABLE_PLUGIN; |
| 2276 |
|
- } |
| 2277 |
|
- elseif(function_exists('DwooPlugin_'.$name) !== false) |
| 2278 |
|
- $pluginType = Dwoo::FUNC_PLUGIN; |
| 2279 |
|
- elseif(function_exists('DwooPlugin_'.$name.'_compile')) |
| 2280 |
|
- $pluginType = Dwoo::FUNC_PLUGIN | Dwoo::COMPILABLE_PLUGIN; |
| 2281 |
|
- elseif(function_exists('smarty_modifier_'.$name) !== false) |
| 2282 |
|
- $pluginType = Dwoo::SMARTY_MODIFIER; |
| 2283 |
|
- elseif(function_exists('smarty_function_'.$name) !== false) |
| 2284 |
|
- $pluginType = Dwoo::SMARTY_FUNCTION; |
| 2285 |
|
- elseif(function_exists('smarty_block_'.$name) !== false) |
| 2286 |
|
- $pluginType = Dwoo::SMARTY_BLOCK; |
| 2287 |
|
- else |
| 2288 |
|
- { |
| 2289 |
|
- if($pluginType===-1) |
| 2290 |
|
- { |
| 2291 |
|
- try { |
| 2292 |
|
- DwooLoader::loadPlugin($name); |
| 2293 |
|
- } catch (Exception $e) { |
| 2294 |
|
- if(isset($phpFunc)) |
| 2295 |
|
- $pluginType = Dwoo::NATIVE_PLUGIN; |
| 2296 |
|
- else |
| 2297 |
|
- throw $e; |
| 2298 |
|
- } |
| 2299 |
|
- } |
| 2300 |
|
- else |
| 2301 |
|
- $this->triggerError('Plugin "'.$name.'" could not be found', E_USER_ERROR); |
| 2302 |
|
- $pluginType++; |
| 2303 |
|
- } |
| 2304 |
|
- } |
| 2305 |
|
- |
| 2306 |
|
- if(($pluginType & Dwoo::COMPILABLE_PLUGIN) === 0 && ($pluginType & Dwoo::NATIVE_PLUGIN) === 0) |
| 2307 |
|
- $this->usedPlugins[$name] = $pluginType; |
| 2308 |
|
- |
| 2309 |
|
- return $pluginType; |
| 2310 |
|
- } |
|
2252 |
+ */ |
|
2253 |
+ protected function getPluginType($name) |
|
2254 |
+ { |
|
2255 |
+ $pluginType = -1; |
|
2256 |
+ |
|
2257 |
+ if(($this->securityPolicy === null && function_exists($name)) || |
|
2258 |
+ ($this->securityPolicy !== null && in_array(strtolower($name), $this->securityPolicy->getAllowedPhpFunctions()) !== false)) |
|
2259 |
+ { |
|
2260 |
+ $phpFunc = true; |
|
2261 |
+ } |
|
2262 |
+ |
|
2263 |
+ while($pluginType <= 0) |
|
2264 |
+ { |
|
2265 |
+ if(isset($this->customPlugins[$name])) |
|
2266 |
+ $pluginType = $this->customPlugins[$name]['type'] | Dwoo::CUSTOM_PLUGIN; |
|
2267 |
+ elseif(class_exists('DwooPlugin_'.$name, false) !== false) |
|
2268 |
+ { |
|
2269 |
+ if(is_subclass_of('DwooPlugin_'.$name, 'DwooBlockPlugin')) |
|
2270 |
+ $pluginType = Dwoo::BLOCK_PLUGIN; |
|
2271 |
+ else |
|
2272 |
+ $pluginType = Dwoo::CLASS_PLUGIN; |
|
2273 |
+ $interfaces = class_implements('DwooPlugin_'.$name, false); |
|
2274 |
+ if(in_array('DwooICompilable', $interfaces) !== false || in_array('DwooICompilableBlock', $interfaces) !== false) |
|
2275 |
+ $pluginType |= Dwoo::COMPILABLE_PLUGIN; |
|
2276 |
+ } |
|
2277 |
+ elseif(function_exists('DwooPlugin_'.$name) !== false) |
|
2278 |
+ $pluginType = Dwoo::FUNC_PLUGIN; |
|
2279 |
+ elseif(function_exists('DwooPlugin_'.$name.'_compile')) |
|
2280 |
+ $pluginType = Dwoo::FUNC_PLUGIN | Dwoo::COMPILABLE_PLUGIN; |
|
2281 |
+ elseif(function_exists('smarty_modifier_'.$name) !== false) |
|
2282 |
+ $pluginType = Dwoo::SMARTY_MODIFIER; |
|
2283 |
+ elseif(function_exists('smarty_function_'.$name) !== false) |
|
2284 |
+ $pluginType = Dwoo::SMARTY_FUNCTION; |
|
2285 |
+ elseif(function_exists('smarty_block_'.$name) !== false) |
|
2286 |
+ $pluginType = Dwoo::SMARTY_BLOCK; |
|
2287 |
+ else |
|
2288 |
+ { |
|
2289 |
+ if($pluginType===-1) |
|
2290 |
+ { |
|
2291 |
+ try { |
|
2292 |
+ DwooLoader::loadPlugin($name); |
|
2293 |
+ } catch (Exception $e) { |
|
2294 |
+ if(isset($phpFunc)) |
|
2295 |
+ $pluginType = Dwoo::NATIVE_PLUGIN; |
|
2296 |
+ else |
|
2297 |
+ throw $e; |
|
2298 |
+ } |
|
2299 |
+ } |
|
2300 |
+ else |
|
2301 |
+ $this->triggerError('Plugin "'.$name.'" could not be found', E_USER_ERROR); |
|
2302 |
+ $pluginType++; |
|
2303 |
+ } |
|
2304 |
+ } |
|
2305 |
+ |
|
2306 |
+ if(($pluginType & Dwoo::COMPILABLE_PLUGIN) === 0 && ($pluginType & Dwoo::NATIVE_PLUGIN) === 0) |
|
2307 |
+ $this->usedPlugins[$name] = $pluginType; |
|
2308 |
+ |
|
2309 |
+ return $pluginType; |
|
2310 |
+ } |
| 2311 |
2311 |
|
| 2312 |
2312 |
/** |
| 2313 |
2313 |
* handles the {strip} blocks regex replacement, do not rely on it as it will eventually be moved into it's own plugin |
| 2314 |
2314 |
* |
| 2315 |
2315 |
* @param array $matches the regex matches with the "1" index being the contents of the {strip} block |
| 2316 |
2316 |
* @return string processed string |
| 2317 |
|
- */ |
| 2318 |
|
- protected function stripPreprocessorHelper(array $matches) |
|
2317 |
+ */ |
|
2318 |
+ protected function stripPreprocessorHelper(array $matches) |
|
2319 |
+ { |
|
2320 |
+ // TODO make this into a separated plugin |
|
2321 |
+ return str_replace(array("\n","\r"), null, preg_replace('#^\s*(.+?)\s*$#m', '$1', $matches[1])); |
|
2322 |
+ } |
|
2323 |
+ |
|
2324 |
+ /** |
|
2325 |
+ * runs htmlentities over the matched <?php ?> blocks when the security policy enforces that |
|
2326 |
+ * |
|
2327 |
+ * @param array $match matched php block |
|
2328 |
+ * @return string the htmlentities-converted string |
|
2329 |
+ */ |
|
2330 |
+ protected function phpTagEncodingHelper($match) |
| 2319 |
2331 |
{ |
| 2320 |
|
- // TODO make this into a separated plugin |
| 2321 |
|
- return str_replace(array("\n","\r"), null, preg_replace('#^\s*(.+?)\s*$#m', '$1', $matches[1])); |
| 2322 |
|
- } |
| 2323 |
|
- |
| 2324 |
|
- /** |
| 2325 |
|
- * runs htmlentities over the matched <?php ?> blocks when the security policy enforces that |
| 2326 |
|
- * |
| 2327 |
|
- * @param array $match matched php block |
| 2328 |
|
- * @return string the htmlentities-converted string |
| 2329 |
|
- */ |
| 2330 |
|
- protected function phpTagEncodingHelper($match) |
| 2331 |
|
- { |
| 2332 |
|
- return htmlspecialchars($match[0]); |
| 2333 |
|
- } |
| 2334 |
|
- |
|
2332 |
+ return htmlspecialchars($match[0]); |
|
2333 |
+ } |
|
2334 |
+ |
| 2335 |
2335 |
/** |
| 2336 |
2336 |
* maps the parameters received from the template onto the parameters required by the given callback |
| 2337 |
2337 |
* |
| 2339 |
2339 |
* @param callback $callback the function or method to reflect on to find out the required parameters |
| 2340 |
2340 |
* @param int $callType the type of call in the template, 0 = no params, 1 = php-style call, 2 = named parameters call |
| 2341 |
2341 |
* @return array parameters sorted in the correct order with missing optional parameters filled |
| 2342 |
|
- */ |
| 2343 |
|
- protected function mapParams(array $params, $callback, $callType=2) |
|
2342 |
+ */ |
|
2343 |
+ protected function mapParams(array $params, $callback, $callType=2) |
| 2344 |
2344 |
{ |
| 2345 |
|
- $map = $this->getParamMap($callback); |
| 2346 |
|
- |
| 2347 |
|
- $paramlist = array(); |
| 2348 |
|
- |
| 2349 |
|
- // named parameters call |
| 2350 |
|
- if($callType===2) |
| 2351 |
|
- { |
| 2352 |
|
- // transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values)) |
| 2353 |
|
- $ps = array(); |
| 2354 |
|
- foreach($params as $p) |
| 2355 |
|
- $ps[$p[0]] = $p[1]; |
| 2356 |
|
- |
| 2357 |
|
- // loops over the param map and assigns values from the template or default value for unset optional params |
| 2358 |
|
- while(list($k,$v) = each($map)) |
| 2359 |
|
- { |
| 2360 |
|
- // "rest" array parameter, fill every remaining params in it and then break |
| 2361 |
|
- if($v[0] === '*') |
| 2362 |
|
- { |
| 2363 |
|
- if(count($ps) === 0) |
| 2364 |
|
- { |
| 2365 |
|
- if($v[1]===false) |
| 2366 |
|
- $this->triggerError('Rest argument missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
| 2367 |
|
- else |
| 2368 |
|
- break; |
| 2369 |
|
- } |
| 2370 |
|
- $tmp = array(); |
| 2371 |
|
- $tmp2 = array(); |
| 2372 |
|
- foreach($ps as $i=>$p) |
| 2373 |
|
- { |
| 2374 |
|
- $tmp[$i] = $p[0]; |
| 2375 |
|
- $tmp2[$i] = $p[1]; |
| 2376 |
|
- } |
| 2377 |
|
- $paramlist[$v[0]] = array($tmp, $tmp2); |
| 2378 |
|
- unset($tmp, $tmp2, $i, $p); |
| 2379 |
|
- break; |
| 2380 |
|
- } |
| 2381 |
|
- // parameter is defined |
| 2382 |
|
- elseif(isset($ps[$v[0]])) |
| 2383 |
|
- { |
| 2384 |
|
- $paramlist[$v[0]] = $ps[$v[0]]; |
| 2385 |
|
- unset($ps[$v[0]]); |
| 2386 |
|
- } |
| 2387 |
|
- // parameter is not defined and not optional, throw error |
| 2388 |
|
- elseif($v[1]===false) |
| 2389 |
|
- $this->triggerError('Argument '.$k.'/'.$v[0].' missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
| 2390 |
|
- // enforce lowercased null if default value is null (php outputs NULL with var export) |
| 2391 |
|
- elseif($v[2]===null) |
| 2392 |
|
- $paramlist[$v[0]] = array('null', null); |
| 2393 |
|
- // outputs default value with var_export |
| 2394 |
|
- else |
| 2395 |
|
- $paramlist[$v[0]] = array(var_export($v[2], true), $v[2]); |
| 2396 |
|
- } |
| 2397 |
|
- } |
| 2398 |
|
- // php call or no parameter call |
| 2399 |
|
- elseif($callType===1||$callType===0) |
| 2400 |
|
- { |
| 2401 |
|
- // loops over the param map and assigns values from the template or default value for unset optional params |
| 2402 |
|
- while(list($k,$v) = each($map)) |
| 2403 |
|
- { |
| 2404 |
|
- // "rest" array parameter, fill every remaining params in it and then break |
| 2405 |
|
- if($v[0] === '*') |
| 2406 |
|
- { |
| 2407 |
|
- if(count($params) === 0) |
| 2408 |
|
- { |
| 2409 |
|
- if($v[1]===false) |
| 2410 |
|
- $this->triggerError('Rest argument missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
| 2411 |
|
- else |
| 2412 |
|
- break; |
| 2413 |
|
- } |
| 2414 |
|
- $tmp = array(); |
| 2415 |
|
- $tmp2 = array(); |
| 2416 |
|
- $i = 0; |
| 2417 |
|
- foreach($params as $p) |
| 2418 |
|
- { |
| 2419 |
|
- $tmp[$i] = $p[0]; |
| 2420 |
|
- $tmp2[$i++] = $p[1]; |
| 2421 |
|
- } |
| 2422 |
|
- $paramlist[$v[0]] = array($tmp, $tmp2); |
| 2423 |
|
- unset($tmp, $tmp2, $i, $p); |
| 2424 |
|
- break; |
| 2425 |
|
- } |
| 2426 |
|
- // parameter is defined |
| 2427 |
|
- elseif(empty($params)===false) |
| 2428 |
|
- { |
| 2429 |
|
- $paramlist[$v[0]] = array_shift($params); |
| 2430 |
|
- } |
| 2431 |
|
- // parameter is not defined and not optional, throw error |
| 2432 |
|
- elseif($v[1]===false) |
| 2433 |
|
- $this->triggerError('Argument '.$k.'/'.$v[0].' missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
| 2434 |
|
- // enforce lowercased null if default value is null (php outputs NULL with var export) |
| 2435 |
|
- elseif($v[2]===null) |
| 2436 |
|
- $paramlist[$v[0]] = array('null', null); |
| 2437 |
|
- // outputs default value with var_export |
| 2438 |
|
- else |
| 2439 |
|
- $paramlist[$v[0]] = array(var_export($v[2], true), $v[2]); |
| 2440 |
|
- } |
| 2441 |
|
- } |
| 2442 |
|
- // parser failed miserably |
| 2443 |
|
- else |
| 2444 |
|
- $this->triggerError('This should not happen, please report it if you see this message', E_USER_ERROR); |
| 2445 |
|
- |
| 2446 |
|
- return $paramlist; |
| 2447 |
|
- } |
|
2345 |
+ $map = $this->getParamMap($callback); |
|
2346 |
+ |
|
2347 |
+ $paramlist = array(); |
|
2348 |
+ |
|
2349 |
+ // named parameters call |
|
2350 |
+ if($callType===2) |
|
2351 |
+ { |
|
2352 |
+ // transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values)) |
|
2353 |
+ $ps = array(); |
|
2354 |
+ foreach($params as $p) |
|
2355 |
+ $ps[$p[0]] = $p[1]; |
|
2356 |
+ |
|
2357 |
+ // loops over the param map and assigns values from the template or default value for unset optional params |
|
2358 |
+ while(list($k,$v) = each($map)) |
|
2359 |
+ { |
|
2360 |
+ // "rest" array parameter, fill every remaining params in it and then break |
|
2361 |
+ if($v[0] === '*') |
|
2362 |
+ { |
|
2363 |
+ if(count($ps) === 0) |
|
2364 |
+ { |
|
2365 |
+ if($v[1]===false) |
|
2366 |
+ $this->triggerError('Rest argument missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
|
2367 |
+ else |
|
2368 |
+ break; |
|
2369 |
+ } |
|
2370 |
+ $tmp = array(); |
|
2371 |
+ $tmp2 = array(); |
|
2372 |
+ foreach($ps as $i=>$p) |
|
2373 |
+ { |
|
2374 |
+ $tmp[$i] = $p[0]; |
|
2375 |
+ $tmp2[$i] = $p[1]; |
|
2376 |
+ } |
|
2377 |
+ $paramlist[$v[0]] = array($tmp, $tmp2); |
|
2378 |
+ unset($tmp, $tmp2, $i, $p); |
|
2379 |
+ break; |
|
2380 |
+ } |
|
2381 |
+ // parameter is defined |
|
2382 |
+ elseif(isset($ps[$v[0]])) |
|
2383 |
+ { |
|
2384 |
+ $paramlist[$v[0]] = $ps[$v[0]]; |
|
2385 |
+ unset($ps[$v[0]]); |
|
2386 |
+ } |
|
2387 |
+ // parameter is not defined and not optional, throw error |
|
2388 |
+ elseif($v[1]===false) |
|
2389 |
+ $this->triggerError('Argument '.$k.'/'.$v[0].' missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
|
2390 |
+ // enforce lowercased null if default value is null (php outputs NULL with var export) |
|
2391 |
+ elseif($v[2]===null) |
|
2392 |
+ $paramlist[$v[0]] = array('null', null); |
|
2393 |
+ // outputs default value with var_export |
|
2394 |
+ else |
|
2395 |
+ $paramlist[$v[0]] = array(var_export($v[2], true), $v[2]); |
|
2396 |
+ } |
|
2397 |
+ } |
|
2398 |
+ // php call or no parameter call |
|
2399 |
+ elseif($callType===1||$callType===0) |
|
2400 |
+ { |
|
2401 |
+ // loops over the param map and assigns values from the template or default value for unset optional params |
|
2402 |
+ while(list($k,$v) = each($map)) |
|
2403 |
+ { |
|
2404 |
+ // "rest" array parameter, fill every remaining params in it and then break |
|
2405 |
+ if($v[0] === '*') |
|
2406 |
+ { |
|
2407 |
+ if(count($params) === 0) |
|
2408 |
+ { |
|
2409 |
+ if($v[1]===false) |
|
2410 |
+ $this->triggerError('Rest argument missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
|
2411 |
+ else |
|
2412 |
+ break; |
|
2413 |
+ } |
|
2414 |
+ $tmp = array(); |
|
2415 |
+ $tmp2 = array(); |
|
2416 |
+ $i = 0; |
|
2417 |
+ foreach($params as $p) |
|
2418 |
+ { |
|
2419 |
+ $tmp[$i] = $p[0]; |
|
2420 |
+ $tmp2[$i++] = $p[1]; |
|
2421 |
+ } |
|
2422 |
+ $paramlist[$v[0]] = array($tmp, $tmp2); |
|
2423 |
+ unset($tmp, $tmp2, $i, $p); |
|
2424 |
+ break; |
|
2425 |
+ } |
|
2426 |
+ // parameter is defined |
|
2427 |
+ elseif(empty($params)===false) |
|
2428 |
+ { |
|
2429 |
+ $paramlist[$v[0]] = array_shift($params); |
|
2430 |
+ } |
|
2431 |
+ // parameter is not defined and not optional, throw error |
|
2432 |
+ elseif($v[1]===false) |
|
2433 |
+ $this->triggerError('Argument '.$k.'/'.$v[0].' missing for '.str_replace(array('DwooPlugin_', '_compile'), '', (is_array($callback) ? $callback[0] : $callback)), E_USER_ERROR); |
|
2434 |
+ // enforce lowercased null if default value is null (php outputs NULL with var export) |
|
2435 |
+ elseif($v[2]===null) |
|
2436 |
+ $paramlist[$v[0]] = array('null', null); |
|
2437 |
+ // outputs default value with var_export |
|
2438 |
+ else |
|
2439 |
+ $paramlist[$v[0]] = array(var_export($v[2], true), $v[2]); |
|
2440 |
+ } |
|
2441 |
+ } |
|
2442 |
+ // parser failed miserably |
|
2443 |
+ else |
|
2444 |
+ $this->triggerError('This should not happen, please report it if you see this message', E_USER_ERROR); |
|
2445 |
+ |
|
2446 |
+ return $paramlist; |
|
2447 |
+ } |
| 2448 |
2448 |
|
| 2449 |
2449 |
/** |
| 2450 |
2450 |
* returns the parameter map of the given callback, it filters out entries typed as Dwoo and DwooCompiler and turns the rest parameter into a "*" |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * dwoo data object, use it for complex data assignments or if you want to easily pass it |
| 6 |
|
- * around multiple functions to avoid passing an array by reference |
| 7 |
|
- * |
| 8 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 9 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 10 |
|
- * |
| 11 |
|
- * This file is released under the LGPL |
| 12 |
|
- * "GNU Lesser General Public License" |
| 13 |
|
- * More information can be found here: |
| 14 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 15 |
|
- * |
| 16 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 17 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 18 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 19 |
|
- * @link http://dwoo.org/ |
| 20 |
|
- * @version 0.3.4 |
| 21 |
|
- * @date 2008-04-09 |
| 22 |
|
- * @package Dwoo |
| 23 |
|
- */ |
| 24 |
|
-class DwooData implements DwooIDataProvider |
| 25 |
|
-{ |
| 26 |
|
- /** |
| 27 |
|
- * data array |
| 28 |
|
- * |
| 29 |
|
- * @var array |
| 30 |
|
- */ |
| 31 |
|
- protected $data = array(); |
| 32 |
|
- |
| 33 |
|
- /** |
| 34 |
|
- * returns the data array |
| 35 |
|
- * |
| 36 |
|
- * @return array |
| 37 |
|
- */ |
| 38 |
|
- public function getData() |
| 39 |
|
- { |
| 40 |
|
- return $this->data; |
| 41 |
|
- } |
| 42 |
|
- |
| 43 |
|
- /** |
| 44 |
|
- * clears a the entire data or only the given key |
| 45 |
|
- * |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * dwoo data object, use it for complex data assignments or if you want to easily pass it |
|
6 |
+ * around multiple functions to avoid passing an array by reference |
|
7 |
+ * |
|
8 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
9 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
10 |
+ * |
|
11 |
+ * This file is released under the LGPL |
|
12 |
+ * "GNU Lesser General Public License" |
|
13 |
+ * More information can be found here: |
|
14 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
15 |
+ * |
|
16 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
17 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
18 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
19 |
+ * @link http://dwoo.org/ |
|
20 |
+ * @version 0.3.4 |
|
21 |
+ * @date 2008-04-09 |
|
22 |
+ * @package Dwoo |
|
23 |
+ */ |
|
24 |
+class DwooData implements DwooIDataProvider |
|
25 |
+{ |
|
26 |
+ /** |
|
27 |
+ * data array |
|
28 |
+ * |
|
29 |
+ * @var array |
|
30 |
+ */ |
|
31 |
+ protected $data = array(); |
|
32 |
+ |
|
33 |
+ /** |
|
34 |
+ * returns the data array |
|
35 |
+ * |
|
36 |
+ * @return array |
|
37 |
+ */ |
|
38 |
+ public function getData() |
|
39 |
+ { |
|
40 |
+ return $this->data; |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ /** |
|
44 |
+ * clears a the entire data or only the given key |
|
45 |
+ * |
| 46 |
46 |
* @param array|string $name clears only one value if you give a name, multiple values if |
| 47 |
|
- * you give an array of names, or the entire data if left null |
| 48 |
|
- */ |
| 49 |
|
- public function clear($name = null) |
| 50 |
|
- { |
| 51 |
|
- if($name === null) |
| 52 |
|
- { |
| 53 |
|
- $this->data = array(); |
| 54 |
|
- } |
| 55 |
|
- elseif(is_array($name)) |
| 56 |
|
- { |
| 57 |
|
- foreach($name as $index) |
| 58 |
|
- unset($this->data[$index]); |
| 59 |
|
- } |
| 60 |
|
- else |
| 61 |
|
- unset($this->data[$name]); |
| 62 |
|
- } |
| 63 |
|
- |
| 64 |
|
- /** |
| 65 |
|
- * overwrites the entire data with the given array |
| 66 |
|
- * |
| 67 |
|
- * @param array $data the new data array to use |
| 68 |
|
- */ |
| 69 |
|
- public function setData(array $data) |
| 70 |
|
- { |
| 71 |
|
- $this->data = $data; |
| 72 |
|
- } |
| 73 |
|
- |
| 74 |
|
- /** |
| 75 |
|
- * merges the given array(s) with the current data with array_merge |
| 76 |
|
- * |
| 77 |
|
- * @param array $data the array to merge |
| 78 |
|
- * @param array $data2 $data3 ... other arrays to merge, optional, etc. |
| 79 |
|
- */ |
| 80 |
|
- public function mergeData(array $data) |
| 81 |
|
- { |
| 82 |
|
- $args = func_get_args(); |
| 83 |
|
- while(list(,$v) = each($args)) |
| 84 |
|
- if(is_array($v)) |
| 85 |
|
- $this->data = array_merge($this->data, $v); |
| 86 |
|
- } |
| 87 |
|
- |
| 88 |
|
- /** |
| 89 |
|
- * assigns a value or an array of values to the data object |
| 90 |
|
- * |
| 91 |
|
- * @param array|string $name an associative array of multiple (index=>value) or a string |
| 92 |
|
- * that is the index to use, i.e. a value assigned to "foo" will be |
| 93 |
|
- * accessible in the template through {$foo} |
| 94 |
|
- * @param mixed $val the value to assign, or null if $name was an array |
| 95 |
|
- */ |
| 96 |
|
- public function assign($name, $val = null) |
| 97 |
|
- { |
| 98 |
|
- if(is_array($name)) |
| 99 |
|
- { |
| 100 |
|
- reset($name); |
| 101 |
|
- while(list($k,$v) = each($name)) |
| 102 |
|
- $this->data[$k] = $v; |
| 103 |
|
- } |
| 104 |
|
- else |
| 105 |
|
- $this->data[$name] = $val; |
| 106 |
|
- } |
| 107 |
|
- |
| 108 |
|
- /** |
| 109 |
|
- * assigns a value by reference to the data object |
| 110 |
|
- * |
| 111 |
|
- * @param string $name the index to use, i.e. a value assigned to "foo" will be |
| 112 |
|
- * accessible in the template through {$foo} |
| 113 |
|
- * @param mixed $val the value to assign by reference |
| 114 |
|
- */ |
| 115 |
|
- public function assignByRef($name, &$val) |
| 116 |
|
- { |
| 117 |
|
- $this->data[$name] =& $val; |
| 118 |
|
- } |
| 119 |
|
- |
| 120 |
|
- /** |
| 121 |
|
- * appends values or an array of values to the data object |
| 122 |
|
- * |
| 123 |
|
- * @param array|string $name an associative array of multiple (index=>value) or a string |
| 124 |
|
- * that is the index to use, i.e. a value assigned to "foo" will be |
| 125 |
|
- * accessible in the template through {$foo} |
| 126 |
|
- * @param mixed $val the value to assign, or null if $name was an array |
| 127 |
|
- * @param bool $merge true to merge data or false to append, defaults to false |
| 128 |
|
- */ |
| 129 |
|
- public function append($name, $val = null, $merge = false) |
| 130 |
|
- { |
| 131 |
|
- if(is_array($name)) |
| 132 |
|
- { |
| 133 |
|
- foreach($name as $key=>$val) |
| 134 |
|
- { |
| 135 |
|
- if(isset($this->data[$key]) && !is_array($this->data[$key])) |
| 136 |
|
- settype($this->data[$key], 'array'); |
| 137 |
|
- |
| 138 |
|
- if($merge === true && is_array($val)) |
| 139 |
|
- $this->data[$key] = $val + $this->data[$key]; |
| 140 |
|
- else |
| 141 |
|
- $this->data[$key][] = $val; |
| 142 |
|
- } |
| 143 |
|
- } |
| 144 |
|
- elseif($val !== null) |
| 145 |
|
- { |
| 146 |
|
- if(isset($this->data[$name]) && !is_array($this->data[$name])) |
| 147 |
|
- settype($this->data[$name], 'array'); |
| 148 |
|
- |
| 149 |
|
- if($merge === true && is_array($val)) |
| 150 |
|
- $this->data[$name] = $val + $this->data[$name]; |
| 151 |
|
- else |
| 152 |
|
- $this->data[$name][] = $val; |
| 153 |
|
- } |
| 154 |
|
- } |
| 155 |
|
- |
| 156 |
|
- /** |
| 157 |
|
- * appends a value by reference to the data object |
| 158 |
|
- * |
| 159 |
|
- * @param string $name the index to use, i.e. a value assigned to "foo" will be |
| 160 |
|
- * accessible in the template through {$foo} |
| 161 |
|
- * @param mixed $val the value to append by reference |
| 162 |
|
- * @param bool $merge true to merge data or false to append, defaults to false |
| 163 |
|
- */ |
| 164 |
|
- public function appendByRef($name, &$val, $merge = false) |
| 165 |
|
- { |
| 166 |
|
- if(isset($this->data[$name]) && !is_array($this->data[$name])) |
| 167 |
|
- settype($this->data[$name], 'array'); |
| 168 |
|
- |
| 169 |
|
- if($merge === true && is_array($val)) |
| 170 |
|
- { |
| 171 |
|
- foreach($val as $key => &$val) |
| 172 |
|
- $this->data[$name][$key] =& $val; |
| 173 |
|
- } |
| 174 |
|
- else |
| 175 |
|
- $this->data[$name][] =& $val; |
| 176 |
|
- } |
| 177 |
|
-} |
| 178 |
|
- |
|
47 |
+ * you give an array of names, or the entire data if left null |
|
48 |
+ */ |
|
49 |
+ public function clear($name = null) |
|
50 |
+ { |
|
51 |
+ if($name === null) |
|
52 |
+ { |
|
53 |
+ $this->data = array(); |
|
54 |
+ } |
|
55 |
+ elseif(is_array($name)) |
|
56 |
+ { |
|
57 |
+ foreach($name as $index) |
|
58 |
+ unset($this->data[$index]); |
|
59 |
+ } |
|
60 |
+ else |
|
61 |
+ unset($this->data[$name]); |
|
62 |
+ } |
|
63 |
+ |
|
64 |
+ /** |
|
65 |
+ * overwrites the entire data with the given array |
|
66 |
+ * |
|
67 |
+ * @param array $data the new data array to use |
|
68 |
+ */ |
|
69 |
+ public function setData(array $data) |
|
70 |
+ { |
|
71 |
+ $this->data = $data; |
|
72 |
+ } |
|
73 |
+ |
|
74 |
+ /** |
|
75 |
+ * merges the given array(s) with the current data with array_merge |
|
76 |
+ * |
|
77 |
+ * @param array $data the array to merge |
|
78 |
+ * @param array $data2 $data3 ... other arrays to merge, optional, etc. |
|
79 |
+ */ |
|
80 |
+ public function mergeData(array $data) |
|
81 |
+ { |
|
82 |
+ $args = func_get_args(); |
|
83 |
+ while(list(,$v) = each($args)) |
|
84 |
+ if(is_array($v)) |
|
85 |
+ $this->data = array_merge($this->data, $v); |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ /** |
|
89 |
+ * assigns a value or an array of values to the data object |
|
90 |
+ * |
|
91 |
+ * @param array|string $name an associative array of multiple (index=>value) or a string |
|
92 |
+ * that is the index to use, i.e. a value assigned to "foo" will be |
|
93 |
+ * accessible in the template through {$foo} |
|
94 |
+ * @param mixed $val the value to assign, or null if $name was an array |
|
95 |
+ */ |
|
96 |
+ public function assign($name, $val = null) |
|
97 |
+ { |
|
98 |
+ if(is_array($name)) |
|
99 |
+ { |
|
100 |
+ reset($name); |
|
101 |
+ while(list($k,$v) = each($name)) |
|
102 |
+ $this->data[$k] = $v; |
|
103 |
+ } |
|
104 |
+ else |
|
105 |
+ $this->data[$name] = $val; |
|
106 |
+ } |
|
107 |
+ |
|
108 |
+ /** |
|
109 |
+ * assigns a value by reference to the data object |
|
110 |
+ * |
|
111 |
+ * @param string $name the index to use, i.e. a value assigned to "foo" will be |
|
112 |
+ * accessible in the template through {$foo} |
|
113 |
+ * @param mixed $val the value to assign by reference |
|
114 |
+ */ |
|
115 |
+ public function assignByRef($name, &$val) |
|
116 |
+ { |
|
117 |
+ $this->data[$name] =& $val; |
|
118 |
+ } |
|
119 |
+ |
|
120 |
+ /** |
|
121 |
+ * appends values or an array of values to the data object |
|
122 |
+ * |
|
123 |
+ * @param array|string $name an associative array of multiple (index=>value) or a string |
|
124 |
+ * that is the index to use, i.e. a value assigned to "foo" will be |
|
125 |
+ * accessible in the template through {$foo} |
|
126 |
+ * @param mixed $val the value to assign, or null if $name was an array |
|
127 |
+ * @param bool $merge true to merge data or false to append, defaults to false |
|
128 |
+ */ |
|
129 |
+ public function append($name, $val = null, $merge = false) |
|
130 |
+ { |
|
131 |
+ if(is_array($name)) |
|
132 |
+ { |
|
133 |
+ foreach($name as $key=>$val) |
|
134 |
+ { |
|
135 |
+ if(isset($this->data[$key]) && !is_array($this->data[$key])) |
|
136 |
+ settype($this->data[$key], 'array'); |
|
137 |
+ |
|
138 |
+ if($merge === true && is_array($val)) |
|
139 |
+ $this->data[$key] = $val + $this->data[$key]; |
|
140 |
+ else |
|
141 |
+ $this->data[$key][] = $val; |
|
142 |
+ } |
|
143 |
+ } |
|
144 |
+ elseif($val !== null) |
|
145 |
+ { |
|
146 |
+ if(isset($this->data[$name]) && !is_array($this->data[$name])) |
|
147 |
+ settype($this->data[$name], 'array'); |
|
148 |
+ |
|
149 |
+ if($merge === true && is_array($val)) |
|
150 |
+ $this->data[$name] = $val + $this->data[$name]; |
|
151 |
+ else |
|
152 |
+ $this->data[$name][] = $val; |
|
153 |
+ } |
|
154 |
+ } |
|
155 |
+ |
|
156 |
+ /** |
|
157 |
+ * appends a value by reference to the data object |
|
158 |
+ * |
|
159 |
+ * @param string $name the index to use, i.e. a value assigned to "foo" will be |
|
160 |
+ * accessible in the template through {$foo} |
|
161 |
+ * @param mixed $val the value to append by reference |
|
162 |
+ * @param bool $merge true to merge data or false to append, defaults to false |
|
163 |
+ */ |
|
164 |
+ public function appendByRef($name, &$val, $merge = false) |
|
165 |
+ { |
|
166 |
+ if(isset($this->data[$name]) && !is_array($this->data[$name])) |
|
167 |
+ settype($this->data[$name], 'array'); |
|
168 |
+ |
|
169 |
+ if($merge === true && is_array($val)) |
|
170 |
+ { |
|
171 |
+ foreach($val as $key => &$val) |
|
172 |
+ $this->data[$name][$key] =& $val; |
|
173 |
+ } |
|
174 |
+ else |
|
175 |
+ $this->data[$name][] =& $val; |
|
176 |
+ } |
|
177 |
+} |
|
178 |
+ |
| 179 |
179 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * base plugin class |
| 6 |
|
- * |
| 7 |
|
- * you have to implement the <em>process()</em> method, it will receive the parameters that |
| 8 |
|
- * are in the template code |
| 9 |
|
- * |
| 10 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 11 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 12 |
|
- * |
| 13 |
|
- * This file is released under the LGPL |
| 14 |
|
- * "GNU Lesser General Public License" |
| 15 |
|
- * More information can be found here: |
| 16 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 17 |
|
- * |
| 18 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 19 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 20 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 21 |
|
- * @link http://dwoo.org/ |
| 22 |
|
- * @version 0.3.4 |
| 23 |
|
- * @date 2008-04-09 |
| 24 |
|
- * @package Dwoo |
| 25 |
|
- */ |
| 26 |
|
-abstract class DwooPlugin |
| 27 |
|
-{ |
| 28 |
|
- /** |
| 29 |
|
- * the dwoo instance that runs this plugin |
| 30 |
|
- * |
| 31 |
|
- * @var Dwoo |
| 32 |
|
- */ |
| 33 |
|
- protected $dwoo; |
| 34 |
|
- |
| 35 |
|
- /** |
| 36 |
|
- * constructor, if you override it, call parent::__construct($dwoo); or assign |
| 37 |
|
- * the dwoo instance yourself if you need it |
| 38 |
|
- * |
| 39 |
|
- * @param Dwoo $dwoo the dwoo instance that runs this plugin |
| 40 |
|
- */ |
| 41 |
|
- public function __construct(Dwoo $dwoo) |
| 42 |
|
- { |
| 43 |
|
- $this->dwoo = $dwoo; |
| 44 |
|
- } |
| 45 |
|
- |
| 46 |
|
- // plugins that have arguments should always implement : |
| 47 |
|
- // public function process($arg, $arg, ...) |
| 48 |
|
- // or for block plugins : |
| 49 |
|
- // public function init($arg, $arg, ...) |
| 50 |
|
- |
| 51 |
|
- // this could be enforced with : |
| 52 |
|
- // public function process(...) |
| 53 |
|
- // if my bug entry gets enough interest one day.. |
| 54 |
|
- // see => http://bugs.php.net/bug.php?id=44043 |
| 55 |
|
-} |
| 56 |
|
- |
| 57 |
|
-/** |
| 58 |
|
- * base class for block plugins |
| 59 |
|
- * |
| 60 |
|
- * you have to implement the <em>init()</em> method, it will receive the parameters that |
| 61 |
|
- * are in the template code and is called when the block starts |
| 62 |
|
- * |
| 63 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 64 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 65 |
|
- * |
| 66 |
|
- * This file is released under the LGPL |
| 67 |
|
- * "GNU Lesser General Public License" |
| 68 |
|
- * More information can be found here: |
| 69 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 70 |
|
- * |
| 71 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 72 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 73 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 74 |
|
- * @link http://dwoo.org/ |
| 75 |
|
- * @version 0.3.4 |
| 76 |
|
- * @date 2008-04-09 |
| 77 |
|
- * @package Dwoo |
| 78 |
|
- */ |
| 79 |
|
-abstract class DwooBlockPlugin extends DwooPlugin |
| 80 |
|
-{ |
| 81 |
|
- /** |
| 82 |
|
- * stores the contents of the block while it runs |
| 83 |
|
- * |
| 84 |
|
- * @var string |
| 85 |
|
- */ |
| 86 |
|
- protected $buffer = ''; |
| 87 |
|
- |
| 88 |
|
- /** |
| 89 |
|
- * buffers input, override only if necessary |
| 90 |
|
- * |
| 91 |
|
- * @var string $input the content that must be buffered |
| 92 |
|
- */ |
| 93 |
|
- public function buffer($input) |
| 94 |
|
- { |
| 95 |
|
- $this->buffer .= $input; |
| 96 |
|
- } |
| 97 |
|
- |
| 98 |
|
- // initialization code, receives the parameters from {block param1, param2} |
| 99 |
|
- // public function init($arg, $arg, ...); |
| 100 |
|
- |
| 101 |
|
- /** |
| 102 |
|
- * called when the block ends, this is most of the time followed right away by a call |
| 103 |
|
- * of <em>process()</em> but not always, so this should be used to do any shutdown operations on the |
| 104 |
|
- * block object, if required. |
| 105 |
|
- */ |
| 106 |
|
- public function end() |
| 107 |
|
- { |
| 108 |
|
- } |
| 109 |
|
- |
| 110 |
|
- /** |
| 111 |
|
- * called when the block output is required by a parent block |
| 112 |
|
- * |
| 113 |
|
- * this must read $this->buffer and return it processed |
| 114 |
|
- * |
| 115 |
|
- * @return string |
| 116 |
|
- */ |
| 117 |
|
- public function process() |
| 118 |
|
- { |
| 119 |
|
- return $this->buffer; |
| 120 |
|
- } |
| 121 |
|
- |
| 122 |
|
- /** |
| 123 |
|
- * called at compile time to define what the block should output in the compiled template code, happens when the block is declared |
| 124 |
|
- * |
| 125 |
|
- * basically this will replace the {block arg arg arg} tag in the template |
| 126 |
|
- * |
| 127 |
|
- * @param DwooCompiler $compiler the compiler instance that calls this function |
| 128 |
|
- * @param array $params an array containing original and compiled parameters |
| 129 |
|
- * @param string $prepend that is just meant to allow a child class to call |
| 130 |
|
- * parent::postProcessing($compiler, $params, "foo();") to add a command before the |
| 131 |
|
- * default commands are executed |
| 132 |
|
- * @param string $append that is just meant to allow a child class to call |
| 133 |
|
- * parent::postProcessing($compiler, $params, null, "foo();") to add a command after the |
| 134 |
|
- * default commands are executed |
| 135 |
|
- * @param string $type the type is the plugin class name used |
| 136 |
|
- */ |
| 137 |
|
- public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
| 138 |
|
- { |
| 139 |
|
- return DwooCompiler::PHP_OPEN.$prepend.'$this->addStack("'.$type.'", array('.implode(', ', $compiler->getCompiledParams($params)).'));'.$append.DwooCompiler::PHP_CLOSE; |
| 140 |
|
- } |
| 141 |
|
- |
| 142 |
|
- /** |
| 143 |
|
- * called at compile time to define what the block should output in the compiled template code, happens when the block is ended |
| 144 |
|
- * |
| 145 |
|
- * basically this will replace the {/block} tag in the template |
| 146 |
|
- * |
| 147 |
|
- * @see preProcessing |
| 148 |
|
- * @param DwooCompiler $compiler the compiler instance that calls this function |
| 149 |
|
- * @param array $params an array containing original and compiled parameters, see preProcessing() for more details |
| 150 |
|
- * @param string $prepend that is just meant to allow a child class to call |
| 151 |
|
- * parent::postProcessing($compiler, $params, "foo();") to add a command before the |
| 152 |
|
- * default commands are executed |
| 153 |
|
- * @param string $append that is just meant to allow a child class to call |
| 154 |
|
- * parent::postProcessing($compiler, $params, null, "foo();") to add a command after the |
| 155 |
|
- * default commands are executed |
| 156 |
|
- */ |
| 157 |
|
- public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
| 158 |
|
- { |
| 159 |
|
- return DwooCompiler::PHP_OPEN.$prepend.'$this->delStack();'.$append.DwooCompiler::PHP_CLOSE; |
| 160 |
|
- } |
| 161 |
|
-} |
| 162 |
|
- |
| 163 |
|
-/** |
| 164 |
|
- * base class for filters |
| 165 |
|
- * |
| 166 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 167 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 168 |
|
- * |
| 169 |
|
- * This file is released under the LGPL |
| 170 |
|
- * "GNU Lesser General Public License" |
| 171 |
|
- * More information can be found here: |
| 172 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 173 |
|
- * |
| 174 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 175 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 176 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 177 |
|
- * @link http://dwoo.org/ |
| 178 |
|
- * @version 0.3.4 |
| 179 |
|
- * @date 2008-04-09 |
| 180 |
|
- * @package Dwoo |
| 181 |
|
- */ |
| 182 |
|
-abstract class DwooFilter |
| 183 |
|
-{ |
| 184 |
|
- /** |
| 185 |
|
- * the dwoo instance that runs this filter |
| 186 |
|
- * |
| 187 |
|
- * @var Dwoo |
| 188 |
|
- */ |
| 189 |
|
- protected $dwoo; |
| 190 |
|
- |
| 191 |
|
- /** |
| 192 |
|
- * constructor, if you override it, call parent::__construct($dwoo); or assign |
| 193 |
|
- * the dwoo instance yourself if you need it |
| 194 |
|
- * |
| 195 |
|
- * @param Dwoo $dwoo the dwoo instance that runs this plugin |
| 196 |
|
- */ |
| 197 |
|
- public function __construct(Dwoo $dwoo) |
| 198 |
|
- { |
| 199 |
|
- $this->dwoo = $dwoo; |
| 200 |
|
- } |
| 201 |
|
- |
| 202 |
|
- /** |
| 203 |
|
- * processes the input and returns it filtered |
| 204 |
|
- * |
| 205 |
|
- * @param string $input the template to process |
| 206 |
|
- * @return string |
| 207 |
|
- */ |
| 208 |
|
- abstract public function process($input); |
| 209 |
|
-} |
| 210 |
|
- |
| 211 |
|
-/** |
| 212 |
|
- * base class for processors |
| 213 |
|
- * |
| 214 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 215 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 216 |
|
- * |
| 217 |
|
- * This file is released under the LGPL |
| 218 |
|
- * "GNU Lesser General Public License" |
| 219 |
|
- * More information can be found here: |
| 220 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 221 |
|
- * |
| 222 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 223 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 224 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 225 |
|
- * @link http://dwoo.org/ |
| 226 |
|
- * @version 0.3.4 |
| 227 |
|
- * @date 2008-04-09 |
| 228 |
|
- * @package Dwoo |
| 229 |
|
- */ |
| 230 |
|
-abstract class DwooProcessor |
| 231 |
|
-{ |
| 232 |
|
- /** |
| 233 |
|
- * the compiler instance that runs this processor |
| 234 |
|
- * |
| 235 |
|
- * @var Dwoo |
| 236 |
|
- */ |
| 237 |
|
- protected $compiler; |
| 238 |
|
- |
| 239 |
|
- /** |
| 240 |
|
- * constructor, if you override it, call parent::__construct($dwoo); or assign |
| 241 |
|
- * the dwoo instance yourself if you need it |
| 242 |
|
- * |
| 243 |
|
- * @param Dwoo $dwoo the dwoo instance that runs this plugin |
| 244 |
|
- */ |
| 245 |
|
- public function __construct(DwooCompiler $compiler) |
| 246 |
|
- { |
| 247 |
|
- $this->compiler = $compiler; |
| 248 |
|
- } |
| 249 |
|
- |
| 250 |
|
- /** |
| 251 |
|
- * processes the input and returns it filtered |
| 252 |
|
- * |
| 253 |
|
- * @param string $input the template to process |
| 254 |
|
- * @return string |
| 255 |
|
- */ |
| 256 |
|
- abstract public function process($input); |
| 257 |
|
-} |
| 258 |
|
- |
| 259 |
|
-/** |
| 260 |
|
- * interface that represents a compilable plugin |
| 261 |
|
- * |
| 262 |
|
- * implement this to notify the compiler that this plugin does not need to be loaded at runtime. |
| 263 |
|
- * |
| 264 |
|
- * to implement it right, you must implement <em>public static function compile(DwooCompiler $compiler, $arg, $arg, ...)</em>, |
| 265 |
|
- * which replaces the <em>process()</em> method (that means <em>compile()</em> should have all arguments it requires). |
| 266 |
|
- * |
| 267 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 268 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 269 |
|
- * |
| 270 |
|
- * This file is released under the LGPL |
| 271 |
|
- * "GNU Lesser General Public License" |
| 272 |
|
- * More information can be found here: |
| 273 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 274 |
|
- * |
| 275 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 276 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 277 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 278 |
|
- * @link http://dwoo.org/ |
| 279 |
|
- * @version 0.3.4 |
| 280 |
|
- * @date 2008-04-09 |
| 281 |
|
- * @package Dwoo |
| 282 |
|
- */ |
| 283 |
|
-interface DwooICompilable |
| 284 |
|
-{ |
| 285 |
|
- // this replaces the process function |
| 286 |
|
- //public static function compile(DwooCompiler $compiler, $arg, $arg, ...); |
| 287 |
|
-} |
| 288 |
|
- |
| 289 |
|
-/** |
| 290 |
|
- * interface that represents a compilable block plugin |
| 291 |
|
- * |
| 292 |
|
- * implement this to notify the compiler that this plugin does not need to be loaded at runtime. |
| 293 |
|
- * |
| 294 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 295 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 296 |
|
- * |
| 297 |
|
- * This file is released under the LGPL |
| 298 |
|
- * "GNU Lesser General Public License" |
| 299 |
|
- * More information can be found here: |
| 300 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 301 |
|
- * |
| 302 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 303 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 304 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 305 |
|
- * @link http://dwoo.org/ |
| 306 |
|
- * @version 0.3.4 |
| 307 |
|
- * @date 2008-04-09 |
| 308 |
|
- * @package Dwoo |
| 309 |
|
- */ |
| 310 |
|
-interface DwooICompilableBlock |
| 311 |
|
-{ |
| 312 |
|
-} |
| 313 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * base plugin class |
|
6 |
+ * |
|
7 |
+ * you have to implement the <em>process()</em> method, it will receive the parameters that |
|
8 |
+ * are in the template code |
|
9 |
+ * |
|
10 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
11 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
12 |
+ * |
|
13 |
+ * This file is released under the LGPL |
|
14 |
+ * "GNU Lesser General Public License" |
|
15 |
+ * More information can be found here: |
|
16 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
17 |
+ * |
|
18 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
19 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
20 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
21 |
+ * @link http://dwoo.org/ |
|
22 |
+ * @version 0.3.4 |
|
23 |
+ * @date 2008-04-09 |
|
24 |
+ * @package Dwoo |
|
25 |
+ */ |
|
26 |
+abstract class DwooPlugin |
|
27 |
+{ |
|
28 |
+ /** |
|
29 |
+ * the dwoo instance that runs this plugin |
|
30 |
+ * |
|
31 |
+ * @var Dwoo |
|
32 |
+ */ |
|
33 |
+ protected $dwoo; |
|
34 |
+ |
|
35 |
+ /** |
|
36 |
+ * constructor, if you override it, call parent::__construct($dwoo); or assign |
|
37 |
+ * the dwoo instance yourself if you need it |
|
38 |
+ * |
|
39 |
+ * @param Dwoo $dwoo the dwoo instance that runs this plugin |
|
40 |
+ */ |
|
41 |
+ public function __construct(Dwoo $dwoo) |
|
42 |
+ { |
|
43 |
+ $this->dwoo = $dwoo; |
|
44 |
+ } |
|
45 |
+ |
|
46 |
+ // plugins that have arguments should always implement : |
|
47 |
+ // public function process($arg, $arg, ...) |
|
48 |
+ // or for block plugins : |
|
49 |
+ // public function init($arg, $arg, ...) |
|
50 |
+ |
|
51 |
+ // this could be enforced with : |
|
52 |
+ // public function process(...) |
|
53 |
+ // if my bug entry gets enough interest one day.. |
|
54 |
+ // see => http://bugs.php.net/bug.php?id=44043 |
|
55 |
+} |
|
56 |
+ |
|
57 |
+/** |
|
58 |
+ * base class for block plugins |
|
59 |
+ * |
|
60 |
+ * you have to implement the <em>init()</em> method, it will receive the parameters that |
|
61 |
+ * are in the template code and is called when the block starts |
|
62 |
+ * |
|
63 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
64 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
65 |
+ * |
|
66 |
+ * This file is released under the LGPL |
|
67 |
+ * "GNU Lesser General Public License" |
|
68 |
+ * More information can be found here: |
|
69 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
70 |
+ * |
|
71 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
72 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
73 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
74 |
+ * @link http://dwoo.org/ |
|
75 |
+ * @version 0.3.4 |
|
76 |
+ * @date 2008-04-09 |
|
77 |
+ * @package Dwoo |
|
78 |
+ */ |
|
79 |
+abstract class DwooBlockPlugin extends DwooPlugin |
|
80 |
+{ |
|
81 |
+ /** |
|
82 |
+ * stores the contents of the block while it runs |
|
83 |
+ * |
|
84 |
+ * @var string |
|
85 |
+ */ |
|
86 |
+ protected $buffer = ''; |
|
87 |
+ |
|
88 |
+ /** |
|
89 |
+ * buffers input, override only if necessary |
|
90 |
+ * |
|
91 |
+ * @var string $input the content that must be buffered |
|
92 |
+ */ |
|
93 |
+ public function buffer($input) |
|
94 |
+ { |
|
95 |
+ $this->buffer .= $input; |
|
96 |
+ } |
|
97 |
+ |
|
98 |
+ // initialization code, receives the parameters from {block param1, param2} |
|
99 |
+ // public function init($arg, $arg, ...); |
|
100 |
+ |
|
101 |
+ /** |
|
102 |
+ * called when the block ends, this is most of the time followed right away by a call |
|
103 |
+ * of <em>process()</em> but not always, so this should be used to do any shutdown operations on the |
|
104 |
+ * block object, if required. |
|
105 |
+ */ |
|
106 |
+ public function end() |
|
107 |
+ { |
|
108 |
+ } |
|
109 |
+ |
|
110 |
+ /** |
|
111 |
+ * called when the block output is required by a parent block |
|
112 |
+ * |
|
113 |
+ * this must read $this->buffer and return it processed |
|
114 |
+ * |
|
115 |
+ * @return string |
|
116 |
+ */ |
|
117 |
+ public function process() |
|
118 |
+ { |
|
119 |
+ return $this->buffer; |
|
120 |
+ } |
|
121 |
+ |
|
122 |
+ /** |
|
123 |
+ * called at compile time to define what the block should output in the compiled template code, happens when the block is declared |
|
124 |
+ * |
|
125 |
+ * basically this will replace the {block arg arg arg} tag in the template |
|
126 |
+ * |
|
127 |
+ * @param DwooCompiler $compiler the compiler instance that calls this function |
|
128 |
+ * @param array $params an array containing original and compiled parameters |
|
129 |
+ * @param string $prepend that is just meant to allow a child class to call |
|
130 |
+ * parent::postProcessing($compiler, $params, "foo();") to add a command before the |
|
131 |
+ * default commands are executed |
|
132 |
+ * @param string $append that is just meant to allow a child class to call |
|
133 |
+ * parent::postProcessing($compiler, $params, null, "foo();") to add a command after the |
|
134 |
+ * default commands are executed |
|
135 |
+ * @param string $type the type is the plugin class name used |
|
136 |
+ */ |
|
137 |
+ public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
|
138 |
+ { |
|
139 |
+ return DwooCompiler::PHP_OPEN.$prepend.'$this->addStack("'.$type.'", array('.implode(', ', $compiler->getCompiledParams($params)).'));'.$append.DwooCompiler::PHP_CLOSE; |
|
140 |
+ } |
|
141 |
+ |
|
142 |
+ /** |
|
143 |
+ * called at compile time to define what the block should output in the compiled template code, happens when the block is ended |
|
144 |
+ * |
|
145 |
+ * basically this will replace the {/block} tag in the template |
|
146 |
+ * |
|
147 |
+ * @see preProcessing |
|
148 |
+ * @param DwooCompiler $compiler the compiler instance that calls this function |
|
149 |
+ * @param array $params an array containing original and compiled parameters, see preProcessing() for more details |
|
150 |
+ * @param string $prepend that is just meant to allow a child class to call |
|
151 |
+ * parent::postProcessing($compiler, $params, "foo();") to add a command before the |
|
152 |
+ * default commands are executed |
|
153 |
+ * @param string $append that is just meant to allow a child class to call |
|
154 |
+ * parent::postProcessing($compiler, $params, null, "foo();") to add a command after the |
|
155 |
+ * default commands are executed |
|
156 |
+ */ |
|
157 |
+ public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
|
158 |
+ { |
|
159 |
+ return DwooCompiler::PHP_OPEN.$prepend.'$this->delStack();'.$append.DwooCompiler::PHP_CLOSE; |
|
160 |
+ } |
|
161 |
+} |
|
162 |
+ |
|
163 |
+/** |
|
164 |
+ * base class for filters |
|
165 |
+ * |
|
166 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
167 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
168 |
+ * |
|
169 |
+ * This file is released under the LGPL |
|
170 |
+ * "GNU Lesser General Public License" |
|
171 |
+ * More information can be found here: |
|
172 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
173 |
+ * |
|
174 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
175 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
176 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
177 |
+ * @link http://dwoo.org/ |
|
178 |
+ * @version 0.3.4 |
|
179 |
+ * @date 2008-04-09 |
|
180 |
+ * @package Dwoo |
|
181 |
+ */ |
|
182 |
+abstract class DwooFilter |
|
183 |
+{ |
|
184 |
+ /** |
|
185 |
+ * the dwoo instance that runs this filter |
|
186 |
+ * |
|
187 |
+ * @var Dwoo |
|
188 |
+ */ |
|
189 |
+ protected $dwoo; |
|
190 |
+ |
|
191 |
+ /** |
|
192 |
+ * constructor, if you override it, call parent::__construct($dwoo); or assign |
|
193 |
+ * the dwoo instance yourself if you need it |
|
194 |
+ * |
|
195 |
+ * @param Dwoo $dwoo the dwoo instance that runs this plugin |
|
196 |
+ */ |
|
197 |
+ public function __construct(Dwoo $dwoo) |
|
198 |
+ { |
|
199 |
+ $this->dwoo = $dwoo; |
|
200 |
+ } |
|
201 |
+ |
|
202 |
+ /** |
|
203 |
+ * processes the input and returns it filtered |
|
204 |
+ * |
|
205 |
+ * @param string $input the template to process |
|
206 |
+ * @return string |
|
207 |
+ */ |
|
208 |
+ abstract public function process($input); |
|
209 |
+} |
|
210 |
+ |
|
211 |
+/** |
|
212 |
+ * base class for processors |
|
213 |
+ * |
|
214 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
215 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
216 |
+ * |
|
217 |
+ * This file is released under the LGPL |
|
218 |
+ * "GNU Lesser General Public License" |
|
219 |
+ * More information can be found here: |
|
220 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
221 |
+ * |
|
222 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
223 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
224 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
225 |
+ * @link http://dwoo.org/ |
|
226 |
+ * @version 0.3.4 |
|
227 |
+ * @date 2008-04-09 |
|
228 |
+ * @package Dwoo |
|
229 |
+ */ |
|
230 |
+abstract class DwooProcessor |
|
231 |
+{ |
|
232 |
+ /** |
|
233 |
+ * the compiler instance that runs this processor |
|
234 |
+ * |
|
235 |
+ * @var Dwoo |
|
236 |
+ */ |
|
237 |
+ protected $compiler; |
|
238 |
+ |
|
239 |
+ /** |
|
240 |
+ * constructor, if you override it, call parent::__construct($dwoo); or assign |
|
241 |
+ * the dwoo instance yourself if you need it |
|
242 |
+ * |
|
243 |
+ * @param Dwoo $dwoo the dwoo instance that runs this plugin |
|
244 |
+ */ |
|
245 |
+ public function __construct(DwooCompiler $compiler) |
|
246 |
+ { |
|
247 |
+ $this->compiler = $compiler; |
|
248 |
+ } |
|
249 |
+ |
|
250 |
+ /** |
|
251 |
+ * processes the input and returns it filtered |
|
252 |
+ * |
|
253 |
+ * @param string $input the template to process |
|
254 |
+ * @return string |
|
255 |
+ */ |
|
256 |
+ abstract public function process($input); |
|
257 |
+} |
|
258 |
+ |
|
259 |
+/** |
|
260 |
+ * interface that represents a compilable plugin |
|
261 |
+ * |
|
262 |
+ * implement this to notify the compiler that this plugin does not need to be loaded at runtime. |
|
263 |
+ * |
|
264 |
+ * to implement it right, you must implement <em>public static function compile(DwooCompiler $compiler, $arg, $arg, ...)</em>, |
|
265 |
+ * which replaces the <em>process()</em> method (that means <em>compile()</em> should have all arguments it requires). |
|
266 |
+ * |
|
267 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
268 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
269 |
+ * |
|
270 |
+ * This file is released under the LGPL |
|
271 |
+ * "GNU Lesser General Public License" |
|
272 |
+ * More information can be found here: |
|
273 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
274 |
+ * |
|
275 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
276 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
277 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
278 |
+ * @link http://dwoo.org/ |
|
279 |
+ * @version 0.3.4 |
|
280 |
+ * @date 2008-04-09 |
|
281 |
+ * @package Dwoo |
|
282 |
+ */ |
|
283 |
+interface DwooICompilable |
|
284 |
+{ |
|
285 |
+ // this replaces the process function |
|
286 |
+ //public static function compile(DwooCompiler $compiler, $arg, $arg, ...); |
|
287 |
+} |
|
288 |
+ |
|
289 |
+/** |
|
290 |
+ * interface that represents a compilable block plugin |
|
291 |
+ * |
|
292 |
+ * implement this to notify the compiler that this plugin does not need to be loaded at runtime. |
|
293 |
+ * |
|
294 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
295 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
296 |
+ * |
|
297 |
+ * This file is released under the LGPL |
|
298 |
+ * "GNU Lesser General Public License" |
|
299 |
+ * More information can be found here: |
|
300 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
301 |
+ * |
|
302 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
303 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
304 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
305 |
+ * @link http://dwoo.org/ |
|
306 |
+ * @version 0.3.4 |
|
307 |
+ * @date 2008-04-09 |
|
308 |
+ * @package Dwoo |
|
309 |
+ */ |
|
310 |
+interface DwooICompilableBlock |
|
311 |
+{ |
|
312 |
+} |
|
313 |
+ |
| 314 |
314 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-if(!defined('DIR_SEP')) { |
| 5 |
|
- define('DIR_SEP', DIRECTORY_SEPARATOR); |
| 6 |
|
-} |
| 7 |
|
- |
| 8 |
|
-if(!defined('SMARTY_PHP_PASSTHRU')) |
| 9 |
|
-{ |
| 10 |
|
- define('SMARTY_PHP_PASSTHRU', 0); |
| 11 |
|
- define('SMARTY_PHP_QUOTE', 1); |
| 12 |
|
- define('SMARTY_PHP_REMOVE', 2); |
| 13 |
|
- define('SMARTY_PHP_ALLOW', 3); |
| 14 |
|
-} |
| 15 |
|
- |
| 16 |
|
-if(class_exists('DwooCompiler', false) === false) |
| 17 |
|
- require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'DwooCompiler.php'; |
| 18 |
|
- |
| 19 |
|
-/** |
| 20 |
|
- * a Smarty compatibility layer for Dwoo |
| 21 |
|
- * |
| 22 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 23 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 24 |
|
- * |
| 25 |
|
- * This file is released under the LGPL |
| 26 |
|
- * "GNU Lesser General Public License" |
| 27 |
|
- * More information can be found here: |
| 28 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 29 |
|
- * |
| 30 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 31 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 32 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 33 |
|
- * @link http://dwoo.org/ |
| 34 |
|
- * @version 0.3.4 |
| 35 |
|
- * @date 2008-04-09 |
| 36 |
|
- * @package Dwoo |
| 37 |
|
- */ |
| 38 |
|
-class DwooSmarty_Adapter extends Dwoo |
| 39 |
|
-{ |
| 40 |
|
- // magic get/set/call functions that handle unsupported features |
| 41 |
|
- public function __set($p, $v) |
| 42 |
|
- { |
| 43 |
|
- if(array_key_exists($p, $this->compat['properties']) !== false) |
| 44 |
|
- { |
| 45 |
|
- if($this->show_compat_errors) |
| 46 |
|
- $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE); |
| 47 |
|
- $this->compat['properties'][$p] = $v; |
| 48 |
|
- } |
| 49 |
|
- else |
| 50 |
|
- { |
| 51 |
|
- if($this->show_compat_errors) |
| 52 |
|
- $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, but it is not listed as such, so you might want to tell me about it at j.boggiano@seld.be', E_USER_NOTICE); |
| 53 |
|
- } |
| 54 |
|
- } |
| 55 |
|
- |
| 56 |
|
- public function __get($p) |
| 57 |
|
- { |
| 58 |
|
- if(array_key_exists($p, $this->compat['properties']) !== false) |
| 59 |
|
- { |
| 60 |
|
- if($this->show_compat_errors) |
| 61 |
|
- $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE); |
| 62 |
|
- return $this->compat['properties'][$p]; |
| 63 |
|
- } |
| 64 |
|
- else |
| 65 |
|
- { |
| 66 |
|
- if($this->show_compat_errors) |
| 67 |
|
- $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, but it is not listed as such, so you might want to tell me about it at j.boggiano@seld.be', E_USER_NOTICE); |
| 68 |
|
- } |
| 69 |
|
- } |
| 70 |
|
- |
| 71 |
|
- public function __call($m, $a) |
| 72 |
|
- { |
| 73 |
|
- if(method_exists($this->dataProvider, $m)) |
| 74 |
|
- call_user_func_array(array($this->dataProvider, $m), $a); |
| 75 |
|
- elseif($this->show_compat_errors) |
| 76 |
|
- { |
| 77 |
|
- if(array_search($m, $this->compat['methods']) !== false) |
| 78 |
|
- $this->triggerError('Method '.$m.' is not available in the DwooSmartyAdapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE); |
| 79 |
|
- else |
| 80 |
|
- $this->triggerError('Method '.$m.' is not available in the DwooSmartyAdapter, but it is not listed as such, so you might want to tell me about it at j.boggiano@seld.be', E_USER_NOTICE); |
| 81 |
|
- } |
| 82 |
|
- } |
| 83 |
|
- |
| 84 |
|
- // list of unsupported properties and methods |
| 85 |
|
- protected $compat = array |
| 86 |
|
- ( |
| 87 |
|
- 'methods' => array |
| 88 |
|
- ( |
| 89 |
|
- 'register_resource', 'unregister_resource', 'load_filter', 'clear_compiled_tpl', |
| 90 |
|
- 'clear_config', 'get_config_vars', 'config_load' |
| 91 |
|
- ), |
| 92 |
|
- 'properties' => array |
| 93 |
|
- ( |
| 94 |
|
- 'cache_handler_func' => null, |
| 95 |
|
- 'debugging' => false, |
| 96 |
|
- 'error_reporting' => null, |
| 97 |
|
- 'debugging_ctrl' => 'NONE', |
| 98 |
|
- 'request_vars_order' => 'EGPCS', |
| 99 |
|
- 'request_use_auto_globals' => true, |
| 100 |
|
- 'use_sub_dirs' => false, |
| 101 |
|
- 'autoload_filters' => array(), |
| 102 |
|
- 'default_template_handler_func' => '', |
| 103 |
|
- 'debug_tpl' => '', |
| 104 |
|
- 'cache_modified_check' => false, |
| 105 |
|
- 'default_modifiers' => array(), |
| 106 |
|
- 'default_resource_type' => 'file', |
| 107 |
|
- 'config_overwrite' => true, |
| 108 |
|
- 'config_booleanize' => true, |
| 109 |
|
- 'config_read_hidden' => false, |
| 110 |
|
- 'config_fix_newlines' => true, |
| 111 |
|
- 'config_class' => 'Config_File', |
| 112 |
|
- ), |
| 113 |
|
- ); |
| 114 |
|
- |
| 115 |
|
- // security vars |
| 116 |
|
- public $security = false; |
| 117 |
|
- public $trusted_dir = array(); |
| 118 |
|
- public $secure_dir = array(); |
| 119 |
|
- public $php_handling = SMARTY_PHP_PASSTHRU; |
| 120 |
|
- public $security_settings = array |
| 121 |
|
- ( |
| 122 |
|
- 'PHP_HANDLING' => false, |
| 123 |
|
- 'IF_FUNCS' => array |
| 124 |
|
- ( |
| 125 |
|
- 'list', 'empty', 'count', 'sizeof', |
| 126 |
|
- 'in_array', 'is_array', |
| 127 |
|
- ), |
| 128 |
|
- 'INCLUDE_ANY' => false, |
| 129 |
|
- 'PHP_TAGS' => false, |
| 130 |
|
- 'MODIFIER_FUNCS' => array(), |
| 131 |
|
- 'ALLOW_CONSTANTS' => false |
| 132 |
|
- ); |
| 133 |
|
- |
| 134 |
|
- // paths |
| 135 |
|
- public $template_dir = 'templates'; |
| 136 |
|
- public $compile_dir = 'templates_c'; |
| 137 |
|
- public $config_dir = 'configs'; |
| 138 |
|
- public $cache_dir = 'cache'; |
| 139 |
|
- public $plugins_dir = array(); |
| 140 |
|
- |
| 141 |
|
- // misc options |
| 142 |
|
- public $left_delimiter = '{'; |
| 143 |
|
- public $right_delimiter = '}'; |
| 144 |
|
- public $compile_check = true; |
| 145 |
|
- public $force_compile = false; |
| 146 |
|
- public $caching = 0; |
| 147 |
|
- public $cache_lifetime = 3600; |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+if(!defined('DIR_SEP')) { |
|
5 |
+ define('DIR_SEP', DIRECTORY_SEPARATOR); |
|
6 |
+} |
|
7 |
+ |
|
8 |
+if(!defined('SMARTY_PHP_PASSTHRU')) |
|
9 |
+{ |
|
10 |
+ define('SMARTY_PHP_PASSTHRU', 0); |
|
11 |
+ define('SMARTY_PHP_QUOTE', 1); |
|
12 |
+ define('SMARTY_PHP_REMOVE', 2); |
|
13 |
+ define('SMARTY_PHP_ALLOW', 3); |
|
14 |
+} |
|
15 |
+ |
|
16 |
+if(class_exists('DwooCompiler', false) === false) |
|
17 |
+ require 'Dwoo/Compiler.php'; |
|
18 |
+ |
|
19 |
+/** |
|
20 |
+ * a Smarty compatibility layer for Dwoo |
|
21 |
+ * |
|
22 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
23 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
24 |
+ * |
|
25 |
+ * This file is released under the LGPL |
|
26 |
+ * "GNU Lesser General Public License" |
|
27 |
+ * More information can be found here: |
|
28 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
29 |
+ * |
|
30 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
31 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
32 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
33 |
+ * @link http://dwoo.org/ |
|
34 |
+ * @version 0.3.4 |
|
35 |
+ * @date 2008-04-09 |
|
36 |
+ * @package Dwoo |
|
37 |
+ */ |
|
38 |
+class DwooSmarty_Adapter extends Dwoo |
|
39 |
+{ |
|
40 |
+ // magic get/set/call functions that handle unsupported features |
|
41 |
+ public function __set($p, $v) |
|
42 |
+ { |
|
43 |
+ if(array_key_exists($p, $this->compat['properties']) !== false) |
|
44 |
+ { |
|
45 |
+ if($this->show_compat_errors) |
|
46 |
+ $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE); |
|
47 |
+ $this->compat['properties'][$p] = $v; |
|
48 |
+ } |
|
49 |
+ else |
|
50 |
+ { |
|
51 |
+ if($this->show_compat_errors) |
|
52 |
+ $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, but it is not listed as such, so you might want to tell me about it at j.boggiano@seld.be', E_USER_NOTICE); |
|
53 |
+ } |
|
54 |
+ } |
|
55 |
+ |
|
56 |
+ public function __get($p) |
|
57 |
+ { |
|
58 |
+ if(array_key_exists($p, $this->compat['properties']) !== false) |
|
59 |
+ { |
|
60 |
+ if($this->show_compat_errors) |
|
61 |
+ $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE); |
|
62 |
+ return $this->compat['properties'][$p]; |
|
63 |
+ } |
|
64 |
+ else |
|
65 |
+ { |
|
66 |
+ if($this->show_compat_errors) |
|
67 |
+ $this->triggerError('Property '.$p.' is not available in the DwooSmartyAdapter, but it is not listed as such, so you might want to tell me about it at j.boggiano@seld.be', E_USER_NOTICE); |
|
68 |
+ } |
|
69 |
+ } |
|
70 |
+ |
|
71 |
+ public function __call($m, $a) |
|
72 |
+ { |
|
73 |
+ if(method_exists($this->dataProvider, $m)) |
|
74 |
+ call_user_func_array(array($this->dataProvider, $m), $a); |
|
75 |
+ elseif($this->show_compat_errors) |
|
76 |
+ { |
|
77 |
+ if(array_search($m, $this->compat['methods']) !== false) |
|
78 |
+ $this->triggerError('Method '.$m.' is not available in the DwooSmartyAdapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE); |
|
79 |
+ else |
|
80 |
+ $this->triggerError('Method '.$m.' is not available in the DwooSmartyAdapter, but it is not listed as such, so you might want to tell me about it at j.boggiano@seld.be', E_USER_NOTICE); |
|
81 |
+ } |
|
82 |
+ } |
|
83 |
+ |
|
84 |
+ // list of unsupported properties and methods |
|
85 |
+ protected $compat = array |
|
86 |
+ ( |
|
87 |
+ 'methods' => array |
|
88 |
+ ( |
|
89 |
+ 'register_resource', 'unregister_resource', 'load_filter', 'clear_compiled_tpl', |
|
90 |
+ 'clear_config', 'get_config_vars', 'config_load' |
|
91 |
+ ), |
|
92 |
+ 'properties' => array |
|
93 |
+ ( |
|
94 |
+ 'cache_handler_func' => null, |
|
95 |
+ 'debugging' => false, |
|
96 |
+ 'error_reporting' => null, |
|
97 |
+ 'debugging_ctrl' => 'NONE', |
|
98 |
+ 'request_vars_order' => 'EGPCS', |
|
99 |
+ 'request_use_auto_globals' => true, |
|
100 |
+ 'use_sub_dirs' => false, |
|
101 |
+ 'autoload_filters' => array(), |
|
102 |
+ 'default_template_handler_func' => '', |
|
103 |
+ 'debug_tpl' => '', |
|
104 |
+ 'cache_modified_check' => false, |
|
105 |
+ 'default_modifiers' => array(), |
|
106 |
+ 'default_resource_type' => 'file', |
|
107 |
+ 'config_overwrite' => true, |
|
108 |
+ 'config_booleanize' => true, |
|
109 |
+ 'config_read_hidden' => false, |
|
110 |
+ 'config_fix_newlines' => true, |
|
111 |
+ 'config_class' => 'Config_File', |
|
112 |
+ ), |
|
113 |
+ ); |
|
114 |
+ |
|
115 |
+ // security vars |
|
116 |
+ public $security = false; |
|
117 |
+ public $trusted_dir = array(); |
|
118 |
+ public $secure_dir = array(); |
|
119 |
+ public $php_handling = SMARTY_PHP_PASSTHRU; |
|
120 |
+ public $security_settings = array |
|
121 |
+ ( |
|
122 |
+ 'PHP_HANDLING' => false, |
|
123 |
+ 'IF_FUNCS' => array |
|
124 |
+ ( |
|
125 |
+ 'list', 'empty', 'count', 'sizeof', |
|
126 |
+ 'in_array', 'is_array', |
|
127 |
+ ), |
|
128 |
+ 'INCLUDE_ANY' => false, |
|
129 |
+ 'PHP_TAGS' => false, |
|
130 |
+ 'MODIFIER_FUNCS' => array(), |
|
131 |
+ 'ALLOW_CONSTANTS' => false |
|
132 |
+ ); |
|
133 |
+ |
|
134 |
+ // paths |
|
135 |
+ public $template_dir = 'templates'; |
|
136 |
+ public $compile_dir = 'templates_c'; |
|
137 |
+ public $config_dir = 'configs'; |
|
138 |
+ public $cache_dir = 'cache'; |
|
139 |
+ public $plugins_dir = array(); |
|
140 |
+ |
|
141 |
+ // misc options |
|
142 |
+ public $left_delimiter = '{'; |
|
143 |
+ public $right_delimiter = '}'; |
|
144 |
+ public $compile_check = true; |
|
145 |
+ public $force_compile = false; |
|
146 |
+ public $caching = 0; |
|
147 |
+ public $cache_lifetime = 3600; |
| 148 |
148 |
public $compile_id = null; |
| 149 |
149 |
public $compiler_file = null; |
| 150 |
150 |
public $compiler_class = null; |
| 151 |
|
- |
| 152 |
|
- // dwoo/smarty compat layer |
| 153 |
|
- public $show_compat_errors = false; |
| 154 |
|
- protected $dataProvider; |
| 155 |
|
- protected $_filters = array('pre'=>array(), 'post'=>array(), 'output'=>array()); |
| 156 |
|
- protected static $tplCache = array(); |
| 157 |
|
- protected $compiler; |
| 158 |
|
- |
| 159 |
|
- public function __construct() |
| 160 |
|
- { |
| 161 |
|
- parent::__construct(); |
| 162 |
|
- $this->charset = 'iso-8859-1'; |
| 163 |
|
- $this->dataProvider = new DwooData(); |
| 164 |
|
- $this->compiler = new DwooCompiler(); |
| 165 |
|
- $this->compiler->smartyCompat = true; |
| 166 |
|
- } |
| 167 |
|
- |
| 168 |
|
- public function display($filename, $cacheId=null, $compileId=null) |
| 169 |
|
- { |
| 170 |
|
- $this->fetch($filename, $cacheId, $compileId, true); |
| 171 |
|
- } |
| 172 |
|
- |
| 173 |
|
- public function fetch($filename, $cacheId=null, $compileId=null, $display=false) |
| 174 |
|
- { |
| 175 |
|
- if($this->security) |
| 176 |
|
- { |
| 177 |
|
- $policy = new DwooSecurityPolicy(); |
| 178 |
|
- $policy->addPhpFunction(array_merge($this->security_settings['IF_FUNCS'], $this->security_settings['MODIFIER_FUNCS'])); |
| 179 |
|
- |
| 180 |
|
- $phpTags = $this->security_settings['PHP_HANDLING'] ? SMARTY_PHP_ALLOW : $this->php_handling; |
| 181 |
|
- if($this->security_settings['PHP_TAGS']) |
| 182 |
|
- $phpTags = SMARTY_PHP_ALLOW; |
| 183 |
|
- switch($phpTags) |
| 184 |
|
- { |
| 185 |
|
- case SMARTY_PHP_ALLOW: |
| 186 |
|
- case SMARTY_PHP_PASSTHRU: |
| 187 |
|
- $phpTags = DwooSecurityPolicy::PHP_ALLOW; |
| 188 |
|
- break; |
| 189 |
|
- case SMARTY_PHP_QUOTE: |
| 190 |
|
- $phpTags = DwooSecurityPolicy::PHP_ENCODE; |
| 191 |
|
- break; |
| 192 |
|
- case SMARTY_PHP_REMOVE: |
| 193 |
|
- default: |
| 194 |
|
- $phpTags = DwooSecurityPolicy::PHP_REMOVE; |
| 195 |
|
- break; |
| 196 |
|
- } |
| 197 |
|
- $policy->setPhpHandling($phpTags); |
| 198 |
|
- |
| 199 |
|
- $policy->setConstantHandling($this->security_settings['ALLOW_CONSTANTS']); |
| 200 |
|
- |
| 201 |
|
- if($this->security_settings['INCLUDE_ANY']) |
| 202 |
|
- $policy->allowDirectory(preg_replace('{^((?:[a-z]:)?[\\\\/]).*}i', '$1', __FILE__)); |
| 203 |
|
- else |
| 204 |
|
- $policy->allowDirectory($this->secure_dir); |
| 205 |
|
- |
| 206 |
|
- $this->setSecurityPolicy($policy); |
| 207 |
|
- } |
| 208 |
|
- |
| 209 |
|
- if(!empty($this->plugins_dir)) |
| 210 |
|
- foreach($this->plugins_dir as $dir) |
| 211 |
|
- DwooLoader::addDirectory(rtrim($dir, '\\/')); |
| 212 |
|
- |
| 213 |
|
- $tpl = $this->makeTemplate($filename, $cacheId, $compileId); |
| 214 |
|
- if($this->force_compile) |
| 215 |
|
- $tpl->forceCompilation(); |
| 216 |
|
- |
| 217 |
|
- if($this->caching > 0) |
| 218 |
|
- $this->cacheTime = $this->cache_lifetime; |
| 219 |
|
- else |
| 220 |
|
- $this->cacheTime = 0; |
|
151 |
+ |
|
152 |
+ // dwoo/smarty compat layer |
|
153 |
+ public $show_compat_errors = false; |
|
154 |
+ protected $dataProvider; |
|
155 |
+ protected $_filters = array('pre'=>array(), 'post'=>array(), 'output'=>array()); |
|
156 |
+ protected static $tplCache = array(); |
|
157 |
+ protected $compiler; |
|
158 |
+ |
|
159 |
+ public function __construct() |
|
160 |
+ { |
|
161 |
+ parent::__construct(); |
|
162 |
+ $this->charset = 'iso-8859-1'; |
|
163 |
+ $this->dataProvider = new DwooData(); |
|
164 |
+ $this->compiler = new DwooCompiler(); |
|
165 |
+ $this->compiler->smartyCompat = true; |
|
166 |
+ } |
|
167 |
+ |
|
168 |
+ public function display($filename, $cacheId=null, $compileId=null) |
|
169 |
+ { |
|
170 |
+ $this->fetch($filename, $cacheId, $compileId, true); |
|
171 |
+ } |
|
172 |
+ |
|
173 |
+ public function fetch($filename, $cacheId=null, $compileId=null, $display=false) |
|
174 |
+ { |
|
175 |
+ if($this->security) |
|
176 |
+ { |
|
177 |
+ $policy = new DwooSecurityPolicy(); |
|
178 |
+ $policy->addPhpFunction(array_merge($this->security_settings['IF_FUNCS'], $this->security_settings['MODIFIER_FUNCS'])); |
|
179 |
+ |
|
180 |
+ $phpTags = $this->security_settings['PHP_HANDLING'] ? SMARTY_PHP_ALLOW : $this->php_handling; |
|
181 |
+ if($this->security_settings['PHP_TAGS']) |
|
182 |
+ $phpTags = SMARTY_PHP_ALLOW; |
|
183 |
+ switch($phpTags) |
|
184 |
+ { |
|
185 |
+ case SMARTY_PHP_ALLOW: |
|
186 |
+ case SMARTY_PHP_PASSTHRU: |
|
187 |
+ $phpTags = DwooSecurityPolicy::PHP_ALLOW; |
|
188 |
+ break; |
|
189 |
+ case SMARTY_PHP_QUOTE: |
|
190 |
+ $phpTags = DwooSecurityPolicy::PHP_ENCODE; |
|
191 |
+ break; |
|
192 |
+ case SMARTY_PHP_REMOVE: |
|
193 |
+ default: |
|
194 |
+ $phpTags = DwooSecurityPolicy::PHP_REMOVE; |
|
195 |
+ break; |
|
196 |
+ } |
|
197 |
+ $policy->setPhpHandling($phpTags); |
|
198 |
+ |
|
199 |
+ $policy->setConstantHandling($this->security_settings['ALLOW_CONSTANTS']); |
|
200 |
+ |
|
201 |
+ if($this->security_settings['INCLUDE_ANY']) |
|
202 |
+ $policy->allowDirectory(preg_replace('{^((?:[a-z]:)?[\\\\/]).*}i', '$1', __FILE__)); |
|
203 |
+ else |
|
204 |
+ $policy->allowDirectory($this->secure_dir); |
|
205 |
+ |
|
206 |
+ $this->setSecurityPolicy($policy); |
|
207 |
+ } |
|
208 |
+ |
|
209 |
+ if(!empty($this->plugins_dir)) |
|
210 |
+ foreach($this->plugins_dir as $dir) |
|
211 |
+ DwooLoader::addDirectory(rtrim($dir, '\\/')); |
|
212 |
+ |
|
213 |
+ $tpl = $this->makeTemplate($filename, $cacheId, $compileId); |
|
214 |
+ if($this->force_compile) |
|
215 |
+ $tpl->forceCompilation(); |
|
216 |
+ |
|
217 |
+ if($this->caching > 0) |
|
218 |
+ $this->cacheTime = $this->cache_lifetime; |
|
219 |
+ else |
|
220 |
+ $this->cacheTime = 0; |
| 221 |
221 |
|
| 222 |
222 |
if($this->compiler_class !== null) |
| 223 |
223 |
{ |
| 224 |
224 |
if($this->compiler_file !== null && !class_exists($this->compiler_class, false)) |
| 225 |
225 |
include $this->compiler_file; |
| 226 |
226 |
$this->compiler = new $this->compiler_class; |
| 227 |
|
- } |
| 228 |
|
- else |
| 229 |
|
- { |
| 230 |
|
- $this->compiler->addPreProcessor('smarty_compat', true); |
| 231 |
|
- $this->compiler->setLooseOpeningHandling(true); |
| 232 |
|
- } |
| 233 |
|
- |
| 234 |
|
- $this->compiler->setDelimiters($this->left_delimiter, $this->right_delimiter); |
| 235 |
|
- |
| 236 |
|
- return $this->get($tpl, $this->dataProvider, $this->compiler, $display===true); |
| 237 |
|
- } |
| 238 |
|
- |
| 239 |
|
- public function register_function($name, $callback, $cacheable=true, $cache_attrs=null) |
| 240 |
|
- { |
| 241 |
|
- if(isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_FUNCTION) |
| 242 |
|
- throw new DwooException('Multiple plugins of different types can not share the same name'); |
| 243 |
|
- $this->plugins[$name] = array('type'=>self::SMARTY_FUNCTION, 'callback'=>$callback); |
| 244 |
|
- } |
| 245 |
|
- |
| 246 |
|
- public function unregister_function($name) |
| 247 |
|
- { |
| 248 |
|
- unset($this->plugins[$name]); |
| 249 |
|
- } |
| 250 |
|
- |
| 251 |
|
- public function register_block($name, $callback, $cacheable=true, $cache_attrs=null) |
| 252 |
|
- { |
| 253 |
|
- if(isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_BLOCK) |
| 254 |
|
- throw new DwooException('Multiple plugins of different types can not share the same name'); |
| 255 |
|
- $this->plugins[$name] = array('type'=>self::SMARTY_BLOCK, 'callback'=>$callback); |
| 256 |
|
- } |
| 257 |
|
- |
| 258 |
|
- public function unregister_block($name) |
| 259 |
|
- { |
| 260 |
|
- unset($this->plugins[$name]); |
| 261 |
|
- } |
| 262 |
|
- |
| 263 |
|
- public function register_modifier($name, $callback) |
| 264 |
|
- { |
| 265 |
|
- if(isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_MODIFIER) |
| 266 |
|
- throw new DwooException('Multiple plugins of different types can not share the same name'); |
| 267 |
|
- $this->plugins[$name] = array('type'=>self::SMARTY_MODIFIER, 'callback'=>$callback); |
| 268 |
|
- } |
| 269 |
|
- |
| 270 |
|
- public function unregister_modifier($name) |
| 271 |
|
- { |
| 272 |
|
- unset($this->plugins[$name]); |
| 273 |
|
- } |
| 274 |
|
- |
| 275 |
|
- public function register_prefilter($callback) |
| 276 |
|
- { |
| 277 |
|
- $processor = new DwooSmartyProcessorAdapter($this->compiler); |
| 278 |
|
- $processor->registerCallback($callback); |
| 279 |
|
- $this->_filters['pre'][] = $processor; |
| 280 |
|
- $this->compiler->addPreProcessor($processor); |
| 281 |
|
- } |
| 282 |
|
- |
| 283 |
|
- public function unregister_prefilter($callback) |
| 284 |
|
- { |
| 285 |
|
- foreach($this->_filters['pre'] as $index => $processor) |
| 286 |
|
- if($processor->callback === $callback) |
| 287 |
|
- { |
| 288 |
|
- $this->compiler->removePostProcessor($processor); |
| 289 |
|
- unset($this->_filters['pre'][$index]); |
| 290 |
|
- } |
| 291 |
|
- } |
| 292 |
|
- |
| 293 |
|
- public function register_postfilter($callback) |
| 294 |
|
- { |
| 295 |
|
- $processor = new DwooSmartyProcessorAdapter($this->compiler); |
| 296 |
|
- $processor->registerCallback($callback); |
| 297 |
|
- $this->_filters['post'][] = $processor; |
| 298 |
|
- $this->compiler->addPostProcessor($processor); |
| 299 |
|
- } |
| 300 |
|
- |
| 301 |
|
- public function unregister_postfilter($callback) |
| 302 |
|
- { |
| 303 |
|
- foreach($this->_filters['post'] as $index => $processor) |
| 304 |
|
- if($processor->callback === $callback) |
| 305 |
|
- { |
| 306 |
|
- $this->compiler->removePostProcessor($processor); |
| 307 |
|
- unset($this->_filters['post'][$index]); |
| 308 |
|
- } |
| 309 |
|
- } |
| 310 |
|
- |
| 311 |
|
- public function register_outputfilter($callback) |
| 312 |
|
- { |
| 313 |
|
- $filter = new DwooSmartyFilterAdapter($this); |
| 314 |
|
- $filter->registerCallback($callback); |
| 315 |
|
- $this->_filters['output'][] = $filter; |
| 316 |
|
- $this->addFilter($filter); |
| 317 |
|
- } |
| 318 |
|
- |
| 319 |
|
- public function unregister_outputfilter($callback) |
| 320 |
|
- { |
| 321 |
|
- foreach($this->_filters['output'] as $index => $filter) |
| 322 |
|
- if($filter->callback === $callback) |
| 323 |
|
- { |
| 324 |
|
- $this->removeOutputFilter($filter); |
| 325 |
|
- unset($this->_filters['output'][$index]); |
| 326 |
|
- } |
| 327 |
|
- } |
| 328 |
|
- |
| 329 |
|
- function register_object($object, $object_impl, $allowed = array(), $smarty_args = false, $block_methods = array()) |
| 330 |
|
- { |
| 331 |
|
- settype($allowed, 'array'); |
| 332 |
|
- settype($block_methods, 'array'); |
| 333 |
|
- settype($smarty_args, 'boolean'); |
| 334 |
|
- |
| 335 |
|
- if(!empty($allowed) && $this->show_compat_errors) |
| 336 |
|
- $this->triggerError('You can register objects but can not restrict the method/properties used, this is PHP5, you have proper OOP access restrictions so use them.', E_USER_NOTICE); |
| 337 |
|
- |
| 338 |
|
- if($smarty_args) |
| 339 |
|
- $this->triggerError('You can register objects but methods will be called using method($arg1, $arg2, $arg3), not as an argument array like smarty did.', E_USER_NOTICE); |
| 340 |
|
- |
| 341 |
|
- if(!empty($block_methods)) |
| 342 |
|
- $this->triggerError('You can register objects but can not use methods as being block methods, you have to build a plugin for that.', E_USER_NOTICE); |
| 343 |
|
- |
| 344 |
|
- $this->dataProvider->assign($object, $object_impl); |
| 345 |
|
- } |
| 346 |
|
- |
| 347 |
|
- function unregister_object($object) |
| 348 |
|
- { |
| 349 |
|
- $this->dataProvider->clear($object); |
| 350 |
|
- } |
| 351 |
|
- |
| 352 |
|
- function get_registered_object($name) { |
| 353 |
|
- $data = $this->dataProvider->getData(); |
| 354 |
|
- if(isset($data[$name]) && is_object($data[$name])) |
| 355 |
|
- return $data[$name]; |
| 356 |
|
- else |
| 357 |
|
- trigger_error('DwooCompiler: object "'.$name.'" was not registered or is not an object', E_USER_ERROR); |
| 358 |
|
- } |
| 359 |
|
- |
| 360 |
|
- public function template_exists($filename) |
| 361 |
|
- { |
| 362 |
|
- return file_exists($this->template_dir.DIRECTORY_SEPARATOR.$filename); |
| 363 |
|
- } |
| 364 |
|
- |
| 365 |
|
- public function is_cached($tpl, $cacheId = null, $compileId = null) |
| 366 |
|
- { |
| 367 |
|
- return $this->isCached($this->makeTemplate($tpl, $cacheId, $compileId)); |
| 368 |
|
- } |
| 369 |
|
- |
| 370 |
|
- public function append_by_ref($var, &$value, $merge=false) |
| 371 |
|
- { |
| 372 |
|
- $this->dataProvider->appendByRef($var, $value, $merge); |
| 373 |
|
- } |
| 374 |
|
- |
| 375 |
|
- public function assign_by_ref($name, &$val) |
| 376 |
|
- { |
| 377 |
|
- $this->dataProvider->assignByRef($name, $val); |
| 378 |
|
- } |
| 379 |
|
- |
| 380 |
|
- public function clear_assign($var) |
| 381 |
|
- { |
| 382 |
|
- $this->dataProvider->clear($var); |
| 383 |
|
- } |
| 384 |
|
- |
| 385 |
|
- public function clear_all_assign() |
| 386 |
|
- { |
| 387 |
|
- $this->dataProvider->clear(); |
| 388 |
|
- } |
| 389 |
|
- |
| 390 |
|
- public function get_template_vars($name=null) |
| 391 |
|
- { |
| 392 |
|
- if($this->show_compat_errors) |
| 393 |
|
- trigger_error('get_template_vars does not return values by reference, if you try to modify the data that way you should modify your code.', E_USER_NOTICE); |
| 394 |
|
- |
| 395 |
|
- $data = $this->dataProvider->getData(); |
| 396 |
|
- if($name === null) |
| 397 |
|
- return $data; |
| 398 |
|
- elseif(isset($data[$name])) |
| 399 |
|
- return $data[$name]; |
| 400 |
|
- return null; |
| 401 |
|
- } |
|
227 |
+ } |
|
228 |
+ else |
|
229 |
+ { |
|
230 |
+ $this->compiler->addPreProcessor('smarty_compat', true); |
|
231 |
+ $this->compiler->setLooseOpeningHandling(true); |
|
232 |
+ } |
|
233 |
+ |
|
234 |
+ $this->compiler->setDelimiters($this->left_delimiter, $this->right_delimiter); |
|
235 |
+ |
|
236 |
+ return $this->get($tpl, $this->dataProvider, $this->compiler, $display===true); |
|
237 |
+ } |
|
238 |
+ |
|
239 |
+ public function register_function($name, $callback, $cacheable=true, $cache_attrs=null) |
|
240 |
+ { |
|
241 |
+ if(isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_FUNCTION) |
|
242 |
+ throw new DwooException('Multiple plugins of different types can not share the same name'); |
|
243 |
+ $this->plugins[$name] = array('type'=>self::SMARTY_FUNCTION, 'callback'=>$callback); |
|
244 |
+ } |
|
245 |
+ |
|
246 |
+ public function unregister_function($name) |
|
247 |
+ { |
|
248 |
+ unset($this->plugins[$name]); |
|
249 |
+ } |
|
250 |
+ |
|
251 |
+ public function register_block($name, $callback, $cacheable=true, $cache_attrs=null) |
|
252 |
+ { |
|
253 |
+ if(isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_BLOCK) |
|
254 |
+ throw new DwooException('Multiple plugins of different types can not share the same name'); |
|
255 |
+ $this->plugins[$name] = array('type'=>self::SMARTY_BLOCK, 'callback'=>$callback); |
|
256 |
+ } |
|
257 |
+ |
|
258 |
+ public function unregister_block($name) |
|
259 |
+ { |
|
260 |
+ unset($this->plugins[$name]); |
|
261 |
+ } |
|
262 |
+ |
|
263 |
+ public function register_modifier($name, $callback) |
|
264 |
+ { |
|
265 |
+ if(isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_MODIFIER) |
|
266 |
+ throw new DwooException('Multiple plugins of different types can not share the same name'); |
|
267 |
+ $this->plugins[$name] = array('type'=>self::SMARTY_MODIFIER, 'callback'=>$callback); |
|
268 |
+ } |
|
269 |
+ |
|
270 |
+ public function unregister_modifier($name) |
|
271 |
+ { |
|
272 |
+ unset($this->plugins[$name]); |
|
273 |
+ } |
|
274 |
+ |
|
275 |
+ public function register_prefilter($callback) |
|
276 |
+ { |
|
277 |
+ $processor = new DwooSmartyProcessorAdapter($this->compiler); |
|
278 |
+ $processor->registerCallback($callback); |
|
279 |
+ $this->_filters['pre'][] = $processor; |
|
280 |
+ $this->compiler->addPreProcessor($processor); |
|
281 |
+ } |
|
282 |
+ |
|
283 |
+ public function unregister_prefilter($callback) |
|
284 |
+ { |
|
285 |
+ foreach($this->_filters['pre'] as $index => $processor) |
|
286 |
+ if($processor->callback === $callback) |
|
287 |
+ { |
|
288 |
+ $this->compiler->removePostProcessor($processor); |
|
289 |
+ unset($this->_filters['pre'][$index]); |
|
290 |
+ } |
|
291 |
+ } |
|
292 |
+ |
|
293 |
+ public function register_postfilter($callback) |
|
294 |
+ { |
|
295 |
+ $processor = new DwooSmartyProcessorAdapter($this->compiler); |
|
296 |
+ $processor->registerCallback($callback); |
|
297 |
+ $this->_filters['post'][] = $processor; |
|
298 |
+ $this->compiler->addPostProcessor($processor); |
|
299 |
+ } |
|
300 |
+ |
|
301 |
+ public function unregister_postfilter($callback) |
|
302 |
+ { |
|
303 |
+ foreach($this->_filters['post'] as $index => $processor) |
|
304 |
+ if($processor->callback === $callback) |
|
305 |
+ { |
|
306 |
+ $this->compiler->removePostProcessor($processor); |
|
307 |
+ unset($this->_filters['post'][$index]); |
|
308 |
+ } |
|
309 |
+ } |
|
310 |
+ |
|
311 |
+ public function register_outputfilter($callback) |
|
312 |
+ { |
|
313 |
+ $filter = new DwooSmartyFilterAdapter($this); |
|
314 |
+ $filter->registerCallback($callback); |
|
315 |
+ $this->_filters['output'][] = $filter; |
|
316 |
+ $this->addFilter($filter); |
|
317 |
+ } |
|
318 |
+ |
|
319 |
+ public function unregister_outputfilter($callback) |
|
320 |
+ { |
|
321 |
+ foreach($this->_filters['output'] as $index => $filter) |
|
322 |
+ if($filter->callback === $callback) |
|
323 |
+ { |
|
324 |
+ $this->removeOutputFilter($filter); |
|
325 |
+ unset($this->_filters['output'][$index]); |
|
326 |
+ } |
|
327 |
+ } |
|
328 |
+ |
|
329 |
+ function register_object($object, $object_impl, $allowed = array(), $smarty_args = false, $block_methods = array()) |
|
330 |
+ { |
|
331 |
+ settype($allowed, 'array'); |
|
332 |
+ settype($block_methods, 'array'); |
|
333 |
+ settype($smarty_args, 'boolean'); |
|
334 |
+ |
|
335 |
+ if(!empty($allowed) && $this->show_compat_errors) |
|
336 |
+ $this->triggerError('You can register objects but can not restrict the method/properties used, this is PHP5, you have proper OOP access restrictions so use them.', E_USER_NOTICE); |
|
337 |
+ |
|
338 |
+ if($smarty_args) |
|
339 |
+ $this->triggerError('You can register objects but methods will be called using method($arg1, $arg2, $arg3), not as an argument array like smarty did.', E_USER_NOTICE); |
|
340 |
+ |
|
341 |
+ if(!empty($block_methods)) |
|
342 |
+ $this->triggerError('You can register objects but can not use methods as being block methods, you have to build a plugin for that.', E_USER_NOTICE); |
|
343 |
+ |
|
344 |
+ $this->dataProvider->assign($object, $object_impl); |
|
345 |
+ } |
|
346 |
+ |
|
347 |
+ function unregister_object($object) |
|
348 |
+ { |
|
349 |
+ $this->dataProvider->clear($object); |
|
350 |
+ } |
|
351 |
+ |
|
352 |
+ function get_registered_object($name) { |
|
353 |
+ $data = $this->dataProvider->getData(); |
|
354 |
+ if(isset($data[$name]) && is_object($data[$name])) |
|
355 |
+ return $data[$name]; |
|
356 |
+ else |
|
357 |
+ trigger_error('DwooCompiler: object "'.$name.'" was not registered or is not an object', E_USER_ERROR); |
|
358 |
+ } |
|
359 |
+ |
|
360 |
+ public function template_exists($filename) |
|
361 |
+ { |
|
362 |
+ return file_exists($this->template_dir.DIRECTORY_SEPARATOR.$filename); |
|
363 |
+ } |
|
364 |
+ |
|
365 |
+ public function is_cached($tpl, $cacheId = null, $compileId = null) |
|
366 |
+ { |
|
367 |
+ return $this->isCached($this->makeTemplate($tpl, $cacheId, $compileId)); |
|
368 |
+ } |
|
369 |
+ |
|
370 |
+ public function append_by_ref($var, &$value, $merge=false) |
|
371 |
+ { |
|
372 |
+ $this->dataProvider->appendByRef($var, $value, $merge); |
|
373 |
+ } |
|
374 |
+ |
|
375 |
+ public function assign_by_ref($name, &$val) |
|
376 |
+ { |
|
377 |
+ $this->dataProvider->assignByRef($name, $val); |
|
378 |
+ } |
|
379 |
+ |
|
380 |
+ public function clear_assign($var) |
|
381 |
+ { |
|
382 |
+ $this->dataProvider->clear($var); |
|
383 |
+ } |
|
384 |
+ |
|
385 |
+ public function clear_all_assign() |
|
386 |
+ { |
|
387 |
+ $this->dataProvider->clear(); |
|
388 |
+ } |
|
389 |
+ |
|
390 |
+ public function get_template_vars($name=null) |
|
391 |
+ { |
|
392 |
+ if($this->show_compat_errors) |
|
393 |
+ trigger_error('get_template_vars does not return values by reference, if you try to modify the data that way you should modify your code.', E_USER_NOTICE); |
|
394 |
+ |
|
395 |
+ $data = $this->dataProvider->getData(); |
|
396 |
+ if($name === null) |
|
397 |
+ return $data; |
|
398 |
+ elseif(isset($data[$name])) |
|
399 |
+ return $data[$name]; |
|
400 |
+ return null; |
|
401 |
+ } |
| 402 |
402 |
|
| 403 |
403 |
public function clear_all_cache($olderThan = 0) |
| 404 |
404 |
{ |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * represents a Dwoo template contained in a file |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-class DwooTemplateFile extends DwooTemplateString |
| 24 |
|
-{ |
| 25 |
|
- /** |
| 26 |
|
- * template filename |
| 27 |
|
- * |
| 28 |
|
- * @var string |
| 29 |
|
- */ |
| 30 |
|
- protected $file; |
| 31 |
|
- |
| 32 |
|
- /** |
| 33 |
|
- * creates a template from a file |
| 34 |
|
- * |
| 35 |
|
- * @param string $file the path to the template file, make sure it exists |
| 36 |
|
- * @param int $cacheTime duration of the cache validity for this template, |
| 37 |
|
- * if null it defaults to the Dwoo instance that will |
| 38 |
|
- * render this template |
| 39 |
|
- * @param string $cacheId the unique cache identifier of this page or anything else that |
| 40 |
|
- * makes this template's content unique, if null it defaults |
| 41 |
|
- * to the current url |
| 42 |
|
- * @param string $compileId the unique compiled identifier, which is used to distinguish this |
| 43 |
|
- * template from others, if null it defaults to the filename+bits of the path |
| 44 |
|
- */ |
| 45 |
|
- public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null) |
| 46 |
|
- { |
| 47 |
|
- $this->file = realpath($file); |
| 48 |
|
- $this->name = basename($file); |
| 49 |
|
- $this->cacheTime = $cacheTime; |
| 50 |
|
- |
| 51 |
|
- // no compile id provided, generate a kind of unique kind of readable one from the filename |
| 52 |
|
- if($compileId === null) |
| 53 |
|
- { |
| 54 |
|
- $parts = explode('/', strtr($file, '\\.', '/_')); |
| 55 |
|
- $compileId = array_pop($parts); |
| 56 |
|
- $compileId = substr(array_pop($parts), 0, 5) .'_'. $compileId; |
| 57 |
|
- $compileId = substr(array_pop($parts), 0, 5) .'_'. $compileId; |
| 58 |
|
- } |
| 59 |
|
- $this->compileId = $compileId; |
| 60 |
|
- |
| 61 |
|
- // no cache id provided, use request_uri if available |
| 62 |
|
- if($cacheId === null) |
| 63 |
|
- { |
| 64 |
|
- if(isset($_SERVER['REQUEST_URI']) === true) |
| 65 |
|
- $cacheId = strtr($_SERVER['REQUEST_URI'], '\\/%?=!:;', '--------'); |
| 66 |
|
- elseif(isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) |
| 67 |
|
- $cacheId = strtr($_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']), '\\/%?=!:;', '--------'); |
| 68 |
|
- } |
| 69 |
|
- $this->cacheId = $this->compileId . $cacheId; |
| 70 |
|
- } |
| 71 |
|
- |
| 72 |
|
- /** |
| 73 |
|
- * returns the compiled template file name |
| 74 |
|
- * |
| 75 |
|
- * @param Dwoo $dwoo the dwoo instance that requests it |
| 76 |
|
- * @param DwooICompiler $compiler the compiler that must be used |
| 77 |
|
- * @return string |
| 78 |
|
- */ |
| 79 |
|
- public function getCompiledTemplate(Dwoo $dwoo, DwooICompiler $compiler = null) |
| 80 |
|
- { |
| 81 |
|
- $compiledFile = $dwoo->getCompileDir() . $this->compileId.'.dwoo'.Dwoo::RELEASE_TAG.'.php'; |
| 82 |
|
- |
| 83 |
|
- // already checked, return compiled file |
| 84 |
|
- if($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) |
| 85 |
|
- { |
| 86 |
|
- } |
| 87 |
|
- // template is compiled and has not been modified since the compilation |
| 88 |
|
- elseif($this->compilationEnforced !== true && file_exists($compiledFile)===true && filemtime($this->file) <= filemtime($compiledFile)) |
| 89 |
|
- { |
| 90 |
|
- self::$cache['compiled'][$this->compileId] = true; |
| 91 |
|
- } |
| 92 |
|
- // compiles the template |
| 93 |
|
- else |
| 94 |
|
- { |
| 95 |
|
- $this->compilationEnforced = false; |
| 96 |
|
- |
| 97 |
|
- if($compiler === null) |
| 98 |
|
- { |
| 99 |
|
- $compiler = $dwoo->getDefaultCompilerFactory('string'); |
| 100 |
|
- |
| 101 |
|
- if($compiler === null || $compiler === array('DwooCompiler', 'compilerFactory')) |
| 102 |
|
- { |
| 103 |
|
- if(class_exists('DwooCompiler', false) === false) |
| 104 |
|
- include DWOO_DIRECTORY . 'DwooCompiler.php'; |
| 105 |
|
- $compiler = DwooCompiler::compilerFactory(); |
| 106 |
|
- } |
| 107 |
|
- else |
| 108 |
|
- $compiler = call_user_func($compiler); |
| 109 |
|
- } |
| 110 |
|
- |
| 111 |
|
- $this->compiler = $compiler; |
| 112 |
|
- |
| 113 |
|
- $compiler->setCustomPlugins($dwoo->getCustomPlugins()); |
| 114 |
|
- $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); |
| 115 |
|
- file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); |
| 116 |
|
- touch($compiledFile, max($_SERVER['REQUEST_TIME'], filemtime($this->file))); |
| 117 |
|
- |
| 118 |
|
- self::$cache['compiled'][$this->compileId] = true; |
| 119 |
|
- } |
| 120 |
|
- |
| 121 |
|
- return $compiledFile; |
| 122 |
|
- } |
| 123 |
|
- |
| 124 |
|
- /** |
| 125 |
|
- * returns the template source of this template |
| 126 |
|
- * |
| 127 |
|
- * @return string |
| 128 |
|
- */ |
| 129 |
|
- public function getSource() |
| 130 |
|
- { |
| 131 |
|
- return file_get_contents($this->file); |
| 132 |
|
- } |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * represents a Dwoo template contained in a file |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+class DwooTemplateFile extends DwooTemplateString |
|
24 |
+{ |
|
25 |
+ /** |
|
26 |
+ * template filename |
|
27 |
+ * |
|
28 |
+ * @var string |
|
29 |
+ */ |
|
30 |
+ protected $file; |
|
31 |
+ |
|
32 |
+ /** |
|
33 |
+ * creates a template from a file |
|
34 |
+ * |
|
35 |
+ * @param string $file the path to the template file, make sure it exists |
|
36 |
+ * @param int $cacheTime duration of the cache validity for this template, |
|
37 |
+ * if null it defaults to the Dwoo instance that will |
|
38 |
+ * render this template |
|
39 |
+ * @param string $cacheId the unique cache identifier of this page or anything else that |
|
40 |
+ * makes this template's content unique, if null it defaults |
|
41 |
+ * to the current url |
|
42 |
+ * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
43 |
+ * template from others, if null it defaults to the filename+bits of the path |
|
44 |
+ */ |
|
45 |
+ public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null) |
|
46 |
+ { |
|
47 |
+ $this->file = realpath($file); |
|
48 |
+ $this->name = basename($file); |
|
49 |
+ $this->cacheTime = $cacheTime; |
|
50 |
+ |
|
51 |
+ // no compile id provided, generate a kind of unique kind of readable one from the filename |
|
52 |
+ if($compileId === null) |
|
53 |
+ { |
|
54 |
+ $parts = explode('/', strtr($file, '\\.', '/_')); |
|
55 |
+ $compileId = array_pop($parts); |
|
56 |
+ $compileId = substr(array_pop($parts), 0, 5) .'_'. $compileId; |
|
57 |
+ $compileId = substr(array_pop($parts), 0, 5) .'_'. $compileId; |
|
58 |
+ } |
|
59 |
+ $this->compileId = $compileId; |
|
60 |
+ |
|
61 |
+ // no cache id provided, use request_uri if available |
|
62 |
+ if($cacheId === null) |
|
63 |
+ { |
|
64 |
+ if(isset($_SERVER['REQUEST_URI']) === true) |
|
65 |
+ $cacheId = strtr($_SERVER['REQUEST_URI'], '\\/%?=!:;', '--------'); |
|
66 |
+ elseif(isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) |
|
67 |
+ $cacheId = strtr($_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']), '\\/%?=!:;', '--------'); |
|
68 |
+ } |
|
69 |
+ $this->cacheId = $this->compileId . $cacheId; |
|
70 |
+ } |
|
71 |
+ |
|
72 |
+ /** |
|
73 |
+ * returns the compiled template file name |
|
74 |
+ * |
|
75 |
+ * @param Dwoo $dwoo the dwoo instance that requests it |
|
76 |
+ * @param DwooICompiler $compiler the compiler that must be used |
|
77 |
+ * @return string |
|
78 |
+ */ |
|
79 |
+ public function getCompiledTemplate(Dwoo $dwoo, DwooICompiler $compiler = null) |
|
80 |
+ { |
|
81 |
+ $compiledFile = $dwoo->getCompileDir() . $this->compileId.'.dwoo'.Dwoo::RELEASE_TAG.'.php'; |
|
82 |
+ |
|
83 |
+ // already checked, return compiled file |
|
84 |
+ if($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) |
|
85 |
+ { |
|
86 |
+ } |
|
87 |
+ // template is compiled and has not been modified since the compilation |
|
88 |
+ elseif($this->compilationEnforced !== true && file_exists($compiledFile)===true && filemtime($this->file) <= filemtime($compiledFile)) |
|
89 |
+ { |
|
90 |
+ self::$cache['compiled'][$this->compileId] = true; |
|
91 |
+ } |
|
92 |
+ // compiles the template |
|
93 |
+ else |
|
94 |
+ { |
|
95 |
+ $this->compilationEnforced = false; |
|
96 |
+ |
|
97 |
+ if($compiler === null) |
|
98 |
+ { |
|
99 |
+ $compiler = $dwoo->getDefaultCompilerFactory('string'); |
|
100 |
+ |
|
101 |
+ if($compiler === null || $compiler === array('DwooCompiler', 'compilerFactory')) |
|
102 |
+ { |
|
103 |
+ if(class_exists('DwooCompiler', false) === false) |
|
104 |
+ include 'Dwoo/Compiler.php'; |
|
105 |
+ $compiler = DwooCompiler::compilerFactory(); |
|
106 |
+ } |
|
107 |
+ else |
|
108 |
+ $compiler = call_user_func($compiler); |
|
109 |
+ } |
|
110 |
+ |
|
111 |
+ $this->compiler = $compiler; |
|
112 |
+ |
|
113 |
+ $compiler->setCustomPlugins($dwoo->getCustomPlugins()); |
|
114 |
+ $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); |
|
115 |
+ file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); |
|
116 |
+ touch($compiledFile, max($_SERVER['REQUEST_TIME'], filemtime($this->file))); |
|
117 |
+ |
|
118 |
+ self::$cache['compiled'][$this->compileId] = true; |
|
119 |
+ } |
|
120 |
+ |
|
121 |
+ return $compiledFile; |
|
122 |
+ } |
|
123 |
+ |
|
124 |
+ /** |
|
125 |
+ * returns the template source of this template |
|
126 |
+ * |
|
127 |
+ * @return string |
|
128 |
+ */ |
|
129 |
+ public function getSource() |
|
130 |
+ { |
|
131 |
+ return file_get_contents($this->file); |
|
132 |
+ } |
| 133 |
133 |
|
| 134 |
134 |
/** |
| 135 |
135 |
* returns the resource name for this template class |
| 2 |
|
------------------------------------------------------------------------------ |
| 3 |
|
--- WHAT IS DWOO? readme - version 0.3.2 |
| 4 |
|
------------------------------------------------------------------------------ |
| 5 |
|
-Dwoo is a PHP5 Template Engine that was started in early 2008. The idea came |
| 6 |
|
-from the fact that Smarty, a well known template engine, is getting older and |
| 7 |
|
-older. It carries the weight of it's age, having old features that are |
| 8 |
|
-inconsistent compared to newer ones, being written for PHP4 its Object |
| 9 |
|
-Oriented aspect doesn't take advantage of PHP5's more advanced features in |
| 10 |
|
-the area, etc. Hence Dwoo was born, hoping to provide a more up to date and |
| 11 |
|
-stronger engine. |
| 12 |
|
- |
| 13 |
|
-So far it has proven to be faster than Smarty in many areas, and it provides |
| 14 |
|
-a compatibility layer to allow developers that have been using Smarty for |
| 15 |
|
-years to switch their application over to Dwoo progressively. |
| 16 |
|
- |
| 17 |
|
------------------------------------------------------------------------------ |
| 18 |
|
--- DOCUMENTATION |
| 19 |
|
------------------------------------------------------------------------------ |
| 20 |
|
-Dwoo's website to get the latest version is at http://dwoo.org/ |
| 21 |
|
- |
| 22 |
|
-The wiki/documentation pages are available at http://wiki.dwoo.org/ |
| 23 |
|
- |
| 24 |
|
------------------------------------------------------------------------------ |
| 25 |
|
--- LICENSE |
| 26 |
|
------------------------------------------------------------------------------ |
| 27 |
|
-Dwoo is released under the GNU Lesser General Public License version 3.0. |
| 28 |
|
-See the LICENSE file included in the archive or go to the URL below to obtain |
| 29 |
|
-a copy. |
| 30 |
|
- |
| 31 |
|
-http://www.gnu.org/licenses/lgpl.html |
| 32 |
|
- |
| 33 |
|
------------------------------------------------------------------------------ |
| 34 |
|
--- QUICK START - RUNNING DWOO |
| 35 |
|
------------------------------------------------------------------------------ |
| 36 |
|
-/***************************** Basic Example *******************************/ |
| 37 |
|
-// Include the main class (it should handle the rest on its own) |
| 38 |
|
-include 'path/to/Dwoo.php'; |
| 39 |
|
- |
| 40 |
|
-// Create the controller, this is reusable |
| 41 |
|
-$dwoo = new Dwoo(); |
| 42 |
|
- |
| 43 |
|
-// Load a template file (name it as you please), this is reusable |
| 44 |
|
-// if you want to render multiple times the same template with different data |
| 45 |
|
-$tpl = new DwooTemplateFile('path/to/index.tpl'); |
| 46 |
|
- |
| 47 |
|
-// Create a data set, if you don't like this you can directly input an |
| 48 |
|
-// associative array in $dwoo->output() |
| 49 |
|
-$data = new DwooData(); |
| 50 |
|
-// Fill it with some data |
| 51 |
|
-$data->assign('foo', 'BAR'); |
| 52 |
|
-$data->assign('bar', 'BAZ'); |
| 53 |
|
- |
| 54 |
|
-// Outputs the result ... |
| 55 |
|
-$dwoo->output($tpl, $data); |
| 56 |
|
-// ... or get it to use it somewhere else |
| 57 |
|
-$dwoo->get($tpl, $data); |
| 58 |
|
- |
| 59 |
|
-/***************************** Loop Example *******************************/ |
| 60 |
|
-// To loop over multiple articles of a blog for instance, if you have a |
| 61 |
|
-// template file representing an article, you could do the following : |
| 62 |
|
- |
| 63 |
|
-include 'path/to/Dwoo.php'; |
| 64 |
|
- |
| 65 |
|
-$dwoo = new Dwoo(); |
| 66 |
|
-$tpl = new DwooTemplateFile('path/to/article.tpl'); |
| 67 |
|
- |
| 68 |
|
-$pageContent = ''; |
| 69 |
|
- |
| 70 |
|
-// Loop over articles that have been retrieved from the DB |
| 71 |
|
-foreach($articles as $article) { |
| 72 |
|
- // Either associate variables one by one |
| 73 |
|
- $data = new DwooData(); |
| 74 |
|
- $data->assign('title', $article['title']; |
| 75 |
|
- $data->assign('content', $article['content']); |
| 76 |
|
- $pageContent .= $dwoo->get($tpl, $data); |
| 77 |
|
- |
| 78 |
|
- // Or use the article directly (which is a lot easier in this case) |
| 79 |
|
- $pageContent .= $dwoo->get($tpl, $article); |
|
2 |
+----------------------------------------------------------------------------- |
|
3 |
+-- WHAT IS DWOO? readme - version 0.3.2 |
|
4 |
+----------------------------------------------------------------------------- |
|
5 |
+Dwoo is a PHP5 Template Engine that was started in early 2008. The idea came |
|
6 |
+from the fact that Smarty, a well known template engine, is getting older and |
|
7 |
+older. It carries the weight of it's age, having old features that are |
|
8 |
+inconsistent compared to newer ones, being written for PHP4 its Object |
|
9 |
+Oriented aspect doesn't take advantage of PHP5's more advanced features in |
|
10 |
+the area, etc. Hence Dwoo was born, hoping to provide a more up to date and |
|
11 |
+stronger engine. |
|
12 |
+ |
|
13 |
+So far it has proven to be faster than Smarty in many areas, and it provides |
|
14 |
+a compatibility layer to allow developers that have been using Smarty for |
|
15 |
+years to switch their application over to Dwoo progressively. |
|
16 |
+ |
|
17 |
+----------------------------------------------------------------------------- |
|
18 |
+-- DOCUMENTATION |
|
19 |
+----------------------------------------------------------------------------- |
|
20 |
+Dwoo's website to get the latest version is at http://dwoo.org/ |
|
21 |
+ |
|
22 |
+The wiki/documentation pages are available at http://wiki.dwoo.org/ |
|
23 |
+ |
|
24 |
+----------------------------------------------------------------------------- |
|
25 |
+-- LICENSE |
|
26 |
+----------------------------------------------------------------------------- |
|
27 |
+Dwoo is released under the GNU Lesser General Public License version 3.0. |
|
28 |
+See the LICENSE file included in the archive or go to the URL below to obtain |
|
29 |
+a copy. |
|
30 |
+ |
|
31 |
+http://www.gnu.org/licenses/lgpl.html |
|
32 |
+ |
|
33 |
+----------------------------------------------------------------------------- |
|
34 |
+-- QUICK START - RUNNING DWOO |
|
35 |
+----------------------------------------------------------------------------- |
|
36 |
+/***************************** Basic Example *******************************/ |
|
37 |
+// Include the main class (it should handle the rest on its own) |
|
38 |
+include 'path/to/Dwoo.php'; |
|
39 |
+ |
|
40 |
+// Create the controller, this is reusable |
|
41 |
+$dwoo = new Dwoo(); |
|
42 |
+ |
|
43 |
+// Load a template file (name it as you please), this is reusable |
|
44 |
+// if you want to render multiple times the same template with different data |
|
45 |
+$tpl = new DwooTemplateFile('path/to/index.tpl'); |
|
46 |
+ |
|
47 |
+// Create a data set, if you don't like this you can directly input an |
|
48 |
+// associative array in $dwoo->output() |
|
49 |
+$data = new DwooData(); |
|
50 |
+// Fill it with some data |
|
51 |
+$data->assign('foo', 'BAR'); |
|
52 |
+$data->assign('bar', 'BAZ'); |
|
53 |
+ |
|
54 |
+// Outputs the result ... |
|
55 |
+$dwoo->output($tpl, $data); |
|
56 |
+// ... or get it to use it somewhere else |
|
57 |
+$dwoo->get($tpl, $data); |
|
58 |
+ |
|
59 |
+/***************************** Loop Example *******************************/ |
|
60 |
+// To loop over multiple articles of a blog for instance, if you have a |
|
61 |
+// template file representing an article, you could do the following : |
|
62 |
+ |
|
63 |
+include 'path/to/Dwoo.php'; |
|
64 |
+ |
|
65 |
+$dwoo = new Dwoo(); |
|
66 |
+$tpl = new DwooTemplateFile('path/to/article.tpl'); |
|
67 |
+ |
|
68 |
+$pageContent = ''; |
|
69 |
+ |
|
70 |
+// Loop over articles that have been retrieved from the DB |
|
71 |
+foreach($articles as $article) { |
|
72 |
+ // Either associate variables one by one |
|
73 |
+ $data = new DwooData(); |
|
74 |
+ $data->assign('title', $article['title']; |
|
75 |
+ $data->assign('content', $article['content']); |
|
76 |
+ $pageContent .= $dwoo->get($tpl, $data); |
|
77 |
+ |
|
78 |
+ // Or use the article directly (which is a lot easier in this case) |
|
79 |
+ $pageContent .= $dwoo->get($tpl, $article); |
| 80 |
80 |
} |
| 2 |
|
-<?php |
| 3 |
|
-/** |
| 4 |
|
- * TOCOM |
| 5 |
|
- * |
| 6 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 7 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 8 |
|
- * |
| 9 |
|
- * This file is released under the LGPL |
| 10 |
|
- * "GNU Lesser General Public License" |
| 11 |
|
- * More information can be found here: |
| 12 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 13 |
|
- * |
| 14 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 15 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 16 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 17 |
|
- * @link http://dwoo.org/ |
| 18 |
|
- * @version 0.3.4 |
| 19 |
|
- * @date 2008-04-09 |
| 20 |
|
- * @package Dwoo |
| 21 |
|
- */ |
| 22 |
|
-class DwooPlugin_for extends DwooBlockPlugin implements DwooICompilableBlock |
| 23 |
|
-{ |
| 24 |
|
- public static $cnt=0; |
| 25 |
|
- |
| 26 |
|
- public function init($name, $from, $to=null, $step=1, $skip=0) |
| 27 |
|
- { |
| 28 |
|
- } |
| 29 |
|
- |
| 30 |
|
- public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
| 31 |
|
- { |
| 32 |
|
- $params = $compiler->getCompiledParams($params); |
| 33 |
|
- $tpl = $compiler->getTemplateSource(true); |
| 34 |
|
- |
| 35 |
|
- // assigns params |
| 36 |
|
- $from = $params['from']; |
| 37 |
|
- $name = $params['name']; |
| 38 |
|
- $step = $params['step']; |
| 39 |
|
- $skip = $params['skip']; |
| 40 |
|
- $to = $params['to']; |
| 41 |
|
- |
| 42 |
|
- // evaluates which global variables have to be computed |
| 43 |
|
- $varName = '$dwoo.for.'.trim($name, '"\'').'.'; |
| 44 |
|
- $shortVarName = '$.for.'.trim($name, '"\'').'.'; |
| 45 |
|
- $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
| 46 |
|
- $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false; |
| 47 |
|
- $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false; |
| 48 |
|
- $usesIndex = strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false; |
| 49 |
|
- $usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false; |
| 50 |
|
- $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false; |
| 51 |
|
- $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false; |
| 52 |
|
- |
| 53 |
|
- // gets foreach id |
| 54 |
|
- $cnt = self::$cnt++; |
| 55 |
|
- |
| 56 |
|
- // builds pre processing output for |
| 57 |
|
- $out = DwooCompiler::PHP_OPEN . "\n".'$_for'.$cnt.'_from = $_for'.$cnt.'_src = '.$from.';'. |
| 58 |
|
- "\n".'$_for'.$cnt.'_to = '.$to.';'. |
| 59 |
|
- "\n".'$_for'.$cnt.'_step = abs('.$step.');'. |
| 60 |
|
- "\n".'$_for'.$cnt.'_skip = abs('.$skip.');'. |
| 61 |
|
- "\n".'if(is_numeric($_for'.$cnt.'_from) && !is_numeric($_for'.$cnt.'_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }'; |
| 62 |
|
- // adds foreach properties |
| 63 |
|
- if($usesAny) |
| 64 |
|
- { |
| 65 |
|
- $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n("; |
| 66 |
|
- if($usesIndex) $out .="\n\t".'"index" => 0,'; |
| 67 |
|
- if($usesIteration) $out .="\n\t".'"iteration" => 1,'; |
| 68 |
|
- if($usesFirst) $out .="\n\t".'"first" => null,'; |
| 69 |
|
- if($usesLast) $out .="\n\t".'"last" => null,'; |
| 70 |
|
- if($usesShow) $out .="\n\t".'"show" => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),'; |
| 71 |
|
- if($usesTotal) $out .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),'; |
| 72 |
|
- $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];'; |
| 73 |
|
- } |
| 74 |
|
- // checks if foreach must be looped |
| 75 |
|
- $out .= "\n".'if($this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0))'."\n{"; |
| 76 |
|
- // iterates over keys |
| 77 |
|
- $out .= "\n\t".'if($this->isArray($_for'.$cnt.'_from, true)) { |
| 78 |
|
- $_for'.$cnt.'_from = 0; |
| 79 |
|
- $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_src)-1; |
| 80 |
|
- } |
| 81 |
|
- $_for'.$cnt.'_keys = array(); |
| 82 |
|
- if(($_for'.$cnt.'_from + $_for'.$cnt.'_skip) <= $_for'.$cnt.'_to) { |
| 83 |
|
- for($tmp=($_for'.$cnt.'_from + $_for'.$cnt.'_skip); $tmp <= $_for'.$cnt.'_to; $tmp += $_for'.$cnt.'_step) |
| 84 |
|
- $_for'.$cnt.'_keys[] = $tmp; |
| 85 |
|
- } else { |
| 86 |
|
- for($tmp=($_for'.$cnt.'_from - $_for'.$cnt.'_skip); $tmp > $_for'.$cnt.'_to; $tmp -= $_for'.$cnt.'_step) |
| 87 |
|
- $_for'.$cnt.'_keys[] = $tmp; |
| 88 |
|
- } |
| 89 |
|
- foreach($_for'.$cnt.'_keys as $this->scope['.$name.'])'."\n\t{"; |
| 90 |
|
- // updates properties |
| 91 |
|
- if($usesIndex) |
| 92 |
|
- $out.="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];'; |
| 93 |
|
- if($usesFirst) |
| 94 |
|
- $out .= "\n\t\t".'$_for'.$cnt.'_glob["first"] = (string) ($_for'.$cnt.'_glob["iteration"] === 1);'; |
| 95 |
|
- if($usesLast) |
| 96 |
|
- $out .= "\n\t\t".'$_for'.$cnt.'_glob["last"] = (string) ($_for'.$cnt.'_glob["iteration"] === $_for'.$cnt.'_glob["total"]);'; |
| 97 |
|
- $out .= "\n// -- for start output\n".DwooCompiler::PHP_CLOSE; |
| 98 |
|
- |
| 99 |
|
- |
| 100 |
|
- // build post processing output and cache it |
| 101 |
|
- $postOut = DwooCompiler::PHP_OPEN . '// -- for end output'; |
| 102 |
|
- // update properties |
| 103 |
|
- if($usesIteration) |
| 104 |
|
- $postOut.="\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;'; |
| 105 |
|
- // end loop |
| 106 |
|
- $postOut .= "\n\t}\n}\n"; |
| 107 |
|
- |
| 108 |
|
- // get block params and save the post-processing output already |
| 109 |
|
- $currentBlock =& $compiler->getCurrentBlock(); |
| 110 |
|
- $currentBlock['params']['postOutput'] = $postOut . DwooCompiler::PHP_CLOSE; |
| 111 |
|
- |
| 112 |
|
- return $out; |
| 113 |
|
- } |
| 114 |
|
- |
| 115 |
|
- public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
| 116 |
|
- { |
| 117 |
|
- return $params['postOutput']; |
| 118 |
|
- } |
| 119 |
|
-} |
| 120 |
|
- |
|
2 |
+<?php |
|
3 |
+/** |
|
4 |
+ * TOCOM |
|
5 |
+ * |
|
6 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
7 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
8 |
+ * |
|
9 |
+ * This file is released under the LGPL |
|
10 |
+ * "GNU Lesser General Public License" |
|
11 |
+ * More information can be found here: |
|
12 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
13 |
+ * |
|
14 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
15 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
16 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
17 |
+ * @link http://dwoo.org/ |
|
18 |
+ * @version 0.3.4 |
|
19 |
+ * @date 2008-04-09 |
|
20 |
+ * @package Dwoo |
|
21 |
+ */ |
|
22 |
+class DwooPlugin_for extends DwooBlockPlugin implements DwooICompilableBlock |
|
23 |
+{ |
|
24 |
+ public static $cnt=0; |
|
25 |
+ |
|
26 |
+ public function init($name, $from, $to=null, $step=1, $skip=0) |
|
27 |
+ { |
|
28 |
+ } |
|
29 |
+ |
|
30 |
+ public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
|
31 |
+ { |
|
32 |
+ $params = $compiler->getCompiledParams($params); |
|
33 |
+ $tpl = $compiler->getTemplateSource(true); |
|
34 |
+ |
|
35 |
+ // assigns params |
|
36 |
+ $from = $params['from']; |
|
37 |
+ $name = $params['name']; |
|
38 |
+ $step = $params['step']; |
|
39 |
+ $skip = $params['skip']; |
|
40 |
+ $to = $params['to']; |
|
41 |
+ |
|
42 |
+ // evaluates which global variables have to be computed |
|
43 |
+ $varName = '$dwoo.for.'.trim($name, '"\'').'.'; |
|
44 |
+ $shortVarName = '$.for.'.trim($name, '"\'').'.'; |
|
45 |
+ $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
|
46 |
+ $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false; |
|
47 |
+ $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false; |
|
48 |
+ $usesIndex = strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false; |
|
49 |
+ $usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false; |
|
50 |
+ $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false; |
|
51 |
+ $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false; |
|
52 |
+ |
|
53 |
+ // gets foreach id |
|
54 |
+ $cnt = self::$cnt++; |
|
55 |
+ |
|
56 |
+ // builds pre processing output for |
|
57 |
+ $out = DwooCompiler::PHP_OPEN . "\n".'$_for'.$cnt.'_from = $_for'.$cnt.'_src = '.$from.';'. |
|
58 |
+ "\n".'$_for'.$cnt.'_to = '.$to.';'. |
|
59 |
+ "\n".'$_for'.$cnt.'_step = abs('.$step.');'. |
|
60 |
+ "\n".'$_for'.$cnt.'_skip = abs('.$skip.');'. |
|
61 |
+ "\n".'if(is_numeric($_for'.$cnt.'_from) && !is_numeric($_for'.$cnt.'_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }'; |
|
62 |
+ // adds foreach properties |
|
63 |
+ if($usesAny) |
|
64 |
+ { |
|
65 |
+ $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n("; |
|
66 |
+ if($usesIndex) $out .="\n\t".'"index" => 0,'; |
|
67 |
+ if($usesIteration) $out .="\n\t".'"iteration" => 1,'; |
|
68 |
+ if($usesFirst) $out .="\n\t".'"first" => null,'; |
|
69 |
+ if($usesLast) $out .="\n\t".'"last" => null,'; |
|
70 |
+ if($usesShow) $out .="\n\t".'"show" => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),'; |
|
71 |
+ if($usesTotal) $out .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),'; |
|
72 |
+ $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];'; |
|
73 |
+ } |
|
74 |
+ // checks if foreach must be looped |
|
75 |
+ $out .= "\n".'if($this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0))'."\n{"; |
|
76 |
+ // iterates over keys |
|
77 |
+ $out .= "\n\t".'if($this->isArray($_for'.$cnt.'_from, true)) { |
|
78 |
+ $_for'.$cnt.'_from = 0; |
|
79 |
+ $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_src)-1; |
|
80 |
+ } |
|
81 |
+ $_for'.$cnt.'_keys = array(); |
|
82 |
+ if(($_for'.$cnt.'_from + $_for'.$cnt.'_skip) <= $_for'.$cnt.'_to) { |
|
83 |
+ for($tmp=($_for'.$cnt.'_from + $_for'.$cnt.'_skip); $tmp <= $_for'.$cnt.'_to; $tmp += $_for'.$cnt.'_step) |
|
84 |
+ $_for'.$cnt.'_keys[] = $tmp; |
|
85 |
+ } else { |
|
86 |
+ for($tmp=($_for'.$cnt.'_from - $_for'.$cnt.'_skip); $tmp > $_for'.$cnt.'_to; $tmp -= $_for'.$cnt.'_step) |
|
87 |
+ $_for'.$cnt.'_keys[] = $tmp; |
|
88 |
+ } |
|
89 |
+ foreach($_for'.$cnt.'_keys as $this->scope['.$name.'])'."\n\t{"; |
|
90 |
+ // updates properties |
|
91 |
+ if($usesIndex) |
|
92 |
+ $out.="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];'; |
|
93 |
+ if($usesFirst) |
|
94 |
+ $out .= "\n\t\t".'$_for'.$cnt.'_glob["first"] = (string) ($_for'.$cnt.'_glob["iteration"] === 1);'; |
|
95 |
+ if($usesLast) |
|
96 |
+ $out .= "\n\t\t".'$_for'.$cnt.'_glob["last"] = (string) ($_for'.$cnt.'_glob["iteration"] === $_for'.$cnt.'_glob["total"]);'; |
|
97 |
+ $out .= "\n// -- for start output\n".DwooCompiler::PHP_CLOSE; |
|
98 |
+ |
|
99 |
+ |
|
100 |
+ // build post processing output and cache it |
|
101 |
+ $postOut = DwooCompiler::PHP_OPEN . '// -- for end output'; |
|
102 |
+ // update properties |
|
103 |
+ if($usesIteration) |
|
104 |
+ $postOut.="\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;'; |
|
105 |
+ // end loop |
|
106 |
+ $postOut .= "\n\t}\n}\n"; |
|
107 |
+ |
|
108 |
+ // get block params and save the post-processing output already |
|
109 |
+ $currentBlock =& $compiler->getCurrentBlock(); |
|
110 |
+ $currentBlock['params']['postOutput'] = $postOut . DwooCompiler::PHP_CLOSE; |
|
111 |
+ |
|
112 |
+ return $out; |
|
113 |
+ } |
|
114 |
+ |
|
115 |
+ public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
|
116 |
+ { |
|
117 |
+ return $params['postOutput']; |
|
118 |
+ } |
|
119 |
+} |
|
120 |
+ |
| 121 |
121 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * TOCOM |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-class DwooPlugin_foreach extends DwooBlockPlugin implements DwooICompilableBlock |
| 24 |
|
-{ |
| 25 |
|
- public static $cnt=0; |
| 26 |
|
- |
| 27 |
|
- public function init($from, $key=null, $item=null, $name='default') |
| 28 |
|
- { |
| 29 |
|
- } |
| 30 |
|
- |
| 31 |
|
- public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
| 32 |
|
- { |
| 33 |
|
- $params = $compiler->getCompiledParams($params); |
| 34 |
|
- $tpl = $compiler->getTemplateSource(true); |
| 35 |
|
- |
| 36 |
|
- // assigns params |
| 37 |
|
- $src = $params['from']; |
| 38 |
|
- |
| 39 |
|
- if($params['item'] !== 'null') |
| 40 |
|
- { |
| 41 |
|
- if($params['key'] !== 'null') |
| 42 |
|
- $key = $params['key']; |
| 43 |
|
- $val = $params['item']; |
| 44 |
|
- } |
| 45 |
|
- elseif($params['key'] !== 'null') |
| 46 |
|
- { |
| 47 |
|
- $val = $params['key']; |
| 48 |
|
- } |
| 49 |
|
- else |
| 50 |
|
- $compiler->triggerError('Foreach <em>item</em> parameter missing', E_USER_ERROR); |
| 51 |
|
- $name = $params['name']; |
| 52 |
|
- |
| 53 |
|
- if(substr($val,0,1) !== '"' && substr($val,0,1) !== '\'') |
| 54 |
|
- $compiler->triggerError('Foreach <em>item</em> parameter must be of type string', E_USER_ERROR); |
| 55 |
|
- if(isset($key) && substr($val,0,1) !== '"' && substr($val,0,1) !== '\'') |
| 56 |
|
- $compiler->triggerError('Foreach <em>key</em> parameter must be of type string', E_USER_ERROR); |
| 57 |
|
- |
| 58 |
|
- // evaluates which global variables have to be computed |
| 59 |
|
- $varName = '$dwoo.foreach.'.trim($name, '"\'').'.'; |
| 60 |
|
- $shortVarName = '$.foreach.'.trim($name, '"\'').'.'; |
| 61 |
|
- $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
| 62 |
|
- $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false; |
| 63 |
|
- $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false; |
| 64 |
|
- $usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false; |
| 65 |
|
- $usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false; |
| 66 |
|
- $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false; |
| 67 |
|
- $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false; |
| 68 |
|
- |
| 69 |
|
- // gets foreach id |
| 70 |
|
- $cnt = self::$cnt++; |
| 71 |
|
- |
| 72 |
|
- // builds pre processing output |
| 73 |
|
- $out = DwooCompiler::PHP_OPEN . "\n".'$_fh'.$cnt.'_data = '.$src.';'; |
| 74 |
|
- // adds foreach properties |
| 75 |
|
- if($usesAny) |
| 76 |
|
- { |
| 77 |
|
- $out .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n("; |
| 78 |
|
- if($usesIndex) $out .="\n\t".'"index" => 0,'; |
| 79 |
|
- if($usesIteration) $out .="\n\t".'"iteration" => 1,'; |
| 80 |
|
- if($usesFirst) $out .="\n\t".'"first" => null,'; |
| 81 |
|
- if($usesLast) $out .="\n\t".'"last" => null,'; |
| 82 |
|
- if($usesShow) $out .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true, true),'; |
| 83 |
|
- if($usesTotal) $out .="\n\t".'"total" => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,'; |
| 84 |
|
- $out.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];'; |
| 85 |
|
- } |
| 86 |
|
- // checks if foreach must be looped |
| 87 |
|
- $out .= "\n".'if($this->isArray($_fh'.$cnt.'_data, true, true) === true)'."\n{"; |
| 88 |
|
- // iterates over keys |
| 89 |
|
- $out .= "\n\t".'foreach($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{"; |
| 90 |
|
- // updates properties |
| 91 |
|
- if($usesFirst) |
| 92 |
|
- $out .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);'; |
| 93 |
|
- if($usesLast) |
| 94 |
|
- $out .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);'; |
| 95 |
|
- $out .= "\n// -- foreach start output\n".DwooCompiler::PHP_CLOSE; |
| 96 |
|
- |
| 97 |
|
- // build post processing output and cache it |
| 98 |
|
- $postOut = DwooCompiler::PHP_OPEN . "\n".'// -- foreach end output'; |
| 99 |
|
- // update properties |
| 100 |
|
- if($usesIndex) |
| 101 |
|
- $postOut.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;'; |
| 102 |
|
- if($usesIteration) |
| 103 |
|
- $postOut.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;'; |
| 104 |
|
- // end loop |
| 105 |
|
- $postOut .= "\n\t}\n}\n"; |
| 106 |
|
- |
| 107 |
|
- // get block params and save the post-processing output already |
| 108 |
|
- $currentBlock =& $compiler->getCurrentBlock(); |
| 109 |
|
- $currentBlock['params']['postOutput'] = $postOut . DwooCompiler::PHP_CLOSE; |
| 110 |
|
- |
| 111 |
|
- return $out; |
| 112 |
|
- } |
| 113 |
|
- |
| 114 |
|
- public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
| 115 |
|
- { |
| 116 |
|
- return $params['postOutput']; |
| 117 |
|
- } |
| 118 |
|
-} |
| 119 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * TOCOM |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+class DwooPlugin_foreach extends DwooBlockPlugin implements DwooICompilableBlock |
|
24 |
+{ |
|
25 |
+ public static $cnt=0; |
|
26 |
+ |
|
27 |
+ public function init($from, $key=null, $item=null, $name='default') |
|
28 |
+ { |
|
29 |
+ } |
|
30 |
+ |
|
31 |
+ public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
|
32 |
+ { |
|
33 |
+ $params = $compiler->getCompiledParams($params); |
|
34 |
+ $tpl = $compiler->getTemplateSource(true); |
|
35 |
+ |
|
36 |
+ // assigns params |
|
37 |
+ $src = $params['from']; |
|
38 |
+ |
|
39 |
+ if($params['item'] !== 'null') |
|
40 |
+ { |
|
41 |
+ if($params['key'] !== 'null') |
|
42 |
+ $key = $params['key']; |
|
43 |
+ $val = $params['item']; |
|
44 |
+ } |
|
45 |
+ elseif($params['key'] !== 'null') |
|
46 |
+ { |
|
47 |
+ $val = $params['key']; |
|
48 |
+ } |
|
49 |
+ else |
|
50 |
+ $compiler->triggerError('Foreach <em>item</em> parameter missing', E_USER_ERROR); |
|
51 |
+ $name = $params['name']; |
|
52 |
+ |
|
53 |
+ if(substr($val,0,1) !== '"' && substr($val,0,1) !== '\'') |
|
54 |
+ $compiler->triggerError('Foreach <em>item</em> parameter must be of type string', E_USER_ERROR); |
|
55 |
+ if(isset($key) && substr($val,0,1) !== '"' && substr($val,0,1) !== '\'') |
|
56 |
+ $compiler->triggerError('Foreach <em>key</em> parameter must be of type string', E_USER_ERROR); |
|
57 |
+ |
|
58 |
+ // evaluates which global variables have to be computed |
|
59 |
+ $varName = '$dwoo.foreach.'.trim($name, '"\'').'.'; |
|
60 |
+ $shortVarName = '$.foreach.'.trim($name, '"\'').'.'; |
|
61 |
+ $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
|
62 |
+ $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false; |
|
63 |
+ $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false; |
|
64 |
+ $usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false; |
|
65 |
+ $usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false; |
|
66 |
+ $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false; |
|
67 |
+ $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false; |
|
68 |
+ |
|
69 |
+ // gets foreach id |
|
70 |
+ $cnt = self::$cnt++; |
|
71 |
+ |
|
72 |
+ // builds pre processing output |
|
73 |
+ $out = DwooCompiler::PHP_OPEN . "\n".'$_fh'.$cnt.'_data = '.$src.';'; |
|
74 |
+ // adds foreach properties |
|
75 |
+ if($usesAny) |
|
76 |
+ { |
|
77 |
+ $out .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n("; |
|
78 |
+ if($usesIndex) $out .="\n\t".'"index" => 0,'; |
|
79 |
+ if($usesIteration) $out .="\n\t".'"iteration" => 1,'; |
|
80 |
+ if($usesFirst) $out .="\n\t".'"first" => null,'; |
|
81 |
+ if($usesLast) $out .="\n\t".'"last" => null,'; |
|
82 |
+ if($usesShow) $out .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true, true),'; |
|
83 |
+ if($usesTotal) $out .="\n\t".'"total" => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,'; |
|
84 |
+ $out.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];'; |
|
85 |
+ } |
|
86 |
+ // checks if foreach must be looped |
|
87 |
+ $out .= "\n".'if($this->isArray($_fh'.$cnt.'_data, true, true) === true)'."\n{"; |
|
88 |
+ // iterates over keys |
|
89 |
+ $out .= "\n\t".'foreach($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{"; |
|
90 |
+ // updates properties |
|
91 |
+ if($usesFirst) |
|
92 |
+ $out .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);'; |
|
93 |
+ if($usesLast) |
|
94 |
+ $out .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);'; |
|
95 |
+ $out .= "\n// -- foreach start output\n".DwooCompiler::PHP_CLOSE; |
|
96 |
+ |
|
97 |
+ // build post processing output and cache it |
|
98 |
+ $postOut = DwooCompiler::PHP_OPEN . "\n".'// -- foreach end output'; |
|
99 |
+ // update properties |
|
100 |
+ if($usesIndex) |
|
101 |
+ $postOut.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;'; |
|
102 |
+ if($usesIteration) |
|
103 |
+ $postOut.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;'; |
|
104 |
+ // end loop |
|
105 |
+ $postOut .= "\n\t}\n}\n"; |
|
106 |
+ |
|
107 |
+ // get block params and save the post-processing output already |
|
108 |
+ $currentBlock =& $compiler->getCurrentBlock(); |
|
109 |
+ $currentBlock['params']['postOutput'] = $postOut . DwooCompiler::PHP_CLOSE; |
|
110 |
+ |
|
111 |
+ return $out; |
|
112 |
+ } |
|
113 |
+ |
|
114 |
+ public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
|
115 |
+ { |
|
116 |
+ return $params['postOutput']; |
|
117 |
+ } |
|
118 |
+} |
|
119 |
+ |
| 120 |
120 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * TOCOM |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-class DwooPlugin_loop extends DwooBlockPlugin implements DwooICompilableBlock |
| 24 |
|
-{ |
| 25 |
|
- public static $cnt=0; |
| 26 |
|
- |
| 27 |
|
- public function init($from, $name='default') |
| 28 |
|
- { |
| 29 |
|
- } |
| 30 |
|
- |
| 31 |
|
- public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
| 32 |
|
- { |
| 33 |
|
- $params = $compiler->getCompiledParams($params); |
| 34 |
|
- $tpl = $compiler->getTemplateSource(true); |
| 35 |
|
- |
| 36 |
|
- // assigns params |
| 37 |
|
- $src = $params['from']; |
| 38 |
|
- $name = $params['name']; |
| 39 |
|
- |
| 40 |
|
- // evaluates which global variables have to be computed |
| 41 |
|
- $varName = '$dwoo.loop.'.trim($name, '"\'').'.'; |
| 42 |
|
- $shortVarName = '$.loop.'.trim($name, '"\'').'.'; |
| 43 |
|
- $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
| 44 |
|
- $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false; |
| 45 |
|
- $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false; |
| 46 |
|
- $usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false; |
| 47 |
|
- $usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false; |
| 48 |
|
- $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false; |
| 49 |
|
- $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false; |
| 50 |
|
- |
| 51 |
|
- // gets foreach id |
| 52 |
|
- $cnt = self::$cnt++; |
| 53 |
|
- |
| 54 |
|
- // builds pre processing output |
| 55 |
|
- $out = DwooCompiler::PHP_OPEN . "\n".'$_loop'.$cnt.'_data = '.$src.';'; |
| 56 |
|
- // adds foreach properties |
| 57 |
|
- if($usesAny) |
| 58 |
|
- { |
| 59 |
|
- $out .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n("; |
| 60 |
|
- if($usesIndex) $out .="\n\t".'"index" => 0,'; |
| 61 |
|
- if($usesIteration) $out .="\n\t".'"iteration" => 1,'; |
| 62 |
|
- if($usesFirst) $out .="\n\t".'"first" => null,'; |
| 63 |
|
- if($usesLast) $out .="\n\t".'"last" => null,'; |
| 64 |
|
- if($usesShow) $out .="\n\t".'"show" => $this->isArray($_loop'.$cnt.'_data, true, true),'; |
| 65 |
|
- if($usesTotal) $out .="\n\t".'"total" => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,'; |
| 66 |
|
- $out.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];'; |
| 67 |
|
- } |
| 68 |
|
- // checks if foreach must be looped |
| 69 |
|
- $out .= "\n".'if($this->isArray($_loop'.$cnt.'_data, true, true) === true)'."\n{"; |
| 70 |
|
- // iterates over keys |
| 71 |
|
- $out .= "\n\t".'foreach($_loop'.$cnt.'_data as $this->scope["-loop-"])'."\n\t{"; |
| 72 |
|
- // updates properties |
| 73 |
|
- if($usesFirst) |
| 74 |
|
- $out .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);'; |
| 75 |
|
- if($usesLast) |
| 76 |
|
- $out .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);'; |
| 77 |
|
- $out .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope("-loop-");'."\n// -- loop start output\n".DwooCompiler::PHP_CLOSE; |
| 78 |
|
- |
| 79 |
|
- // build post processing output and cache it |
| 80 |
|
- $postOut = DwooCompiler::PHP_OPEN . "\n".'// -- loop end output'."\n\t\t".'$this->forceScope($_loop'.$cnt.'_scope);'; |
| 81 |
|
- // update properties |
| 82 |
|
- if($usesIndex) |
| 83 |
|
- $postOut.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;'; |
| 84 |
|
- if($usesIteration) |
| 85 |
|
- $postOut.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;'; |
| 86 |
|
- // end loop |
| 87 |
|
- $postOut .= "\n\t}\n}\n"; |
| 88 |
|
- |
| 89 |
|
- // get block params and save the post-processing output already |
| 90 |
|
- $currentBlock =& $compiler->getCurrentBlock(); |
| 91 |
|
- $currentBlock['params']['postOutput'] = $postOut . DwooCompiler::PHP_CLOSE; |
| 92 |
|
- |
| 93 |
|
- return $out; |
| 94 |
|
- } |
| 95 |
|
- |
| 96 |
|
- public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
| 97 |
|
- { |
| 98 |
|
- return $params['postOutput']; |
| 99 |
|
- } |
| 100 |
|
-} |
| 101 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * TOCOM |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+class DwooPlugin_loop extends DwooBlockPlugin implements DwooICompilableBlock |
|
24 |
+{ |
|
25 |
+ public static $cnt=0; |
|
26 |
+ |
|
27 |
+ public function init($from, $name='default') |
|
28 |
+ { |
|
29 |
+ } |
|
30 |
+ |
|
31 |
+ public static function preProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='', $type) |
|
32 |
+ { |
|
33 |
+ $params = $compiler->getCompiledParams($params); |
|
34 |
+ $tpl = $compiler->getTemplateSource(true); |
|
35 |
+ |
|
36 |
+ // assigns params |
|
37 |
+ $src = $params['from']; |
|
38 |
+ $name = $params['name']; |
|
39 |
+ |
|
40 |
+ // evaluates which global variables have to be computed |
|
41 |
+ $varName = '$dwoo.loop.'.trim($name, '"\'').'.'; |
|
42 |
+ $shortVarName = '$.loop.'.trim($name, '"\'').'.'; |
|
43 |
+ $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
|
44 |
+ $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false; |
|
45 |
+ $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false; |
|
46 |
+ $usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false; |
|
47 |
+ $usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false; |
|
48 |
+ $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false; |
|
49 |
+ $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false; |
|
50 |
+ |
|
51 |
+ // gets foreach id |
|
52 |
+ $cnt = self::$cnt++; |
|
53 |
+ |
|
54 |
+ // builds pre processing output |
|
55 |
+ $out = DwooCompiler::PHP_OPEN . "\n".'$_loop'.$cnt.'_data = '.$src.';'; |
|
56 |
+ // adds foreach properties |
|
57 |
+ if($usesAny) |
|
58 |
+ { |
|
59 |
+ $out .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n("; |
|
60 |
+ if($usesIndex) $out .="\n\t".'"index" => 0,'; |
|
61 |
+ if($usesIteration) $out .="\n\t".'"iteration" => 1,'; |
|
62 |
+ if($usesFirst) $out .="\n\t".'"first" => null,'; |
|
63 |
+ if($usesLast) $out .="\n\t".'"last" => null,'; |
|
64 |
+ if($usesShow) $out .="\n\t".'"show" => $this->isArray($_loop'.$cnt.'_data, true, true),'; |
|
65 |
+ if($usesTotal) $out .="\n\t".'"total" => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,'; |
|
66 |
+ $out.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];'; |
|
67 |
+ } |
|
68 |
+ // checks if foreach must be looped |
|
69 |
+ $out .= "\n".'if($this->isArray($_loop'.$cnt.'_data, true, true) === true)'."\n{"; |
|
70 |
+ // iterates over keys |
|
71 |
+ $out .= "\n\t".'foreach($_loop'.$cnt.'_data as $this->scope["-loop-"])'."\n\t{"; |
|
72 |
+ // updates properties |
|
73 |
+ if($usesFirst) |
|
74 |
+ $out .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);'; |
|
75 |
+ if($usesLast) |
|
76 |
+ $out .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);'; |
|
77 |
+ $out .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope("-loop-");'."\n// -- loop start output\n".DwooCompiler::PHP_CLOSE; |
|
78 |
+ |
|
79 |
+ // build post processing output and cache it |
|
80 |
+ $postOut = DwooCompiler::PHP_OPEN . "\n".'// -- loop end output'."\n\t\t".'$this->forceScope($_loop'.$cnt.'_scope);'; |
|
81 |
+ // update properties |
|
82 |
+ if($usesIndex) |
|
83 |
+ $postOut.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;'; |
|
84 |
+ if($usesIteration) |
|
85 |
+ $postOut.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;'; |
|
86 |
+ // end loop |
|
87 |
+ $postOut .= "\n\t}\n}\n"; |
|
88 |
+ |
|
89 |
+ // get block params and save the post-processing output already |
|
90 |
+ $currentBlock =& $compiler->getCurrentBlock(); |
|
91 |
+ $currentBlock['params']['postOutput'] = $postOut . DwooCompiler::PHP_CLOSE; |
|
92 |
+ |
|
93 |
+ return $out; |
|
94 |
+ } |
|
95 |
+ |
|
96 |
+ public static function postProcessing(DwooCompiler $compiler, array $params, $prepend='', $append='') |
|
97 |
+ { |
|
98 |
+ return $params['postOutput']; |
|
99 |
+ } |
|
100 |
+} |
|
101 |
+ |
| 102 |
102 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * TOCOM |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-class DwooFilter_html_format extends DwooFilter |
| 24 |
|
-{ |
| 25 |
|
- /** |
| 26 |
|
- * tab count to auto-indent the source |
| 27 |
|
- * |
| 28 |
|
- * @var int |
| 29 |
|
- */ |
| 30 |
|
- protected static $tabCount = -1; |
| 31 |
|
- |
| 32 |
|
- /** |
| 33 |
|
- * stores the additional data (following a tag) of the last call to open/close/singleTag |
| 34 |
|
- * |
| 35 |
|
- * @var string |
| 36 |
|
- */ |
| 37 |
|
- protected static $lastCallAdd = ''; |
| 38 |
|
- |
| 39 |
|
- /** |
| 40 |
|
- * formats the input using the singleTag/closeTag/openTag functions |
| 41 |
|
- * |
| 42 |
|
- * It is auto indenting the whole code, excluding <textarea>, <code> and <pre> tags that must be kept intact. |
| 43 |
|
- * Those tags must however contain only htmlentities-escaped text for everything to work properly. |
| 44 |
|
- * Inline tags are presented on a single line with their content |
| 45 |
|
- * |
| 46 |
|
- * @param Dwoo $dwoo the dwoo instance rendering this |
| 47 |
|
- * @param string $input the xhtml to format |
| 48 |
|
- * @return string formatted xhtml |
| 49 |
|
- */ |
| 50 |
|
- public function process($input) |
| 51 |
|
- { |
| 52 |
|
- self::$tabCount = -1; |
| 53 |
|
- |
| 54 |
|
- // auto indent all but textareas & pre (or we have weird tabs inside) |
| 55 |
|
- $input = preg_replace_callback("#(<[^>]+>)(\s*)([^<]*)#", array('self', 'tagDispatcher'), $input); |
| 56 |
|
- |
| 57 |
|
- return $input; |
| 58 |
|
- } |
| 59 |
|
- |
| 60 |
|
- /** |
| 61 |
|
- * helper function for format()'s preg_replace call |
| 62 |
|
- * |
| 63 |
|
- * @param array $input array of matches (1=>tag, 2=>whitespace(optional), 3=>additional non-html content) |
| 64 |
|
- * @return string the indented tag |
| 65 |
|
- */ |
| 66 |
|
- protected static function tagDispatcher($input) |
| 67 |
|
- { |
| 68 |
|
- // textarea, pre, code tags and comments are to be left alone to avoid any non-wanted whitespace inside them so it just outputs them as they were |
| 69 |
|
- if(substr($input[1],0,9) == "<textarea" || substr($input[1],0,4) == "<pre" || substr($input[1],0,5) == "<code" || substr($input[1],0,4) == "<!--" || substr($input[1],0,9) == "<![CDATA[") { |
| 70 |
|
- return $input[1] . $input[3]; |
| 71 |
|
- } |
| 72 |
|
- // closing textarea, code and pre tags and self-closed tags (i.e. <br />) are printed as singleTags because we didn't use openTag for the formers and the latter is a single tag |
| 73 |
|
- if(substr($input[1],0,10) == "</textarea" || substr($input[1],0,5) == "</pre" || substr($input[1],0,6) == "</code" || substr($input[1],-2) == "/>") { |
| 74 |
|
- return self::singleTag($input[1],$input[3],$input[2]); |
| 75 |
|
- } |
| 76 |
|
- // it's the closing tag |
| 77 |
|
- if($input[0][1]=="/"){ |
| 78 |
|
- return self::closeTag($input[1],$input[3],$input[2]); |
| 79 |
|
- } |
| 80 |
|
- // opening tag |
| 81 |
|
- return self::openTag($input[1],$input[3],$input[2]); |
| 82 |
|
- } |
| 83 |
|
- |
| 84 |
|
- /** |
| 85 |
|
- * returns an open tag and adds a tab into the auto indenting |
| 86 |
|
- * |
| 87 |
|
- * @param string $tag content of the tag |
| 88 |
|
- * @param string $add additional data (anything before the following tag) |
| 89 |
|
- * @param string $whitespace white space between the tag and the additional data |
| 90 |
|
- * @return string |
| 91 |
|
- */ |
| 92 |
|
- protected static function openTag($tag,$add,$whitespace) |
| 93 |
|
- { |
| 94 |
|
- $tabs = str_pad('',self::$tabCount++,"\t"); |
| 95 |
|
- |
| 96 |
|
- // if it's one of those tag it's inline so it does not require a leading line break |
| 97 |
|
- if(preg_match('#^<(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)(?: [^>]*|)>#', $tag)) |
| 98 |
|
- { |
| 99 |
|
- $result = $tag . $whitespace . str_replace("\n","\n".$tabs,$add); |
| 100 |
|
- } |
| 101 |
|
- // it's the doctype declaration so no line break here either |
| 102 |
|
- elseif(substr($tag,0,9) == '<!DOCTYPE') |
| 103 |
|
- { |
| 104 |
|
- $result = $tabs . $tag; |
| 105 |
|
- } |
| 106 |
|
- // normal block tag |
| 107 |
|
- else |
| 108 |
|
- { |
| 109 |
|
- $result = "\n".$tabs . $tag; |
| 110 |
|
- |
| 111 |
|
- if(!empty($add)) |
| 112 |
|
- { |
| 113 |
|
- $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add); |
| 114 |
|
- } |
| 115 |
|
- } |
| 116 |
|
- |
| 117 |
|
- self::$lastCallAdd = $add; |
| 118 |
|
- |
| 119 |
|
- return $result; |
| 120 |
|
- } |
| 121 |
|
- |
| 122 |
|
- /** |
| 123 |
|
- * returns a closing tag and removes a tab from the auto indenting |
| 124 |
|
- * |
| 125 |
|
- * @param string $tag content of the tag |
| 126 |
|
- * @param string $add additional data (anything before the following tag) |
| 127 |
|
- * @param string $whitespace white space between the tag and the additional data |
| 128 |
|
- * @return string |
| 129 |
|
- */ |
| 130 |
|
- protected static function closeTag($tag,$add,$whitespace) |
| 131 |
|
- { |
| 132 |
|
- $tabs = str_pad('',--self::$tabCount,"\t"); |
| 133 |
|
- |
| 134 |
|
- // if it's one of those tag it's inline so it does not require a leading line break |
| 135 |
|
- if(preg_match('#^</(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)>#', $tag)) |
| 136 |
|
- { |
| 137 |
|
- $result = $tag . $whitespace . str_replace("\n","\n".$tabs,$add); |
| 138 |
|
- } |
| 139 |
|
- else |
| 140 |
|
- { |
| 141 |
|
- $result = "\n".$tabs.$tag; |
| 142 |
|
- |
| 143 |
|
- if(!empty($add)) |
| 144 |
|
- { |
| 145 |
|
- $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add); |
| 146 |
|
- } |
| 147 |
|
- } |
| 148 |
|
- |
| 149 |
|
- self::$lastCallAdd = $add; |
| 150 |
|
- |
| 151 |
|
- return $result; |
| 152 |
|
- } |
| 153 |
|
- |
| 154 |
|
- /** |
| 155 |
|
- * returns a single tag with auto indenting |
| 156 |
|
- * |
| 157 |
|
- * @param string $tag content of the tag |
| 158 |
|
- * @param string $add additional data (anything before the following tag) |
| 159 |
|
- * @return string |
| 160 |
|
- */ |
| 161 |
|
- protected static function singleTag($tag,$add,$whitespace) |
| 162 |
|
- { |
| 163 |
|
- $tabs = str_pad('',self::$tabCount,"\t"); |
| 164 |
|
- |
| 165 |
|
- // if it's img, br it's inline so it does not require a leading line break |
| 166 |
|
- // if it's a closing textarea, code or pre tag, it does not require a leading line break either or it creates whitespace at the end of those blocks |
| 167 |
|
- if(preg_match('#^<(img|br|/textarea|/pre|/code)(?: [^>]*|)>#', $tag)) |
| 168 |
|
- { |
| 169 |
|
- $result = $tag.$whitespace; |
| 170 |
|
- |
| 171 |
|
- if(!empty($add)) |
| 172 |
|
- { |
| 173 |
|
- $result .= str_replace("\n","\n".$tabs,$add); |
| 174 |
|
- } |
| 175 |
|
- } |
| 176 |
|
- else |
| 177 |
|
- { |
| 178 |
|
- $result = "\n".$tabs.$tag; |
| 179 |
|
- |
| 180 |
|
- if(!empty($add)) |
| 181 |
|
- { |
| 182 |
|
- $result .= "\n".$tabs.str_replace("\n","\n".$tabs,$add); |
| 183 |
|
- } |
| 184 |
|
- } |
| 185 |
|
- |
| 186 |
|
- self::$lastCallAdd = $add; |
| 187 |
|
- |
| 188 |
|
- return $result; |
| 189 |
|
- } |
| 190 |
|
-} |
| 191 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * TOCOM |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+class DwooFilter_html_format extends DwooFilter |
|
24 |
+{ |
|
25 |
+ /** |
|
26 |
+ * tab count to auto-indent the source |
|
27 |
+ * |
|
28 |
+ * @var int |
|
29 |
+ */ |
|
30 |
+ protected static $tabCount = -1; |
|
31 |
+ |
|
32 |
+ /** |
|
33 |
+ * stores the additional data (following a tag) of the last call to open/close/singleTag |
|
34 |
+ * |
|
35 |
+ * @var string |
|
36 |
+ */ |
|
37 |
+ protected static $lastCallAdd = ''; |
|
38 |
+ |
|
39 |
+ /** |
|
40 |
+ * formats the input using the singleTag/closeTag/openTag functions |
|
41 |
+ * |
|
42 |
+ * It is auto indenting the whole code, excluding <textarea>, <code> and <pre> tags that must be kept intact. |
|
43 |
+ * Those tags must however contain only htmlentities-escaped text for everything to work properly. |
|
44 |
+ * Inline tags are presented on a single line with their content |
|
45 |
+ * |
|
46 |
+ * @param Dwoo $dwoo the dwoo instance rendering this |
|
47 |
+ * @param string $input the xhtml to format |
|
48 |
+ * @return string formatted xhtml |
|
49 |
+ */ |
|
50 |
+ public function process($input) |
|
51 |
+ { |
|
52 |
+ self::$tabCount = -1; |
|
53 |
+ |
|
54 |
+ // auto indent all but textareas & pre (or we have weird tabs inside) |
|
55 |
+ $input = preg_replace_callback("#(<[^>]+>)(\s*)([^<]*)#", array('self', 'tagDispatcher'), $input); |
|
56 |
+ |
|
57 |
+ return $input; |
|
58 |
+ } |
|
59 |
+ |
|
60 |
+ /** |
|
61 |
+ * helper function for format()'s preg_replace call |
|
62 |
+ * |
|
63 |
+ * @param array $input array of matches (1=>tag, 2=>whitespace(optional), 3=>additional non-html content) |
|
64 |
+ * @return string the indented tag |
|
65 |
+ */ |
|
66 |
+ protected static function tagDispatcher($input) |
|
67 |
+ { |
|
68 |
+ // textarea, pre, code tags and comments are to be left alone to avoid any non-wanted whitespace inside them so it just outputs them as they were |
|
69 |
+ if(substr($input[1],0,9) == "<textarea" || substr($input[1],0,4) == "<pre" || substr($input[1],0,5) == "<code" || substr($input[1],0,4) == "<!--" || substr($input[1],0,9) == "<![CDATA[") { |
|
70 |
+ return $input[1] . $input[3]; |
|
71 |
+ } |
|
72 |
+ // closing textarea, code and pre tags and self-closed tags (i.e. <br />) are printed as singleTags because we didn't use openTag for the formers and the latter is a single tag |
|
73 |
+ if(substr($input[1],0,10) == "</textarea" || substr($input[1],0,5) == "</pre" || substr($input[1],0,6) == "</code" || substr($input[1],-2) == "/>") { |
|
74 |
+ return self::singleTag($input[1],$input[3],$input[2]); |
|
75 |
+ } |
|
76 |
+ // it's the closing tag |
|
77 |
+ if($input[0][1]=="/"){ |
|
78 |
+ return self::closeTag($input[1],$input[3],$input[2]); |
|
79 |
+ } |
|
80 |
+ // opening tag |
|
81 |
+ return self::openTag($input[1],$input[3],$input[2]); |
|
82 |
+ } |
|
83 |
+ |
|
84 |
+ /** |
|
85 |
+ * returns an open tag and adds a tab into the auto indenting |
|
86 |
+ * |
|
87 |
+ * @param string $tag content of the tag |
|
88 |
+ * @param string $add additional data (anything before the following tag) |
|
89 |
+ * @param string $whitespace white space between the tag and the additional data |
|
90 |
+ * @return string |
|
91 |
+ */ |
|
92 |
+ protected static function openTag($tag,$add,$whitespace) |
|
93 |
+ { |
|
94 |
+ $tabs = str_pad('',self::$tabCount++,"\t"); |
|
95 |
+ |
|
96 |
+ // if it's one of those tag it's inline so it does not require a leading line break |
|
97 |
+ if(preg_match('#^<(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)(?: [^>]*|)>#', $tag)) |
|
98 |
+ { |
|
99 |
+ $result = $tag . $whitespace . str_replace("\n","\n".$tabs,$add); |
|
100 |
+ } |
|
101 |
+ // it's the doctype declaration so no line break here either |
|
102 |
+ elseif(substr($tag,0,9) == '<!DOCTYPE') |
|
103 |
+ { |
|
104 |
+ $result = $tabs . $tag; |
|
105 |
+ } |
|
106 |
+ // normal block tag |
|
107 |
+ else |
|
108 |
+ { |
|
109 |
+ $result = "\n".$tabs . $tag; |
|
110 |
+ |
|
111 |
+ if(!empty($add)) |
|
112 |
+ { |
|
113 |
+ $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add); |
|
114 |
+ } |
|
115 |
+ } |
|
116 |
+ |
|
117 |
+ self::$lastCallAdd = $add; |
|
118 |
+ |
|
119 |
+ return $result; |
|
120 |
+ } |
|
121 |
+ |
|
122 |
+ /** |
|
123 |
+ * returns a closing tag and removes a tab from the auto indenting |
|
124 |
+ * |
|
125 |
+ * @param string $tag content of the tag |
|
126 |
+ * @param string $add additional data (anything before the following tag) |
|
127 |
+ * @param string $whitespace white space between the tag and the additional data |
|
128 |
+ * @return string |
|
129 |
+ */ |
|
130 |
+ protected static function closeTag($tag,$add,$whitespace) |
|
131 |
+ { |
|
132 |
+ $tabs = str_pad('',--self::$tabCount,"\t"); |
|
133 |
+ |
|
134 |
+ // if it's one of those tag it's inline so it does not require a leading line break |
|
135 |
+ if(preg_match('#^</(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)>#', $tag)) |
|
136 |
+ { |
|
137 |
+ $result = $tag . $whitespace . str_replace("\n","\n".$tabs,$add); |
|
138 |
+ } |
|
139 |
+ else |
|
140 |
+ { |
|
141 |
+ $result = "\n".$tabs.$tag; |
|
142 |
+ |
|
143 |
+ if(!empty($add)) |
|
144 |
+ { |
|
145 |
+ $result .= "\n".$tabs."\t".str_replace("\n","\n\t".$tabs,$add); |
|
146 |
+ } |
|
147 |
+ } |
|
148 |
+ |
|
149 |
+ self::$lastCallAdd = $add; |
|
150 |
+ |
|
151 |
+ return $result; |
|
152 |
+ } |
|
153 |
+ |
|
154 |
+ /** |
|
155 |
+ * returns a single tag with auto indenting |
|
156 |
+ * |
|
157 |
+ * @param string $tag content of the tag |
|
158 |
+ * @param string $add additional data (anything before the following tag) |
|
159 |
+ * @return string |
|
160 |
+ */ |
|
161 |
+ protected static function singleTag($tag,$add,$whitespace) |
|
162 |
+ { |
|
163 |
+ $tabs = str_pad('',self::$tabCount,"\t"); |
|
164 |
+ |
|
165 |
+ // if it's img, br it's inline so it does not require a leading line break |
|
166 |
+ // if it's a closing textarea, code or pre tag, it does not require a leading line break either or it creates whitespace at the end of those blocks |
|
167 |
+ if(preg_match('#^<(img|br|/textarea|/pre|/code)(?: [^>]*|)>#', $tag)) |
|
168 |
+ { |
|
169 |
+ $result = $tag.$whitespace; |
|
170 |
+ |
|
171 |
+ if(!empty($add)) |
|
172 |
+ { |
|
173 |
+ $result .= str_replace("\n","\n".$tabs,$add); |
|
174 |
+ } |
|
175 |
+ } |
|
176 |
+ else |
|
177 |
+ { |
|
178 |
+ $result = "\n".$tabs.$tag; |
|
179 |
+ |
|
180 |
+ if(!empty($add)) |
|
181 |
+ { |
|
182 |
+ $result .= "\n".$tabs.str_replace("\n","\n".$tabs,$add); |
|
183 |
+ } |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+ self::$lastCallAdd = $add; |
|
187 |
+ |
|
188 |
+ return $result; |
|
189 |
+ } |
|
190 |
+} |
|
191 |
+ |
| 192 |
192 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * TOCOM |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-class DwooPlugin_extends extends DwooPlugin implements DwooICompilable |
| 24 |
|
-{ |
| 25 |
|
- protected static $childSource; |
| 26 |
|
- protected static $l; |
| 27 |
|
- protected static $r; |
| 28 |
|
- protected static $lastReplacement; |
| 29 |
|
- |
| 30 |
|
- public static function compile(DwooCompiler $compiler, $file) |
| 31 |
|
- { |
| 32 |
|
- list($l, $r) = $compiler->getDelimiters(); |
| 33 |
|
- self::$l = preg_quote($l); |
| 34 |
|
- self::$r = preg_quote($r); |
| 35 |
|
- |
| 36 |
|
- if($compiler->getLooseOpeningHandling()) |
| 37 |
|
- { |
| 38 |
|
- self::$l .= '\s*'; |
| 39 |
|
- self::$r = '\s*'.self::$r; |
| 40 |
|
- } |
| 41 |
|
- $inheritanceTree = array(array('source'=>$compiler->getTemplateSource())); |
| 42 |
|
- |
| 43 |
|
- while(!empty($file)) |
| 44 |
|
- { |
| 45 |
|
- if($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '"')) |
| 46 |
|
- { |
| 47 |
|
- $compiler->triggerError('Extends : The file name must be a non-empty string', E_USER_ERROR); |
| 48 |
|
- return; |
| 49 |
|
- } |
| 50 |
|
- |
| 51 |
|
- if(preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) |
| 52 |
|
- { |
| 53 |
|
- $resource = $m[1]; |
| 54 |
|
- $identifier = $m[2]; |
| 55 |
|
- } |
| 56 |
|
- else |
| 57 |
|
- { |
| 58 |
|
- $resource = 'file'; |
| 59 |
|
- $identifier = substr($file, 1, -1); |
| 60 |
|
- } |
| 61 |
|
- |
| 62 |
|
- if($resource === 'file' && $policy = $compiler->getSecurityPolicy()) |
| 63 |
|
- { |
| 64 |
|
- while(true) |
| 65 |
|
- { |
| 66 |
|
- if(preg_match('{^([a-z]+?)://}i', $identifier)) |
| 67 |
|
- return $compiler->triggerError('The security policy prevents you to read files from external sources.', E_USER_ERROR); |
| 68 |
|
- |
| 69 |
|
- $identifier = realpath($identifier); |
| 70 |
|
- $dirs = $policy->getAllowedDirectories(); |
| 71 |
|
- foreach($dirs as $dir=>$dummy) |
| 72 |
|
- { |
| 73 |
|
- if(strpos($identifier, $dir) === 0) |
| 74 |
|
- break 2; |
| 75 |
|
- } |
| 76 |
|
- return $compiler->triggerError('The security policy prevents you to read <em>'.$identifier.'</em>', E_USER_ERROR); |
| 77 |
|
- } |
| 78 |
|
- } |
| 79 |
|
- |
| 80 |
|
- try { |
| 81 |
|
- $parent = $compiler->getDwoo()->getTemplate($resource, $identifier); |
| 82 |
|
- } catch (DwooException $e) { |
| 83 |
|
- $compiler->triggerError('Extends : Resource <em>'.$resource.'</em> was not added to Dwoo, can not include <em>'.$identifier.'</em>', E_USER_ERROR); |
| 84 |
|
- } |
| 85 |
|
- |
| 86 |
|
- if($parent === null) |
| 87 |
|
- $compiler->triggerError('Extends : Resource "'.$resource.':'.$identifier.'" was not found.', E_USER_ERROR); |
| 88 |
|
- elseif($parent === false) |
| 89 |
|
- $compiler->triggerError('Extends : Extending "'.$resource.':'.$identifier.'" was not allowed for an unknown reason.', E_USER_ERROR); |
| 90 |
|
- |
| 91 |
|
- $newParent = array('source'=>$parent->getSource(), 'resource'=>$resource, 'identifier'=>$identifier, 'uid'=>$parent->getUid()); |
| 92 |
|
- if(array_search($newParent, $inheritanceTree, true) !== false) |
| 93 |
|
- { |
| 94 |
|
- $compiler->triggerError('Extends : Recursive template inheritance detected', E_USER_ERROR); |
| 95 |
|
- } |
| 96 |
|
- |
| 97 |
|
- $inheritanceTree[] = $newParent; |
| 98 |
|
- |
| 99 |
|
- if(preg_match('/^'.self::$l.'extends\s+(?:file=)?\s*(\S+?)'.self::$r.'/i', $parent->getSource(), $match)) |
| 100 |
|
- $file = (substr($match[1], 0, 1) !== '"' && substr($match[1], 0, 1) !== '"') ? '"'.str_replace('"', '\\"', $match[1]).'"' : $match[1]; |
| 101 |
|
- else |
| 102 |
|
- $file = false; |
| 103 |
|
- } |
| 104 |
|
- |
| 105 |
|
- while(true) |
| 106 |
|
- { |
| 107 |
|
- $parent = array_pop($inheritanceTree); |
| 108 |
|
- $child = end($inheritanceTree); |
| 109 |
|
- self::$childSource = $child['source']; |
| 110 |
|
- self::$lastReplacement = count($inheritanceTree) === 1; |
| 111 |
|
- if(!isset($newSource)) |
| 112 |
|
- $newSource = $parent['source']; |
| 113 |
|
- $newSource = preg_replace_callback('/'.self::$l.'block (["\']?)(.+?)\1'.self::$r.'(?:\r?\n?)(.*?)(?:\r?\n?)'.self::$l.'\/block'.self::$r.'/is', array('DwooPlugin_extends', 'replaceBlock'), $newSource); |
| 114 |
|
- |
| 115 |
|
- $newSource = $l.'do extendsCheck("'.$parent['resource'].':'.$parent['identifier'].'" "'.str_replace('"', '\\"', $parent['uid']).'")'.$r.$newSource; |
| 116 |
|
- |
| 117 |
|
- if(self::$lastReplacement) |
| 118 |
|
- break; |
| 119 |
|
- } |
| 120 |
|
- |
| 121 |
|
- $compiler->setTemplateSource($newSource); |
| 122 |
|
- $compiler->setPointer(0); |
| 123 |
|
- } |
| 124 |
|
- |
| 125 |
|
- protected static function replaceBlock(array $matches) |
| 126 |
|
- { |
| 127 |
|
- if(preg_match('/'.self::$l.'block (["\']?)'.preg_quote($matches[2]).'\1'.self::$r.'(?:\r?\n?)(.*?)(?:\r?\n?)'.self::$l.'\/block'.self::$r.'/is', self::$childSource, $override)) |
| 128 |
|
- { |
| 129 |
|
- $l = stripslashes(self::$l); |
| 130 |
|
- $r = stripslashes(self::$r); |
| 131 |
|
- |
| 132 |
|
- if(self::$lastReplacement) |
| 133 |
|
- return preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is', $matches[3], $override[2]); |
| 134 |
|
- else |
| 135 |
|
- return $l.'block '.$matches[1].$matches[2].$matches[1].$r.preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is', $matches[3], $override[2]).$l.'/block'.$r; |
| 136 |
|
- } |
| 137 |
|
- else |
| 138 |
|
- { |
| 139 |
|
- if(self::$lastReplacement) |
| 140 |
|
- return $matches[3]; |
| 141 |
|
- else |
| 142 |
|
- return $matches[0]; |
| 143 |
|
- } |
| 144 |
|
- } |
| 145 |
|
-} |
| 146 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * TOCOM |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+class DwooPlugin_extends extends DwooPlugin implements DwooICompilable |
|
24 |
+{ |
|
25 |
+ protected static $childSource; |
|
26 |
+ protected static $l; |
|
27 |
+ protected static $r; |
|
28 |
+ protected static $lastReplacement; |
|
29 |
+ |
|
30 |
+ public static function compile(DwooCompiler $compiler, $file) |
|
31 |
+ { |
|
32 |
+ list($l, $r) = $compiler->getDelimiters(); |
|
33 |
+ self::$l = preg_quote($l); |
|
34 |
+ self::$r = preg_quote($r); |
|
35 |
+ |
|
36 |
+ if($compiler->getLooseOpeningHandling()) |
|
37 |
+ { |
|
38 |
+ self::$l .= '\s*'; |
|
39 |
+ self::$r = '\s*'.self::$r; |
|
40 |
+ } |
|
41 |
+ $inheritanceTree = array(array('source'=>$compiler->getTemplateSource())); |
|
42 |
+ |
|
43 |
+ while(!empty($file)) |
|
44 |
+ { |
|
45 |
+ if($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '"')) |
|
46 |
+ { |
|
47 |
+ $compiler->triggerError('Extends : The file name must be a non-empty string', E_USER_ERROR); |
|
48 |
+ return; |
|
49 |
+ } |
|
50 |
+ |
|
51 |
+ if(preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) |
|
52 |
+ { |
|
53 |
+ $resource = $m[1]; |
|
54 |
+ $identifier = $m[2]; |
|
55 |
+ } |
|
56 |
+ else |
|
57 |
+ { |
|
58 |
+ $resource = 'file'; |
|
59 |
+ $identifier = substr($file, 1, -1); |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ if($resource === 'file' && $policy = $compiler->getSecurityPolicy()) |
|
63 |
+ { |
|
64 |
+ while(true) |
|
65 |
+ { |
|
66 |
+ if(preg_match('{^([a-z]+?)://}i', $identifier)) |
|
67 |
+ return $compiler->triggerError('The security policy prevents you to read files from external sources.', E_USER_ERROR); |
|
68 |
+ |
|
69 |
+ $identifier = realpath($identifier); |
|
70 |
+ $dirs = $policy->getAllowedDirectories(); |
|
71 |
+ foreach($dirs as $dir=>$dummy) |
|
72 |
+ { |
|
73 |
+ if(strpos($identifier, $dir) === 0) |
|
74 |
+ break 2; |
|
75 |
+ } |
|
76 |
+ return $compiler->triggerError('The security policy prevents you to read <em>'.$identifier.'</em>', E_USER_ERROR); |
|
77 |
+ } |
|
78 |
+ } |
|
79 |
+ |
|
80 |
+ try { |
|
81 |
+ $parent = $compiler->getDwoo()->templateFactory($resource, $identifier); |
|
82 |
+ } catch (DwooException $e) { |
|
83 |
+ $compiler->triggerError('Extends : Resource <em>'.$resource.'</em> was not added to Dwoo, can not include <em>'.$identifier.'</em>', E_USER_ERROR); |
|
84 |
+ } |
|
85 |
+ |
|
86 |
+ if($parent === null) |
|
87 |
+ $compiler->triggerError('Extends : Resource "'.$resource.':'.$identifier.'" was not found.', E_USER_ERROR); |
|
88 |
+ elseif($parent === false) |
|
89 |
+ $compiler->triggerError('Extends : Extending "'.$resource.':'.$identifier.'" was not allowed for an unknown reason.', E_USER_ERROR); |
|
90 |
+ |
|
91 |
+ $newParent = array('source'=>$parent->getSource(), 'resource'=>$resource, 'identifier'=>$identifier, 'uid'=>$parent->getUid()); |
|
92 |
+ if(array_search($newParent, $inheritanceTree, true) !== false) |
|
93 |
+ { |
|
94 |
+ $compiler->triggerError('Extends : Recursive template inheritance detected', E_USER_ERROR); |
|
95 |
+ } |
|
96 |
+ |
|
97 |
+ $inheritanceTree[] = $newParent; |
|
98 |
+ |
|
99 |
+ if(preg_match('/^'.self::$l.'extends\s+(?:file=)?\s*(\S+?)'.self::$r.'/i', $parent->getSource(), $match)) |
|
100 |
+ $file = (substr($match[1], 0, 1) !== '"' && substr($match[1], 0, 1) !== '"') ? '"'.str_replace('"', '\\"', $match[1]).'"' : $match[1]; |
|
101 |
+ else |
|
102 |
+ $file = false; |
|
103 |
+ } |
|
104 |
+ |
|
105 |
+ while(true) |
|
106 |
+ { |
|
107 |
+ $parent = array_pop($inheritanceTree); |
|
108 |
+ $child = end($inheritanceTree); |
|
109 |
+ self::$childSource = $child['source']; |
|
110 |
+ self::$lastReplacement = count($inheritanceTree) === 1; |
|
111 |
+ if(!isset($newSource)) |
|
112 |
+ $newSource = $parent['source']; |
|
113 |
+ $newSource = preg_replace_callback('/'.self::$l.'block (["\']?)(.+?)\1'.self::$r.'(?:\r?\n?)(.*?)(?:\r?\n?)'.self::$l.'\/block'.self::$r.'/is', array('DwooPlugin_extends', 'replaceBlock'), $newSource); |
|
114 |
+ |
|
115 |
+ $newSource = $l.'do extendsCheck("'.$parent['resource'].':'.$parent['identifier'].'" "'.str_replace('"', '\\"', $parent['uid']).'")'.$r.$newSource; |
|
116 |
+ |
|
117 |
+ if(self::$lastReplacement) |
|
118 |
+ break; |
|
119 |
+ } |
|
120 |
+ |
|
121 |
+ $compiler->setTemplateSource($newSource); |
|
122 |
+ $compiler->setPointer(0); |
|
123 |
+ } |
|
124 |
+ |
|
125 |
+ protected static function replaceBlock(array $matches) |
|
126 |
+ { |
|
127 |
+ if(preg_match('/'.self::$l.'block (["\']?)'.preg_quote($matches[2]).'\1'.self::$r.'(?:\r?\n?)(.*?)(?:\r?\n?)'.self::$l.'\/block'.self::$r.'/is', self::$childSource, $override)) |
|
128 |
+ { |
|
129 |
+ $l = stripslashes(self::$l); |
|
130 |
+ $r = stripslashes(self::$r); |
|
131 |
+ |
|
132 |
+ if(self::$lastReplacement) |
|
133 |
+ return preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is', $matches[3], $override[2]); |
|
134 |
+ else |
|
135 |
+ return $l.'block '.$matches[1].$matches[2].$matches[1].$r.preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is', $matches[3], $override[2]).$l.'/block'.$r; |
|
136 |
+ } |
|
137 |
+ else |
|
138 |
+ { |
|
139 |
+ if(self::$lastReplacement) |
|
140 |
+ return $matches[3]; |
|
141 |
+ else |
|
142 |
+ return $matches[0]; |
|
143 |
+ } |
|
144 |
+ } |
|
145 |
+} |
|
146 |
+ |
| 147 |
147 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * TOCOM |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-function DwooPlugin_math_compile(DwooCompiler $compiler, $equation, $format='', $assign='', array $rest=array()) |
| 24 |
|
-{ |
| 25 |
|
- /** |
| 26 |
|
- * Holds the allowed function, characters, operators and constants |
| 27 |
|
- */ |
| 28 |
|
- $allowed = array |
| 29 |
|
- ( |
| 30 |
|
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 31 |
|
- '+', '-', '/', '*', '.', ' ', '<<', '>>', '%', '&', '^', '|', '~', |
| 32 |
|
- 'abs(', 'ceil(', 'floor(', 'exp(', 'log10(', |
| 33 |
|
- 'cos(', 'sin(', 'sqrt(', 'tan(', |
| 34 |
|
- 'M_PI', 'INF', 'M_E', |
| 35 |
|
- ); |
| 36 |
|
- |
| 37 |
|
- /** |
| 38 |
|
- * Holds the functions that can accept multiple arguments |
| 39 |
|
- */ |
| 40 |
|
- $funcs = array |
| 41 |
|
- ( |
| 42 |
|
- 'round(', 'log(', 'pow(', |
| 43 |
|
- 'max(', 'min(', 'rand(', |
| 44 |
|
- ); |
| 45 |
|
- |
| 46 |
|
- $equation = $equationSrc = str_ireplace(array('pi', 'M_PI()', 'inf', ' e '), array('M_PI', 'M_PI', 'INF', ' M_E '), $equation); |
| 47 |
|
- |
| 48 |
|
- $delim = $equation[0]; |
| 49 |
|
- $open = $delim.'.'; |
| 50 |
|
- $close = '.'.$delim; |
| 51 |
|
- $equation = substr($equation, 1, -1); |
| 52 |
|
- $out = ''; |
| 53 |
|
- $ptr = 1; |
| 54 |
|
- $allowcomma = 0; |
| 55 |
|
- while(strlen($equation) > 0) |
| 56 |
|
- { |
| 57 |
|
- $substr = substr($equation, 0, $ptr); |
| 58 |
|
- // allowed string |
| 59 |
|
- if (array_search($substr, $allowed) !== false) { |
| 60 |
|
- $out.=$substr; |
| 61 |
|
- $equation = substr($equation, $ptr); |
| 62 |
|
- $ptr = 0; |
| 63 |
|
- } |
| 64 |
|
- // allowed func |
| 65 |
|
- elseif (array_search($substr, $funcs) !== false) { |
| 66 |
|
- $out.=$substr; |
| 67 |
|
- $equation = substr($equation, $ptr); |
| 68 |
|
- $ptr = 0; |
| 69 |
|
- $allowcomma++; |
| 70 |
|
- if($allowcomma === 1) { |
| 71 |
|
- $allowed[] = ','; |
| 72 |
|
- } |
| 73 |
|
- } |
| 74 |
|
- // variable |
| 75 |
|
- elseif(isset($rest[$substr])) |
| 76 |
|
- { |
| 77 |
|
- $out.=$rest[$substr]; |
| 78 |
|
- $equation = substr($equation, $ptr); |
| 79 |
|
- $ptr = 0; |
| 80 |
|
- } |
| 81 |
|
- // pre-replaced variable |
| 82 |
|
- elseif($substr === $open) |
| 83 |
|
- { |
| 84 |
|
- preg_match('#.*\((?:[^()]*?|(?R))\)'.str_replace('.', '\\.', $close).'#', substr($equation, 2), $m); |
| 85 |
|
- if(empty($m)) |
| 86 |
|
- preg_match('#.*?'.str_replace('.', '\\.', $close).'#', substr($equation, 2), $m); |
| 87 |
|
- $out.=substr($m[0], 0, -2); |
| 88 |
|
- $equation = substr($equation, strlen($m[0])+2); |
| 89 |
|
- $ptr = 0; |
| 90 |
|
- } |
| 91 |
|
- // opening parenthesis |
| 92 |
|
- elseif ($substr==='(') { |
| 93 |
|
- if($allowcomma>0) |
| 94 |
|
- $allowcomma++; |
| 95 |
|
- |
| 96 |
|
- $out.=$substr; |
| 97 |
|
- $equation = substr($equation, $ptr); |
| 98 |
|
- $ptr = 0; |
| 99 |
|
- } |
| 100 |
|
- // closing parenthesis |
| 101 |
|
- elseif ($substr===')') { |
| 102 |
|
- if($allowcomma>0) { |
| 103 |
|
- $allowcomma--; |
| 104 |
|
- if($allowcomma===0) { |
| 105 |
|
- array_pop($allowed); |
| 106 |
|
- } |
| 107 |
|
- } |
| 108 |
|
- |
| 109 |
|
- $out.=$substr; |
| 110 |
|
- $equation = substr($equation, $ptr); |
| 111 |
|
- $ptr = 0; |
| 112 |
|
- } |
| 113 |
|
- // parse error if we've consumed the entire equation without finding anything valid |
| 114 |
|
- elseif ($ptr >= strlen($equation)) { |
| 115 |
|
- $compiler->triggerError('Math : Syntax error or variable undefined in equation '.$equationSrc.' at '.$substr, E_USER_ERROR); |
| 116 |
|
- return; |
| 117 |
|
- } |
| 118 |
|
- else |
| 119 |
|
- { |
| 120 |
|
- $ptr++; |
| 121 |
|
- } |
| 122 |
|
- } |
| 123 |
|
- if($format !== '\'\'') |
| 124 |
|
- $out = 'sprintf('.$format.', '.$out.')'; |
| 125 |
|
- if($assign !== '\'\'') |
| 126 |
|
- return '($this->assignInScope('.$out.', '.$assign.'))'; |
| 127 |
|
- return '('.$out.')'; |
| 128 |
|
-} |
| 129 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * TOCOM |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+function DwooPlugin_math_compile(DwooCompiler $compiler, $equation, $format='', $assign='', array $rest=array()) |
|
24 |
+{ |
|
25 |
+ /** |
|
26 |
+ * Holds the allowed function, characters, operators and constants |
|
27 |
+ */ |
|
28 |
+ $allowed = array |
|
29 |
+ ( |
|
30 |
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
|
31 |
+ '+', '-', '/', '*', '.', ' ', '<<', '>>', '%', '&', '^', '|', '~', |
|
32 |
+ 'abs(', 'ceil(', 'floor(', 'exp(', 'log10(', |
|
33 |
+ 'cos(', 'sin(', 'sqrt(', 'tan(', |
|
34 |
+ 'M_PI', 'INF', 'M_E', |
|
35 |
+ ); |
|
36 |
+ |
|
37 |
+ /** |
|
38 |
+ * Holds the functions that can accept multiple arguments |
|
39 |
+ */ |
|
40 |
+ $funcs = array |
|
41 |
+ ( |
|
42 |
+ 'round(', 'log(', 'pow(', |
|
43 |
+ 'max(', 'min(', 'rand(', |
|
44 |
+ ); |
|
45 |
+ |
|
46 |
+ $equation = $equationSrc = str_ireplace(array('pi', 'M_PI()', 'inf', ' e '), array('M_PI', 'M_PI', 'INF', ' M_E '), $equation); |
|
47 |
+ |
|
48 |
+ $delim = $equation[0]; |
|
49 |
+ $open = $delim.'.'; |
|
50 |
+ $close = '.'.$delim; |
|
51 |
+ $equation = substr($equation, 1, -1); |
|
52 |
+ $out = ''; |
|
53 |
+ $ptr = 1; |
|
54 |
+ $allowcomma = 0; |
|
55 |
+ while(strlen($equation) > 0) |
|
56 |
+ { |
|
57 |
+ $substr = substr($equation, 0, $ptr); |
|
58 |
+ // allowed string |
|
59 |
+ if (array_search($substr, $allowed) !== false) { |
|
60 |
+ $out.=$substr; |
|
61 |
+ $equation = substr($equation, $ptr); |
|
62 |
+ $ptr = 0; |
|
63 |
+ } |
|
64 |
+ // allowed func |
|
65 |
+ elseif (array_search($substr, $funcs) !== false) { |
|
66 |
+ $out.=$substr; |
|
67 |
+ $equation = substr($equation, $ptr); |
|
68 |
+ $ptr = 0; |
|
69 |
+ $allowcomma++; |
|
70 |
+ if($allowcomma === 1) { |
|
71 |
+ $allowed[] = ','; |
|
72 |
+ } |
|
73 |
+ } |
|
74 |
+ // variable |
|
75 |
+ elseif(isset($rest[$substr])) |
|
76 |
+ { |
|
77 |
+ $out.=$rest[$substr]; |
|
78 |
+ $equation = substr($equation, $ptr); |
|
79 |
+ $ptr = 0; |
|
80 |
+ } |
|
81 |
+ // pre-replaced variable |
|
82 |
+ elseif($substr === $open) |
|
83 |
+ { |
|
84 |
+ preg_match('#.*\((?:[^()]*?|(?R))\)'.str_replace('.', '\\.', $close).'#', substr($equation, 2), $m); |
|
85 |
+ if(empty($m)) |
|
86 |
+ preg_match('#.*?'.str_replace('.', '\\.', $close).'#', substr($equation, 2), $m); |
|
87 |
+ $out.=substr($m[0], 0, -2); |
|
88 |
+ $equation = substr($equation, strlen($m[0])+2); |
|
89 |
+ $ptr = 0; |
|
90 |
+ } |
|
91 |
+ // opening parenthesis |
|
92 |
+ elseif ($substr==='(') { |
|
93 |
+ if($allowcomma>0) |
|
94 |
+ $allowcomma++; |
|
95 |
+ |
|
96 |
+ $out.=$substr; |
|
97 |
+ $equation = substr($equation, $ptr); |
|
98 |
+ $ptr = 0; |
|
99 |
+ } |
|
100 |
+ // closing parenthesis |
|
101 |
+ elseif ($substr===')') { |
|
102 |
+ if($allowcomma>0) { |
|
103 |
+ $allowcomma--; |
|
104 |
+ if($allowcomma===0) { |
|
105 |
+ array_pop($allowed); |
|
106 |
+ } |
|
107 |
+ } |
|
108 |
+ |
|
109 |
+ $out.=$substr; |
|
110 |
+ $equation = substr($equation, $ptr); |
|
111 |
+ $ptr = 0; |
|
112 |
+ } |
|
113 |
+ // parse error if we've consumed the entire equation without finding anything valid |
|
114 |
+ elseif ($ptr >= strlen($equation)) { |
|
115 |
+ $compiler->triggerError('Math : Syntax error or variable undefined in equation '.$equationSrc.' at '.$substr, E_USER_ERROR); |
|
116 |
+ return; |
|
117 |
+ } |
|
118 |
+ else |
|
119 |
+ { |
|
120 |
+ $ptr++; |
|
121 |
+ } |
|
122 |
+ } |
|
123 |
+ if($format !== '\'\'') |
|
124 |
+ $out = 'sprintf('.$format.', '.$out.')'; |
|
125 |
+ if($assign !== '\'\'') |
|
126 |
+ return '($this->assignInScope('.$out.', '.$assign.'))'; |
|
127 |
+ return '('.$out.')'; |
|
128 |
+} |
|
129 |
+ |
| 130 |
130 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-/** |
| 5 |
|
- * TOCOM |
| 6 |
|
- * |
| 7 |
|
- * This software is provided 'as-is', without any express or implied warranty. |
| 8 |
|
- * In no event will the authors be held liable for any damages arising from the use of this software. |
| 9 |
|
- * |
| 10 |
|
- * This file is released under the LGPL |
| 11 |
|
- * "GNU Lesser General Public License" |
| 12 |
|
- * More information can be found here: |
| 13 |
|
- * {@link http://www.gnu.org/copyleft/lesser.html} |
| 14 |
|
- * |
| 15 |
|
- * @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
|
- * @copyright Copyright (c) 2008, Jordi Boggiano |
| 17 |
|
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 18 |
|
- * @link http://dwoo.org/ |
| 19 |
|
- * @version 0.3.4 |
| 20 |
|
- * @date 2008-04-09 |
| 21 |
|
- * @package Dwoo |
| 22 |
|
- */ |
| 23 |
|
-class DwooProcessor_smarty_compat extends DwooProcessor |
| 24 |
|
-{ |
| 25 |
|
- public function process($input) |
| 26 |
|
- { |
| 27 |
|
- list($l, $r) = $this->compiler->getDelimiters(); |
| 28 |
|
- |
| 29 |
|
- $rl = preg_quote($l); |
| 30 |
|
- $rr = preg_quote($r); |
| 31 |
|
- $sectionParam = '(?:(name|loop|start|step|max|show)\s*=\s*(\S+))?\s*'; |
| 32 |
|
- $input = preg_replace_callback('/'.$rl.'\s*section '.str_repeat($sectionParam, 6).'\s*'.$rr.'(.+?)(?:'.$rl.'\s*sectionelse\s*'.$rr.'(.+?))?'.$rl.'\s*\/section\s*'.$rr.'/is', array($this, 'convertSection'), $input); |
| 33 |
|
- $input = str_replace('$smarty.section.', '$smarty.for.', $input); |
| 34 |
|
- |
| 35 |
|
- $smarty = array |
| 36 |
|
- ( |
| 37 |
|
- '/'.$rl.'\s*ldelim\s*'.$rr.'/', |
| 38 |
|
- '/'.$rl.'\s*rdelim\s*'.$rr.'/', |
| 39 |
|
- '/'.$rl.'\s*\$smarty\.ldelim\s*'.$rr.'/', |
| 40 |
|
- '/'.$rl.'\s*\$smarty\.rdelim\s*'.$rr.'/', |
| 41 |
|
- '/\$smarty\./', |
| 42 |
|
- '/'.$rl.'\s*php\s*'.$rr.'/', |
| 43 |
|
- '/'.$rl.'\s*\/php\s*'.$rr.'/', |
| 44 |
|
- ); |
| 45 |
|
- |
| 46 |
|
- $dwoo = array |
| 47 |
|
- ( |
| 48 |
|
- '\\'.$l, |
| 49 |
|
- $r, |
| 50 |
|
- '\\'.$l, |
| 51 |
|
- $r, |
| 52 |
|
- '$dwoo.', |
| 53 |
|
- '<?php ', |
| 54 |
|
- ' ?>', |
| 55 |
|
- ); |
| 56 |
|
- |
| 57 |
|
- if(preg_match('{\|@([a-z][a-z0-9_]*)}i', $input, $matches)) |
| 58 |
|
- $this->compiler->triggerError('The Smarty Compatibility Module has detected that you use |@'.$matches[1].' in your template, this might lead to problems as Dwoo interprets the @ operator differently than Smarty, see http://wiki.dwoo.org/index.php/Syntax#The_.40_Operator', E_USER_NOTICE); |
| 59 |
|
- |
| 60 |
|
- return preg_replace($smarty, $dwoo, $input); |
| 61 |
|
- } |
| 62 |
|
- |
| 63 |
|
- protected function convertSection(array $matches) |
| 64 |
|
- { |
| 65 |
|
- $params = array(); |
| 66 |
|
- $index = 1; |
| 67 |
|
- while(!empty($matches[$index]) && $index < 13) |
| 68 |
|
- { |
| 69 |
|
- $params[$matches[$index]] = $matches[$index+1]; |
| 70 |
|
- $index += 2; |
| 71 |
|
- } |
| 72 |
|
- $params['content'] = $matches[13]; |
| 73 |
|
- if(isset($matches[14]) && !empty($matches[14])) |
| 74 |
|
- $params['altcontent'] = $matches[14]; |
| 75 |
|
- |
| 76 |
|
- if(empty($params['name'])) |
| 77 |
|
- $this->compiler->triggerError('Missing parameter <em>name</em> for section tag'); |
| 78 |
|
- $name = $params['name']; |
| 79 |
|
- |
| 80 |
|
- if(isset($params['loop'])) |
| 81 |
|
- $loops = $params['loop']; |
| 82 |
|
- |
| 83 |
|
- if(isset($params['max'])) |
| 84 |
|
- $max = $params['max']; |
| 85 |
|
- |
| 86 |
|
- if(isset($params['start'])) |
| 87 |
|
- $start = $params['start']; |
| 88 |
|
- |
| 89 |
|
- if(isset($params['step'])) |
| 90 |
|
- $step = $params['step']; |
| 91 |
|
- |
| 92 |
|
- if (!isset($loops)) |
| 93 |
|
- $loops = null; |
| 94 |
|
- |
| 95 |
|
- if (!isset($max) || $max < 0) |
| 96 |
|
- { |
| 97 |
|
- if(is_numeric($loops)) |
| 98 |
|
- $max = $loops; |
| 99 |
|
- else |
| 100 |
|
- $max = 'null'; |
| 101 |
|
- } |
| 102 |
|
- |
| 103 |
|
- if (!isset($step)) |
| 104 |
|
- $step = 1; |
| 105 |
|
- if (!isset($start)) |
| 106 |
|
- $start = $loops - 1; |
| 107 |
|
- elseif(!is_numeric($loops)) |
| 108 |
|
- $start = 0; |
| 109 |
|
- |
| 110 |
|
- list($l, $r) = $this->compiler->getDelimiters(); |
| 111 |
|
- |
| 112 |
|
- if(is_numeric($loops)) |
| 113 |
|
- { |
| 114 |
|
- if(isset($params['start']) && isset($params['loop']) && !isset($params['max'])) |
| 115 |
|
- $output = $l.'for '.$name.' '.$start.' '.($loops-$step).' '.$step.$r; |
| 116 |
|
- else |
| 117 |
|
- $output = $l.'for '.$name.' '.$start.' '.($start+floor($step*$max+($step>0?-1:1))).' '.$step.$r; |
| 118 |
|
- } |
| 119 |
|
- else |
| 120 |
|
- $output = $l.'for '.$name.' '.$loops.' '.($start+floor($max/$step)).' '.$step.' '.$start.$r; |
| 121 |
|
- |
| 122 |
|
- $output .= str_replace('['.trim($name, '"\'').']', '[$'.trim($name, '"\'').']', $params['content']); |
| 123 |
|
- |
| 124 |
|
- if(isset($params['altcontent'])) |
| 125 |
|
- $output .= $l.'forelse'.$r.$params['altcontent']; |
| 126 |
|
- |
| 127 |
|
- $output .= $l.'/for'.$r; |
| 128 |
|
- |
| 129 |
|
- return $output; |
| 130 |
|
- } |
| 131 |
|
-} |
| 132 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+/** |
|
5 |
+ * TOCOM |
|
6 |
+ * |
|
7 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
8 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
9 |
+ * |
|
10 |
+ * This file is released under the LGPL |
|
11 |
+ * "GNU Lesser General Public License" |
|
12 |
+ * More information can be found here: |
|
13 |
+ * {@link http://www.gnu.org/copyleft/lesser.html} |
|
14 |
+ * |
|
15 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
16 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
17 |
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
|
18 |
+ * @link http://dwoo.org/ |
|
19 |
+ * @version 0.3.4 |
|
20 |
+ * @date 2008-04-09 |
|
21 |
+ * @package Dwoo |
|
22 |
+ */ |
|
23 |
+class DwooProcessor_smarty_compat extends DwooProcessor |
|
24 |
+{ |
|
25 |
+ public function process($input) |
|
26 |
+ { |
|
27 |
+ list($l, $r) = $this->compiler->getDelimiters(); |
|
28 |
+ |
|
29 |
+ $rl = preg_quote($l); |
|
30 |
+ $rr = preg_quote($r); |
|
31 |
+ $sectionParam = '(?:(name|loop|start|step|max|show)\s*=\s*(\S+))?\s*'; |
|
32 |
+ $input = preg_replace_callback('/'.$rl.'\s*section '.str_repeat($sectionParam, 6).'\s*'.$rr.'(.+?)(?:'.$rl.'\s*sectionelse\s*'.$rr.'(.+?))?'.$rl.'\s*\/section\s*'.$rr.'/is', array($this, 'convertSection'), $input); |
|
33 |
+ $input = str_replace('$smarty.section.', '$smarty.for.', $input); |
|
34 |
+ |
|
35 |
+ $smarty = array |
|
36 |
+ ( |
|
37 |
+ '/'.$rl.'\s*ldelim\s*'.$rr.'/', |
|
38 |
+ '/'.$rl.'\s*rdelim\s*'.$rr.'/', |
|
39 |
+ '/'.$rl.'\s*\$smarty\.ldelim\s*'.$rr.'/', |
|
40 |
+ '/'.$rl.'\s*\$smarty\.rdelim\s*'.$rr.'/', |
|
41 |
+ '/\$smarty\./', |
|
42 |
+ '/'.$rl.'\s*php\s*'.$rr.'/', |
|
43 |
+ '/'.$rl.'\s*\/php\s*'.$rr.'/', |
|
44 |
+ ); |
|
45 |
+ |
|
46 |
+ $dwoo = array |
|
47 |
+ ( |
|
48 |
+ '\\'.$l, |
|
49 |
+ $r, |
|
50 |
+ '\\'.$l, |
|
51 |
+ $r, |
|
52 |
+ '$dwoo.', |
|
53 |
+ '<?php ', |
|
54 |
+ ' ?>', |
|
55 |
+ ); |
|
56 |
+ |
|
57 |
+ if(preg_match('{\|@([a-z][a-z0-9_]*)}i', $input, $matches)) |
|
58 |
+ $this->compiler->triggerError('The Smarty Compatibility Module has detected that you use |@'.$matches[1].' in your template, this might lead to problems as Dwoo interprets the @ operator differently than Smarty, see http://wiki.dwoo.org/index.php/Syntax#The_.40_Operator', E_USER_NOTICE); |
|
59 |
+ |
|
60 |
+ return preg_replace($smarty, $dwoo, $input); |
|
61 |
+ } |
|
62 |
+ |
|
63 |
+ protected function convertSection(array $matches) |
|
64 |
+ { |
|
65 |
+ $params = array(); |
|
66 |
+ $index = 1; |
|
67 |
+ while(!empty($matches[$index]) && $index < 13) |
|
68 |
+ { |
|
69 |
+ $params[$matches[$index]] = $matches[$index+1]; |
|
70 |
+ $index += 2; |
|
71 |
+ } |
|
72 |
+ $params['content'] = $matches[13]; |
|
73 |
+ if(isset($matches[14]) && !empty($matches[14])) |
|
74 |
+ $params['altcontent'] = $matches[14]; |
|
75 |
+ |
|
76 |
+ if(empty($params['name'])) |
|
77 |
+ $this->compiler->triggerError('Missing parameter <em>name</em> for section tag'); |
|
78 |
+ $name = $params['name']; |
|
79 |
+ |
|
80 |
+ if(isset($params['loop'])) |
|
81 |
+ $loops = $params['loop']; |
|
82 |
+ |
|
83 |
+ if(isset($params['max'])) |
|
84 |
+ $max = $params['max']; |
|
85 |
+ |
|
86 |
+ if(isset($params['start'])) |
|
87 |
+ $start = $params['start']; |
|
88 |
+ |
|
89 |
+ if(isset($params['step'])) |
|
90 |
+ $step = $params['step']; |
|
91 |
+ |
|
92 |
+ if (!isset($loops)) |
|
93 |
+ $loops = null; |
|
94 |
+ |
|
95 |
+ if (!isset($max) || $max < 0) |
|
96 |
+ { |
|
97 |
+ if(is_numeric($loops)) |
|
98 |
+ $max = $loops; |
|
99 |
+ else |
|
100 |
+ $max = 'null'; |
|
101 |
+ } |
|
102 |
+ |
|
103 |
+ if (!isset($step)) |
|
104 |
+ $step = 1; |
|
105 |
+ if (!isset($start)) |
|
106 |
+ $start = $loops - 1; |
|
107 |
+ elseif(!is_numeric($loops)) |
|
108 |
+ $start = 0; |
|
109 |
+ |
|
110 |
+ list($l, $r) = $this->compiler->getDelimiters(); |
|
111 |
+ |
|
112 |
+ if(is_numeric($loops)) |
|
113 |
+ { |
|
114 |
+ if(isset($params['start']) && isset($params['loop']) && !isset($params['max'])) |
|
115 |
+ $output = $l.'for '.$name.' '.$start.' '.($loops-$step).' '.$step.$r; |
|
116 |
+ else |
|
117 |
+ $output = $l.'for '.$name.' '.$start.' '.($start+floor($step*$max+($step>0?-1:1))).' '.$step.$r; |
|
118 |
+ } |
|
119 |
+ else |
|
120 |
+ $output = $l.'for '.$name.' '.$loops.' '.($start+floor($max/$step)).' '.$step.' '.$start.$r; |
|
121 |
+ |
|
122 |
+ $output .= str_replace('['.trim($name, '"\'').']', '[$'.trim($name, '"\'').']', $params['content']); |
|
123 |
+ |
|
124 |
+ if(isset($params['altcontent'])) |
|
125 |
+ $output .= $l.'forelse'.$r.$params['altcontent']; |
|
126 |
+ |
|
127 |
+ $output .= $l.'/for'.$r; |
|
128 |
+ |
|
129 |
+ return $output; |
|
130 |
+ } |
|
131 |
+} |
|
132 |
+ |
| 133 |
133 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-require_once dirname(dirname(__FILE__)).'/DwooCompiler.php'; |
| 5 |
|
- |
| 6 |
|
-class BlockTests extends PHPUnit_Framework_TestCase |
| 7 |
|
-{ |
| 8 |
|
- protected $compiler; |
| 9 |
|
- protected $dwoo; |
| 10 |
|
- |
| 11 |
|
- public function __construct() |
| 12 |
|
- { |
| 13 |
|
- // extend this class and override this in your constructor to test a modded compiler |
| 14 |
|
- $this->compiler = new DwooCompiler(); |
| 15 |
|
- $this->dwoo = new Dwoo(); |
| 16 |
|
- } |
| 17 |
|
- |
| 18 |
|
- public function testCapture() |
| 19 |
|
- { |
| 20 |
|
- $tpl = new DwooTemplateString('{capture name="foo" assign="foo"}BAR{/capture}{$dwoo.capture.foo}-{$foo}'); |
| 21 |
|
- $tpl->forceCompilation(); |
| 22 |
|
- |
| 23 |
|
- $this->assertEquals('BAR-BAR', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 24 |
|
- |
| 25 |
|
- $tpl = new DwooTemplateString('{capture "foo" "foo" true}BAR{/capture}{capture "foo" "foo" true}BAR{/capture}{$foo}'); |
| 26 |
|
- $tpl->forceCompilation(); |
| 27 |
|
- |
| 28 |
|
- $this->assertEquals('BARBAR', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 29 |
|
- |
| 30 |
|
- // fixes the init call not being called (which is normal) |
| 31 |
|
- $fixCall = new DwooPlugin_capture($this->dwoo); |
| 32 |
|
- $fixCall->init(''); |
| 33 |
|
- } |
| 34 |
|
- |
| 35 |
|
- public function testExtends() |
| 36 |
|
- { |
| 37 |
|
- $tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend1.html'); |
| 38 |
|
- $tpl->forceCompilation(); |
| 39 |
|
- |
| 40 |
|
- $this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo |
| 41 |
|
-child1 |
| 42 |
|
-toplevelContent1 |
| 43 |
|
-bar |
| 44 |
|
-toplevelContent2 |
| 45 |
|
-baz")); |
| 46 |
|
- } |
| 47 |
|
- |
| 48 |
|
- public function testNonExtendedBlocksFromParent() |
| 49 |
|
- { |
| 50 |
|
- $tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'toplevel.html'); |
| 51 |
|
- $tpl->forceCompilation(); |
| 52 |
|
- |
| 53 |
|
- $this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo |
| 54 |
|
- |
| 55 |
|
-toplevelContent1 |
| 56 |
|
- |
| 57 |
|
-bar |
| 58 |
|
- |
| 59 |
|
-toplevelContent2 |
| 60 |
|
- |
| 61 |
|
-baz")); |
| 62 |
|
- // fixes the init call not being called (which is normal) |
| 63 |
|
- $fixCall = new DwooPlugin_block($this->dwoo); |
| 64 |
|
- $fixCall->init(''); |
| 65 |
|
- } |
| 66 |
|
- |
| 67 |
|
- public function testExtendsMultiple() |
| 68 |
|
- { |
| 69 |
|
- $tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend2.html'); |
| 70 |
|
- $tpl->forceCompilation(); |
| 71 |
|
- |
| 72 |
|
- $this->assertThat($this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler), new DwooConstraintStringEquals("foo |
| 73 |
|
-child1 |
| 74 |
|
-toplevelContent1child2 |
| 75 |
|
-bar |
| 76 |
|
-FOObartoplevelContent2 |
| 77 |
|
-baz")); |
| 78 |
|
- } |
| 79 |
|
- |
| 80 |
|
- public function testIf() |
| 81 |
|
- { |
| 82 |
|
- $tpl = new DwooTemplateString('{if "BAR"==reverse($foo|reverse|upper)}true{/if}'); |
| 83 |
|
- $tpl->forceCompilation(); |
| 84 |
|
- |
| 85 |
|
- $this->assertEquals('true', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 86 |
|
- |
| 87 |
|
- $tpl = new DwooTemplateString('{if 4/2==2 && 2!=1 && 3>0 && 4<5 && 5<=5 && 6>=3 && 3===3 && "3"!==3}true{/if}'); |
| 88 |
|
- $tpl->forceCompilation(); |
| 89 |
|
- |
| 90 |
|
- $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 91 |
|
- |
| 92 |
|
- $tpl = new DwooTemplateString('{if 5%2==1 && !isset($foo)}true{/if}'); |
| 93 |
|
- $tpl->forceCompilation(); |
| 94 |
|
- |
| 95 |
|
- $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 96 |
|
- |
| 97 |
|
- $tpl = new DwooTemplateString('{if 5 is not div by 2 && 4 is div by 2 && 6 is even && 6 is not even by 5 && (3 is odd && 9 is odd by 3)}true{/if}'); |
| 98 |
|
- $tpl->forceCompilation(); |
| 99 |
|
- |
| 100 |
|
- $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 101 |
|
- |
| 102 |
|
- $tpl = new DwooTemplateString('{if (3==4 && 5==5) || 3==3}true{/if}'); |
| 103 |
|
- $tpl->forceCompilation(); |
| 104 |
|
- |
| 105 |
|
- $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 106 |
|
- |
| 107 |
|
- // fixes the init call not being called (which is normal) |
| 108 |
|
- $fixCall = new DwooPlugin_if($this->dwoo); |
| 109 |
|
- $fixCall->init(array()); |
| 110 |
|
- } |
| 111 |
|
- |
| 112 |
|
- public function testIfElseif() |
| 113 |
|
- { |
| 114 |
|
- $tpl = new DwooTemplateString('{if "BAR" == "bar"}true{elseif "BAR"=="BAR"}false{/if}'); |
| 115 |
|
- $tpl->forceCompilation(); |
| 116 |
|
- |
| 117 |
|
- $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 118 |
|
- |
| 119 |
|
- // fixes the init call not being called (which is normal) |
| 120 |
|
- $fixCall = new DwooPlugin_elseif($this->dwoo); |
| 121 |
|
- $fixCall->init(array()); |
| 122 |
|
- } |
| 123 |
|
- |
| 124 |
|
- public function testIfElse() |
| 125 |
|
- { |
| 126 |
|
- $tpl = new DwooTemplateString('{if "BAR" == "bar"}true{else}false{/if}'); |
| 127 |
|
- $tpl->forceCompilation(); |
| 128 |
|
- |
| 129 |
|
- $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 130 |
|
- |
| 131 |
|
- // fixes the init call not being called (which is normal) |
| 132 |
|
- $fixCall = new DwooPlugin_else($this->dwoo); |
| 133 |
|
- $fixCall->init(); |
| 134 |
|
- } |
| 135 |
|
- |
| 136 |
|
- public function testIfElseImplicitUnset() |
| 137 |
|
- { |
| 138 |
|
- $tpl = new DwooTemplateString('{if $moo}true{else}false{/if}'); |
| 139 |
|
- $tpl->forceCompilation(); |
| 140 |
|
- |
| 141 |
|
- $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 142 |
|
- } |
| 143 |
|
- |
| 144 |
|
- public function testIfElseImplicitTrue() |
| 145 |
|
- { |
| 146 |
|
- $tpl = new DwooTemplateString('{if $moo}true{else}false{/if}'); |
| 147 |
|
- $tpl->forceCompilation(); |
| 148 |
|
- |
| 149 |
|
- $this->assertEquals('true', $this->dwoo->get($tpl, array('moo'=>'i'), $this->compiler)); |
| 150 |
|
- } |
| 151 |
|
- |
| 152 |
|
- public function testFor() |
| 153 |
|
- { |
| 154 |
|
- $tpl = new DwooTemplateString('{for name=i from=$sub}{$i}.{$sub[$i]}{/for}'); |
| 155 |
|
- $tpl->forceCompilation(); |
| 156 |
|
- |
| 157 |
|
- $this->assertEquals('0.foo1.bar2.baz3.qux', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler)); |
| 158 |
|
- |
| 159 |
|
- // fixes the init call not being called (which is normal) |
| 160 |
|
- $fixCall = new DwooPlugin_for($this->dwoo); |
| 161 |
|
- $fixCall->init(null,null); |
| 162 |
|
- } |
| 163 |
|
- |
| 164 |
|
- public function testForElse() |
| 165 |
|
- { |
| 166 |
|
- $tpl = new DwooTemplateString('{for name=i from=0 to=0}{$i}{else}Narp!{/for}'); |
| 167 |
|
- $tpl->forceCompilation(); |
| 168 |
|
- |
| 169 |
|
- $this->assertEquals('Narp!', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 170 |
|
- |
| 171 |
|
- $tpl = new DwooTemplateString('{for name=i from=0 to=0}{$i}{forelse}Narp!{/for}'); |
| 172 |
|
- $tpl->forceCompilation(); |
| 173 |
|
- |
| 174 |
|
- $this->assertEquals('Narp!', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 175 |
|
- |
| 176 |
|
- $tpl = new DwooTemplateString('{for name=i from=0 to=10 step=3}{$i}{else}Narp!{/for}'); |
| 177 |
|
- $tpl->forceCompilation(); |
| 178 |
|
- |
| 179 |
|
- $this->assertEquals('0369', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 180 |
|
- |
| 181 |
|
- // fixes the init call not being called (which is normal) |
| 182 |
|
- $fixCall = new DwooPlugin_forelse($this->dwoo); |
| 183 |
|
- $fixCall->init(null,null); |
| 184 |
|
- } |
| 185 |
|
- |
| 186 |
|
- public function testForeachSmarty() |
| 187 |
|
- { |
| 188 |
|
- $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{/foreach}'); |
| 189 |
|
- $tpl->forceCompilation(); |
| 190 |
|
- |
| 191 |
|
- $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 192 |
|
- |
| 193 |
|
- // fixes the init call not being called (which is normal) |
| 194 |
|
- $fixCall = new DwooPlugin_foreach($this->dwoo); |
| 195 |
|
- $fixCall->init(''); |
| 196 |
|
- } |
| 197 |
|
- |
| 198 |
|
- public function testForeachSmartyAlt() |
| 199 |
|
- { |
| 200 |
|
- $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{/foreach}'); |
| 201 |
|
- $tpl->forceCompilation(); |
| 202 |
|
- |
| 203 |
|
- $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 204 |
|
- } |
| 205 |
|
- |
| 206 |
|
- public function testForeachDwoo() |
| 207 |
|
- { |
| 208 |
|
- // Item only, key arg is mapped to it just as foreach($foo as $item) |
| 209 |
|
- $tpl = new DwooTemplateString('{foreach $sub item}{$item}{/foreach}'); |
| 210 |
|
- $tpl->forceCompilation(); |
| 211 |
|
- |
| 212 |
|
- $this->assertEquals('foobar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 213 |
|
- |
| 214 |
|
- // Item and key used, key is second just as foreach($foo as $key=>$item) |
| 215 |
|
- $tpl = new DwooTemplateString('{foreach $sub key item}{$key}.{$item}{/foreach}'); |
| 216 |
|
- $tpl->forceCompilation(); |
| 217 |
|
- |
| 218 |
|
- $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 219 |
|
- } |
| 220 |
|
- |
| 221 |
|
- public function testForeachWithGlobalVars() |
| 222 |
|
- { |
| 223 |
|
- $tpl = new DwooTemplateString('{foreach $sub key item foo}{if $dwoo.foreach.foo.first}F{elseif $dwoo.foreach.foo.last}L{/if}{/foreach}'); |
| 224 |
|
- $tpl->forceCompilation(); |
| 225 |
|
- |
| 226 |
|
- $this->assertEquals('FL', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 227 |
|
- } |
| 228 |
|
- |
| 229 |
|
- public function testForeachWithGlobalVarsPreceding() |
| 230 |
|
- { |
| 231 |
|
- $tpl = new DwooTemplateString('{if isset($dwoo.foreach.foo.total)}fail{/if}{foreach $sub key item foo}{/foreach}'); |
| 232 |
|
- $tpl->forceCompilation(); |
| 233 |
|
- |
| 234 |
|
- $this->assertEquals('', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 235 |
|
- } |
| 236 |
|
- |
| 237 |
|
- public function testForeachWithGlobalVarsFollowing() |
| 238 |
|
- { |
| 239 |
|
- $tpl = new DwooTemplateString('{foreach $sub key item foo}{/foreach}{$dwoo.foreach.foo.total}'); |
| 240 |
|
- $tpl->forceCompilation(); |
| 241 |
|
- |
| 242 |
|
- $this->assertEquals('2', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 243 |
|
- } |
| 244 |
|
- |
| 245 |
|
- public function testForeachDwooAlt() |
| 246 |
|
- { |
| 247 |
|
- $tpl = new DwooTemplateString('{foreach $sub key item}{$key}.{$item}{/foreach}'); |
| 248 |
|
- $tpl->forceCompilation(); |
| 249 |
|
- |
| 250 |
|
- $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
| 251 |
|
- } |
| 252 |
|
- |
| 253 |
|
- public function testForeachElseEmpty() |
| 254 |
|
- { |
| 255 |
|
- $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{foreachelse}bar{/foreach}'); |
| 256 |
|
- $tpl->forceCompilation(); |
| 257 |
|
- |
| 258 |
|
- $this->assertEquals('bar', $this->dwoo->get($tpl, array('sub'=>array()), $this->compiler)); |
| 259 |
|
- |
| 260 |
|
- // fixes the init call not being called (which is normal) |
| 261 |
|
- $fixCall = new DwooPlugin_foreachelse($this->dwoo); |
| 262 |
|
- $fixCall->init(''); |
| 263 |
|
- } |
| 264 |
|
- |
| 265 |
|
- public function testForeachElseUnset() |
| 266 |
|
- { |
| 267 |
|
- $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{foreachelse}bar{/foreach}'); |
| 268 |
|
- $tpl->forceCompilation(); |
| 269 |
|
- |
| 270 |
|
- $this->assertEquals('bar', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 271 |
|
- } |
| 272 |
|
- |
| 273 |
|
- public function testLoop() |
| 274 |
|
- { |
| 275 |
|
- $tpl = new DwooTemplateString('{loop $foo}{$.loop.default.index}>{$0}/{$1}{/loop}'); |
| 276 |
|
- $tpl->forceCompilation(); |
| 277 |
|
- |
| 278 |
|
- $this->assertEquals('0>a/b1>c/d', $this->dwoo->get($tpl, array('foo'=>array(array('a','b'), array('c','d'))) , $this->compiler)); |
| 279 |
|
- |
| 280 |
|
- // fixes the init call not being called (which is normal) |
| 281 |
|
- $fixCall = new DwooPlugin_loop($this->dwoo); |
| 282 |
|
- $fixCall->init(''); |
| 283 |
|
- } |
| 284 |
|
- |
| 285 |
|
- public function testLoopElse() |
| 286 |
|
- { |
| 287 |
|
- $tpl = new DwooTemplateString('{loop $foo}{$.loop.default.index}>{$0}/{$1}{else}MOO{/loop}'); |
| 288 |
|
- $tpl->forceCompilation(); |
| 289 |
|
- |
| 290 |
|
- $this->assertEquals('MOO', $this->dwoo->get($tpl, array() , $this->compiler)); |
| 291 |
|
- |
| 292 |
|
- // fixes the init call not being called (which is normal) |
| 293 |
|
- $fixCall = new DwooPlugin_loop($this->dwoo); |
| 294 |
|
- $fixCall->init(''); |
| 295 |
|
- } |
| 296 |
|
- |
| 297 |
|
- public function testTextFormat() |
| 298 |
|
- { |
| 299 |
|
- $tpl = new DwooTemplateString('aa{textformat wrap=10}hello world is so unoriginal but hey.. {textformat wrap=4}a a a a a a{/textformat}it works.{/textformat}bb'); |
| 300 |
|
- $tpl->forceCompilation(); |
| 301 |
|
- |
| 302 |
|
- $this->assertEquals('aahello |
| 303 |
|
-world is |
| 304 |
|
-so |
| 305 |
|
-unoriginal |
| 306 |
|
-but hey.. |
| 307 |
|
-a a |
| 308 |
|
- |
| 309 |
|
-a a |
| 310 |
|
- |
| 311 |
|
-a ait |
| 312 |
|
-works.bb', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 313 |
|
- |
| 314 |
|
- $tpl = new DwooTemplateString('{textformat style=email indent=50}hello world is so unoriginal but hey.. it works.{/textformat}'); |
| 315 |
|
- $tpl->forceCompilation(); |
| 316 |
|
- |
| 317 |
|
- $this->assertEquals(' hello world is so |
| 318 |
|
- unoriginal but hey.. |
| 319 |
|
- it works.', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 320 |
|
- |
| 321 |
|
- $tpl = new DwooTemplateString('{textformat style=email indent=50 assign=foo}hello world is so unoriginal but hey.. it works.{/textformat}-{$foo}'); |
| 322 |
|
- $tpl->forceCompilation(); |
| 323 |
|
- |
| 324 |
|
- $this->assertEquals('- hello world is so |
| 325 |
|
- unoriginal but hey.. |
| 326 |
|
- it works.', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 327 |
|
- |
| 328 |
|
- $tpl = new DwooTemplateString('{textformat style=html wrap=10}hello world{/textformat}'); |
| 329 |
|
- $tpl->forceCompilation(); |
| 330 |
|
- |
| 331 |
|
- $this->assertEquals('hello<br />world', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 332 |
|
- } |
| 333 |
|
- |
| 334 |
|
- public function testWith() |
| 335 |
|
- { |
| 336 |
|
- $tpl = new DwooTemplateString('{with $foo}{$a}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}'); |
| 337 |
|
- $tpl->forceCompilation(); |
| 338 |
|
- |
| 339 |
|
- $this->assertEquals('bar--', $this->dwoo->get($tpl, array('foo'=>array('a'=>'bar')), $this->compiler)); |
| 340 |
|
- |
| 341 |
|
- $tpl = new DwooTemplateString('{with $foo}{$a.0}{with $a}{$0}{/with}{with $b}B{else}NOB{/with}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}{with $fooo}a{withelse}b{/with}'); |
| 342 |
|
- $tpl->forceCompilation(); |
| 343 |
|
- |
| 344 |
|
- $this->assertEquals('barbarNOB--b', $this->dwoo->get($tpl, array('foo'=>array('a'=>array('bar'))), $this->compiler)); |
| 345 |
|
- |
| 346 |
|
- // fixes the init call not being called (which is normal) |
| 347 |
|
- $fixCall = new DwooPlugin_with($this->dwoo); |
| 348 |
|
- $fixCall->init(''); |
| 349 |
|
- $fixCall = new DwooPlugin_withelse($this->dwoo); |
| 350 |
|
- $fixCall->init(''); |
| 351 |
|
- } |
| 352 |
|
-} |
| 353 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+require_once 'Dwoo/Compiler.php'; |
|
5 |
+ |
|
6 |
+class BlockTests extends PHPUnit_Framework_TestCase |
|
7 |
+{ |
|
8 |
+ protected $compiler; |
|
9 |
+ protected $dwoo; |
|
10 |
+ |
|
11 |
+ public function __construct() |
|
12 |
+ { |
|
13 |
+ // extend this class and override this in your constructor to test a modded compiler |
|
14 |
+ $this->compiler = new DwooCompiler(); |
|
15 |
+ $this->dwoo = new Dwoo(); |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ public function testCapture() |
|
19 |
+ { |
|
20 |
+ $tpl = new DwooTemplateString('{capture name="foo" assign="foo"}BAR{/capture}{$dwoo.capture.foo}-{$foo}'); |
|
21 |
+ $tpl->forceCompilation(); |
|
22 |
+ |
|
23 |
+ $this->assertEquals('BAR-BAR', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
24 |
+ |
|
25 |
+ $tpl = new DwooTemplateString('{capture "foo" "foo" true}BAR{/capture}{capture "foo" "foo" true}BAR{/capture}{$foo}'); |
|
26 |
+ $tpl->forceCompilation(); |
|
27 |
+ |
|
28 |
+ $this->assertEquals('BARBAR', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
29 |
+ |
|
30 |
+ // fixes the init call not being called (which is normal) |
|
31 |
+ $fixCall = new DwooPlugin_capture($this->dwoo); |
|
32 |
+ $fixCall->init(''); |
|
33 |
+ } |
|
34 |
+ |
|
35 |
+ public function testExtends() |
|
36 |
+ { |
|
37 |
+ $tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend1.html'); |
|
38 |
+ $tpl->forceCompilation(); |
|
39 |
+ |
|
40 |
+ $this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo |
|
41 |
+child1 |
|
42 |
+toplevelContent1 |
|
43 |
+bar |
|
44 |
+toplevelContent2 |
|
45 |
+baz")); |
|
46 |
+ } |
|
47 |
+ |
|
48 |
+ public function testNonExtendedBlocksFromParent() |
|
49 |
+ { |
|
50 |
+ $tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'toplevel.html'); |
|
51 |
+ $tpl->forceCompilation(); |
|
52 |
+ |
|
53 |
+ $this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo |
|
54 |
+ |
|
55 |
+toplevelContent1 |
|
56 |
+ |
|
57 |
+bar |
|
58 |
+ |
|
59 |
+toplevelContent2 |
|
60 |
+ |
|
61 |
+baz")); |
|
62 |
+ // fixes the init call not being called (which is normal) |
|
63 |
+ $fixCall = new DwooPlugin_block($this->dwoo); |
|
64 |
+ $fixCall->init(''); |
|
65 |
+ } |
|
66 |
+ |
|
67 |
+ public function testExtendsMultiple() |
|
68 |
+ { |
|
69 |
+ $tpl = new DwooTemplateFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend2.html'); |
|
70 |
+ $tpl->forceCompilation(); |
|
71 |
+ |
|
72 |
+ $this->assertThat($this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler), new DwooConstraintStringEquals("foo |
|
73 |
+child1 |
|
74 |
+toplevelContent1child2 |
|
75 |
+bar |
|
76 |
+FOObartoplevelContent2 |
|
77 |
+baz")); |
|
78 |
+ } |
|
79 |
+ |
|
80 |
+ public function testIf() |
|
81 |
+ { |
|
82 |
+ $tpl = new DwooTemplateString('{if "BAR"==reverse($foo|reverse|upper)}true{/if}'); |
|
83 |
+ $tpl->forceCompilation(); |
|
84 |
+ |
|
85 |
+ $this->assertEquals('true', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
86 |
+ |
|
87 |
+ $tpl = new DwooTemplateString('{if 4/2==2 && 2!=1 && 3>0 && 4<5 && 5<=5 && 6>=3 && 3===3 && "3"!==3}true{/if}'); |
|
88 |
+ $tpl->forceCompilation(); |
|
89 |
+ |
|
90 |
+ $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
91 |
+ |
|
92 |
+ $tpl = new DwooTemplateString('{if 5%2==1 && !isset($foo)}true{/if}'); |
|
93 |
+ $tpl->forceCompilation(); |
|
94 |
+ |
|
95 |
+ $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
96 |
+ |
|
97 |
+ $tpl = new DwooTemplateString('{if 5 is not div by 2 && 4 is div by 2 && 6 is even && 6 is not even by 5 && (3 is odd && 9 is odd by 3)}true{/if}'); |
|
98 |
+ $tpl->forceCompilation(); |
|
99 |
+ |
|
100 |
+ $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
101 |
+ |
|
102 |
+ $tpl = new DwooTemplateString('{if (3==4 && 5==5) || 3==3}true{/if}'); |
|
103 |
+ $tpl->forceCompilation(); |
|
104 |
+ |
|
105 |
+ $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
106 |
+ |
|
107 |
+ // fixes the init call not being called (which is normal) |
|
108 |
+ $fixCall = new DwooPlugin_if($this->dwoo); |
|
109 |
+ $fixCall->init(array()); |
|
110 |
+ } |
|
111 |
+ |
|
112 |
+ public function testIfElseif() |
|
113 |
+ { |
|
114 |
+ $tpl = new DwooTemplateString('{if "BAR" == "bar"}true{elseif "BAR"=="BAR"}false{/if}'); |
|
115 |
+ $tpl->forceCompilation(); |
|
116 |
+ |
|
117 |
+ $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
118 |
+ |
|
119 |
+ // fixes the init call not being called (which is normal) |
|
120 |
+ $fixCall = new DwooPlugin_elseif($this->dwoo); |
|
121 |
+ $fixCall->init(array()); |
|
122 |
+ } |
|
123 |
+ |
|
124 |
+ public function testIfElse() |
|
125 |
+ { |
|
126 |
+ $tpl = new DwooTemplateString('{if "BAR" == "bar"}true{else}false{/if}'); |
|
127 |
+ $tpl->forceCompilation(); |
|
128 |
+ |
|
129 |
+ $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
130 |
+ |
|
131 |
+ // fixes the init call not being called (which is normal) |
|
132 |
+ $fixCall = new DwooPlugin_else($this->dwoo); |
|
133 |
+ $fixCall->init(); |
|
134 |
+ } |
|
135 |
+ |
|
136 |
+ public function testIfElseImplicitUnset() |
|
137 |
+ { |
|
138 |
+ $tpl = new DwooTemplateString('{if $moo}true{else}false{/if}'); |
|
139 |
+ $tpl->forceCompilation(); |
|
140 |
+ |
|
141 |
+ $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
142 |
+ } |
|
143 |
+ |
|
144 |
+ public function testIfElseImplicitTrue() |
|
145 |
+ { |
|
146 |
+ $tpl = new DwooTemplateString('{if $moo}true{else}false{/if}'); |
|
147 |
+ $tpl->forceCompilation(); |
|
148 |
+ |
|
149 |
+ $this->assertEquals('true', $this->dwoo->get($tpl, array('moo'=>'i'), $this->compiler)); |
|
150 |
+ } |
|
151 |
+ |
|
152 |
+ public function testFor() |
|
153 |
+ { |
|
154 |
+ $tpl = new DwooTemplateString('{for name=i from=$sub}{$i}.{$sub[$i]}{/for}'); |
|
155 |
+ $tpl->forceCompilation(); |
|
156 |
+ |
|
157 |
+ $this->assertEquals('0.foo1.bar2.baz3.qux', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler)); |
|
158 |
+ |
|
159 |
+ // fixes the init call not being called (which is normal) |
|
160 |
+ $fixCall = new DwooPlugin_for($this->dwoo); |
|
161 |
+ $fixCall->init(null,null); |
|
162 |
+ } |
|
163 |
+ |
|
164 |
+ public function testForElse() |
|
165 |
+ { |
|
166 |
+ $tpl = new DwooTemplateString('{for name=i from=0 to=0}{$i}{else}Narp!{/for}'); |
|
167 |
+ $tpl->forceCompilation(); |
|
168 |
+ |
|
169 |
+ $this->assertEquals('Narp!', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
170 |
+ |
|
171 |
+ $tpl = new DwooTemplateString('{for name=i from=0 to=0}{$i}{forelse}Narp!{/for}'); |
|
172 |
+ $tpl->forceCompilation(); |
|
173 |
+ |
|
174 |
+ $this->assertEquals('Narp!', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
175 |
+ |
|
176 |
+ $tpl = new DwooTemplateString('{for name=i from=0 to=10 step=3}{$i}{else}Narp!{/for}'); |
|
177 |
+ $tpl->forceCompilation(); |
|
178 |
+ |
|
179 |
+ $this->assertEquals('0369', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
180 |
+ |
|
181 |
+ // fixes the init call not being called (which is normal) |
|
182 |
+ $fixCall = new DwooPlugin_forelse($this->dwoo); |
|
183 |
+ $fixCall->init(null,null); |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+ public function testForeachSmarty() |
|
187 |
+ { |
|
188 |
+ $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{/foreach}'); |
|
189 |
+ $tpl->forceCompilation(); |
|
190 |
+ |
|
191 |
+ $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
192 |
+ |
|
193 |
+ // fixes the init call not being called (which is normal) |
|
194 |
+ $fixCall = new DwooPlugin_foreach($this->dwoo); |
|
195 |
+ $fixCall->init(''); |
|
196 |
+ } |
|
197 |
+ |
|
198 |
+ public function testForeachSmartyAlt() |
|
199 |
+ { |
|
200 |
+ $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{/foreach}'); |
|
201 |
+ $tpl->forceCompilation(); |
|
202 |
+ |
|
203 |
+ $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
204 |
+ } |
|
205 |
+ |
|
206 |
+ public function testForeachDwoo() |
|
207 |
+ { |
|
208 |
+ // Item only, key arg is mapped to it just as foreach($foo as $item) |
|
209 |
+ $tpl = new DwooTemplateString('{foreach $sub item}{$item}{/foreach}'); |
|
210 |
+ $tpl->forceCompilation(); |
|
211 |
+ |
|
212 |
+ $this->assertEquals('foobar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
213 |
+ |
|
214 |
+ // Item and key used, key is second just as foreach($foo as $key=>$item) |
|
215 |
+ $tpl = new DwooTemplateString('{foreach $sub key item}{$key}.{$item}{/foreach}'); |
|
216 |
+ $tpl->forceCompilation(); |
|
217 |
+ |
|
218 |
+ $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
219 |
+ } |
|
220 |
+ |
|
221 |
+ public function testForeachWithGlobalVars() |
|
222 |
+ { |
|
223 |
+ $tpl = new DwooTemplateString('{foreach $sub key item foo}{if $dwoo.foreach.foo.first}F{elseif $dwoo.foreach.foo.last}L{/if}{/foreach}'); |
|
224 |
+ $tpl->forceCompilation(); |
|
225 |
+ |
|
226 |
+ $this->assertEquals('FL', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
227 |
+ } |
|
228 |
+ |
|
229 |
+ public function testForeachWithGlobalVarsPreceding() |
|
230 |
+ { |
|
231 |
+ $tpl = new DwooTemplateString('{if isset($dwoo.foreach.foo.total)}fail{/if}{foreach $sub key item foo}{/foreach}'); |
|
232 |
+ $tpl->forceCompilation(); |
|
233 |
+ |
|
234 |
+ $this->assertEquals('', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
235 |
+ } |
|
236 |
+ |
|
237 |
+ public function testForeachWithGlobalVarsFollowing() |
|
238 |
+ { |
|
239 |
+ $tpl = new DwooTemplateString('{foreach $sub key item foo}{/foreach}{$dwoo.foreach.foo.total}'); |
|
240 |
+ $tpl->forceCompilation(); |
|
241 |
+ |
|
242 |
+ $this->assertEquals('2', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
243 |
+ } |
|
244 |
+ |
|
245 |
+ public function testForeachDwooAlt() |
|
246 |
+ { |
|
247 |
+ $tpl = new DwooTemplateString('{foreach $sub key item}{$key}.{$item}{/foreach}'); |
|
248 |
+ $tpl->forceCompilation(); |
|
249 |
+ |
|
250 |
+ $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); |
|
251 |
+ } |
|
252 |
+ |
|
253 |
+ public function testForeachElseEmpty() |
|
254 |
+ { |
|
255 |
+ $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{foreachelse}bar{/foreach}'); |
|
256 |
+ $tpl->forceCompilation(); |
|
257 |
+ |
|
258 |
+ $this->assertEquals('bar', $this->dwoo->get($tpl, array('sub'=>array()), $this->compiler)); |
|
259 |
+ |
|
260 |
+ // fixes the init call not being called (which is normal) |
|
261 |
+ $fixCall = new DwooPlugin_foreachelse($this->dwoo); |
|
262 |
+ $fixCall->init(''); |
|
263 |
+ } |
|
264 |
+ |
|
265 |
+ public function testForeachElseUnset() |
|
266 |
+ { |
|
267 |
+ $tpl = new DwooTemplateString('{foreach from=$sub key=key item=item}{$key}.{$item}{foreachelse}bar{/foreach}'); |
|
268 |
+ $tpl->forceCompilation(); |
|
269 |
+ |
|
270 |
+ $this->assertEquals('bar', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
271 |
+ } |
|
272 |
+ |
|
273 |
+ public function testLoop() |
|
274 |
+ { |
|
275 |
+ $tpl = new DwooTemplateString('{loop $foo}{$.loop.default.index}>{$0}/{$1}{/loop}'); |
|
276 |
+ $tpl->forceCompilation(); |
|
277 |
+ |
|
278 |
+ $this->assertEquals('0>a/b1>c/d', $this->dwoo->get($tpl, array('foo'=>array(array('a','b'), array('c','d'))) , $this->compiler)); |
|
279 |
+ |
|
280 |
+ // fixes the init call not being called (which is normal) |
|
281 |
+ $fixCall = new DwooPlugin_loop($this->dwoo); |
|
282 |
+ $fixCall->init(''); |
|
283 |
+ } |
|
284 |
+ |
|
285 |
+ public function testLoopElse() |
|
286 |
+ { |
|
287 |
+ $tpl = new DwooTemplateString('{loop $foo}{$.loop.default.index}>{$0}/{$1}{else}MOO{/loop}'); |
|
288 |
+ $tpl->forceCompilation(); |
|
289 |
+ |
|
290 |
+ $this->assertEquals('MOO', $this->dwoo->get($tpl, array() , $this->compiler)); |
|
291 |
+ |
|
292 |
+ // fixes the init call not being called (which is normal) |
|
293 |
+ $fixCall = new DwooPlugin_loop($this->dwoo); |
|
294 |
+ $fixCall->init(''); |
|
295 |
+ } |
|
296 |
+ |
|
297 |
+ public function testTextFormat() |
|
298 |
+ { |
|
299 |
+ $tpl = new DwooTemplateString('aa{textformat wrap=10}hello world is so unoriginal but hey.. {textformat wrap=4}a a a a a a{/textformat}it works.{/textformat}bb'); |
|
300 |
+ $tpl->forceCompilation(); |
|
301 |
+ |
|
302 |
+ $this->assertEquals('aahello |
|
303 |
+world is |
|
304 |
+so |
|
305 |
+unoriginal |
|
306 |
+but hey.. |
|
307 |
+a a |
|
308 |
+ |
|
309 |
+a a |
|
310 |
+ |
|
311 |
+a ait |
|
312 |
+works.bb', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
313 |
+ |
|
314 |
+ $tpl = new DwooTemplateString('{textformat style=email indent=50}hello world is so unoriginal but hey.. it works.{/textformat}'); |
|
315 |
+ $tpl->forceCompilation(); |
|
316 |
+ |
|
317 |
+ $this->assertEquals(' hello world is so |
|
318 |
+ unoriginal but hey.. |
|
319 |
+ it works.', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
320 |
+ |
|
321 |
+ $tpl = new DwooTemplateString('{textformat style=email indent=50 assign=foo}hello world is so unoriginal but hey.. it works.{/textformat}-{$foo}'); |
|
322 |
+ $tpl->forceCompilation(); |
|
323 |
+ |
|
324 |
+ $this->assertEquals('- hello world is so |
|
325 |
+ unoriginal but hey.. |
|
326 |
+ it works.', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
327 |
+ |
|
328 |
+ $tpl = new DwooTemplateString('{textformat style=html wrap=10}hello world{/textformat}'); |
|
329 |
+ $tpl->forceCompilation(); |
|
330 |
+ |
|
331 |
+ $this->assertEquals('hello<br />world', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
332 |
+ } |
|
333 |
+ |
|
334 |
+ public function testWith() |
|
335 |
+ { |
|
336 |
+ $tpl = new DwooTemplateString('{with $foo}{$a}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}'); |
|
337 |
+ $tpl->forceCompilation(); |
|
338 |
+ |
|
339 |
+ $this->assertEquals('bar--', $this->dwoo->get($tpl, array('foo'=>array('a'=>'bar')), $this->compiler)); |
|
340 |
+ |
|
341 |
+ $tpl = new DwooTemplateString('{with $foo}{$a.0}{with $a}{$0}{/with}{with $b}B{else}NOB{/with}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}{with $fooo}a{withelse}b{/with}'); |
|
342 |
+ $tpl->forceCompilation(); |
|
343 |
+ |
|
344 |
+ $this->assertEquals('barbarNOB--b', $this->dwoo->get($tpl, array('foo'=>array('a'=>array('bar'))), $this->compiler)); |
|
345 |
+ |
|
346 |
+ // fixes the init call not being called (which is normal) |
|
347 |
+ $fixCall = new DwooPlugin_with($this->dwoo); |
|
348 |
+ $fixCall->init(''); |
|
349 |
+ $fixCall = new DwooPlugin_withelse($this->dwoo); |
|
350 |
+ $fixCall->init(''); |
|
351 |
+ } |
|
352 |
+} |
|
353 |
+ |
| 354 |
354 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-require_once dirname(dirname(__FILE__)).'/DwooCompiler.php'; |
| 5 |
|
- |
| 6 |
|
-class CallTests extends PHPUnit_Framework_TestCase |
| 7 |
|
-{ |
| 8 |
|
- protected $compiler; |
| 9 |
|
- protected $dwoo; |
| 10 |
|
- |
| 11 |
|
- public function __construct() |
| 12 |
|
- { |
| 13 |
|
- // extend this class and override this in your constructor to test a modded compiler |
| 14 |
|
- $this->compiler = new DwooCompiler(); |
| 15 |
|
- $this->dwoo = new Dwoo(); |
| 16 |
|
- } |
| 17 |
|
- |
| 18 |
|
- public function testCustomFunctionPlugin() |
| 19 |
|
- { |
| 20 |
|
- $this->dwoo->addPlugin('test', 'plugin_custom_name'); |
| 21 |
|
- $tpl = new DwooTemplateString('{test "xxx"}'); |
| 22 |
|
- $tpl->forceCompilation(); |
| 23 |
|
- |
| 24 |
|
- $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 25 |
|
- $this->dwoo->removePlugin('test'); |
| 26 |
|
- } |
| 27 |
|
- |
| 28 |
|
- public function testHalfCustomClassPluginByClassMethodCallback() |
| 29 |
|
- { |
| 30 |
|
- $this->dwoo->addPlugin('test', array('plugin_half_custom', 'process')); |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+require_once 'Dwoo/Compiler.php'; |
|
5 |
+ |
|
6 |
+class CallTests extends PHPUnit_Framework_TestCase |
|
7 |
+{ |
|
8 |
+ protected $compiler; |
|
9 |
+ protected $dwoo; |
|
10 |
+ |
|
11 |
+ public function __construct() |
|
12 |
+ { |
|
13 |
+ // extend this class and override this in your constructor to test a modded compiler |
|
14 |
+ $this->compiler = new DwooCompiler(); |
|
15 |
+ $this->dwoo = new Dwoo(); |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ public function testCustomFunctionPlugin() |
|
19 |
+ { |
|
20 |
+ $this->dwoo->addPlugin('test', 'plugin_custom_name'); |
|
21 |
+ $tpl = new DwooTemplateString('{test "xxx"}'); |
|
22 |
+ $tpl->forceCompilation(); |
|
23 |
+ |
|
24 |
+ $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
25 |
+ $this->dwoo->removePlugin('test'); |
|
26 |
+ } |
|
27 |
+ |
|
28 |
+ public function testHalfCustomClassPluginByClassMethodCallback() |
|
29 |
+ { |
|
30 |
+ $this->dwoo->addPlugin('test', array('plugin_half_custom', 'process')); |
| 31 |
31 |
$tpl = new DwooTemplateString('{test "xxx"}'); |
| 32 |
32 |
$tpl->forceCompilation(); |
| 33 |
33 |
|
| 34 |
|
- $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
34 |
+ $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 35 |
35 |
$this->dwoo->removePlugin('test'); |
| 36 |
|
- } |
|
36 |
+ } |
| 37 |
37 |
|
| 38 |
|
- public function testFullCustomClassPluginByClassMethodCallback() |
| 39 |
|
- { |
| 40 |
|
- $this->dwoo->addPlugin('test', array('plugin_full_custom', 'process')); |
| 41 |
|
- $tpl = new DwooTemplateString('{test "xxx"}'); |
|
38 |
+ public function testFullCustomClassPluginByClassMethodCallback() |
|
39 |
+ { |
|
40 |
+ $this->dwoo->addPlugin('test', array('plugin_full_custom', 'process')); |
|
41 |
+ $tpl = new DwooTemplateString('{test "xxx"}'); |
| 42 |
42 |
$tpl->forceCompilation(); |
| 43 |
43 |
|
| 44 |
|
- $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
44 |
+ $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 45 |
45 |
$this->dwoo->removePlugin('test'); |
| 46 |
|
- } |
|
46 |
+ } |
| 47 |
47 |
|
| 48 |
|
- public function testCustomClassPluginByClassname() |
|
48 |
+ public function testCustomClassPluginByClassname() |
| 49 |
49 |
{ |
| 50 |
|
- $this->dwoo->addPlugin('test', 'plugin_full_custom'); |
| 51 |
|
- $tpl = new DwooTemplateString('{test "xxx"}'); |
|
50 |
+ $this->dwoo->addPlugin('test', 'plugin_full_custom'); |
|
51 |
+ $tpl = new DwooTemplateString('{test "xxx"}'); |
| 52 |
52 |
$tpl->forceCompilation(); |
| 53 |
53 |
|
| 54 |
|
- $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
54 |
+ $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 55 |
55 |
$this->dwoo->removePlugin('test'); |
| 56 |
|
- } |
|
56 |
+ } |
| 57 |
57 |
|
| 58 |
|
- public function testCustomObjectPluginByObjectMethodCallback() |
| 59 |
|
- { |
| 60 |
|
- $this->dwoo->addPlugin('test', array(new plugin_full_custom(), 'process')); |
| 61 |
|
- $tpl = new DwooTemplateString('{test "xxx"}'); |
|
58 |
+ public function testCustomObjectPluginByObjectMethodCallback() |
|
59 |
+ { |
|
60 |
+ $this->dwoo->addPlugin('test', array(new plugin_full_custom(), 'process')); |
|
61 |
+ $tpl = new DwooTemplateString('{test "xxx"}'); |
| 62 |
62 |
$tpl->forceCompilation(); |
| 63 |
63 |
|
| 64 |
|
- $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
64 |
+ $this->assertEquals('xxxbar', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 65 |
65 |
$this->dwoo->removePlugin('test'); |
| 66 |
|
- } |
|
66 |
+ } |
| 67 |
67 |
|
| 68 |
|
- public function testCustomBlockPluginByClassMethodCallback() |
| 69 |
|
- { |
| 70 |
|
- $this->dwoo->addPlugin('test', array('blockplugin_custom', 'process')); |
|
68 |
+ public function testCustomBlockPluginByClassMethodCallback() |
|
69 |
+ { |
|
70 |
+ $this->dwoo->addPlugin('test', array('blockplugin_custom', 'process')); |
| 71 |
71 |
$tpl = new DwooTemplateString('{test "xxx"}aaa{/test}'); |
| 72 |
72 |
$tpl->forceCompilation(); |
| 73 |
73 |
|
| 74 |
|
- $this->assertEquals('xxxbaraaa', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
74 |
+ $this->assertEquals('xxxbaraaa', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 75 |
75 |
$this->dwoo->removePlugin('test'); |
| 76 |
|
- } |
|
76 |
+ } |
| 77 |
77 |
|
| 78 |
|
- public function testCustomBlockPluginByClassname() |
| 79 |
|
- { |
| 80 |
|
- $this->dwoo->addPlugin('test', 'blockplugin_custom'); |
|
78 |
+ public function testCustomBlockPluginByClassname() |
|
79 |
+ { |
|
80 |
+ $this->dwoo->addPlugin('test', 'blockplugin_custom'); |
| 81 |
81 |
$tpl = new DwooTemplateString('{test "xxx"}aaa{/test}'); |
| 82 |
82 |
$tpl->forceCompilation(); |
| 83 |
83 |
|
| 84 |
|
- $this->assertEquals('xxxbaraaa', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
84 |
+ $this->assertEquals('xxxbaraaa', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 85 |
85 |
$this->dwoo->removePlugin('test'); |
| 86 |
|
- } |
| 87 |
|
- |
| 88 |
|
- /** |
| 89 |
|
- * @expectedException DwooException |
| 90 |
|
- */ |
| 91 |
|
- public function testCustomInvalidPlugin() |
| 92 |
|
- { |
| 93 |
|
- $this->dwoo->addPlugin('test', 'sdfmslkfmsle'); |
| 94 |
|
- } |
| 95 |
|
-} |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ /** |
|
89 |
+ * @expectedException DwooException |
|
90 |
+ */ |
|
91 |
+ public function testCustomInvalidPlugin() |
|
92 |
+ { |
|
93 |
+ $this->dwoo->addPlugin('test', 'sdfmslkfmsle'); |
|
94 |
+ } |
|
95 |
+} |
| 96 |
96 |
|
| 97 |
97 |
function plugin_custom_name(Dwoo $dwoo, $foo, $bar="bar") |
| 98 |
98 |
{ |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-require_once dirname(dirname(__FILE__)).'/DwooCompiler.php'; |
| 5 |
|
- |
| 6 |
|
-class CompilerTests extends PHPUnit_Framework_TestCase |
| 7 |
|
-{ |
| 8 |
|
- protected $compiler; |
| 9 |
|
- protected $dwoo; |
| 10 |
|
- |
| 11 |
|
- public function __construct() |
| 12 |
|
- { |
| 13 |
|
- // extend this class and override this in your constructor to test a modded compiler |
| 14 |
|
- $this->compiler = new DwooCompiler(); |
| 15 |
|
- $this->dwoo = new Dwoo(); |
| 16 |
|
- } |
| 17 |
|
- |
| 18 |
|
- public function testVarReplacement() |
| 19 |
|
- { |
| 20 |
|
- $tpl = new DwooTemplateString('{$foo}'); |
| 21 |
|
- $tpl->forceCompilation(); |
| 22 |
|
- |
| 23 |
|
- $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 24 |
|
- } |
| 25 |
|
- |
| 26 |
|
- public function testModifier() |
| 27 |
|
- { |
| 28 |
|
- $tpl = new DwooTemplateString('{$foo|upper}'); |
| 29 |
|
- $tpl->forceCompilation(); |
| 30 |
|
- |
| 31 |
|
- $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 32 |
|
- } |
| 33 |
|
- |
| 34 |
|
- public function testModifierArgs() |
| 35 |
|
- { |
| 36 |
|
- $tpl = new DwooTemplateString('{$foo|spacify:"-"|upper}'); |
| 37 |
|
- $tpl->forceCompilation(); |
| 38 |
|
- |
| 39 |
|
- $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 40 |
|
- } |
| 41 |
|
- |
| 42 |
|
- public function testModifierWithModifier() |
| 43 |
|
- { |
| 44 |
|
- $tpl = new DwooTemplateString('{$foo.0}{assign $foo|reverse foo}{$foo.0}{assign $foo|@reverse foo}{$foo.0}'); |
| 45 |
|
- $tpl->forceCompilation(); |
| 46 |
|
- |
| 47 |
|
- $this->assertEquals('barbazzab', $this->dwoo->get($tpl, array('foo'=>array('bar','baz')), $this->compiler)); |
| 48 |
|
- } |
| 49 |
|
- |
| 50 |
|
- public function testDwooFunc() |
| 51 |
|
- { |
| 52 |
|
- $tpl = new DwooTemplateString('{upper($foo)}'); |
| 53 |
|
- $tpl->forceCompilation(); |
| 54 |
|
- |
| 55 |
|
- $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 56 |
|
- } |
| 57 |
|
- |
| 58 |
|
- public function testDwooLoose() |
| 59 |
|
- { |
| 60 |
|
- $tpl = new DwooTemplateString('{upper $foo}'); |
| 61 |
|
- $tpl->forceCompilation(); |
| 62 |
|
- |
| 63 |
|
- $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 64 |
|
- } |
| 65 |
|
- |
| 66 |
|
- public function testNamedParameter() |
| 67 |
|
- { |
| 68 |
|
- $tpl = new DwooTemplateString('{upper value=$foo}'); |
| 69 |
|
- $tpl->forceCompilation(); |
| 70 |
|
- |
| 71 |
|
- $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 72 |
|
- } |
| 73 |
|
- |
| 74 |
|
- public function testNamedParameter2() |
| 75 |
|
- { |
| 76 |
|
- $tpl = new DwooTemplateString('{replace value=$foo search="BAR"|lower replace="BAR"}'); |
| 77 |
|
- $tpl->forceCompilation(); |
| 78 |
|
- |
| 79 |
|
- $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 80 |
|
- } |
| 81 |
|
- |
| 82 |
|
- public function testNamedParameter3() |
| 83 |
|
- { |
| 84 |
|
- $tpl = new DwooTemplateString('{assign value=reverse(array(foo=3,boo=5, 3=4), true) var=arr}{foreach $arr k v}{$k}{$v}{/foreach}'); |
| 85 |
|
- $tpl->forceCompilation(); |
| 86 |
|
- |
| 87 |
|
- $this->assertEquals('34boo5foo3', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 88 |
|
- } |
| 89 |
|
- |
| 90 |
|
- public function testRecursiveCall() |
| 91 |
|
- { |
| 92 |
|
- $tpl = new DwooTemplateString('{lower(reverse(upper($foo)))}'); |
| 93 |
|
- $tpl->forceCompilation(); |
| 94 |
|
- |
| 95 |
|
- $this->assertEquals('rab', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
| 96 |
|
- } |
| 97 |
|
- |
| 98 |
|
- public function testComplexRecursiveCall() |
| 99 |
|
- { |
| 100 |
|
- $tpl = new DwooTemplateString('{lower reverse($foo|reverse|upper)}'); |
| 101 |
|
- $tpl->forceCompilation(); |
| 102 |
|
- |
| 103 |
|
- $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'BaR'), $this->compiler)); |
| 104 |
|
- } |
| 105 |
|
- |
| 106 |
|
- public function testComplexRecursiveCall2() |
| 107 |
|
- { |
| 108 |
|
- $tpl = new DwooTemplateString('{str_repeat "AB`$foo|reverse|spacify:o`CD" 3}'); |
| 109 |
|
- $tpl->forceCompilation(); |
| 110 |
|
- $this->assertEquals('AB3o2o1CDAB3o2o1CDAB3o2o1CD', $this->dwoo->get($tpl, array('foo'=>'123'), $this->compiler)); |
| 111 |
|
- } |
| 112 |
|
- |
| 113 |
|
- public function testStrip() |
| 114 |
|
- { |
| 115 |
|
- $tpl = new DwooTemplateString("{strip}a\nb\nc{/strip}a\nb\nc"); |
| 116 |
|
- $tpl->forceCompilation(); |
| 117 |
|
- $this->assertEquals("abca\nb\nc", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 118 |
|
- } |
| 119 |
|
- |
| 120 |
|
- public function testWhitespace() |
| 121 |
|
- { |
| 122 |
|
- $tpl = new DwooTemplateString("{\$foo}{\$foo}\n{\$foo}\n\n{\$foo}\n\n\n{\$foo}"); |
| 123 |
|
- $tpl->forceCompilation(); |
| 124 |
|
- $this->assertEquals("aa\na\n\na\n\n\na", $this->dwoo->get($tpl, array('foo'=>'a'), $this->compiler)); |
| 125 |
|
- } |
| 126 |
|
- |
| 127 |
|
- public function testLiteral() |
| 128 |
|
- { |
| 129 |
|
- $tpl = new DwooTemplateString('{literal}{$foo}{hurray}{/literal}'); |
| 130 |
|
- $tpl->forceCompilation(); |
| 131 |
|
- $this->assertEquals('{$foo}{hurray}', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 132 |
|
- } |
| 133 |
|
- |
| 134 |
|
- public function testEscaping() |
| 135 |
|
- { |
| 136 |
|
- $tpl = new DwooTemplateString('\{foo\{bar\\\\{$var}}{"tes\}t"}{"foo\"lol\"bar"}'); |
| 137 |
|
- $tpl->forceCompilation(); |
| 138 |
|
- $this->assertEquals('{foo{bar\\1}tes}tfoo"lol"bar', $this->dwoo->get($tpl, array('var'=>1), $this->compiler)); |
| 139 |
|
- } |
| 140 |
|
- |
| 141 |
|
- public function testFunctions() |
| 142 |
|
- { |
| 143 |
|
- $tpl = new DwooTemplateString('{dump()}{dump( )}{dump}'); |
| 144 |
|
- $tpl->forceCompilation(); |
| 145 |
|
- $this->assertEquals('<div style="background:#aaa; padding:5px; margin:5px;">data (current scope): <div style="background:#ccc;"></div></div><div style="background:#aaa; padding:5px; margin:5px;">data (current scope): <div style="background:#ccc;"></div></div><div style="background:#aaa; padding:5px; margin:5px;">data (current scope): <div style="background:#ccc;"></div></div>', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 146 |
|
- } |
| 147 |
|
- |
| 148 |
|
- public function testExpressions() |
| 149 |
|
- { |
| 150 |
|
- $tpl = new DwooTemplateString('{$foo+5} {$foo+$foo} {$foo+3*$foo} {$foo*$foo+4*$foo} {$foo*2/2|number_format} {$foo*2/3|number_format:1} {number_format $foo*2/3 1} {if $foo+5>9 && $foo < 7 && $foo+$foo==$foo*2}win{/if} {$arr[$foo+3]}'); |
| 151 |
|
- $tpl->forceCompilation(); |
| 152 |
|
- |
| 153 |
|
- $this->assertEquals("10 10 40 145 5 3.3 3.3 win win", $this->dwoo->get($tpl, array('foo'=>5, 'arr'=>array(8=>'win')), $this->compiler)); |
| 154 |
|
- |
| 155 |
|
- $tpl = new DwooTemplateString('{"$foo/$foo"}'); |
| 156 |
|
- $tpl->forceCompilation(); |
| 157 |
|
- |
| 158 |
|
- $this->assertEquals("1", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler)); |
| 159 |
|
- } |
| 160 |
|
- |
| 161 |
|
- public function testNonExpressions() |
| 162 |
|
- { |
| 163 |
|
- $tpl = new DwooTemplateString('{"`$foo`/`$foo`"}'); |
| 164 |
|
- $tpl->forceCompilation(); |
| 165 |
|
- |
| 166 |
|
- $this->assertEquals("5/5", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler)); |
| 167 |
|
- } |
| 168 |
|
- |
| 169 |
|
- public function testConstants() |
| 170 |
|
- { |
| 171 |
|
- if(!defined('TEST')) |
| 172 |
|
- define('TEST', 'Test'); |
| 173 |
|
- $tpl = new DwooTemplateString('{$dwoo.const.TEST} {$dwoo.const.Dwoo::FUNC_PLUGIN*$dwoo.const.Dwoo::BLOCK_PLUGIN}'); |
| 174 |
|
- $tpl->forceCompilation(); |
| 175 |
|
- |
| 176 |
|
- $this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 177 |
|
- } |
| 178 |
|
- |
| 179 |
|
- public function testShortConstants() |
| 180 |
|
- { |
| 181 |
|
- if(!defined('TEST')) |
| 182 |
|
- define('TEST', 'Test'); |
| 183 |
|
- $tpl = new DwooTemplateString('{%TEST} {$dwoo.const.Dwoo::FUNC_PLUGIN*%Dwoo::BLOCK_PLUGIN}'); |
| 184 |
|
- $tpl->forceCompilation(); |
| 185 |
|
- |
| 186 |
|
- $this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 187 |
|
- } |
| 188 |
|
- |
| 189 |
|
- public function testAltDelimiters() |
| 190 |
|
- { |
| 191 |
|
- $tpl = new DwooTemplateString('{"test"} <%"test"%> <%"foo{lol}\%>"%>'); |
| 192 |
|
- $tpl->forceCompilation(); |
| 193 |
|
- $this->compiler->setDelimiters('<%', '%>'); |
| 194 |
|
- $this->assertEquals('{"test"} test foo{lol}%>', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 195 |
|
- |
| 196 |
|
- $tpl = new DwooTemplateString('d"O"b'); |
| 197 |
|
- $tpl->forceCompilation(); |
| 198 |
|
- $this->compiler->setDelimiters('d', 'b'); |
| 199 |
|
- $this->assertEquals('O', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 200 |
|
- |
| 201 |
|
- $tpl = new DwooTemplateString('<!-- "O" --> \<!-- '); |
| 202 |
|
- $tpl->forceCompilation(); |
| 203 |
|
- $this->compiler->setDelimiters('<!-- ', ' -->'); |
| 204 |
|
- $this->assertEquals('O <!-- ', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 205 |
|
- |
| 206 |
|
- $this->compiler->setDelimiters('{', '}'); |
| 207 |
|
- } |
| 208 |
|
- |
| 209 |
|
- public function testNumberedIndexes() |
| 210 |
|
- { |
| 211 |
|
- $tpl = new DwooTemplateString('{$100}-{$150}-{if $0}FAIL{/if}'); |
| 212 |
|
- $tpl->forceCompilation(); |
| 213 |
|
- |
| 214 |
|
- $this->assertEquals('bar-foo-', $this->dwoo->get($tpl, array('100'=>'bar', 150=>'foo'), $this->compiler)); |
| 215 |
|
- } |
| 216 |
|
- |
| 217 |
|
- public function testParseBool() |
| 218 |
|
- { |
| 219 |
|
- $tpl = new DwooTemplateString('{if (true === yes && true === on) && (false===off && false===no)}okay{/if}'); |
| 220 |
|
- $tpl->forceCompilation(); |
| 221 |
|
- |
| 222 |
|
- $this->assertEquals('okay', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 223 |
|
- } |
| 224 |
|
- |
| 225 |
|
- public function testMethodCalls() |
| 226 |
|
- { |
| 227 |
|
- $tpl = new DwooTemplateString('{$a} {$a->foo()} {$b[$c]->foo()} {$a->bar()+$a->bar()} {$a->baz(5, $foo)} {$a->make(5)->getInt()} {$a->make(5)->getInt()/2}'); |
| 228 |
|
- $tpl->forceCompilation(); |
| 229 |
|
- |
| 230 |
|
- $a = new MethodCallsHelper(); |
| 231 |
|
- $this->assertEquals('obj 0 1 7 10bar 5 2.5', $this->dwoo->get($tpl, array('a'=>$a, 'b'=>array('test'=>$a), 'c'=>'test', 'foo'=>'bar'), $this->compiler)); |
| 232 |
|
- } |
| 233 |
|
- |
| 234 |
|
- public function testLooseTagHandling() |
| 235 |
|
- { |
| 236 |
|
- $this->compiler->setLooseOpeningHandling(true); |
| 237 |
|
- $this->assertEquals($this->compiler->getLooseOpeningHandling(), true); |
| 238 |
|
- |
| 239 |
|
- $tpl = new DwooTemplateString('{ $a }{$a }{ $a}{$a}'); |
| 240 |
|
- $tpl->forceCompilation(); |
| 241 |
|
- |
| 242 |
|
- $this->assertEquals('moomoomoomoo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler)); |
| 243 |
|
- |
| 244 |
|
- $this->compiler->setLooseOpeningHandling(false); |
| 245 |
|
- $tpl = new DwooTemplateString('{ $a }{$a }{ $a}{$a}'); |
| 246 |
|
- $tpl->forceCompilation(); |
| 247 |
|
- |
| 248 |
|
- $this->assertEquals('{ $a }moo{ $a}moo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler)); |
| 249 |
|
- } |
| 250 |
|
- |
| 251 |
|
- public function testDwooDotShortcut() |
| 252 |
|
- { |
| 253 |
|
- $tpl = new DwooTemplateString('{$.server.SCRIPT_NAME}{foreach $a item}{$.foreach.default.iteration}{$item}{$.foreach.$b.$c}{/foreach}'); |
| 254 |
|
- $tpl->forceCompilation(); |
| 255 |
|
- |
| 256 |
|
- $this->assertEquals($_SERVER['SCRIPT_NAME'].'1a12b2', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default', 'c'=>'iteration'), $this->compiler)); |
| 257 |
|
- } |
| 258 |
|
- |
| 259 |
|
- public function testAssignAndIncrement() |
| 260 |
|
- { |
| 261 |
|
- $tpl = new DwooTemplateString('{$foo}{$foo+=3}{$foo} |
| 262 |
|
-{$foo}{$foo-=3}{$foo} |
| 263 |
|
-{$foo++}{$foo++}{$foo}{$foo*=$foo}{$foo} |
| 264 |
|
-{$foo--}{$foo=5}{$foo}'); |
| 265 |
|
- $tpl->forceCompilation(); |
| 266 |
|
- |
| 267 |
|
- $this->assertEquals("03\n30\n0124\n45", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler)); |
| 268 |
|
- |
| 269 |
|
- $tpl = new DwooTemplateString('{$foo="moo"}{$foo}{$foo=math("3+5")}{$foo}'); |
| 270 |
|
- $tpl->forceCompilation(); |
| 271 |
|
- |
| 272 |
|
- $this->assertEquals("moo8", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler)); |
| 273 |
|
- } |
| 274 |
|
- |
| 275 |
|
- public function testSetStringValToTrueWhenUsingNamedParams() |
| 276 |
|
- { |
| 277 |
|
- $this->dwoo->addPlugin('test', create_function('Dwoo $dwoo, $name, $bool=false', 'return $bool ? $name."!" : $name."?";')); |
| 278 |
|
- $tpl = new DwooTemplateString('{test name="Name"}{test name="Name" bool}'); |
| 279 |
|
- $tpl->forceCompilation(); |
| 280 |
|
- |
| 281 |
|
- $this->assertEquals("Name?Name!", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 282 |
|
- $this->dwoo->removePlugin('test'); |
| 283 |
|
- } |
| 284 |
|
-} |
| 285 |
|
- |
| 286 |
|
-class MethodCallsHelper { |
| 287 |
|
- public function __construct($int=0) { |
| 288 |
|
- $this->int = $int; |
| 289 |
|
- } |
| 290 |
|
- public function getInt() { |
| 291 |
|
- return $this->int; |
| 292 |
|
- } |
| 293 |
|
- public function make($a=0) { |
| 294 |
|
- return new self($a); |
| 295 |
|
- } |
| 296 |
|
- public function foo() { |
| 297 |
|
- static $a=0; |
| 298 |
|
- return $a++; |
| 299 |
|
- } |
| 300 |
|
- public function bar() { |
| 301 |
|
- static $a=3; |
| 302 |
|
- return $a++; |
| 303 |
|
- } |
| 304 |
|
- public function baz($int, $str) { |
| 305 |
|
- return ($int+5).$str; |
| 306 |
|
- } |
| 307 |
|
- public function __toString() { return 'obj'; } |
| 308 |
|
-} |
| 309 |
|
- |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+require_once 'Dwoo/Compiler.php'; |
|
5 |
+ |
|
6 |
+class CompilerTests extends PHPUnit_Framework_TestCase |
|
7 |
+{ |
|
8 |
+ protected $compiler; |
|
9 |
+ protected $dwoo; |
|
10 |
+ |
|
11 |
+ public function __construct() |
|
12 |
+ { |
|
13 |
+ // extend this class and override this in your constructor to test a modded compiler |
|
14 |
+ $this->compiler = new DwooCompiler(); |
|
15 |
+ $this->dwoo = new Dwoo(); |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ public function testVarReplacement() |
|
19 |
+ { |
|
20 |
+ $tpl = new DwooTemplateString('{$foo}'); |
|
21 |
+ $tpl->forceCompilation(); |
|
22 |
+ |
|
23 |
+ $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
24 |
+ } |
|
25 |
+ |
|
26 |
+ public function testModifier() |
|
27 |
+ { |
|
28 |
+ $tpl = new DwooTemplateString('{$foo|upper}'); |
|
29 |
+ $tpl->forceCompilation(); |
|
30 |
+ |
|
31 |
+ $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
32 |
+ } |
|
33 |
+ |
|
34 |
+ public function testModifierArgs() |
|
35 |
+ { |
|
36 |
+ $tpl = new DwooTemplateString('{$foo|spacify:"-"|upper}'); |
|
37 |
+ $tpl->forceCompilation(); |
|
38 |
+ |
|
39 |
+ $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ public function testModifierWithModifier() |
|
43 |
+ { |
|
44 |
+ $tpl = new DwooTemplateString('{$foo.0}{assign $foo|reverse foo}{$foo.0}{assign $foo|@reverse foo}{$foo.0}'); |
|
45 |
+ $tpl->forceCompilation(); |
|
46 |
+ |
|
47 |
+ $this->assertEquals('barbazzab', $this->dwoo->get($tpl, array('foo'=>array('bar','baz')), $this->compiler)); |
|
48 |
+ } |
|
49 |
+ |
|
50 |
+ public function testDwooFunc() |
|
51 |
+ { |
|
52 |
+ $tpl = new DwooTemplateString('{upper($foo)}'); |
|
53 |
+ $tpl->forceCompilation(); |
|
54 |
+ |
|
55 |
+ $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
56 |
+ } |
|
57 |
+ |
|
58 |
+ public function testDwooLoose() |
|
59 |
+ { |
|
60 |
+ $tpl = new DwooTemplateString('{upper $foo}'); |
|
61 |
+ $tpl->forceCompilation(); |
|
62 |
+ |
|
63 |
+ $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
64 |
+ } |
|
65 |
+ |
|
66 |
+ public function testNamedParameter() |
|
67 |
+ { |
|
68 |
+ $tpl = new DwooTemplateString('{upper value=$foo}'); |
|
69 |
+ $tpl->forceCompilation(); |
|
70 |
+ |
|
71 |
+ $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
72 |
+ } |
|
73 |
+ |
|
74 |
+ public function testNamedParameter2() |
|
75 |
+ { |
|
76 |
+ $tpl = new DwooTemplateString('{replace value=$foo search="BAR"|lower replace="BAR"}'); |
|
77 |
+ $tpl->forceCompilation(); |
|
78 |
+ |
|
79 |
+ $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
80 |
+ } |
|
81 |
+ |
|
82 |
+ public function testNamedParameter3() |
|
83 |
+ { |
|
84 |
+ $tpl = new DwooTemplateString('{assign value=reverse(array(foo=3,boo=5, 3=4), true) var=arr}{foreach $arr k v}{$k}{$v}{/foreach}'); |
|
85 |
+ $tpl->forceCompilation(); |
|
86 |
+ |
|
87 |
+ $this->assertEquals('34boo5foo3', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
88 |
+ } |
|
89 |
+ |
|
90 |
+ public function testRecursiveCall() |
|
91 |
+ { |
|
92 |
+ $tpl = new DwooTemplateString('{lower(reverse(upper($foo)))}'); |
|
93 |
+ $tpl->forceCompilation(); |
|
94 |
+ |
|
95 |
+ $this->assertEquals('rab', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); |
|
96 |
+ } |
|
97 |
+ |
|
98 |
+ public function testComplexRecursiveCall() |
|
99 |
+ { |
|
100 |
+ $tpl = new DwooTemplateString('{lower reverse($foo|reverse|upper)}'); |
|
101 |
+ $tpl->forceCompilation(); |
|
102 |
+ |
|
103 |
+ $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'BaR'), $this->compiler)); |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ public function testComplexRecursiveCall2() |
|
107 |
+ { |
|
108 |
+ $tpl = new DwooTemplateString('{str_repeat "AB`$foo|reverse|spacify:o`CD" 3}'); |
|
109 |
+ $tpl->forceCompilation(); |
|
110 |
+ $this->assertEquals('AB3o2o1CDAB3o2o1CDAB3o2o1CD', $this->dwoo->get($tpl, array('foo'=>'123'), $this->compiler)); |
|
111 |
+ } |
|
112 |
+ |
|
113 |
+ public function testStrip() |
|
114 |
+ { |
|
115 |
+ $tpl = new DwooTemplateString("{strip}a\nb\nc{/strip}a\nb\nc"); |
|
116 |
+ $tpl->forceCompilation(); |
|
117 |
+ $this->assertEquals("abca\nb\nc", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
118 |
+ } |
|
119 |
+ |
|
120 |
+ public function testWhitespace() |
|
121 |
+ { |
|
122 |
+ $tpl = new DwooTemplateString("{\$foo}{\$foo}\n{\$foo}\n\n{\$foo}\n\n\n{\$foo}"); |
|
123 |
+ $tpl->forceCompilation(); |
|
124 |
+ $this->assertEquals("aa\na\n\na\n\n\na", $this->dwoo->get($tpl, array('foo'=>'a'), $this->compiler)); |
|
125 |
+ } |
|
126 |
+ |
|
127 |
+ public function testLiteral() |
|
128 |
+ { |
|
129 |
+ $tpl = new DwooTemplateString('{literal}{$foo}{hurray}{/literal}'); |
|
130 |
+ $tpl->forceCompilation(); |
|
131 |
+ $this->assertEquals('{$foo}{hurray}', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
132 |
+ } |
|
133 |
+ |
|
134 |
+ public function testEscaping() |
|
135 |
+ { |
|
136 |
+ $tpl = new DwooTemplateString('\{foo\{bar\\\\{$var}}{"tes\}t"}{"foo\"lol\"bar"}'); |
|
137 |
+ $tpl->forceCompilation(); |
|
138 |
+ $this->assertEquals('{foo{bar\\1}tes}tfoo"lol"bar', $this->dwoo->get($tpl, array('var'=>1), $this->compiler)); |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ public function testFunctions() |
|
142 |
+ { |
|
143 |
+ $tpl = new DwooTemplateString('{dump()}{dump( )}{dump}'); |
|
144 |
+ $tpl->forceCompilation(); |
|
145 |
+ $this->assertEquals('<div style="background:#aaa; padding:5px; margin:5px;">data (current scope): <div style="background:#ccc;"></div></div><div style="background:#aaa; padding:5px; margin:5px;">data (current scope): <div style="background:#ccc;"></div></div><div style="background:#aaa; padding:5px; margin:5px;">data (current scope): <div style="background:#ccc;"></div></div>', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
146 |
+ } |
|
147 |
+ |
|
148 |
+ public function testExpressions() |
|
149 |
+ { |
|
150 |
+ $tpl = new DwooTemplateString('{$foo+5} {$foo+$foo} {$foo+3*$foo} {$foo*$foo+4*$foo} {$foo*2/2|number_format} {$foo*2/3|number_format:1} {number_format $foo*2/3 1} {if $foo+5>9 && $foo < 7 && $foo+$foo==$foo*2}win{/if} {$arr[$foo+3]}'); |
|
151 |
+ $tpl->forceCompilation(); |
|
152 |
+ |
|
153 |
+ $this->assertEquals("10 10 40 145 5 3.3 3.3 win win", $this->dwoo->get($tpl, array('foo'=>5, 'arr'=>array(8=>'win')), $this->compiler)); |
|
154 |
+ |
|
155 |
+ $tpl = new DwooTemplateString('{"$foo/$foo"}'); |
|
156 |
+ $tpl->forceCompilation(); |
|
157 |
+ |
|
158 |
+ $this->assertEquals("1", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler)); |
|
159 |
+ } |
|
160 |
+ |
|
161 |
+ public function testNonExpressions() |
|
162 |
+ { |
|
163 |
+ $tpl = new DwooTemplateString('{"`$foo`/`$foo`"}'); |
|
164 |
+ $tpl->forceCompilation(); |
|
165 |
+ |
|
166 |
+ $this->assertEquals("5/5", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler)); |
|
167 |
+ } |
|
168 |
+ |
|
169 |
+ public function testConstants() |
|
170 |
+ { |
|
171 |
+ if(!defined('TEST')) |
|
172 |
+ define('TEST', 'Test'); |
|
173 |
+ $tpl = new DwooTemplateString('{$dwoo.const.TEST} {$dwoo.const.Dwoo::FUNC_PLUGIN*$dwoo.const.Dwoo::BLOCK_PLUGIN}'); |
|
174 |
+ $tpl->forceCompilation(); |
|
175 |
+ |
|
176 |
+ $this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
177 |
+ } |
|
178 |
+ |
|
179 |
+ public function testShortConstants() |
|
180 |
+ { |
|
181 |
+ if(!defined('TEST')) |
|
182 |
+ define('TEST', 'Test'); |
|
183 |
+ $tpl = new DwooTemplateString('{%TEST} {$dwoo.const.Dwoo::FUNC_PLUGIN*%Dwoo::BLOCK_PLUGIN}'); |
|
184 |
+ $tpl->forceCompilation(); |
|
185 |
+ |
|
186 |
+ $this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
187 |
+ } |
|
188 |
+ |
|
189 |
+ public function testAltDelimiters() |
|
190 |
+ { |
|
191 |
+ $tpl = new DwooTemplateString('{"test"} <%"test"%> <%"foo{lol}\%>"%>'); |
|
192 |
+ $tpl->forceCompilation(); |
|
193 |
+ $this->compiler->setDelimiters('<%', '%>'); |
|
194 |
+ $this->assertEquals('{"test"} test foo{lol}%>', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
195 |
+ |
|
196 |
+ $tpl = new DwooTemplateString('d"O"b'); |
|
197 |
+ $tpl->forceCompilation(); |
|
198 |
+ $this->compiler->setDelimiters('d', 'b'); |
|
199 |
+ $this->assertEquals('O', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
200 |
+ |
|
201 |
+ $tpl = new DwooTemplateString('<!-- "O" --> \<!-- '); |
|
202 |
+ $tpl->forceCompilation(); |
|
203 |
+ $this->compiler->setDelimiters('<!-- ', ' -->'); |
|
204 |
+ $this->assertEquals('O <!-- ', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
205 |
+ |
|
206 |
+ $this->compiler->setDelimiters('{', '}'); |
|
207 |
+ } |
|
208 |
+ |
|
209 |
+ public function testNumberedIndexes() |
|
210 |
+ { |
|
211 |
+ $tpl = new DwooTemplateString('{$100}-{$150}-{if $0}FAIL{/if}'); |
|
212 |
+ $tpl->forceCompilation(); |
|
213 |
+ |
|
214 |
+ $this->assertEquals('bar-foo-', $this->dwoo->get($tpl, array('100'=>'bar', 150=>'foo'), $this->compiler)); |
|
215 |
+ } |
|
216 |
+ |
|
217 |
+ public function testParseBool() |
|
218 |
+ { |
|
219 |
+ $tpl = new DwooTemplateString('{if (true === yes && true === on) && (false===off && false===no)}okay{/if}'); |
|
220 |
+ $tpl->forceCompilation(); |
|
221 |
+ |
|
222 |
+ $this->assertEquals('okay', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
223 |
+ } |
|
224 |
+ |
|
225 |
+ public function testMethodCalls() |
|
226 |
+ { |
|
227 |
+ $tpl = new DwooTemplateString('{$a} {$a->foo()} {$b[$c]->foo()} {$a->bar()+$a->bar()} {$a->baz(5, $foo)} {$a->make(5)->getInt()} {$a->make(5)->getInt()/2}'); |
|
228 |
+ $tpl->forceCompilation(); |
|
229 |
+ |
|
230 |
+ $a = new MethodCallsHelper(); |
|
231 |
+ $this->assertEquals('obj 0 1 7 10bar 5 2.5', $this->dwoo->get($tpl, array('a'=>$a, 'b'=>array('test'=>$a), 'c'=>'test', 'foo'=>'bar'), $this->compiler)); |
|
232 |
+ } |
|
233 |
+ |
|
234 |
+ public function testLooseTagHandling() |
|
235 |
+ { |
|
236 |
+ $this->compiler->setLooseOpeningHandling(true); |
|
237 |
+ $this->assertEquals($this->compiler->getLooseOpeningHandling(), true); |
|
238 |
+ |
|
239 |
+ $tpl = new DwooTemplateString('{ $a }{$a }{ $a}{$a}'); |
|
240 |
+ $tpl->forceCompilation(); |
|
241 |
+ |
|
242 |
+ $this->assertEquals('moomoomoomoo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler)); |
|
243 |
+ |
|
244 |
+ $this->compiler->setLooseOpeningHandling(false); |
|
245 |
+ $tpl = new DwooTemplateString('{ $a }{$a }{ $a}{$a}'); |
|
246 |
+ $tpl->forceCompilation(); |
|
247 |
+ |
|
248 |
+ $this->assertEquals('{ $a }moo{ $a}moo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler)); |
|
249 |
+ } |
|
250 |
+ |
|
251 |
+ public function testDwooDotShortcut() |
|
252 |
+ { |
|
253 |
+ $tpl = new DwooTemplateString('{$.server.SCRIPT_NAME}{foreach $a item}{$.foreach.default.iteration}{$item}{$.foreach.$b.$c}{/foreach}'); |
|
254 |
+ $tpl->forceCompilation(); |
|
255 |
+ |
|
256 |
+ $this->assertEquals($_SERVER['SCRIPT_NAME'].'1a12b2', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default', 'c'=>'iteration'), $this->compiler)); |
|
257 |
+ } |
|
258 |
+ |
|
259 |
+ public function testAssignAndIncrement() |
|
260 |
+ { |
|
261 |
+ $tpl = new DwooTemplateString('{$foo}{$foo+=3}{$foo} |
|
262 |
+{$foo}{$foo-=3}{$foo} |
|
263 |
+{$foo++}{$foo++}{$foo}{$foo*=$foo}{$foo} |
|
264 |
+{$foo--}{$foo=5}{$foo}'); |
|
265 |
+ $tpl->forceCompilation(); |
|
266 |
+ |
|
267 |
+ $this->assertEquals("03\n30\n0124\n45", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler)); |
|
268 |
+ |
|
269 |
+ $tpl = new DwooTemplateString('{$foo="moo"}{$foo}{$foo=math("3+5")}{$foo}'); |
|
270 |
+ $tpl->forceCompilation(); |
|
271 |
+ |
|
272 |
+ $this->assertEquals("moo8", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler)); |
|
273 |
+ } |
|
274 |
+ |
|
275 |
+ public function testSetStringValToTrueWhenUsingNamedParams() |
|
276 |
+ { |
|
277 |
+ $this->dwoo->addPlugin('test', create_function('Dwoo $dwoo, $name, $bool=false', 'return $bool ? $name."!" : $name."?";')); |
|
278 |
+ $tpl = new DwooTemplateString('{test name="Name"}{test name="Name" bool}'); |
|
279 |
+ $tpl->forceCompilation(); |
|
280 |
+ |
|
281 |
+ $this->assertEquals("Name?Name!", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
282 |
+ $this->dwoo->removePlugin('test'); |
|
283 |
+ } |
|
284 |
+} |
|
285 |
+ |
|
286 |
+class MethodCallsHelper { |
|
287 |
+ public function __construct($int=0) { |
|
288 |
+ $this->int = $int; |
|
289 |
+ } |
|
290 |
+ public function getInt() { |
|
291 |
+ return $this->int; |
|
292 |
+ } |
|
293 |
+ public function make($a=0) { |
|
294 |
+ return new self($a); |
|
295 |
+ } |
|
296 |
+ public function foo() { |
|
297 |
+ static $a=0; |
|
298 |
+ return $a++; |
|
299 |
+ } |
|
300 |
+ public function bar() { |
|
301 |
+ static $a=3; |
|
302 |
+ return $a++; |
|
303 |
+ } |
|
304 |
+ public function baz($int, $str) { |
|
305 |
+ return ($int+5).$str; |
|
306 |
+ } |
|
307 |
+ public function __toString() { return 'obj'; } |
|
308 |
+} |
|
309 |
+ |
| 310 |
310 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-require_once dirname(dirname(__FILE__)).'/DwooCompiler.php'; |
| 5 |
|
- |
| 6 |
|
-class CoreTests extends PHPUnit_Framework_TestCase |
| 7 |
|
-{ |
| 8 |
|
- protected $compiler; |
| 9 |
|
- protected $dwoo; |
| 10 |
|
- |
| 11 |
|
- public function __construct() |
| 12 |
|
- { |
| 13 |
|
- $this->compiler = new DwooCompiler(); |
| 14 |
|
- $this->dwoo = new Dwoo(); |
| 15 |
|
- } |
| 16 |
|
- |
| 17 |
|
- public function testCoverConstructorsEtc() |
| 18 |
|
- { |
| 19 |
|
- // extend this class and override this in your constructor to test a modded compiler |
| 20 |
|
- $this->compiler = new DwooCompiler(); |
| 21 |
|
- $this->dwoo = new Dwoo(); |
| 22 |
|
- DwooLoader::rebuildClassPathCache(DWOO_DIRECTORY.'plugins', DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php'); |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+require_once 'Dwoo/Compiler.php'; |
|
5 |
+ |
|
6 |
+class CoreTests extends PHPUnit_Framework_TestCase |
|
7 |
+{ |
|
8 |
+ protected $compiler; |
|
9 |
+ protected $dwoo; |
|
10 |
+ |
|
11 |
+ public function __construct() |
|
12 |
+ { |
|
13 |
+ $this->compiler = new DwooCompiler(); |
|
14 |
+ $this->dwoo = new Dwoo(); |
|
15 |
+ } |
|
16 |
+ |
|
17 |
+ public function testCoverConstructorsEtc() |
|
18 |
+ { |
|
19 |
+ // extend this class and override this in your constructor to test a modded compiler |
|
20 |
+ $this->compiler = new DwooCompiler(); |
|
21 |
+ $this->dwoo = new Dwoo(); |
|
22 |
+ DwooLoader::rebuildClassPathCache(DWOO_DIRECTORY.'plugins', DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php'); |
| 23 |
23 |
|
| 24 |
24 |
// fixes the init call not being called (which is normal) |
| 25 |
|
- $fixCall = new DwooPlugin_topLevelBlock($this->dwoo); |
| 26 |
|
- $fixCall->init(''); |
| 27 |
|
- } |
| 28 |
|
- |
| 29 |
|
- public function testReadVar() |
| 30 |
|
- { |
| 31 |
|
- $tpl = new DwooTemplateString('{$foo.$bar[$baz->qux][moo]}{with $foo}{$a.b.moo}{/with}{$baz->qux}'); |
| 32 |
|
- $tpl->forceCompilation(); |
| 33 |
|
- |
| 34 |
|
- $Obj = new stdClass; |
| 35 |
|
- $Obj->qux = 'b'; |
| 36 |
|
- $data = array( |
| 37 |
|
- 'foo'=>array('a' => array('b'=>array('moo'=>'Yay!'))), |
| 38 |
|
- 'bar'=>'a', |
| 39 |
|
- 'baz'=>$Obj, |
| 40 |
|
- ); |
| 41 |
|
- $this->assertEquals("Yay!Yay!b", $this->dwoo->get($tpl, $data, $this->compiler)); |
| 42 |
|
- |
| 43 |
|
- $this->assertEquals('Yay!aaab', $this->dwoo->readVar('foo.a.b.moo') . $this->dwoo->readVar('bar') . $this->dwoo->readVar('_root.bar') . $this->dwoo->readVar('_parent.bar') . $this->dwoo->readVar('baz->qux')); |
| 44 |
|
- $this->assertEquals($data, $this->dwoo->readVar('_root')); |
| 45 |
|
- $this->assertEquals($data, $this->dwoo->readVar('_parent')); |
| 46 |
|
- |
| 47 |
|
- } |
| 48 |
|
- |
| 49 |
|
- public function testReadParentVar() |
| 50 |
|
- { |
| 51 |
|
- $tpl = new DwooTemplateString('{assign "Yay!" a.b->qux}{$a.b->qux}'); |
| 52 |
|
- $tpl->forceCompilation(); |
| 53 |
|
- |
| 54 |
|
- $this->assertEquals("Yay!", $this->dwoo->get($tpl, array('bar'=>'a'), $this->compiler)); |
| 55 |
|
- |
| 56 |
|
- $this->assertEquals('a', $this->dwoo->readParentVar(2, 'bar')); |
| 57 |
|
- } |
| 58 |
|
- |
| 59 |
|
- public function testDwooOutput() |
| 60 |
|
- { |
| 61 |
|
- $tpl = new DwooTemplateString('a'); |
|
25 |
+ $fixCall = new DwooPlugin_topLevelBlock($this->dwoo); |
|
26 |
+ $fixCall->init(''); |
|
27 |
+ } |
|
28 |
+ |
|
29 |
+ public function testReadVar() |
|
30 |
+ { |
|
31 |
+ $tpl = new DwooTemplateString('{$foo.$bar[$baz->qux][moo]}{with $foo}{$a.b.moo}{/with}{$baz->qux}'); |
|
32 |
+ $tpl->forceCompilation(); |
|
33 |
+ |
|
34 |
+ $Obj = new stdClass; |
|
35 |
+ $Obj->qux = 'b'; |
|
36 |
+ $data = array( |
|
37 |
+ 'foo'=>array('a' => array('b'=>array('moo'=>'Yay!'))), |
|
38 |
+ 'bar'=>'a', |
|
39 |
+ 'baz'=>$Obj, |
|
40 |
+ ); |
|
41 |
+ $this->assertEquals("Yay!Yay!b", $this->dwoo->get($tpl, $data, $this->compiler)); |
|
42 |
+ |
|
43 |
+ $this->assertEquals('Yay!aaab', $this->dwoo->readVar('foo.a.b.moo') . $this->dwoo->readVar('bar') . $this->dwoo->readVar('_root.bar') . $this->dwoo->readVar('_parent.bar') . $this->dwoo->readVar('baz->qux')); |
|
44 |
+ $this->assertEquals($data, $this->dwoo->readVar('_root')); |
|
45 |
+ $this->assertEquals($data, $this->dwoo->readVar('_parent')); |
|
46 |
+ |
|
47 |
+ } |
|
48 |
+ |
|
49 |
+ public function testReadParentVar() |
|
50 |
+ { |
|
51 |
+ $tpl = new DwooTemplateString('{assign "Yay!" a.b->qux}{$a.b->qux}'); |
|
52 |
+ $tpl->forceCompilation(); |
|
53 |
+ |
|
54 |
+ $this->assertEquals("Yay!", $this->dwoo->get($tpl, array('bar'=>'a'), $this->compiler)); |
|
55 |
+ |
|
56 |
+ $this->assertEquals('a', $this->dwoo->readParentVar(2, 'bar')); |
|
57 |
+ } |
|
58 |
+ |
|
59 |
+ public function testDwooOutput() |
|
60 |
+ { |
|
61 |
+ $tpl = new DwooTemplateString('a'); |
| 62 |
62 |
$tpl->forceCompilation(); |
| 63 |
63 |
|
| 64 |
64 |
ob_start(); |
| 65 |
65 |
$this->dwoo->output($tpl, array()); |
| 66 |
66 |
$output = ob_get_clean(); |
| 67 |
67 |
$this->assertEquals('a', $output); |
| 68 |
|
- } |
|
68 |
+ } |
| 69 |
69 |
|
| 70 |
|
- /** |
| 71 |
|
- * @expectedException DwooException |
| 72 |
|
- */ |
| 73 |
|
- public function testDwooGetNonTemplate() |
| 74 |
|
- { |
|
70 |
+ /** |
|
71 |
+ * @expectedException DwooException |
|
72 |
+ */ |
|
73 |
+ public function testDwooGetNonTemplate() |
|
74 |
+ { |
| 75 |
75 |
$this->dwoo->output(null, array()); |
| 76 |
|
- } |
| 77 |
|
- |
| 78 |
|
- /** |
| 79 |
|
- * @expectedException DwooException |
| 80 |
|
- */ |
| 81 |
|
- public function testDwooGetNonData() |
| 82 |
|
- { |
| 83 |
|
- $tpl = new DwooTemplateString('a'); |
| 84 |
|
- $this->dwoo->get($tpl, null); |
| 85 |
|
- } |
| 86 |
|
- |
| 87 |
|
- public function testGetSetSecurityPolicy() |
| 88 |
|
- { |
| 89 |
|
- $dwoo = new Dwoo(); |
| 90 |
|
- $policy = new DwooSecurityPolicy(); |
| 91 |
|
- $policy->setConstantHandling(DwooSecurityPolicy::CONST_ALLOW); |
| 92 |
|
- $dwoo->setSecurityPolicy($policy); |
| 93 |
|
- $this->assertEquals($policy, $dwoo->getSecurityPolicy()); |
| 94 |
|
- $this->assertEquals($policy->getConstantHandling(), $dwoo->getSecurityPolicy()->getConstantHandling()); |
| 95 |
|
- } |
| 96 |
|
- |
| 97 |
|
- /** |
| 98 |
|
- * @expectedException DwooException |
| 99 |
|
- */ |
| 100 |
|
- public function testWrongResourceName() |
| 101 |
|
- { |
| 102 |
|
- $this->dwoo->getTemplate('sdmlb', 'fookm'); |
| 103 |
|
- } |
| 104 |
|
- |
| 105 |
|
- public function testIsCached() |
| 106 |
|
- { |
| 107 |
|
- $tpl = new DwooTemplateString('foo'); |
| 108 |
|
- $this->assertEquals(false, $this->dwoo->isCached($tpl)); |
| 109 |
|
- } |
| 110 |
|
- |
| 111 |
|
- public function testClearCache() |
| 112 |
|
- { |
| 113 |
|
- $cacheDir = $this->dwoo->getCacheDir(); |
| 114 |
|
- $this->dwoo->clearCache(); |
| 115 |
|
- file_put_contents($cacheDir.DIRECTORY_SEPARATOR.'junk.html', 'test'); |
| 116 |
|
- |
| 117 |
|
- $this->assertEquals(1, $this->dwoo->clearCache()); |
| 118 |
|
- } |
| 119 |
|
- |
| 120 |
|
- public function testDwooGetFilename() |
| 121 |
|
- { |
|
76 |
+ } |
|
77 |
+ |
|
78 |
+ /** |
|
79 |
+ * @expectedException DwooException |
|
80 |
+ */ |
|
81 |
+ public function testDwooGetNonData() |
|
82 |
+ { |
|
83 |
+ $tpl = new DwooTemplateString('a'); |
|
84 |
+ $this->dwoo->get($tpl, null); |
|
85 |
+ } |
|
86 |
+ |
|
87 |
+ public function testGetSetSecurityPolicy() |
|
88 |
+ { |
|
89 |
+ $dwoo = new Dwoo(); |
|
90 |
+ $policy = new DwooSecurityPolicy(); |
|
91 |
+ $policy->setConstantHandling(DwooSecurityPolicy::CONST_ALLOW); |
|
92 |
+ $dwoo->setSecurityPolicy($policy); |
|
93 |
+ $this->assertEquals($policy, $dwoo->getSecurityPolicy()); |
|
94 |
+ $this->assertEquals($policy->getConstantHandling(), $dwoo->getSecurityPolicy()->getConstantHandling()); |
|
95 |
+ } |
|
96 |
+ |
|
97 |
+ /** |
|
98 |
+ * @expectedException DwooException |
|
99 |
+ */ |
|
100 |
+ public function testWrongResourceName() |
|
101 |
+ { |
|
102 |
+ $this->dwoo->getTemplate('sdmlb', 'fookm'); |
|
103 |
+ } |
|
104 |
+ |
|
105 |
+ public function testIsCached() |
|
106 |
+ { |
|
107 |
+ $tpl = new DwooTemplateString('foo'); |
|
108 |
+ $this->assertEquals(false, $this->dwoo->isCached($tpl)); |
|
109 |
+ } |
|
110 |
+ |
|
111 |
+ public function testClearCache() |
|
112 |
+ { |
|
113 |
+ $cacheDir = $this->dwoo->getCacheDir(); |
|
114 |
+ $this->dwoo->clearCache(); |
|
115 |
+ file_put_contents($cacheDir.DIRECTORY_SEPARATOR.'junk.html', 'test'); |
|
116 |
+ |
|
117 |
+ $this->assertEquals(1, $this->dwoo->clearCache()); |
|
118 |
+ } |
|
119 |
+ |
|
120 |
+ public function testDwooGetFilename() |
|
121 |
+ { |
| 122 |
122 |
$this->assertEquals('44BAR', $this->dwoo->get(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'test.html', array('foo'=>44, 'bar'=>'BAR'))); |
| 123 |
|
- } |
| 124 |
|
- |
| 125 |
|
- public function testAssignVarInScope() |
| 126 |
|
- { |
| 127 |
|
- $tpl = new DwooTemplateString('{assign "Yay!" a.b->qux}{$a.b->qux}'); |
| 128 |
|
- $tpl->forceCompilation(); |
| 129 |
|
- |
| 130 |
|
- $Obj = new stdClass; |
| 131 |
|
- $Obj->qux = 'Noes:('; |
| 132 |
|
- |
| 133 |
|
- $this->assertEquals("Yay!", $this->dwoo->get($tpl, array('a'=>array('b'=>$Obj)), $this->compiler)); |
| 134 |
|
- |
| 135 |
|
- $tpl = new DwooTemplateString('{assign "Yay!" a->b.qux}{$a->b.qux}'); |
| 136 |
|
- $tpl->forceCompilation(); |
| 137 |
|
- |
| 138 |
|
- $Obj = new stdClass; |
| 139 |
|
- $Obj->b = array('qux'=>'Noes:('); |
| 140 |
|
- |
| 141 |
|
- $this->assertEquals("Yay!", $this->dwoo->get($tpl, array('a'=>$Obj), $this->compiler)); |
| 142 |
|
- } |
| 143 |
|
- |
| 144 |
|
- public function testPhpCall() |
| 145 |
|
- { |
| 146 |
|
- $tpl = new DwooTemplateString('{"foo"|strtoupper}'); |
| 147 |
|
- $tpl->forceCompilation(); |
| 148 |
|
- |
| 149 |
|
- $this->assertEquals("FOO", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 150 |
|
- |
| 151 |
|
- $tpl = new DwooTemplateString('{foreach $foo|@count subitems}{$subitems}{/foreach}'); |
| 152 |
|
- $tpl->forceCompilation(); |
| 153 |
|
- |
| 154 |
|
- $this->assertEquals("21", $this->dwoo->get($tpl, array('foo'=>array('a'=>array(1,2), 'b'=>array(2))), $this->compiler)); |
| 155 |
|
- } |
| 156 |
|
- |
| 157 |
|
- public function testClassCall() |
| 158 |
|
- { |
| 159 |
|
- $tpl = new DwooTemplateString('{dump $foo.b.0}'); |
| 160 |
|
- $tpl->forceCompilation(); |
| 161 |
|
- |
| 162 |
|
- $this->assertEquals("2<br />", $this->dwoo->get($tpl, array('foo'=>array('a'=>array(1,2), 'b'=>array(2))), $this->compiler)); |
| 163 |
|
- } |
| 164 |
|
- |
| 165 |
|
- public function testSuperGlobals() |
| 166 |
|
- { |
| 167 |
|
- $_GET[5] = 'Yay'; |
| 168 |
|
- $tpl = new DwooTemplateString('{$dwoo.get.5} {$dwoo.get.$foo}'); |
| 169 |
|
- $tpl->forceCompilation(); |
| 170 |
|
- |
| 171 |
|
- $this->assertEquals("Yay Yay", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler)); |
| 172 |
|
- } |
| 173 |
|
- |
| 174 |
|
- public function testGettersSetters() |
| 175 |
|
- { |
| 176 |
|
- $this->dwoo->setCacheTime(5); |
| 177 |
|
- $this->assertEquals(5, $this->dwoo->getCacheTime()); |
| 178 |
|
- $this->dwoo->setCacheTime(0); |
| 179 |
|
- |
| 180 |
|
- $this->dwoo->setCharset('foo'); |
| 181 |
|
- $this->assertEquals('foo', $this->dwoo->getCharset()); |
| 182 |
|
- $this->dwoo->setCharset('utf-8'); |
| 183 |
|
- |
| 184 |
|
- $this->dwoo->setDefaultCompilerFactory('file', 'Moo'); |
| 185 |
|
- $this->assertEquals('Moo', $this->dwoo->getDefaultCompilerFactory('file')); |
| 186 |
|
- $this->dwoo->setDefaultCompilerFactory('file', array('DwooCompiler', 'compilerFactory')); |
| 187 |
|
- |
| 188 |
|
- } |
| 189 |
|
- |
| 190 |
|
- public function testAddAndRemoveResource() |
| 191 |
|
- { |
| 192 |
|
- $this->dwoo->addResource('news', 'DwooTemplateFile', array('DwooCompiler', 'compilerFactory')); |
| 193 |
|
- $tpl = new DwooTemplateString('{include file="news:'.DWOO_DIRECTORY.'tests/resources/test.html" foo=3 bar=4}'); |
| 194 |
|
- $tpl->forceCompilation(); |
| 195 |
|
- $this->assertEquals("34", $this->dwoo->get($tpl, array())); |
| 196 |
|
- $this->dwoo->removeResource('news'); |
| 197 |
|
- |
| 198 |
|
- $this->dwoo->addResource('file', 'DwooTemplateString', 'Fake'); |
| 199 |
|
- $this->dwoo->removeResource('file'); |
| 200 |
|
- $tpl = new DwooTemplateString('{include file="file:'.DWOO_DIRECTORY.'tests/resources/test.html" foo=3 bar=4}'); |
| 201 |
|
- $tpl->forceCompilation(); |
| 202 |
|
- $this->assertEquals("34", $this->dwoo->get($tpl, array())); |
| 203 |
|
- } |
| 204 |
|
- |
| 205 |
|
- public function testTemplateFile() |
| 206 |
|
- { |
| 207 |
|
- $tpl = new DwooTemplateFile(DWOO_DIRECTORY.'tests/resources/test.html'); |
| 208 |
|
- $tpl->forceCompilation(); |
| 209 |
|
- |
| 210 |
|
- $this->assertEquals("12", $this->dwoo->get($tpl, array('foo'=>1, 'bar'=>2))); |
| 211 |
|
- } |
| 212 |
|
- |
| 213 |
|
- public function testTemplateString() |
| 214 |
|
- { |
| 215 |
|
- $tpl = new DwooTemplateString('foo', 13); |
| 216 |
|
- |
| 217 |
|
- $this->assertEquals("13", $tpl->getCacheTime()); |
| 218 |
|
- $this->assertEquals(null, $tpl->getCompiler()); |
| 219 |
|
- $this->assertEquals(false, DwooTemplateString::templateFactory($this->dwoo, 'foo', 5)); |
| 220 |
|
- } |
|
123 |
+ } |
|
124 |
+ |
|
125 |
+ public function testAssignVarInScope() |
|
126 |
+ { |
|
127 |
+ $tpl = new DwooTemplateString('{assign "Yay!" a.b->qux}{$a.b->qux}'); |
|
128 |
+ $tpl->forceCompilation(); |
|
129 |
+ |
|
130 |
+ $Obj = new stdClass; |
|
131 |
+ $Obj->qux = 'Noes:('; |
|
132 |
+ |
|
133 |
+ $this->assertEquals("Yay!", $this->dwoo->get($tpl, array('a'=>array('b'=>$Obj)), $this->compiler)); |
|
134 |
+ |
|
135 |
+ $tpl = new DwooTemplateString('{assign "Yay!" a->b.qux}{$a->b.qux}'); |
|
136 |
+ $tpl->forceCompilation(); |
|
137 |
+ |
|
138 |
+ $Obj = new stdClass; |
|
139 |
+ $Obj->b = array('qux'=>'Noes:('); |
|
140 |
+ |
|
141 |
+ $this->assertEquals("Yay!", $this->dwoo->get($tpl, array('a'=>$Obj), $this->compiler)); |
|
142 |
+ } |
|
143 |
+ |
|
144 |
+ public function testPhpCall() |
|
145 |
+ { |
|
146 |
+ $tpl = new DwooTemplateString('{"foo"|strtoupper}'); |
|
147 |
+ $tpl->forceCompilation(); |
|
148 |
+ |
|
149 |
+ $this->assertEquals("FOO", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
150 |
+ |
|
151 |
+ $tpl = new DwooTemplateString('{foreach $foo|@count subitems}{$subitems}{/foreach}'); |
|
152 |
+ $tpl->forceCompilation(); |
|
153 |
+ |
|
154 |
+ $this->assertEquals("21", $this->dwoo->get($tpl, array('foo'=>array('a'=>array(1,2), 'b'=>array(2))), $this->compiler)); |
|
155 |
+ } |
|
156 |
+ |
|
157 |
+ public function testClassCall() |
|
158 |
+ { |
|
159 |
+ $tpl = new DwooTemplateString('{dump $foo.b.0}'); |
|
160 |
+ $tpl->forceCompilation(); |
|
161 |
+ |
|
162 |
+ $this->assertEquals("2<br />", $this->dwoo->get($tpl, array('foo'=>array('a'=>array(1,2), 'b'=>array(2))), $this->compiler)); |
|
163 |
+ } |
|
164 |
+ |
|
165 |
+ public function testSuperGlobals() |
|
166 |
+ { |
|
167 |
+ $_GET[5] = 'Yay'; |
|
168 |
+ $tpl = new DwooTemplateString('{$dwoo.get.5} {$dwoo.get.$foo}'); |
|
169 |
+ $tpl->forceCompilation(); |
|
170 |
+ |
|
171 |
+ $this->assertEquals("Yay Yay", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler)); |
|
172 |
+ } |
|
173 |
+ |
|
174 |
+ public function testGettersSetters() |
|
175 |
+ { |
|
176 |
+ $this->dwoo->setCacheTime(5); |
|
177 |
+ $this->assertEquals(5, $this->dwoo->getCacheTime()); |
|
178 |
+ $this->dwoo->setCacheTime(0); |
|
179 |
+ |
|
180 |
+ $this->dwoo->setCharset('foo'); |
|
181 |
+ $this->assertEquals('foo', $this->dwoo->getCharset()); |
|
182 |
+ $this->dwoo->setCharset('utf-8'); |
|
183 |
+ |
|
184 |
+ $this->dwoo->setDefaultCompilerFactory('file', 'Moo'); |
|
185 |
+ $this->assertEquals('Moo', $this->dwoo->getDefaultCompilerFactory('file')); |
|
186 |
+ $this->dwoo->setDefaultCompilerFactory('file', array('DwooCompiler', 'compilerFactory')); |
|
187 |
+ |
|
188 |
+ } |
|
189 |
+ |
|
190 |
+ public function testAddAndRemoveResource() |
|
191 |
+ { |
|
192 |
+ $this->dwoo->addResource('news', 'DwooTemplateFile', array('DwooCompiler', 'compilerFactory')); |
|
193 |
+ $tpl = new DwooTemplateString('{include file="news:'.DWOO_DIRECTORY.'tests/resources/test.html" foo=3 bar=4}'); |
|
194 |
+ $tpl->forceCompilation(); |
|
195 |
+ $this->assertEquals("34", $this->dwoo->get($tpl, array())); |
|
196 |
+ $this->dwoo->removeResource('news'); |
|
197 |
+ |
|
198 |
+ $this->dwoo->addResource('file', 'DwooTemplateString', 'Fake'); |
|
199 |
+ $this->dwoo->removeResource('file'); |
|
200 |
+ $tpl = new DwooTemplateString('{include file="file:'.DWOO_DIRECTORY.'tests/resources/test.html" foo=3 bar=4}'); |
|
201 |
+ $tpl->forceCompilation(); |
|
202 |
+ $this->assertEquals("34", $this->dwoo->get($tpl, array())); |
|
203 |
+ } |
|
204 |
+ |
|
205 |
+ public function testTemplateFile() |
|
206 |
+ { |
|
207 |
+ $tpl = new DwooTemplateFile(DWOO_DIRECTORY.'tests/resources/test.html'); |
|
208 |
+ $tpl->forceCompilation(); |
|
209 |
+ |
|
210 |
+ $this->assertEquals("12", $this->dwoo->get($tpl, array('foo'=>1, 'bar'=>2))); |
|
211 |
+ } |
|
212 |
+ |
|
213 |
+ public function testTemplateString() |
|
214 |
+ { |
|
215 |
+ $tpl = new DwooTemplateString('foo', 13); |
|
216 |
+ |
|
217 |
+ $this->assertEquals("13", $tpl->getCacheTime()); |
|
218 |
+ $this->assertEquals(null, $tpl->getCompiler()); |
|
219 |
+ $this->assertEquals(false, DwooTemplateString::templateFactory($this->dwoo, 'foo', 5)); |
|
220 |
+ } |
| 221 |
221 |
|
| 222 |
222 |
public function testCachedTemplateAndClearCache() |
| 223 |
223 |
{ |
| 224 |
|
- $tpl = new DwooTemplateString('foo{$foo}', 10, 'cachetest'); |
|
224 |
+ $tpl = new DwooTemplateString('foo{$foo}', 10, 'cachetest'); |
| 225 |
225 |
$tpl->forceCompilation(); |
| 226 |
226 |
|
| 227 |
227 |
$this->assertEquals("foo1", $this->dwoo->get($tpl, array('foo'=>1))); |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-require_once dirname(dirname(__FILE__)).'/DwooCompiler.php'; |
| 5 |
|
- |
| 6 |
|
-class FuncTests extends PHPUnit_Framework_TestCase |
| 7 |
|
-{ |
| 8 |
|
- protected $compiler; |
| 9 |
|
- protected $dwoo; |
| 10 |
|
- |
| 11 |
|
- public function __construct() |
| 12 |
|
- { |
| 13 |
|
- // extend this class and override this in your constructor to test a modded compiler |
| 14 |
|
- $this->compiler = new DwooCompiler(); |
| 15 |
|
- $this->dwoo = new Dwoo(); |
| 16 |
|
- } |
| 17 |
|
- |
| 18 |
|
- public function testAssign() |
| 19 |
|
- { |
| 20 |
|
- // test simple assign |
| 21 |
|
- $tpl = new DwooTemplateString('{assign "bar" foo}{$foo}'); |
| 22 |
|
- $tpl->forceCompilation(); |
| 23 |
|
- |
| 24 |
|
- $tpl2 = new DwooTemplateString('{assign baz foo}{$foo}'); |
| 25 |
|
- $tpl2->forceCompilation(); |
| 26 |
|
- |
| 27 |
|
- $this->assertEquals('barbaz', $this->dwoo->get($tpl, array(), $this->compiler) . $this->dwoo->get($tpl2, array(), $this->compiler)); |
| 28 |
|
- |
| 29 |
|
- // test array assignation with function call |
| 30 |
|
- $tpl = new DwooTemplateString('{assign reverse($foo) foo}{foreach $foo val}{$val}{/foreach}'); |
| 31 |
|
- $tpl->forceCompilation(); |
| 32 |
|
- |
| 33 |
|
- $this->assertEquals('321', $this->dwoo->get($tpl, array('foo'=>array(1,2,3)), $this->compiler)); |
| 34 |
|
- } |
| 35 |
|
- |
| 36 |
|
- public function testCapitalize() |
| 37 |
|
- { |
| 38 |
|
- $tpl = new DwooTemplateString('{capitalize "hello world 1st"}-{capitalize "hello world 1st" true}'); |
| 39 |
|
- $tpl->forceCompilation(); |
| 40 |
|
- |
| 41 |
|
- $this->assertEquals('Hello World 1st-Hello World 1St', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 42 |
|
- } |
| 43 |
|
- |
| 44 |
|
- public function testCat() |
| 45 |
|
- { |
| 46 |
|
- $tpl = new DwooTemplateString('{cat 3 bar "FOO"}'); |
| 47 |
|
- $tpl->forceCompilation(); |
| 48 |
|
- |
| 49 |
|
- $this->assertEquals('3barFOO', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 50 |
|
- } |
| 51 |
|
- |
| 52 |
|
- public function testCounter() |
| 53 |
|
- { |
| 54 |
|
- $tpl = new DwooTemplateString('{counter start=0 skip=2}{counter}{counter}{counter}'); |
| 55 |
|
- $tpl->forceCompilation(); |
| 56 |
|
- |
| 57 |
|
- $this->assertEquals('0246', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 58 |
|
- |
| 59 |
|
- $tpl = new DwooTemplateString('{counter start=0 skip=2 assign=foo}{counter}{counter}{counter}{$foo}'); |
| 60 |
|
- $tpl->forceCompilation(); |
| 61 |
|
- |
| 62 |
|
- $this->assertEquals('6', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 63 |
|
- |
| 64 |
|
- $tpl = new DwooTemplateString('{counter start=0 skip=2 assign=foo direction=down}{counter}{counter}{counter}{$foo}'); |
| 65 |
|
- $tpl->forceCompilation(); |
| 66 |
|
- |
| 67 |
|
- $this->assertEquals('-6', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 68 |
|
- |
| 69 |
|
- $tpl = new DwooTemplateString('{counter start=0 skip=3 print=false}{counter}{counter}{counter}{counter print=true}'); |
| 70 |
|
- $tpl->forceCompilation(); |
| 71 |
|
- |
| 72 |
|
- $this->assertEquals('12', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 73 |
|
- } |
| 74 |
|
- |
| 75 |
|
- public function testCountChars() |
| 76 |
|
- { |
| 77 |
|
- $tpl = new DwooTemplateString('{count_characters "hello world"}{count_characters "hello world" true}'); |
| 78 |
|
- $tpl->forceCompilation(); |
| 79 |
|
- |
| 80 |
|
- $this->assertEquals('1011', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 81 |
|
- } |
| 82 |
|
- |
| 83 |
|
- public function testCountParagraphs() |
| 84 |
|
- { |
| 85 |
|
- $tpl = new DwooTemplateString('{count_paragraphs $foo}'); |
| 86 |
|
- $tpl->forceCompilation(); |
| 87 |
|
- |
| 88 |
|
- $this->assertEquals('4', $this->dwoo->get($tpl, array('foo'=>"amdslfk smdkmsfd\nmsldkfmsldsdfml\nmlskfmlsdksdf\nmklsdspo"), $this->compiler)); |
| 89 |
|
- } |
| 90 |
|
- |
| 91 |
|
- public function testCountSentences() |
| 92 |
|
- { |
| 93 |
|
- $tpl = new DwooTemplateString('{count_sentences $foo}'); |
| 94 |
|
- $tpl->forceCompilation(); |
| 95 |
|
- |
| 96 |
|
- $this->assertEquals('4', $this->dwoo->get($tpl, array('foo'=>"amdslfk smdkmsfd.\nmsldkfmsldsdfml. sdfsml\nmlskfmlsdksdf... mklsdspo."), $this->compiler)); |
| 97 |
|
- } |
| 98 |
|
- |
| 99 |
|
- public function testCountWords() |
| 100 |
|
- { |
| 101 |
|
- $tpl = new DwooTemplateString('{count_words $foo}'); |
| 102 |
|
- $tpl->forceCompilation(); |
| 103 |
|
- |
| 104 |
|
- $this->assertEquals('5', $this->dwoo->get($tpl, array('foo'=>"sfsdf.smdf\nmkoep\tsdlk sdfsdf"), $this->compiler)); |
| 105 |
|
- } |
| 106 |
|
- |
| 107 |
|
- public function testCycle() |
| 108 |
|
- { |
| 109 |
|
- $tpl = new DwooTemplateString('{cycle "hoy" array(foo,bar) }{cycle hoy}{cycle hoy}{cycle name=hoy print=false}{cycle name="hoy"}{cycle name=hoy reset=true advance=false}{cycle name="hoy" advance=true}'); |
| 110 |
|
- $tpl->forceCompilation(); |
| 111 |
|
- |
| 112 |
|
- $this->assertEquals('foobarfoofoofoofoo', $this->dwoo->get($tpl, array('foo'=>"sfsdf.smdf\nmkoep\tsdlk sdfsdf"), $this->compiler)); |
| 113 |
|
- } |
| 114 |
|
- |
| 115 |
|
- public function testDateFormat() |
| 116 |
|
- { |
| 117 |
|
- $tpl = new DwooTemplateString('{date_format $dwoo.now "%Y%H:%M:%S"}{date_format $foo "%Y%H:%M:%S" "one hour ago"}{date_format ""}'); |
| 118 |
|
- $tpl->forceCompilation(); |
| 119 |
|
- |
| 120 |
|
- $this->assertEquals(strftime("%Y%H:%M:%S", $_SERVER['REQUEST_TIME']).strftime('%Y%H:%M:%S', strtotime("one hour ago")), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 121 |
|
- } |
| 122 |
|
- |
| 123 |
|
- public function testDefault() |
| 124 |
|
- { |
| 125 |
|
- $tpl = new DwooTemplateString('{default $foo bar}{default $foo2 bar}{default $foo3 bar}'); |
| 126 |
|
- $tpl->forceCompilation(); |
| 127 |
|
- |
| 128 |
|
- $this->assertEquals("barbarfoo3", $this->dwoo->get($tpl, array('foo2'=>"", 'foo3'=>"foo3"), $this->compiler)); |
| 129 |
|
- } |
| 130 |
|
- |
| 131 |
|
- public function testDo() |
| 132 |
|
- { |
| 133 |
|
- $tpl = new DwooTemplateString('{do assign($foo bar)}{do $bar}{$bar}'); |
| 134 |
|
- $tpl->forceCompilation(); |
| 135 |
|
- |
| 136 |
|
- $this->assertEquals("moo", $this->dwoo->get($tpl, array('foo'=>"moo"), $this->compiler)); |
| 137 |
|
- } |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+require_once 'Dwoo/Compiler.php'; |
|
5 |
+ |
|
6 |
+class FuncTests extends PHPUnit_Framework_TestCase |
|
7 |
+{ |
|
8 |
+ protected $compiler; |
|
9 |
+ protected $dwoo; |
|
10 |
+ |
|
11 |
+ public function __construct() |
|
12 |
+ { |
|
13 |
+ // extend this class and override this in your constructor to test a modded compiler |
|
14 |
+ $this->compiler = new DwooCompiler(); |
|
15 |
+ $this->dwoo = new Dwoo(); |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ public function testAssign() |
|
19 |
+ { |
|
20 |
+ // test simple assign |
|
21 |
+ $tpl = new DwooTemplateString('{assign "bar" foo}{$foo}'); |
|
22 |
+ $tpl->forceCompilation(); |
|
23 |
+ |
|
24 |
+ $tpl2 = new DwooTemplateString('{assign baz foo}{$foo}'); |
|
25 |
+ $tpl2->forceCompilation(); |
|
26 |
+ |
|
27 |
+ $this->assertEquals('barbaz', $this->dwoo->get($tpl, array(), $this->compiler) . $this->dwoo->get($tpl2, array(), $this->compiler)); |
|
28 |
+ |
|
29 |
+ // test array assignation with function call |
|
30 |
+ $tpl = new DwooTemplateString('{assign reverse($foo) foo}{foreach $foo val}{$val}{/foreach}'); |
|
31 |
+ $tpl->forceCompilation(); |
|
32 |
+ |
|
33 |
+ $this->assertEquals('321', $this->dwoo->get($tpl, array('foo'=>array(1,2,3)), $this->compiler)); |
|
34 |
+ } |
|
35 |
+ |
|
36 |
+ public function testCapitalize() |
|
37 |
+ { |
|
38 |
+ $tpl = new DwooTemplateString('{capitalize "hello world 1st"}-{capitalize "hello world 1st" true}'); |
|
39 |
+ $tpl->forceCompilation(); |
|
40 |
+ |
|
41 |
+ $this->assertEquals('Hello World 1st-Hello World 1St', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
42 |
+ } |
|
43 |
+ |
|
44 |
+ public function testCat() |
|
45 |
+ { |
|
46 |
+ $tpl = new DwooTemplateString('{cat 3 bar "FOO"}'); |
|
47 |
+ $tpl->forceCompilation(); |
|
48 |
+ |
|
49 |
+ $this->assertEquals('3barFOO', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
50 |
+ } |
|
51 |
+ |
|
52 |
+ public function testCounter() |
|
53 |
+ { |
|
54 |
+ $tpl = new DwooTemplateString('{counter start=0 skip=2}{counter}{counter}{counter}'); |
|
55 |
+ $tpl->forceCompilation(); |
|
56 |
+ |
|
57 |
+ $this->assertEquals('0246', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
58 |
+ |
|
59 |
+ $tpl = new DwooTemplateString('{counter start=0 skip=2 assign=foo}{counter}{counter}{counter}{$foo}'); |
|
60 |
+ $tpl->forceCompilation(); |
|
61 |
+ |
|
62 |
+ $this->assertEquals('6', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
63 |
+ |
|
64 |
+ $tpl = new DwooTemplateString('{counter start=0 skip=2 assign=foo direction=down}{counter}{counter}{counter}{$foo}'); |
|
65 |
+ $tpl->forceCompilation(); |
|
66 |
+ |
|
67 |
+ $this->assertEquals('-6', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
68 |
+ |
|
69 |
+ $tpl = new DwooTemplateString('{counter start=0 skip=3 print=false}{counter}{counter}{counter}{counter print=true}'); |
|
70 |
+ $tpl->forceCompilation(); |
|
71 |
+ |
|
72 |
+ $this->assertEquals('12', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
73 |
+ } |
|
74 |
+ |
|
75 |
+ public function testCountChars() |
|
76 |
+ { |
|
77 |
+ $tpl = new DwooTemplateString('{count_characters "hello world"}{count_characters "hello world" true}'); |
|
78 |
+ $tpl->forceCompilation(); |
|
79 |
+ |
|
80 |
+ $this->assertEquals('1011', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
81 |
+ } |
|
82 |
+ |
|
83 |
+ public function testCountParagraphs() |
|
84 |
+ { |
|
85 |
+ $tpl = new DwooTemplateString('{count_paragraphs $foo}'); |
|
86 |
+ $tpl->forceCompilation(); |
|
87 |
+ |
|
88 |
+ $this->assertEquals('4', $this->dwoo->get($tpl, array('foo'=>"amdslfk smdkmsfd\nmsldkfmsldsdfml\nmlskfmlsdksdf\nmklsdspo"), $this->compiler)); |
|
89 |
+ } |
|
90 |
+ |
|
91 |
+ public function testCountSentences() |
|
92 |
+ { |
|
93 |
+ $tpl = new DwooTemplateString('{count_sentences $foo}'); |
|
94 |
+ $tpl->forceCompilation(); |
|
95 |
+ |
|
96 |
+ $this->assertEquals('4', $this->dwoo->get($tpl, array('foo'=>"amdslfk smdkmsfd.\nmsldkfmsldsdfml. sdfsml\nmlskfmlsdksdf... mklsdspo."), $this->compiler)); |
|
97 |
+ } |
|
98 |
+ |
|
99 |
+ public function testCountWords() |
|
100 |
+ { |
|
101 |
+ $tpl = new DwooTemplateString('{count_words $foo}'); |
|
102 |
+ $tpl->forceCompilation(); |
|
103 |
+ |
|
104 |
+ $this->assertEquals('5', $this->dwoo->get($tpl, array('foo'=>"sfsdf.smdf\nmkoep\tsdlk sdfsdf"), $this->compiler)); |
|
105 |
+ } |
|
106 |
+ |
|
107 |
+ public function testCycle() |
|
108 |
+ { |
|
109 |
+ $tpl = new DwooTemplateString('{cycle "hoy" array(foo,bar) }{cycle hoy}{cycle hoy}{cycle name=hoy print=false}{cycle name="hoy"}{cycle name=hoy reset=true advance=false}{cycle name="hoy" advance=true}'); |
|
110 |
+ $tpl->forceCompilation(); |
|
111 |
+ |
|
112 |
+ $this->assertEquals('foobarfoofoofoofoo', $this->dwoo->get($tpl, array('foo'=>"sfsdf.smdf\nmkoep\tsdlk sdfsdf"), $this->compiler)); |
|
113 |
+ } |
|
114 |
+ |
|
115 |
+ public function testDateFormat() |
|
116 |
+ { |
|
117 |
+ $tpl = new DwooTemplateString('{date_format $dwoo.now "%Y%H:%M:%S"}{date_format $foo "%Y%H:%M:%S" "one hour ago"}{date_format ""}'); |
|
118 |
+ $tpl->forceCompilation(); |
|
119 |
+ |
|
120 |
+ $this->assertEquals(strftime("%Y%H:%M:%S", $_SERVER['REQUEST_TIME']).strftime('%Y%H:%M:%S', strtotime("one hour ago")), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
121 |
+ } |
|
122 |
+ |
|
123 |
+ public function testDefault() |
|
124 |
+ { |
|
125 |
+ $tpl = new DwooTemplateString('{default $foo bar}{default $foo2 bar}{default $foo3 bar}'); |
|
126 |
+ $tpl->forceCompilation(); |
|
127 |
+ |
|
128 |
+ $this->assertEquals("barbarfoo3", $this->dwoo->get($tpl, array('foo2'=>"", 'foo3'=>"foo3"), $this->compiler)); |
|
129 |
+ } |
|
130 |
+ |
|
131 |
+ public function testDo() |
|
132 |
+ { |
|
133 |
+ $tpl = new DwooTemplateString('{do assign($foo bar)}{do $bar}{$bar}'); |
|
134 |
+ $tpl->forceCompilation(); |
|
135 |
+ |
|
136 |
+ $this->assertEquals("moo", $this->dwoo->get($tpl, array('foo'=>"moo"), $this->compiler)); |
|
137 |
+ } |
| 138 |
138 |
|
| 139 |
139 |
public function testDoEmpty() |
| 140 |
140 |
{ |
| 143 |
143 |
|
| 144 |
144 |
$this->assertEquals("", $this->dwoo->get($tpl, array('foo'=>"moo"), $this->compiler)); |
| 145 |
145 |
} |
| 146 |
|
- |
| 147 |
|
- public function testEscape() |
| 148 |
|
- { |
| 149 |
|
- $tpl = new DwooTemplateString('{escape $foo}'); |
| 150 |
|
- $tpl->forceCompilation(); |
| 151 |
|
- |
| 152 |
|
- $this->assertEquals(""", $this->dwoo->get($tpl, array('foo'=>'"'), $this->compiler)); |
| 153 |
|
- |
| 154 |
|
- $tpl = new DwooTemplateString('{escape $foo htmlall}'); |
| 155 |
|
- $tpl->forceCompilation(); |
| 156 |
|
- |
| 157 |
|
- $this->assertEquals("é", $this->dwoo->get($tpl, array('foo'=>'é'), $this->compiler)); |
| 158 |
|
- |
| 159 |
|
- $tpl = new DwooTemplateString('{escape $foo htmlall iso-8859-1}'); |
| 160 |
|
- $tpl->forceCompilation(); |
| 161 |
|
- |
| 162 |
|
- $this->assertEquals("ü", $this->dwoo->get($tpl, array('foo'=>utf8_decode('ü')), $this->compiler)); |
| 163 |
|
- |
| 164 |
|
- $tpl = new DwooTemplateString('{escape $foo url}'); |
| 165 |
|
- $tpl->forceCompilation(); |
| 166 |
|
- |
| 167 |
|
- $this->assertEquals(rawurlencode(':#?/'), $this->dwoo->get($tpl, array('foo'=>':#?/'), $this->compiler)); |
| 168 |
|
- |
| 169 |
|
- $tpl = new DwooTemplateString('{escape $foo quotes}'); |
| 170 |
|
- $tpl->forceCompilation(); |
| 171 |
|
- |
| 172 |
|
- $this->assertEquals("\\'", $this->dwoo->get($tpl, array('foo'=>'\''), $this->compiler)); |
| 173 |
|
- |
| 174 |
|
- $tpl = new DwooTemplateString('{escape $foo urlpathinfo}'); |
| 175 |
|
- $tpl->forceCompilation(); |
| 176 |
|
- |
| 177 |
|
- $this->assertEquals(rawurlencode(':#?').'/', $this->dwoo->get($tpl, array('foo'=>':#?/'), $this->compiler)); |
| 178 |
|
- |
| 179 |
|
- $tpl = new DwooTemplateString('{escape $foo hex}'); |
| 180 |
|
- $tpl->forceCompilation(); |
| 181 |
|
- |
| 182 |
|
- $this->assertEquals('%0a', $this->dwoo->get($tpl, array('foo'=>chr(10)), $this->compiler)); |
| 183 |
|
- |
| 184 |
|
- $tpl = new DwooTemplateString('{escape $foo hexentity}'); |
| 185 |
|
- $tpl->forceCompilation(); |
| 186 |
|
- |
| 187 |
|
- $this->assertEquals('
', $this->dwoo->get($tpl, array('foo'=>chr(10)), $this->compiler)); |
| 188 |
|
- |
| 189 |
|
- $tpl = new DwooTemplateString('{escape $foo javascript}'); |
| 190 |
|
- $tpl->forceCompilation(); |
| 191 |
|
- |
| 192 |
|
- $this->assertEquals('\\\\n', $this->dwoo->get($tpl, array('foo'=>'\\n'), $this->compiler)); |
| 193 |
|
- |
| 194 |
|
- $tpl = new DwooTemplateString('{escape $foo mail}'); |
| 195 |
|
- $tpl->forceCompilation(); |
| 196 |
|
- |
| 197 |
|
- $this->assertEquals('test (AT) foo (DOT) bar', $this->dwoo->get($tpl, array('foo'=>'test@foo.bar'), $this->compiler)); |
| 198 |
|
- } |
| 199 |
|
- |
| 200 |
|
- public function testEval() |
| 201 |
|
- { |
| 202 |
|
- $tpl = new DwooTemplateString('{eval $foo}{assign "baz" test}{eval $foo bar}+{$bar}'); |
| 203 |
|
- $tpl->forceCompilation(); |
| 204 |
|
- |
| 205 |
|
- $this->assertEquals("moo+baz", $this->dwoo->get($tpl, array('foo'=>'{$test}', 'test'=>'moo'), $this->compiler)); |
| 206 |
|
- } |
| 207 |
|
- |
| 208 |
|
- public function testFetch() |
| 209 |
|
- { |
| 210 |
|
- $tpl = new DwooTemplateString('{fetch file="'.DWOO_DIRECTORY.'tests/resources/test.html"}'); |
| 211 |
|
- $tpl->forceCompilation(); |
| 212 |
|
- |
| 213 |
|
- $this->assertEquals('{$foo}{$bar}', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 214 |
|
- } |
| 215 |
|
- |
| 216 |
|
- public function testInclude() |
| 217 |
|
- { |
| 218 |
|
- $tpl = new DwooTemplateString('{include file=\'file:'.DWOO_DIRECTORY.'tests/resources/test.html\' foo=$a bar=$b}'); |
| 219 |
|
- $tpl->forceCompilation(); |
| 220 |
|
- |
| 221 |
|
- $this->assertEquals("AB", $this->dwoo->get($tpl, array('a'=>'A', 'b'=>'B'))); |
| 222 |
|
- |
| 223 |
|
- $tpl = new DwooTemplateString('{include file=\'file:'.DWOO_DIRECTORY.'tests/resources/test.html\'}'); |
| 224 |
|
- $tpl->forceCompilation(); |
| 225 |
|
- |
| 226 |
|
- $this->assertEquals("ab", $this->dwoo->get($tpl, array('foo'=>'a', 'bar'=>'b'), $this->compiler)); |
| 227 |
|
- |
| 228 |
|
- $tpl = new DwooTemplateString('{include file=\'file:/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/test.html\'}'); |
| 229 |
|
- $tpl->forceCompilation(); |
| 230 |
|
- |
| 231 |
|
- $this->assertEquals("", $this->dwoo->get($tpl, array('foo'=>'a', 'bar'=>'b'), $this->compiler)); |
| 232 |
|
- |
| 233 |
|
- $tpl = new DwooTemplateFile(DWOO_DIRECTORY.'tests/resources/inctest.html'); |
| 234 |
|
- $tpl->forceCompilation(); |
| 235 |
|
- |
| 236 |
|
- $this->assertEquals("34", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 237 |
|
- } |
| 238 |
|
- |
| 239 |
|
- public function testIndent() |
| 240 |
|
- { |
| 241 |
|
- $tpl = new DwooTemplateString('{indent $foo 6 "-"}'); |
| 242 |
|
- $tpl->forceCompilation(); |
| 243 |
|
- |
| 244 |
|
- $this->assertEquals("------FOO\n------BAR", $this->dwoo->get($tpl, array('foo'=>"FOO\nBAR"), $this->compiler)); |
| 245 |
|
- } |
| 246 |
|
- |
| 247 |
|
- public function testIsset() |
| 248 |
|
- { |
| 249 |
|
- $tpl = new DwooTemplateString('{if isset($foo)}set{else}not set{/if}'); |
| 250 |
|
- $tpl->forceCompilation(); |
| 251 |
|
- $this->assertEquals("not set", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 252 |
|
- $this->assertEquals("set", $this->dwoo->get($tpl, array('foo'=>'a'), $this->compiler)); |
| 253 |
|
- $this->assertEquals("set", $this->dwoo->get($tpl, array('foo'=>''), $this->compiler)); |
| 254 |
|
- } |
| 255 |
|
- |
| 256 |
|
- public function testLower() |
| 257 |
|
- { |
| 258 |
|
- $tpl = new DwooTemplateString('{lower "FOO"}'); |
| 259 |
|
- $tpl->forceCompilation(); |
| 260 |
|
- |
| 261 |
|
- $this->assertEquals("foo", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 262 |
|
- } |
| 263 |
|
- |
| 264 |
|
- public function testMailto() |
| 265 |
|
- { |
| 266 |
|
- $tpl = new DwooTemplateString('{mailto address="me@example.com" encode="jschar"}'); |
| 267 |
|
- $tpl->forceCompilation(); |
| 268 |
|
- |
| 269 |
|
- $this->assertEquals('<script type="text/javascript">'."\n".'<!--'."\n".'document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,109,101,64,101,120,97,109,112,108,101,46,99,111,109,34,32,62,109,101,64,101,120,97,109,112,108,101,46,99,111,109,60,47,97,62));'."\n".'-->'."\n".'</script>'."\n", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 270 |
|
- |
| 271 |
|
- $tpl = new DwooTemplateString('{mailto address="me@example.com" text="send me some mail"}'); |
| 272 |
|
- $tpl->forceCompilation(); |
| 273 |
|
- |
| 274 |
|
- $this->assertEquals('<a href="mailto:me@example.com" >send me some mail</a>', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 275 |
|
- |
| 276 |
|
- $tpl = new DwooTemplateString('{mailto address="me@example.com" encode="javascript"}'); |
| 277 |
|
- $tpl->forceCompilation(); |
| 278 |
|
- |
| 279 |
|
- $this->assertEquals('<script type="text/javascript">eval(unescape(\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%22%20%3e%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%3c%2f%61%3e%27%29%3b\'));</script>', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 280 |
|
- |
| 281 |
|
- $tpl = new DwooTemplateString('{mailto address="me@example.com" encode="hex"}'); |
| 282 |
|
- $tpl->forceCompilation(); |
| 283 |
|
- |
| 284 |
|
- $this->assertEquals('<a href="mailto:%6d%65@%65%78%61%6d%70%6c%65.%63%6f%6d" >me@example.com</a>', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 285 |
|
- |
| 286 |
|
- $tpl = new DwooTemplateString('{mailto address="me@example.com" subject="Hello to you!" cc="you@example.com,they@example.com" extra=\'class="email"\'}'); |
| 287 |
|
- $tpl->forceCompilation(); |
| 288 |
|
- |
| 289 |
|
- $this->assertEquals('<a href="mailto:me@example.com?subject=Hello%20to%20you%21&cc=you%40example.com%2Cthey%40example.com" class="email">me@example.com</a>', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 290 |
|
- } |
| 291 |
|
- |
| 292 |
|
- public function testMath() |
| 293 |
|
- { |
| 294 |
|
- $tpl = new DwooTemplateString('{math equation="3+5+a+b" a="100" b="20"}'); |
| 295 |
|
- $tpl->forceCompilation(); |
| 296 |
|
- |
| 297 |
|
- $this->assertEquals((string)(3+5+100+20), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 298 |
|
- |
| 299 |
|
- $tpl = new DwooTemplateString('{math equation="3+5+`$a`+b" b="20"}'); |
| 300 |
|
- $tpl->forceCompilation(); |
| 301 |
|
- |
| 302 |
|
- $this->assertEquals((string)(3+5+100+20), $this->dwoo->get($tpl, array('a'=>100), $this->compiler)); |
| 303 |
|
- |
| 304 |
|
- $tpl = new DwooTemplateString('{math equation="3+5+cos(a)" a="1"}'); |
| 305 |
|
- $tpl->forceCompilation(); |
| 306 |
|
- |
| 307 |
|
- $this->assertEquals((string)(3+5+cos(1)), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 308 |
|
- |
| 309 |
|
- $tpl = new DwooTemplateString('{math equation="3+5+(cos(a) + max(3,2))" a="1"}'); |
| 310 |
|
- $tpl->forceCompilation(); |
| 311 |
|
- |
| 312 |
|
- $this->assertEquals((string)(3+5+(cos(1) + max(3,2))), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 313 |
|
- |
| 314 |
|
- $tpl = new DwooTemplateString('{math equation="3+5+(cos(a) + max(3,2))" a="1" assign="foo"}-{$foo}'); |
| 315 |
|
- $tpl->forceCompilation(); |
| 316 |
|
- |
| 317 |
|
- $this->assertEquals('-'.(3+5+(cos(1) + max(3,2))), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 318 |
|
- } |
| 319 |
|
- |
| 320 |
|
- public function testNl2br() |
| 321 |
|
- { |
| 322 |
|
- $tpl = new DwooTemplateString('{nl2br "f |
| 323 |
|
-a"}'); |
| 324 |
|
- $tpl->forceCompilation(); |
| 325 |
|
- |
| 326 |
|
- $this->assertEquals("f<br />\r\na", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 327 |
|
- } |
| 328 |
|
- |
| 329 |
|
- public function testRegexReplace() |
| 330 |
|
- { |
| 331 |
|
- $tpl = new DwooTemplateString('{regex_replace "FOOMOO" "/OO\$/" "L"}'); |
| 332 |
|
- $tpl->forceCompilation(); |
| 333 |
|
- |
| 334 |
|
- $this->assertEquals("FOOML", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 335 |
|
- |
| 336 |
|
- $tpl = new DwooTemplateString('{regex_replace "FOOMOO" "/OO\$/e" "die"}'); |
| 337 |
|
- $tpl->forceCompilation(); |
| 338 |
|
- |
| 339 |
|
- $this->assertEquals("FOOMdie", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 340 |
|
- } |
| 341 |
|
- |
| 342 |
|
- public function testReplace() |
| 343 |
|
- { |
| 344 |
|
- $tpl = new DwooTemplateString('{replace "FOOMOO" "OO" "L"}'); |
| 345 |
|
- $tpl->forceCompilation(); |
| 346 |
|
- |
| 347 |
|
- $this->assertEquals("FLML", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 348 |
|
- } |
| 349 |
|
- |
| 350 |
|
- public function testReverse() |
| 351 |
|
- { |
| 352 |
|
- $tpl = new DwooTemplateString('{reverse "abc"}'); |
| 353 |
|
- $tpl->forceCompilation(); |
| 354 |
|
- |
| 355 |
|
- $this->assertEquals("cba", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 356 |
|
- |
| 357 |
|
- $tpl = new DwooTemplateString('{foreach reverse(array(a,b,c)) foo}{$foo}{/foreach}'); |
| 358 |
|
- $tpl->forceCompilation(); |
| 359 |
|
- |
| 360 |
|
- $this->assertEquals("cba", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 361 |
|
- } |
| 362 |
|
- |
| 363 |
|
- public function testSpacify() |
| 364 |
|
- { |
| 365 |
|
- $tpl = new DwooTemplateString('{spacify "ABC" "+"}'); |
| 366 |
|
- $tpl->forceCompilation(); |
| 367 |
|
- |
| 368 |
|
- $this->assertEquals("A+B+C", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 369 |
|
- } |
| 370 |
|
- |
| 371 |
|
- public function testStringFormat() |
| 372 |
|
- { |
| 373 |
|
- $tpl = new DwooTemplateString('{string_format 53.3942 "%.2f"}'); |
| 374 |
|
- $tpl->forceCompilation(); |
| 375 |
|
- |
| 376 |
|
- $this->assertEquals("53.39", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 377 |
|
- } |
| 378 |
|
- |
| 379 |
|
- public function testStripTags() |
| 380 |
|
- { |
| 381 |
|
- $tpl = new DwooTemplateString('{strip_tags "<a href=\'foo\'>test</a>test"}'); |
| 382 |
|
- $tpl->forceCompilation(); |
| 383 |
|
- |
| 384 |
|
- $this->assertEquals(" test test", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 385 |
|
- |
| 386 |
|
- $tpl = new DwooTemplateString('{strip_tags "<a href=\'foo\'>test</a>test" false}'); |
| 387 |
|
- $tpl->forceCompilation(); |
| 388 |
|
- |
| 389 |
|
- $this->assertEquals("testtest", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 390 |
|
- } |
| 391 |
|
- |
| 392 |
|
- public function testStrip() |
| 393 |
|
- { |
| 394 |
|
- $tpl = new DwooTemplateString('{strip "a b c d"}'); |
| 395 |
|
- $tpl->forceCompilation(); |
| 396 |
|
- |
| 397 |
|
- $this->assertEquals("a b c d", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 398 |
|
- } |
| 399 |
|
- |
| 400 |
|
- public function testTruncate() |
| 401 |
|
- { |
| 402 |
|
- $tpl = new DwooTemplateString('{truncate "abcdefghijklmnopqrstuvwxyz" 20 "..." true}'); |
| 403 |
|
- $tpl->forceCompilation(); |
| 404 |
|
- |
| 405 |
|
- $this->assertEquals("abcdefghijklmnopq...", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 406 |
|
- |
| 407 |
|
- $tpl = new DwooTemplateString('{truncate "abcdefghijklmnopqrstuvwxyz" 20 "..." true true}'); |
| 408 |
|
- $tpl->forceCompilation(); |
| 409 |
|
- |
| 410 |
|
- $this->assertEquals("abcdefghi...stuvwxyz", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 411 |
|
- } |
| 412 |
|
- |
| 413 |
|
- public function testUpper() |
| 414 |
|
- { |
| 415 |
|
- $tpl = new DwooTemplateString('{upper "foo"}'); |
| 416 |
|
- $tpl->forceCompilation(); |
| 417 |
|
- |
| 418 |
|
- $this->assertEquals("FOO", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 419 |
|
- } |
| 420 |
|
- |
| 421 |
|
- public function testWordwrap() |
| 422 |
|
- { |
| 423 |
|
- $tpl = new DwooTemplateString('{wordwrap "abcdefghijklmnopqrstuvwxyz" 8 "\n" true}'); |
| 424 |
|
- $tpl->forceCompilation(); |
| 425 |
|
- |
| 426 |
|
- $this->assertEquals("abcdefgh\nijklmnop\nqrstuvwx\nyz", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 427 |
|
- } |
| 428 |
|
-} |
| 429 |
|
- |
|
146 |
+ |
|
147 |
+ public function testEscape() |
|
148 |
+ { |
|
149 |
+ $tpl = new DwooTemplateString('{escape $foo}'); |
|
150 |
+ $tpl->forceCompilation(); |
|
151 |
+ |
|
152 |
+ $this->assertEquals(""", $this->dwoo->get($tpl, array('foo'=>'"'), $this->compiler)); |
|
153 |
+ |
|
154 |
+ $tpl = new DwooTemplateString('{escape $foo htmlall}'); |
|
155 |
+ $tpl->forceCompilation(); |
|
156 |
+ |
|
157 |
+ $this->assertEquals("é", $this->dwoo->get($tpl, array('foo'=>'é'), $this->compiler)); |
|
158 |
+ |
|
159 |
+ $tpl = new DwooTemplateString('{escape $foo htmlall iso-8859-1}'); |
|
160 |
+ $tpl->forceCompilation(); |
|
161 |
+ |
|
162 |
+ $this->assertEquals("ü", $this->dwoo->get($tpl, array('foo'=>utf8_decode('ü')), $this->compiler)); |
|
163 |
+ |
|
164 |
+ $tpl = new DwooTemplateString('{escape $foo url}'); |
|
165 |
+ $tpl->forceCompilation(); |
|
166 |
+ |
|
167 |
+ $this->assertEquals(rawurlencode(':#?/'), $this->dwoo->get($tpl, array('foo'=>':#?/'), $this->compiler)); |
|
168 |
+ |
|
169 |
+ $tpl = new DwooTemplateString('{escape $foo quotes}'); |
|
170 |
+ $tpl->forceCompilation(); |
|
171 |
+ |
|
172 |
+ $this->assertEquals("\\'", $this->dwoo->get($tpl, array('foo'=>'\''), $this->compiler)); |
|
173 |
+ |
|
174 |
+ $tpl = new DwooTemplateString('{escape $foo urlpathinfo}'); |
|
175 |
+ $tpl->forceCompilation(); |
|
176 |
+ |
|
177 |
+ $this->assertEquals(rawurlencode(':#?').'/', $this->dwoo->get($tpl, array('foo'=>':#?/'), $this->compiler)); |
|
178 |
+ |
|
179 |
+ $tpl = new DwooTemplateString('{escape $foo hex}'); |
|
180 |
+ $tpl->forceCompilation(); |
|
181 |
+ |
|
182 |
+ $this->assertEquals('%0a', $this->dwoo->get($tpl, array('foo'=>chr(10)), $this->compiler)); |
|
183 |
+ |
|
184 |
+ $tpl = new DwooTemplateString('{escape $foo hexentity}'); |
|
185 |
+ $tpl->forceCompilation(); |
|
186 |
+ |
|
187 |
+ $this->assertEquals('
', $this->dwoo->get($tpl, array('foo'=>chr(10)), $this->compiler)); |
|
188 |
+ |
|
189 |
+ $tpl = new DwooTemplateString('{escape $foo javascript}'); |
|
190 |
+ $tpl->forceCompilation(); |
|
191 |
+ |
|
192 |
+ $this->assertEquals('\\\\n', $this->dwoo->get($tpl, array('foo'=>'\\n'), $this->compiler)); |
|
193 |
+ |
|
194 |
+ $tpl = new DwooTemplateString('{escape $foo mail}'); |
|
195 |
+ $tpl->forceCompilation(); |
|
196 |
+ |
|
197 |
+ $this->assertEquals('test (AT) foo (DOT) bar', $this->dwoo->get($tpl, array('foo'=>'test@foo.bar'), $this->compiler)); |
|
198 |
+ } |
|
199 |
+ |
|
200 |
+ public function testEval() |
|
201 |
+ { |
|
202 |
+ $tpl = new DwooTemplateString('{eval $foo}{assign "baz" test}{eval $foo bar}+{$bar}'); |
|
203 |
+ $tpl->forceCompilation(); |
|
204 |
+ |
|
205 |
+ $this->assertEquals("moo+baz", $this->dwoo->get($tpl, array('foo'=>'{$test}', 'test'=>'moo'), $this->compiler)); |
|
206 |
+ } |
|
207 |
+ |
|
208 |
+ public function testFetch() |
|
209 |
+ { |
|
210 |
+ $tpl = new DwooTemplateString('{fetch file="'.DWOO_DIRECTORY.'tests/resources/test.html"}'); |
|
211 |
+ $tpl->forceCompilation(); |
|
212 |
+ |
|
213 |
+ $this->assertEquals('{$foo}{$bar}', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
214 |
+ } |
|
215 |
+ |
|
216 |
+ public function testInclude() |
|
217 |
+ { |
|
218 |
+ $tpl = new DwooTemplateString('{include file=\'file:'.DWOO_DIRECTORY.'tests/resources/test.html\' foo=$a bar=$b}'); |
|
219 |
+ $tpl->forceCompilation(); |
|
220 |
+ |
|
221 |
+ $this->assertEquals("AB", $this->dwoo->get($tpl, array('a'=>'A', 'b'=>'B'))); |
|
222 |
+ |
|
223 |
+ $tpl = new DwooTemplateString('{include file=\'file:'.DWOO_DIRECTORY.'tests/resources/test.html\'}'); |
|
224 |
+ $tpl->forceCompilation(); |
|
225 |
+ |
|
226 |
+ $this->assertEquals("ab", $this->dwoo->get($tpl, array('foo'=>'a', 'bar'=>'b'), $this->compiler)); |
|
227 |
+ |
|
228 |
+ $tpl = new DwooTemplateString('{include file=\'file:/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/test.html\'}'); |
|
229 |
+ $tpl->forceCompilation(); |
|
230 |
+ |
|
231 |
+ $this->assertEquals("", $this->dwoo->get($tpl, array('foo'=>'a', 'bar'=>'b'), $this->compiler)); |
|
232 |
+ |
|
233 |
+ $tpl = new DwooTemplateFile(DWOO_DIRECTORY.'tests/resources/inctest.html'); |
|
234 |
+ $tpl->forceCompilation(); |
|
235 |
+ |
|
236 |
+ $this->assertEquals("34", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
237 |
+ } |
|
238 |
+ |
|
239 |
+ public function testIndent() |
|
240 |
+ { |
|
241 |
+ $tpl = new DwooTemplateString('{indent $foo 6 "-"}'); |
|
242 |
+ $tpl->forceCompilation(); |
|
243 |
+ |
|
244 |
+ $this->assertEquals("------FOO\n------BAR", $this->dwoo->get($tpl, array('foo'=>"FOO\nBAR"), $this->compiler)); |
|
245 |
+ } |
|
246 |
+ |
|
247 |
+ public function testIsset() |
|
248 |
+ { |
|
249 |
+ $tpl = new DwooTemplateString('{if isset($foo)}set{else}not set{/if}'); |
|
250 |
+ $tpl->forceCompilation(); |
|
251 |
+ $this->assertEquals("not set", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
252 |
+ $this->assertEquals("set", $this->dwoo->get($tpl, array('foo'=>'a'), $this->compiler)); |
|
253 |
+ $this->assertEquals("set", $this->dwoo->get($tpl, array('foo'=>''), $this->compiler)); |
|
254 |
+ } |
|
255 |
+ |
|
256 |
+ public function testLower() |
|
257 |
+ { |
|
258 |
+ $tpl = new DwooTemplateString('{lower "FOO"}'); |
|
259 |
+ $tpl->forceCompilation(); |
|
260 |
+ |
|
261 |
+ $this->assertEquals("foo", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
262 |
+ } |
|
263 |
+ |
|
264 |
+ public function testMailto() |
|
265 |
+ { |
|
266 |
+ $tpl = new DwooTemplateString('{mailto address="me@example.com" encode="jschar"}'); |
|
267 |
+ $tpl->forceCompilation(); |
|
268 |
+ |
|
269 |
+ $this->assertEquals('<script type="text/javascript">'."\n".'<!--'."\n".'document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,109,101,64,101,120,97,109,112,108,101,46,99,111,109,34,32,62,109,101,64,101,120,97,109,112,108,101,46,99,111,109,60,47,97,62));'."\n".'-->'."\n".'</script>'."\n", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
270 |
+ |
|
271 |
+ $tpl = new DwooTemplateString('{mailto address="me@example.com" text="send me some mail"}'); |
|
272 |
+ $tpl->forceCompilation(); |
|
273 |
+ |
|
274 |
+ $this->assertEquals('<a href="mailto:me@example.com" >send me some mail</a>', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
275 |
+ |
|
276 |
+ $tpl = new DwooTemplateString('{mailto address="me@example.com" encode="javascript"}'); |
|
277 |
+ $tpl->forceCompilation(); |
|
278 |
+ |
|
279 |
+ $this->assertEquals('<script type="text/javascript">eval(unescape(\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%22%20%3e%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%3c%2f%61%3e%27%29%3b\'));</script>', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
280 |
+ |
|
281 |
+ $tpl = new DwooTemplateString('{mailto address="me@example.com" encode="hex"}'); |
|
282 |
+ $tpl->forceCompilation(); |
|
283 |
+ |
|
284 |
+ $this->assertEquals('<a href="mailto:%6d%65@%65%78%61%6d%70%6c%65.%63%6f%6d" >me@example.com</a>', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
285 |
+ |
|
286 |
+ $tpl = new DwooTemplateString('{mailto address="me@example.com" subject="Hello to you!" cc="you@example.com,they@example.com" extra=\'class="email"\'}'); |
|
287 |
+ $tpl->forceCompilation(); |
|
288 |
+ |
|
289 |
+ $this->assertEquals('<a href="mailto:me@example.com?subject=Hello%20to%20you%21&cc=you%40example.com%2Cthey%40example.com" class="email">me@example.com</a>', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
290 |
+ } |
|
291 |
+ |
|
292 |
+ public function testMath() |
|
293 |
+ { |
|
294 |
+ $tpl = new DwooTemplateString('{math equation="3+5+a+b" a="100" b="20"}'); |
|
295 |
+ $tpl->forceCompilation(); |
|
296 |
+ |
|
297 |
+ $this->assertEquals((string)(3+5+100+20), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
298 |
+ |
|
299 |
+ $tpl = new DwooTemplateString('{math equation="3+5+`$a`+b" b="20"}'); |
|
300 |
+ $tpl->forceCompilation(); |
|
301 |
+ |
|
302 |
+ $this->assertEquals((string)(3+5+100+20), $this->dwoo->get($tpl, array('a'=>100), $this->compiler)); |
|
303 |
+ |
|
304 |
+ $tpl = new DwooTemplateString('{math equation="3+5+cos(a)" a="1"}'); |
|
305 |
+ $tpl->forceCompilation(); |
|
306 |
+ |
|
307 |
+ $this->assertEquals((string)(3+5+cos(1)), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
308 |
+ |
|
309 |
+ $tpl = new DwooTemplateString('{math equation="3+5+(cos(a) + max(3,2))" a="1"}'); |
|
310 |
+ $tpl->forceCompilation(); |
|
311 |
+ |
|
312 |
+ $this->assertEquals((string)(3+5+(cos(1) + max(3,2))), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
313 |
+ |
|
314 |
+ $tpl = new DwooTemplateString('{math equation="3+5+(cos(a) + max(3,2))" a="1" assign="foo"}-{$foo}'); |
|
315 |
+ $tpl->forceCompilation(); |
|
316 |
+ |
|
317 |
+ $this->assertEquals('-'.(3+5+(cos(1) + max(3,2))), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
318 |
+ } |
|
319 |
+ |
|
320 |
+ public function testNl2br() |
|
321 |
+ { |
|
322 |
+ $tpl = new DwooTemplateString('{nl2br "f |
|
323 |
+a"}'); |
|
324 |
+ $tpl->forceCompilation(); |
|
325 |
+ |
|
326 |
+ $this->assertEquals("f<br />\r\na", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
327 |
+ } |
|
328 |
+ |
|
329 |
+ public function testRegexReplace() |
|
330 |
+ { |
|
331 |
+ $tpl = new DwooTemplateString('{regex_replace "FOOMOO" "/OO\$/" "L"}'); |
|
332 |
+ $tpl->forceCompilation(); |
|
333 |
+ |
|
334 |
+ $this->assertEquals("FOOML", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
335 |
+ |
|
336 |
+ $tpl = new DwooTemplateString('{regex_replace "FOOMOO" "/OO\$/e" "die"}'); |
|
337 |
+ $tpl->forceCompilation(); |
|
338 |
+ |
|
339 |
+ $this->assertEquals("FOOMdie", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
340 |
+ } |
|
341 |
+ |
|
342 |
+ public function testReplace() |
|
343 |
+ { |
|
344 |
+ $tpl = new DwooTemplateString('{replace "FOOMOO" "OO" "L"}'); |
|
345 |
+ $tpl->forceCompilation(); |
|
346 |
+ |
|
347 |
+ $this->assertEquals("FLML", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
348 |
+ } |
|
349 |
+ |
|
350 |
+ public function testReverse() |
|
351 |
+ { |
|
352 |
+ $tpl = new DwooTemplateString('{reverse "abc"}'); |
|
353 |
+ $tpl->forceCompilation(); |
|
354 |
+ |
|
355 |
+ $this->assertEquals("cba", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
356 |
+ |
|
357 |
+ $tpl = new DwooTemplateString('{foreach reverse(array(a,b,c)) foo}{$foo}{/foreach}'); |
|
358 |
+ $tpl->forceCompilation(); |
|
359 |
+ |
|
360 |
+ $this->assertEquals("cba", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
361 |
+ } |
|
362 |
+ |
|
363 |
+ public function testSpacify() |
|
364 |
+ { |
|
365 |
+ $tpl = new DwooTemplateString('{spacify "ABC" "+"}'); |
|
366 |
+ $tpl->forceCompilation(); |
|
367 |
+ |
|
368 |
+ $this->assertEquals("A+B+C", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
369 |
+ } |
|
370 |
+ |
|
371 |
+ public function testStringFormat() |
|
372 |
+ { |
|
373 |
+ $tpl = new DwooTemplateString('{string_format 53.3942 "%.2f"}'); |
|
374 |
+ $tpl->forceCompilation(); |
|
375 |
+ |
|
376 |
+ $this->assertEquals("53.39", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
377 |
+ } |
|
378 |
+ |
|
379 |
+ public function testStripTags() |
|
380 |
+ { |
|
381 |
+ $tpl = new DwooTemplateString('{strip_tags "<a href=\'foo\'>test</a>test"}'); |
|
382 |
+ $tpl->forceCompilation(); |
|
383 |
+ |
|
384 |
+ $this->assertEquals(" test test", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
385 |
+ |
|
386 |
+ $tpl = new DwooTemplateString('{strip_tags "<a href=\'foo\'>test</a>test" false}'); |
|
387 |
+ $tpl->forceCompilation(); |
|
388 |
+ |
|
389 |
+ $this->assertEquals("testtest", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
390 |
+ } |
|
391 |
+ |
|
392 |
+ public function testStrip() |
|
393 |
+ { |
|
394 |
+ $tpl = new DwooTemplateString('{strip "a b c d"}'); |
|
395 |
+ $tpl->forceCompilation(); |
|
396 |
+ |
|
397 |
+ $this->assertEquals("a b c d", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
398 |
+ } |
|
399 |
+ |
|
400 |
+ public function testTruncate() |
|
401 |
+ { |
|
402 |
+ $tpl = new DwooTemplateString('{truncate "abcdefghijklmnopqrstuvwxyz" 20 "..." true}'); |
|
403 |
+ $tpl->forceCompilation(); |
|
404 |
+ |
|
405 |
+ $this->assertEquals("abcdefghijklmnopq...", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
406 |
+ |
|
407 |
+ $tpl = new DwooTemplateString('{truncate "abcdefghijklmnopqrstuvwxyz" 20 "..." true true}'); |
|
408 |
+ $tpl->forceCompilation(); |
|
409 |
+ |
|
410 |
+ $this->assertEquals("abcdefghi...stuvwxyz", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
411 |
+ } |
|
412 |
+ |
|
413 |
+ public function testUpper() |
|
414 |
+ { |
|
415 |
+ $tpl = new DwooTemplateString('{upper "foo"}'); |
|
416 |
+ $tpl->forceCompilation(); |
|
417 |
+ |
|
418 |
+ $this->assertEquals("FOO", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
419 |
+ } |
|
420 |
+ |
|
421 |
+ public function testWordwrap() |
|
422 |
+ { |
|
423 |
+ $tpl = new DwooTemplateString('{wordwrap "abcdefghijklmnopqrstuvwxyz" 8 "\n" true}'); |
|
424 |
+ $tpl->forceCompilation(); |
|
425 |
+ |
|
426 |
+ $this->assertEquals("abcdefgh\nijklmnop\nqrstuvwx\nyz", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
427 |
+ } |
|
428 |
+} |
|
429 |
+ |
| 430 |
430 |
?> |
| 2 |
|
-<?php |
| 3 |
|
- |
| 4 |
|
-require_once dirname(dirname(__FILE__)).'/DwooCompiler.php'; |
| 5 |
|
- |
| 6 |
|
-function testphpfunc($input) { return $input.'OK'; } |
| 7 |
|
- |
| 8 |
|
-class SecurityTests extends PHPUnit_Framework_TestCase |
| 9 |
|
-{ |
| 10 |
|
- protected $compiler; |
| 11 |
|
- protected $dwoo; |
| 12 |
|
- protected $policy; |
| 13 |
|
- |
| 14 |
|
- public function __construct() |
| 15 |
|
- { |
| 16 |
|
- $this->compiler = new DwooCompiler(); |
| 17 |
|
- $this->dwoo = new Dwoo(); |
| 18 |
|
- $this->policy = new DwooSecurityPolicy(); |
| 19 |
|
- $this->dwoo->setSecurityPolicy($this->policy); |
| 20 |
|
- } |
| 21 |
|
- |
| 22 |
|
- public function testConstantHandling() |
| 23 |
|
- { |
| 24 |
|
- $tpl = new DwooTemplateString('{$dwoo.const.DWOO_DIRECTORY}'); |
| 25 |
|
- $tpl->forceCompilation(); |
| 26 |
|
- |
| 27 |
|
- $this->assertEquals("", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 28 |
|
- |
| 29 |
|
- $this->policy->setConstantHandling(DwooSecurityPolicy::CONST_ALLOW); |
| 30 |
|
- |
| 31 |
|
- $tpl = new DwooTemplateString('{$dwoo.const.DWOO_DIRECTORY}'); |
| 32 |
|
- $tpl->forceCompilation(); |
| 33 |
|
- |
| 34 |
|
- $this->assertEquals(DWOO_DIRECTORY, $this->dwoo->get($tpl, array(), $this->compiler)); |
| 35 |
|
- } |
| 36 |
|
- |
| 37 |
|
- public function testPhpHandling() |
| 38 |
|
- { |
| 39 |
|
- $this->policy->setPhpHandling(DwooSecurityPolicy::PHP_ALLOW); |
| 40 |
|
- |
| 41 |
|
- $tpl = new DwooTemplateString('<?php echo "moo"; ?>'); |
| 42 |
|
- $tpl->forceCompilation(); |
| 43 |
|
- |
| 44 |
|
- $this->assertEquals("moo", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 45 |
|
- |
| 46 |
|
- |
| 47 |
|
- $this->policy->setPhpHandling(DwooSecurityPolicy::PHP_ENCODE); |
| 48 |
|
- |
| 49 |
|
- $tpl = new DwooTemplateString('<?php echo "moo"; ?>'); |
| 50 |
|
- $tpl->forceCompilation(); |
| 51 |
|
- |
| 52 |
|
- $this->assertEquals(htmlspecialchars('<?php echo "moo"; ?>'), $this->dwoo->get($tpl, array(), $this->compiler)); |
| 53 |
|
- |
| 54 |
|
- |
| 55 |
|
- $this->policy->setPhpHandling(DwooSecurityPolicy::PHP_REMOVE); |
| 56 |
|
- |
| 57 |
|
- $tpl = new DwooTemplateString('<?php echo "moo"; ?>'); |
| 58 |
|
- $tpl->forceCompilation(); |
| 59 |
|
- |
| 60 |
|
- $this->assertEquals('', $this->dwoo->get($tpl, array(), $this->compiler)); |
| 61 |
|
- } |
| 62 |
|
- |
| 63 |
|
- public function testAllowPhpFunction() |
| 64 |
|
- { |
| 65 |
|
- $this->policy->allowPhpFunction('testphpfunc'); |
| 66 |
|
- |
| 67 |
|
- $tpl = new DwooTemplateString('{testphpfunc("foo")}'); |
| 68 |
|
- $tpl->forceCompilation(); |
| 69 |
|
- |
| 70 |
|
- $this->assertEquals("fooOK", $this->dwoo->get($tpl, array(), $this->compiler)); |
| 71 |
|
- } |
| 72 |
|
- |
| 73 |
|
- public function testAllowDirectoryGetSet() |
| 74 |
|
- { |
| 75 |
|
- $old = $this->policy->getAllowedDirectories(); |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+require_once 'Dwoo/Compiler.php'; |
|
5 |
+ |
|
6 |
+function testphpfunc($input) { return $input.'OK'; } |
|
7 |
+ |
|
8 |
+class SecurityTests extends PHPUnit_Framework_TestCase |
|
9 |
+{ |
|
10 |
+ protected $compiler; |
|
11 |
+ protected $dwoo; |
|
12 |
+ protected $policy; |
|
13 |
+ |
|
14 |
+ public function __construct() |
|
15 |
+ { |
|
16 |
+ $this->compiler = new DwooCompiler(); |
|
17 |
+ $this->dwoo = new Dwoo(); |
|
18 |
+ $this->policy = new DwooSecurityPolicy(); |
|
19 |
+ $this->dwoo->setSecurityPolicy($this->policy); |
|
20 |
+ } |
|
21 |
+ |
|
22 |
+ public function testConstantHandling() |
|
23 |
+ { |
|
24 |
+ $tpl = new DwooTemplateString('{$dwoo.const.DWOO_DIRECTORY}'); |
|
25 |
+ $tpl->forceCompilation(); |
|
26 |
+ |
|
27 |
+ $this->assertEquals("", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
28 |
+ |
|
29 |
+ $this->policy->setConstantHandling(DwooSecurityPolicy::CONST_ALLOW); |
|
30 |
+ |
|
31 |
+ $tpl = new DwooTemplateString('{$dwoo.const.DWOO_DIRECTORY}'); |
|
32 |
+ $tpl->forceCompilation(); |
|
33 |
+ |
|
34 |
+ $this->assertEquals(DWOO_DIRECTORY, $this->dwoo->get($tpl, array(), $this->compiler)); |
|
35 |
+ } |
|
36 |
+ |
|
37 |
+ public function testPhpHandling() |
|
38 |
+ { |
|
39 |
+ $this->policy->setPhpHandling(DwooSecurityPolicy::PHP_ALLOW); |
|
40 |
+ |
|
41 |
+ $tpl = new DwooTemplateString('<?php echo "moo"; ?>'); |
|
42 |
+ $tpl->forceCompilation(); |
|
43 |
+ |
|
44 |
+ $this->assertEquals("moo", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
45 |
+ |
|
46 |
+ |
|
47 |
+ $this->policy->setPhpHandling(DwooSecurityPolicy::PHP_ENCODE); |
|
48 |
+ |
|
49 |
+ $tpl = new DwooTemplateString('<?php echo "moo"; ?>'); |
|
50 |
+ $tpl->forceCompilation(); |
|
51 |
+ |
|
52 |
+ $this->assertEquals(htmlspecialchars('<?php echo "moo"; ?>'), $this->dwoo->get($tpl, array(), $this->compiler)); |
|
53 |
+ |
|
54 |
+ |
|
55 |
+ $this->policy->setPhpHandling(DwooSecurityPolicy::PHP_REMOVE); |
|
56 |
+ |
|
57 |
+ $tpl = new DwooTemplateString('<?php echo "moo"; ?>'); |
|
58 |
+ $tpl->forceCompilation(); |
|
59 |
+ |
|
60 |
+ $this->assertEquals('', $this->dwoo->get($tpl, array(), $this->compiler)); |
|
61 |
+ } |
|
62 |
+ |
|
63 |
+ public function testAllowPhpFunction() |
|
64 |
+ { |
|
65 |
+ $this->policy->allowPhpFunction('testphpfunc'); |
|
66 |
+ |
|
67 |
+ $tpl = new DwooTemplateString('{testphpfunc("foo")}'); |
|
68 |
+ $tpl->forceCompilation(); |
|
69 |
+ |
|
70 |
+ $this->assertEquals("fooOK", $this->dwoo->get($tpl, array(), $this->compiler)); |
|
71 |
+ } |
|
72 |
+ |
|
73 |
+ public function testAllowDirectoryGetSet() |
|
74 |
+ { |
|
75 |
+ $old = $this->policy->getAllowedDirectories(); |
| 76 |
76 |
$this->policy->allowDirectory(array('./resources')); |
| 77 |
77 |
$this->policy->allowDirectory('./temp'); |
| 78 |
78 |
$this->assertEquals(array_merge($old, array(realpath('./resources')=>true, realpath('./temp')=>true)), $this->policy->getAllowedDirectories()); |
| 79 |
79 |
|
| 80 |
|
- $this->policy->disallowDirectory(array('./resources')); |
|
80 |
+ $this->policy->disallowDirectory(array('./resources')); |
| 81 |
81 |
$this->policy->disallowDirectory('./temp'); |
| 82 |
82 |
$this->assertEquals($old, $this->policy->getAllowedDirectories()); |
| 83 |
|
- } |
|
83 |
+ } |
| 84 |
84 |
|
| 85 |
|
- public function testAllowPhpGetSet() |
| 86 |
|
- { |
| 87 |
|
- $old = $this->policy->getAllowedPhpFunctions(); |
|
85 |
+ public function testAllowPhpGetSet() |
|
86 |
+ { |
|
87 |
+ $old = $this->policy->getAllowedPhpFunctions(); |
| 88 |
88 |
$this->policy->allowPhpFunction(array('a','b')); |
| 89 |
89 |
$this->policy->allowPhpFunction('c'); |
| 90 |
90 |
$this->assertEquals(array_merge($old, array('a'=>true, 'b'=>true, 'c'=>true)), $this->policy->getAllowedPhpFunctions()); |