@@ -23,159 +23,159 @@ |
||
23 | 23 | */ |
24 | 24 | class Loader implements ILoader |
25 | 25 | { |
26 | - /** |
|
27 | - * Stores the plugin directories. |
|
28 | - * |
|
29 | - * @see addDirectory |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $paths = array(); |
|
26 | + /** |
|
27 | + * Stores the plugin directories. |
|
28 | + * |
|
29 | + * @see addDirectory |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $paths = array(); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Stores the plugins names/paths relationships |
|
36 | - * don't edit this on your own, use addDirectory. |
|
37 | - * |
|
38 | - * @see addDirectory |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - protected $classPath = array(); |
|
34 | + /** |
|
35 | + * Stores the plugins names/paths relationships |
|
36 | + * don't edit this on your own, use addDirectory. |
|
37 | + * |
|
38 | + * @see addDirectory |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + protected $classPath = array(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * Path where class paths cache files are written. |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $cacheDir; |
|
43 | + /** |
|
44 | + * Path where class paths cache files are written. |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $cacheDir; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Path where builtin plugins are stored. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - protected $corePluginDir; |
|
50 | + /** |
|
51 | + * Path where builtin plugins are stored. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + protected $corePluginDir; |
|
56 | 56 | |
57 | - /** |
|
58 | - * Loader constructor. |
|
59 | - * |
|
60 | - * @param $cacheDir |
|
61 | - */ |
|
62 | - public function __construct($cacheDir) |
|
63 | - { |
|
64 | - $this->corePluginDir = __DIR__ . DIRECTORY_SEPARATOR . 'Plugins'; |
|
65 | - $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
57 | + /** |
|
58 | + * Loader constructor. |
|
59 | + * |
|
60 | + * @param $cacheDir |
|
61 | + */ |
|
62 | + public function __construct($cacheDir) |
|
63 | + { |
|
64 | + $this->corePluginDir = __DIR__ . DIRECTORY_SEPARATOR . 'Plugins'; |
|
65 | + $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
66 | 66 | |
67 | - // include class paths or rebuild paths if the cache file isn't there |
|
68 | - $cacheFile = $this->cacheDir . 'classpath.cache.d' . Core::RELEASE_TAG . '.php'; |
|
69 | - if (file_exists($cacheFile)) { |
|
70 | - $classpath = file_get_contents($cacheFile); |
|
71 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
72 | - } else { |
|
73 | - $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); |
|
74 | - } |
|
75 | - } |
|
67 | + // include class paths or rebuild paths if the cache file isn't there |
|
68 | + $cacheFile = $this->cacheDir . 'classpath.cache.d' . Core::RELEASE_TAG . '.php'; |
|
69 | + if (file_exists($cacheFile)) { |
|
70 | + $classpath = file_get_contents($cacheFile); |
|
71 | + $this->classPath = unserialize($classpath) + $this->classPath; |
|
72 | + } else { |
|
73 | + $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Rebuilds class paths, scans the given directory recursively and saves all paths in the given file. |
|
79 | - * |
|
80 | - * @param string $path the plugin path to scan |
|
81 | - * @param string|boolean $cacheFile the file where to store the plugin paths cache, it will be overwritten |
|
82 | - * |
|
83 | - * @throws Exception |
|
84 | - */ |
|
85 | - protected function rebuildClassPathCache($path, $cacheFile) |
|
86 | - { |
|
87 | - $tmp = array(); |
|
88 | - if ($cacheFile !== false) { |
|
89 | - $tmp = $this->classPath; |
|
90 | - $this->classPath = array(); |
|
91 | - } |
|
77 | + /** |
|
78 | + * Rebuilds class paths, scans the given directory recursively and saves all paths in the given file. |
|
79 | + * |
|
80 | + * @param string $path the plugin path to scan |
|
81 | + * @param string|boolean $cacheFile the file where to store the plugin paths cache, it will be overwritten |
|
82 | + * |
|
83 | + * @throws Exception |
|
84 | + */ |
|
85 | + protected function rebuildClassPathCache($path, $cacheFile) |
|
86 | + { |
|
87 | + $tmp = array(); |
|
88 | + if ($cacheFile !== false) { |
|
89 | + $tmp = $this->classPath; |
|
90 | + $this->classPath = array(); |
|
91 | + } |
|
92 | 92 | |
93 | - // iterates over all files/folders |
|
94 | - foreach (new \DirectoryIterator($path) as $fileInfo) { |
|
95 | - if (!$fileInfo->isDot()) { |
|
96 | - if ($fileInfo->isDir()) { |
|
97 | - $this->rebuildClassPathCache($fileInfo->getPathname(), false); |
|
98 | - } else { |
|
99 | - $this->classPath[$fileInfo->getBasename('.php')] = $fileInfo->getPathname(); |
|
100 | - } |
|
101 | - } |
|
102 | - } |
|
93 | + // iterates over all files/folders |
|
94 | + foreach (new \DirectoryIterator($path) as $fileInfo) { |
|
95 | + if (!$fileInfo->isDot()) { |
|
96 | + if ($fileInfo->isDir()) { |
|
97 | + $this->rebuildClassPathCache($fileInfo->getPathname(), false); |
|
98 | + } else { |
|
99 | + $this->classPath[$fileInfo->getBasename('.php')] = $fileInfo->getPathname(); |
|
100 | + } |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - // save in file if it's the first call (not recursed) |
|
105 | - if ($cacheFile !== false) { |
|
106 | - if (!file_put_contents($cacheFile, serialize($this->classPath))) { |
|
107 | - throw new Exception('Could not write into ' . $cacheFile . ', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); |
|
108 | - } |
|
109 | - $this->classPath += $tmp; |
|
110 | - } |
|
111 | - } |
|
104 | + // save in file if it's the first call (not recursed) |
|
105 | + if ($cacheFile !== false) { |
|
106 | + if (!file_put_contents($cacheFile, serialize($this->classPath))) { |
|
107 | + throw new Exception('Could not write into ' . $cacheFile . ', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); |
|
108 | + } |
|
109 | + $this->classPath += $tmp; |
|
110 | + } |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Loads a plugin file. |
|
115 | - * |
|
116 | - * @param string $class the plugin name, without the `Plugin` prefix |
|
117 | - * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it |
|
118 | - * has just been added, defaults to true |
|
119 | - * |
|
120 | - * @throws Exception |
|
121 | - */ |
|
122 | - public function loadPlugin($class, $forceRehash = true) |
|
123 | - { |
|
124 | - /** |
|
125 | - * An unknown class was requested (maybe newly added) or the |
|
126 | - * include failed so we rebuild the cache. include() will fail |
|
127 | - * with an uncatchable error if the file doesn't exist, which |
|
128 | - * usually means that the cache is stale and must be rebuilt, |
|
129 | - * so we check for that before trying to include() the plugin. |
|
130 | - */ |
|
131 | - if ((!isset($this->classPath[$class]) || !is_readable($this->classPath[$class])) || (!isset |
|
132 | - ($this->classPath[$class . 'Compile']) || !is_readable($this->classPath[$class . 'Compile']))) { |
|
133 | - if ($forceRehash) { |
|
134 | - $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d' . |
|
135 | - Core::RELEASE_TAG . '.php'); |
|
136 | - foreach ($this->paths as $path => $file) { |
|
137 | - $this->rebuildClassPathCache($path, $file); |
|
138 | - } |
|
139 | - if (isset($this->classPath[$class])) { |
|
140 | - include_once $this->classPath[$class]; |
|
141 | - } elseif (isset($this->classPath[$class . 'Compile'])) { |
|
142 | - include_once $this->classPath[$class . 'Compile']; |
|
143 | - } else { |
|
144 | - throw new Exception('Plugin "' . $class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
145 | - } |
|
146 | - } else { |
|
147 | - throw new Exception('Plugin "' . $class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
148 | - } |
|
149 | - } |
|
150 | - } |
|
113 | + /** |
|
114 | + * Loads a plugin file. |
|
115 | + * |
|
116 | + * @param string $class the plugin name, without the `Plugin` prefix |
|
117 | + * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it |
|
118 | + * has just been added, defaults to true |
|
119 | + * |
|
120 | + * @throws Exception |
|
121 | + */ |
|
122 | + public function loadPlugin($class, $forceRehash = true) |
|
123 | + { |
|
124 | + /** |
|
125 | + * An unknown class was requested (maybe newly added) or the |
|
126 | + * include failed so we rebuild the cache. include() will fail |
|
127 | + * with an uncatchable error if the file doesn't exist, which |
|
128 | + * usually means that the cache is stale and must be rebuilt, |
|
129 | + * so we check for that before trying to include() the plugin. |
|
130 | + */ |
|
131 | + if ((!isset($this->classPath[$class]) || !is_readable($this->classPath[$class])) || (!isset |
|
132 | + ($this->classPath[$class . 'Compile']) || !is_readable($this->classPath[$class . 'Compile']))) { |
|
133 | + if ($forceRehash) { |
|
134 | + $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d' . |
|
135 | + Core::RELEASE_TAG . '.php'); |
|
136 | + foreach ($this->paths as $path => $file) { |
|
137 | + $this->rebuildClassPathCache($path, $file); |
|
138 | + } |
|
139 | + if (isset($this->classPath[$class])) { |
|
140 | + include_once $this->classPath[$class]; |
|
141 | + } elseif (isset($this->classPath[$class . 'Compile'])) { |
|
142 | + include_once $this->classPath[$class . 'Compile']; |
|
143 | + } else { |
|
144 | + throw new Exception('Plugin "' . $class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
145 | + } |
|
146 | + } else { |
|
147 | + throw new Exception('Plugin "' . $class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
148 | + } |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * Adds a plugin directory, the plugins found in the new plugin directory |
|
154 | - * will take precedence over the other directories (including the default |
|
155 | - * dwoo plugin directory), you can use this for example to override plugins |
|
156 | - * in a specific directory for a specific application while keeping all your |
|
157 | - * usual plugins in the same place for all applications. |
|
158 | - * TOCOM don't forget that php functions overrides are not rehashed so you |
|
159 | - * need to clear the classpath caches by hand when adding those. |
|
160 | - * |
|
161 | - * @param string $pluginDirectory the plugin path to scan |
|
162 | - * |
|
163 | - * @throws Exception |
|
164 | - */ |
|
165 | - public function addDirectory($pluginDirectory) |
|
166 | - { |
|
167 | - $pluginDir = realpath($pluginDirectory); |
|
168 | - if (!$pluginDir) { |
|
169 | - throw new Exception('Plugin directory does not exist or can not be read : ' . $pluginDirectory); |
|
170 | - } |
|
171 | - $cacheFile = $this->cacheDir . 'classpath-' . substr(strtr($pluginDir, '/\\:' . PATH_SEPARATOR, '----'), |
|
172 | - strlen($pluginDir) > 80 ? - 80 : 0) . '.d' . Core::RELEASE_TAG . '.php'; |
|
173 | - $this->paths[$pluginDir] = $cacheFile; |
|
174 | - if (file_exists($cacheFile)) { |
|
175 | - $classpath = file_get_contents($cacheFile); |
|
176 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
177 | - } else { |
|
178 | - $this->rebuildClassPathCache($pluginDir, $cacheFile); |
|
179 | - } |
|
180 | - } |
|
152 | + /** |
|
153 | + * Adds a plugin directory, the plugins found in the new plugin directory |
|
154 | + * will take precedence over the other directories (including the default |
|
155 | + * dwoo plugin directory), you can use this for example to override plugins |
|
156 | + * in a specific directory for a specific application while keeping all your |
|
157 | + * usual plugins in the same place for all applications. |
|
158 | + * TOCOM don't forget that php functions overrides are not rehashed so you |
|
159 | + * need to clear the classpath caches by hand when adding those. |
|
160 | + * |
|
161 | + * @param string $pluginDirectory the plugin path to scan |
|
162 | + * |
|
163 | + * @throws Exception |
|
164 | + */ |
|
165 | + public function addDirectory($pluginDirectory) |
|
166 | + { |
|
167 | + $pluginDir = realpath($pluginDirectory); |
|
168 | + if (!$pluginDir) { |
|
169 | + throw new Exception('Plugin directory does not exist or can not be read : ' . $pluginDirectory); |
|
170 | + } |
|
171 | + $cacheFile = $this->cacheDir . 'classpath-' . substr(strtr($pluginDir, '/\\:' . PATH_SEPARATOR, '----'), |
|
172 | + strlen($pluginDir) > 80 ? - 80 : 0) . '.d' . Core::RELEASE_TAG . '.php'; |
|
173 | + $this->paths[$pluginDir] = $cacheFile; |
|
174 | + if (file_exists($cacheFile)) { |
|
175 | + $classpath = file_get_contents($cacheFile); |
|
176 | + $this->classPath = unserialize($classpath) + $this->classPath; |
|
177 | + } else { |
|
178 | + $this->rebuildClassPathCache($pluginDir, $cacheFile); |
|
179 | + } |
|
180 | + } |
|
181 | 181 | } |
@@ -61,14 +61,14 @@ discard block |
||
61 | 61 | */ |
62 | 62 | public function __construct($cacheDir) |
63 | 63 | { |
64 | - $this->corePluginDir = __DIR__ . DIRECTORY_SEPARATOR . 'Plugins'; |
|
65 | - $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
64 | + $this->corePluginDir = __DIR__.DIRECTORY_SEPARATOR.'Plugins'; |
|
65 | + $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
|
66 | 66 | |
67 | 67 | // include class paths or rebuild paths if the cache file isn't there |
68 | - $cacheFile = $this->cacheDir . 'classpath.cache.d' . Core::RELEASE_TAG . '.php'; |
|
68 | + $cacheFile = $this->cacheDir.'classpath.cache.d'.Core::RELEASE_TAG.'.php'; |
|
69 | 69 | if (file_exists($cacheFile)) { |
70 | 70 | $classpath = file_get_contents($cacheFile); |
71 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
71 | + $this->classPath = unserialize($classpath)+$this->classPath; |
|
72 | 72 | } else { |
73 | 73 | $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); |
74 | 74 | } |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | // save in file if it's the first call (not recursed) |
105 | 105 | if ($cacheFile !== false) { |
106 | 106 | if (!file_put_contents($cacheFile, serialize($this->classPath))) { |
107 | - throw new Exception('Could not write into ' . $cacheFile . ', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); |
|
107 | + throw new Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); |
|
108 | 108 | } |
109 | 109 | $this->classPath += $tmp; |
110 | 110 | } |
@@ -129,22 +129,22 @@ discard block |
||
129 | 129 | * so we check for that before trying to include() the plugin. |
130 | 130 | */ |
131 | 131 | if ((!isset($this->classPath[$class]) || !is_readable($this->classPath[$class])) || (!isset |
132 | - ($this->classPath[$class . 'Compile']) || !is_readable($this->classPath[$class . 'Compile']))) { |
|
132 | + ($this->classPath[$class.'Compile']) || !is_readable($this->classPath[$class.'Compile']))) { |
|
133 | 133 | if ($forceRehash) { |
134 | - $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d' . |
|
135 | - Core::RELEASE_TAG . '.php'); |
|
134 | + $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir.'classpath.cache.d'. |
|
135 | + Core::RELEASE_TAG.'.php'); |
|
136 | 136 | foreach ($this->paths as $path => $file) { |
137 | 137 | $this->rebuildClassPathCache($path, $file); |
138 | 138 | } |
139 | 139 | if (isset($this->classPath[$class])) { |
140 | 140 | include_once $this->classPath[$class]; |
141 | - } elseif (isset($this->classPath[$class . 'Compile'])) { |
|
142 | - include_once $this->classPath[$class . 'Compile']; |
|
141 | + } elseif (isset($this->classPath[$class.'Compile'])) { |
|
142 | + include_once $this->classPath[$class.'Compile']; |
|
143 | 143 | } else { |
144 | - throw new Exception('Plugin "' . $class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
144 | + throw new Exception('Plugin "'.$class.'" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
145 | 145 | } |
146 | 146 | } else { |
147 | - throw new Exception('Plugin "' . $class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
147 | + throw new Exception('Plugin "'.$class.'" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); |
|
148 | 148 | } |
149 | 149 | } |
150 | 150 | } |
@@ -166,14 +166,14 @@ discard block |
||
166 | 166 | { |
167 | 167 | $pluginDir = realpath($pluginDirectory); |
168 | 168 | if (!$pluginDir) { |
169 | - throw new Exception('Plugin directory does not exist or can not be read : ' . $pluginDirectory); |
|
169 | + throw new Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory); |
|
170 | 170 | } |
171 | - $cacheFile = $this->cacheDir . 'classpath-' . substr(strtr($pluginDir, '/\\:' . PATH_SEPARATOR, '----'), |
|
172 | - strlen($pluginDir) > 80 ? - 80 : 0) . '.d' . Core::RELEASE_TAG . '.php'; |
|
171 | + $cacheFile = $this->cacheDir.'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), |
|
172 | + strlen($pluginDir) > 80 ? -80 : 0).'.d'.Core::RELEASE_TAG.'.php'; |
|
173 | 173 | $this->paths[$pluginDir] = $cacheFile; |
174 | 174 | if (file_exists($cacheFile)) { |
175 | 175 | $classpath = file_get_contents($cacheFile); |
176 | - $this->classPath = unserialize($classpath) + $this->classPath; |
|
176 | + $this->classPath = unserialize($classpath)+$this->classPath; |
|
177 | 177 | } else { |
178 | 178 | $this->rebuildClassPathCache($pluginDir, $cacheFile); |
179 | 179 | } |
@@ -26,35 +26,35 @@ |
||
26 | 26 | */ |
27 | 27 | class Exception extends DwooException |
28 | 28 | { |
29 | - protected $compiler; |
|
30 | - protected $template; |
|
29 | + protected $compiler; |
|
30 | + protected $template; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Exception constructor. |
|
34 | - * |
|
35 | - * @param DwooCompiler $compiler |
|
36 | - * @param int $message |
|
37 | - */ |
|
38 | - public function __construct(DwooCompiler $compiler, $message) |
|
39 | - { |
|
40 | - $this->compiler = $compiler; |
|
41 | - $this->template = $compiler->getCore()->getTemplate(); |
|
42 | - parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
43 | - } |
|
32 | + /** |
|
33 | + * Exception constructor. |
|
34 | + * |
|
35 | + * @param DwooCompiler $compiler |
|
36 | + * @param int $message |
|
37 | + */ |
|
38 | + public function __construct(DwooCompiler $compiler, $message) |
|
39 | + { |
|
40 | + $this->compiler = $compiler; |
|
41 | + $this->template = $compiler->getCore()->getTemplate(); |
|
42 | + parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return DwooCompiler |
|
47 | - */ |
|
48 | - public function getCompiler() |
|
49 | - { |
|
50 | - return $this->compiler; |
|
51 | - } |
|
45 | + /** |
|
46 | + * @return DwooCompiler |
|
47 | + */ |
|
48 | + public function getCompiler() |
|
49 | + { |
|
50 | + return $this->compiler; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return \Dwoo\ITemplate|null |
|
55 | - */ |
|
56 | - public function getTemplate() |
|
57 | - { |
|
58 | - return $this->template; |
|
59 | - } |
|
53 | + /** |
|
54 | + * @return \Dwoo\ITemplate|null |
|
55 | + */ |
|
56 | + public function getTemplate() |
|
57 | + { |
|
58 | + return $this->template; |
|
59 | + } |
|
60 | 60 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | { |
40 | 40 | $this->compiler = $compiler; |
41 | 41 | $this->template = $compiler->getCore()->getTemplate(); |
42 | - parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
42 | + parent::__construct('Compilation error at line '.$compiler->getLine().' in "'.$this->template->getResourceName().':'.$this->template->getResourceIdentifier().'" : '.$message); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -34,28 +34,28 @@ discard block |
||
34 | 34 | */ |
35 | 35 | class PluginExtends extends Plugin implements ICompilable |
36 | 36 | { |
37 | - protected static $childSource; |
|
38 | - protected static $regex; |
|
39 | - protected static $l; |
|
40 | - protected static $r; |
|
41 | - protected static $lastReplacement; |
|
42 | - |
|
43 | - public function process() |
|
44 | - { |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @param Compiler $compiler |
|
49 | - * @param $file |
|
50 | - * |
|
51 | - * @throws CompilationException |
|
52 | - */ |
|
53 | - public static function compile(Compiler $compiler, $file) |
|
54 | - { |
|
55 | - list($l, $r) = $compiler->getDelimiters(); |
|
56 | - self::$l = preg_quote($l, '/'); |
|
57 | - self::$r = preg_quote($r, '/'); |
|
58 | - self::$regex = '/ |
|
37 | + protected static $childSource; |
|
38 | + protected static $regex; |
|
39 | + protected static $l; |
|
40 | + protected static $r; |
|
41 | + protected static $lastReplacement; |
|
42 | + |
|
43 | + public function process() |
|
44 | + { |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @param Compiler $compiler |
|
49 | + * @param $file |
|
50 | + * |
|
51 | + * @throws CompilationException |
|
52 | + */ |
|
53 | + public static function compile(Compiler $compiler, $file) |
|
54 | + { |
|
55 | + list($l, $r) = $compiler->getDelimiters(); |
|
56 | + self::$l = preg_quote($l, '/'); |
|
57 | + self::$r = preg_quote($r, '/'); |
|
58 | + self::$regex = '/ |
|
59 | 59 | ' . self::$l . 'block\s(["\']?)(.+?)\1' . self::$r . '(?:\r?\n?) |
60 | 60 | ((?: |
61 | 61 | (?R) |
@@ -70,137 +70,137 @@ discard block |
||
70 | 70 | ' . self::$l . '\/block' . self::$r . ' |
71 | 71 | /six'; |
72 | 72 | |
73 | - if ($compiler->getLooseOpeningHandling()) { |
|
74 | - self::$l .= '\s*'; |
|
75 | - self::$r = '\s*' . self::$r; |
|
76 | - } |
|
77 | - $inheritanceTree = array(array('source' => $compiler->getTemplateSource())); |
|
78 | - $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR; |
|
79 | - $curTpl = $compiler->getCore()->getTemplate(); |
|
80 | - |
|
81 | - while (!empty($file)) { |
|
82 | - if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) { |
|
83 | - throw new CompilationException($compiler, 'Extends : The file name must be a non-empty string'); |
|
84 | - } |
|
85 | - |
|
86 | - if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) { |
|
87 | - // resource:identifier given, extract them |
|
88 | - $resource = $m[1]; |
|
89 | - $identifier = $m[2]; |
|
90 | - } else { |
|
91 | - // get the current template's resource |
|
92 | - $resource = $curTpl->getResourceName(); |
|
93 | - $identifier = substr($file, 1, - 1); |
|
94 | - } |
|
95 | - |
|
96 | - try { |
|
97 | - $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
|
98 | - } |
|
99 | - catch (SecurityException $e) { |
|
100 | - throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); |
|
101 | - } |
|
102 | - catch (Exception $e) { |
|
103 | - throw new CompilationException($compiler, 'Extends : ' . $e->getMessage()); |
|
104 | - } |
|
105 | - |
|
106 | - if ($parent === null) { |
|
107 | - throw new CompilationException($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
108 | - } elseif ($parent === false) { |
|
109 | - throw new CompilationException($compiler, 'Extends : Resource "' . $resource . '" does not support extends.'); |
|
110 | - } |
|
111 | - |
|
112 | - $curTpl = $parent; |
|
113 | - $newParent = array( |
|
114 | - 'source' => $parent->getSource(), |
|
115 | - 'resource' => $resource, |
|
116 | - 'identifier' => $parent->getResourceIdentifier(), |
|
117 | - 'uid' => $parent->getUid() |
|
118 | - ); |
|
119 | - if (array_search($newParent, $inheritanceTree, true) !== false) { |
|
120 | - throw new CompilationException($compiler, 'Extends : Recursive template inheritance detected'); |
|
121 | - } |
|
122 | - $inheritanceTree[] = $newParent; |
|
123 | - |
|
124 | - if (preg_match('/^' . self::$l . 'extends(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?' . self::$r . '/i', $parent->getSource(), $match)) { |
|
125 | - $curPath = dirname($identifier) . DIRECTORY_SEPARATOR; |
|
126 | - if (isset($match[2]) && $match[2] == '"') { |
|
127 | - $file = '"' . str_replace('"', '\\"', substr($match[1], 1, - 1)) . '"'; |
|
128 | - } elseif (isset($match[2]) && $match[2] == "'") { |
|
129 | - $file = '"' . substr($match[1], 1, - 1) . '"'; |
|
130 | - } else { |
|
131 | - $file = '"' . $match[1] . '"'; |
|
132 | - } |
|
133 | - } else { |
|
134 | - $file = false; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - while (true) { |
|
139 | - $parent = array_pop($inheritanceTree); |
|
140 | - $child = end($inheritanceTree); |
|
141 | - self::$childSource = $child['source']; |
|
142 | - self::$lastReplacement = count($inheritanceTree) === 1; |
|
143 | - if (!isset($newSource)) { |
|
144 | - $newSource = $parent['source']; |
|
145 | - } |
|
146 | - $newSource = preg_replace_callback(self::$regex, array( |
|
147 | - 'Dwoo\Plugins\Functions\PluginExtends', |
|
148 | - 'replaceBlock' |
|
149 | - ), $newSource); |
|
150 | - $newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource; |
|
151 | - |
|
152 | - if (self::$lastReplacement) { |
|
153 | - break; |
|
154 | - } |
|
155 | - } |
|
156 | - $compiler->setTemplateSource($newSource); |
|
157 | - $compiler->recompile(); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @param array $matches |
|
162 | - * |
|
163 | - * @return mixed|string |
|
164 | - */ |
|
165 | - protected static function replaceBlock(array $matches) |
|
166 | - { |
|
167 | - $matches[3] = self::removeTrailingNewline($matches[3]); |
|
168 | - |
|
169 | - if (preg_match_all(self::$regex, self::$childSource, $override) && in_array($matches[2], $override[2])) { |
|
170 | - $key = array_search($matches[2], $override[2]); |
|
171 | - $override = self::removeTrailingNewline($override[3][$key]); |
|
172 | - |
|
173 | - $l = stripslashes(self::$l); |
|
174 | - $r = stripslashes(self::$r); |
|
175 | - |
|
176 | - if (self::$lastReplacement) { |
|
177 | - return preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override); |
|
178 | - } |
|
179 | - |
|
180 | - return $l . 'block ' . $matches[1] . $matches[2] . $matches[1] . $r . preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override) . $l . '/block' . $r; |
|
181 | - } |
|
182 | - |
|
183 | - if (preg_match(self::$regex, $matches[3])) { |
|
184 | - return preg_replace_callback(self::$regex, array( |
|
185 | - 'Dwoo\Plugins\Functions\PluginExtends', |
|
186 | - 'replaceBlock' |
|
187 | - ), $matches[3]); |
|
188 | - } |
|
189 | - |
|
190 | - if (self::$lastReplacement) { |
|
191 | - return $matches[3]; |
|
192 | - } |
|
193 | - |
|
194 | - return $matches[0]; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param $text |
|
199 | - * |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - protected static function removeTrailingNewline($text) |
|
203 | - { |
|
204 | - return substr($text, - 1) === "\n" ? substr($text, - 2, 1) === "\r" ? substr($text, 0, - 2) : substr($text, 0, - 1) : $text; |
|
205 | - } |
|
73 | + if ($compiler->getLooseOpeningHandling()) { |
|
74 | + self::$l .= '\s*'; |
|
75 | + self::$r = '\s*' . self::$r; |
|
76 | + } |
|
77 | + $inheritanceTree = array(array('source' => $compiler->getTemplateSource())); |
|
78 | + $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR; |
|
79 | + $curTpl = $compiler->getCore()->getTemplate(); |
|
80 | + |
|
81 | + while (!empty($file)) { |
|
82 | + if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) { |
|
83 | + throw new CompilationException($compiler, 'Extends : The file name must be a non-empty string'); |
|
84 | + } |
|
85 | + |
|
86 | + if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) { |
|
87 | + // resource:identifier given, extract them |
|
88 | + $resource = $m[1]; |
|
89 | + $identifier = $m[2]; |
|
90 | + } else { |
|
91 | + // get the current template's resource |
|
92 | + $resource = $curTpl->getResourceName(); |
|
93 | + $identifier = substr($file, 1, - 1); |
|
94 | + } |
|
95 | + |
|
96 | + try { |
|
97 | + $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
|
98 | + } |
|
99 | + catch (SecurityException $e) { |
|
100 | + throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); |
|
101 | + } |
|
102 | + catch (Exception $e) { |
|
103 | + throw new CompilationException($compiler, 'Extends : ' . $e->getMessage()); |
|
104 | + } |
|
105 | + |
|
106 | + if ($parent === null) { |
|
107 | + throw new CompilationException($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
108 | + } elseif ($parent === false) { |
|
109 | + throw new CompilationException($compiler, 'Extends : Resource "' . $resource . '" does not support extends.'); |
|
110 | + } |
|
111 | + |
|
112 | + $curTpl = $parent; |
|
113 | + $newParent = array( |
|
114 | + 'source' => $parent->getSource(), |
|
115 | + 'resource' => $resource, |
|
116 | + 'identifier' => $parent->getResourceIdentifier(), |
|
117 | + 'uid' => $parent->getUid() |
|
118 | + ); |
|
119 | + if (array_search($newParent, $inheritanceTree, true) !== false) { |
|
120 | + throw new CompilationException($compiler, 'Extends : Recursive template inheritance detected'); |
|
121 | + } |
|
122 | + $inheritanceTree[] = $newParent; |
|
123 | + |
|
124 | + if (preg_match('/^' . self::$l . 'extends(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?' . self::$r . '/i', $parent->getSource(), $match)) { |
|
125 | + $curPath = dirname($identifier) . DIRECTORY_SEPARATOR; |
|
126 | + if (isset($match[2]) && $match[2] == '"') { |
|
127 | + $file = '"' . str_replace('"', '\\"', substr($match[1], 1, - 1)) . '"'; |
|
128 | + } elseif (isset($match[2]) && $match[2] == "'") { |
|
129 | + $file = '"' . substr($match[1], 1, - 1) . '"'; |
|
130 | + } else { |
|
131 | + $file = '"' . $match[1] . '"'; |
|
132 | + } |
|
133 | + } else { |
|
134 | + $file = false; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + while (true) { |
|
139 | + $parent = array_pop($inheritanceTree); |
|
140 | + $child = end($inheritanceTree); |
|
141 | + self::$childSource = $child['source']; |
|
142 | + self::$lastReplacement = count($inheritanceTree) === 1; |
|
143 | + if (!isset($newSource)) { |
|
144 | + $newSource = $parent['source']; |
|
145 | + } |
|
146 | + $newSource = preg_replace_callback(self::$regex, array( |
|
147 | + 'Dwoo\Plugins\Functions\PluginExtends', |
|
148 | + 'replaceBlock' |
|
149 | + ), $newSource); |
|
150 | + $newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource; |
|
151 | + |
|
152 | + if (self::$lastReplacement) { |
|
153 | + break; |
|
154 | + } |
|
155 | + } |
|
156 | + $compiler->setTemplateSource($newSource); |
|
157 | + $compiler->recompile(); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @param array $matches |
|
162 | + * |
|
163 | + * @return mixed|string |
|
164 | + */ |
|
165 | + protected static function replaceBlock(array $matches) |
|
166 | + { |
|
167 | + $matches[3] = self::removeTrailingNewline($matches[3]); |
|
168 | + |
|
169 | + if (preg_match_all(self::$regex, self::$childSource, $override) && in_array($matches[2], $override[2])) { |
|
170 | + $key = array_search($matches[2], $override[2]); |
|
171 | + $override = self::removeTrailingNewline($override[3][$key]); |
|
172 | + |
|
173 | + $l = stripslashes(self::$l); |
|
174 | + $r = stripslashes(self::$r); |
|
175 | + |
|
176 | + if (self::$lastReplacement) { |
|
177 | + return preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override); |
|
178 | + } |
|
179 | + |
|
180 | + return $l . 'block ' . $matches[1] . $matches[2] . $matches[1] . $r . preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override) . $l . '/block' . $r; |
|
181 | + } |
|
182 | + |
|
183 | + if (preg_match(self::$regex, $matches[3])) { |
|
184 | + return preg_replace_callback(self::$regex, array( |
|
185 | + 'Dwoo\Plugins\Functions\PluginExtends', |
|
186 | + 'replaceBlock' |
|
187 | + ), $matches[3]); |
|
188 | + } |
|
189 | + |
|
190 | + if (self::$lastReplacement) { |
|
191 | + return $matches[3]; |
|
192 | + } |
|
193 | + |
|
194 | + return $matches[0]; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param $text |
|
199 | + * |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + protected static function removeTrailingNewline($text) |
|
203 | + { |
|
204 | + return substr($text, - 1) === "\n" ? substr($text, - 2, 1) === "\r" ? substr($text, 0, - 2) : substr($text, 0, - 1) : $text; |
|
205 | + } |
|
206 | 206 | } |
@@ -56,26 +56,26 @@ discard block |
||
56 | 56 | self::$l = preg_quote($l, '/'); |
57 | 57 | self::$r = preg_quote($r, '/'); |
58 | 58 | self::$regex = '/ |
59 | - ' . self::$l . 'block\s(["\']?)(.+?)\1' . self::$r . '(?:\r?\n?) |
|
59 | + ' . self::$l.'block\s(["\']?)(.+?)\1'.self::$r.'(?:\r?\n?) |
|
60 | 60 | ((?: |
61 | 61 | (?R) |
62 | 62 | | |
63 | - [^' . self::$l . ']* |
|
63 | + [^' . self::$l.']* |
|
64 | 64 | (?: |
65 | - (?! ' . self::$l . '\/?block\b ) |
|
66 | - ' . self::$l . ' |
|
67 | - [^' . self::$l . ']*+ |
|
65 | + (?! ' . self::$l.'\/?block\b ) |
|
66 | + ' . self::$l.' |
|
67 | + [^' . self::$l.']*+ |
|
68 | 68 | )* |
69 | 69 | )*) |
70 | - ' . self::$l . '\/block' . self::$r . ' |
|
70 | + ' . self::$l.'\/block'.self::$r.' |
|
71 | 71 | /six'; |
72 | 72 | |
73 | 73 | if ($compiler->getLooseOpeningHandling()) { |
74 | 74 | self::$l .= '\s*'; |
75 | - self::$r = '\s*' . self::$r; |
|
75 | + self::$r = '\s*'.self::$r; |
|
76 | 76 | } |
77 | 77 | $inheritanceTree = array(array('source' => $compiler->getTemplateSource())); |
78 | - $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR; |
|
78 | + $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()).DIRECTORY_SEPARATOR; |
|
79 | 79 | $curTpl = $compiler->getCore()->getTemplate(); |
80 | 80 | |
81 | 81 | while (!empty($file)) { |
@@ -97,16 +97,16 @@ discard block |
||
97 | 97 | $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
98 | 98 | } |
99 | 99 | catch (SecurityException $e) { |
100 | - throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); |
|
100 | + throw new CompilationException($compiler, 'Extends : Security restriction : '.$e->getMessage()); |
|
101 | 101 | } |
102 | 102 | catch (Exception $e) { |
103 | - throw new CompilationException($compiler, 'Extends : ' . $e->getMessage()); |
|
103 | + throw new CompilationException($compiler, 'Extends : '.$e->getMessage()); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | if ($parent === null) { |
107 | - throw new CompilationException($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
107 | + throw new CompilationException($compiler, 'Extends : Resource "'.$resource.':'.$identifier.'" not found.'); |
|
108 | 108 | } elseif ($parent === false) { |
109 | - throw new CompilationException($compiler, 'Extends : Resource "' . $resource . '" does not support extends.'); |
|
109 | + throw new CompilationException($compiler, 'Extends : Resource "'.$resource.'" does not support extends.'); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | $curTpl = $parent; |
@@ -121,14 +121,14 @@ discard block |
||
121 | 121 | } |
122 | 122 | $inheritanceTree[] = $newParent; |
123 | 123 | |
124 | - if (preg_match('/^' . self::$l . 'extends(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?' . self::$r . '/i', $parent->getSource(), $match)) { |
|
125 | - $curPath = dirname($identifier) . DIRECTORY_SEPARATOR; |
|
124 | + if (preg_match('/^'.self::$l.'extends(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?'.self::$r.'/i', $parent->getSource(), $match)) { |
|
125 | + $curPath = dirname($identifier).DIRECTORY_SEPARATOR; |
|
126 | 126 | if (isset($match[2]) && $match[2] == '"') { |
127 | - $file = '"' . str_replace('"', '\\"', substr($match[1], 1, - 1)) . '"'; |
|
127 | + $file = '"'.str_replace('"', '\\"', substr($match[1], 1, - 1)).'"'; |
|
128 | 128 | } elseif (isset($match[2]) && $match[2] == "'") { |
129 | - $file = '"' . substr($match[1], 1, - 1) . '"'; |
|
129 | + $file = '"'.substr($match[1], 1, - 1).'"'; |
|
130 | 130 | } else { |
131 | - $file = '"' . $match[1] . '"'; |
|
131 | + $file = '"'.$match[1].'"'; |
|
132 | 132 | } |
133 | 133 | } else { |
134 | 134 | $file = false; |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | 'Dwoo\Plugins\Functions\PluginExtends', |
148 | 148 | 'replaceBlock' |
149 | 149 | ), $newSource); |
150 | - $newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource; |
|
150 | + $newSource = $l.'do extendsCheck('.var_export($parent['resource'].':'.$parent['identifier'], true).')'.$r.$newSource; |
|
151 | 151 | |
152 | 152 | if (self::$lastReplacement) { |
153 | 153 | break; |
@@ -174,10 +174,10 @@ discard block |
||
174 | 174 | $r = stripslashes(self::$r); |
175 | 175 | |
176 | 176 | if (self::$lastReplacement) { |
177 | - return preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override); |
|
177 | + return preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is', $matches[3], $override); |
|
178 | 178 | } |
179 | 179 | |
180 | - return $l . 'block ' . $matches[1] . $matches[2] . $matches[1] . $r . preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override) . $l . '/block' . $r; |
|
180 | + return $l.'block '.$matches[1].$matches[2].$matches[1].$r.preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is', $matches[3], $override).$l.'/block'.$r; |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | if (preg_match(self::$regex, $matches[3])) { |
@@ -95,11 +95,9 @@ |
||
95 | 95 | |
96 | 96 | try { |
97 | 97 | $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
98 | - } |
|
99 | - catch (SecurityException $e) { |
|
98 | + } catch (SecurityException $e) { |
|
100 | 99 | throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); |
101 | - } |
|
102 | - catch (Exception $e) { |
|
100 | + } catch (Exception $e) { |
|
103 | 101 | throw new CompilationException($compiler, 'Extends : ' . $e->getMessage()); |
104 | 102 | } |
105 | 103 |
@@ -36,70 +36,70 @@ |
||
36 | 36 | */ |
37 | 37 | class PluginTif extends Plugin implements ICompilable |
38 | 38 | { |
39 | - /** |
|
40 | - * @param Compiler $compiler |
|
41 | - * @param array $rest |
|
42 | - * @param array $tokens |
|
43 | - * |
|
44 | - * @return mixed|string |
|
45 | - * @throws CompilationException |
|
46 | - */ |
|
47 | - public static function compile(Compiler $compiler, array $rest, array $tokens) |
|
48 | - { |
|
49 | - // load if plugin |
|
50 | - if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
|
51 | - try { |
|
52 | - $compiler->getCore()->getLoader()->loadPlugin('if'); |
|
53 | - } |
|
54 | - catch (Exception $e) { |
|
55 | - throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); |
|
56 | - } |
|
57 | - } |
|
39 | + /** |
|
40 | + * @param Compiler $compiler |
|
41 | + * @param array $rest |
|
42 | + * @param array $tokens |
|
43 | + * |
|
44 | + * @return mixed|string |
|
45 | + * @throws CompilationException |
|
46 | + */ |
|
47 | + public static function compile(Compiler $compiler, array $rest, array $tokens) |
|
48 | + { |
|
49 | + // load if plugin |
|
50 | + if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
|
51 | + try { |
|
52 | + $compiler->getCore()->getLoader()->loadPlugin('if'); |
|
53 | + } |
|
54 | + catch (Exception $e) { |
|
55 | + throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); |
|
56 | + } |
|
57 | + } |
|
58 | 58 | |
59 | - if (count($rest) == 1) { |
|
60 | - return $rest[0]; |
|
61 | - } |
|
59 | + if (count($rest) == 1) { |
|
60 | + return $rest[0]; |
|
61 | + } |
|
62 | 62 | |
63 | - // fetch false result and remove the ":" if it was present |
|
64 | - $falseResult = array_pop($rest); |
|
63 | + // fetch false result and remove the ":" if it was present |
|
64 | + $falseResult = array_pop($rest); |
|
65 | 65 | |
66 | - if (trim(end($rest), '"\'') === ':') { |
|
67 | - // remove the ':' if present |
|
68 | - array_pop($rest); |
|
69 | - } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { |
|
70 | - if ($falseResult === '?' || $falseResult === ':') { |
|
71 | - throw new CompilationException($compiler, |
|
72 | - 'Tif: incomplete tif statement, value missing after ' . $falseResult); |
|
73 | - } |
|
74 | - // there was in fact no false result provided, so we move it to be the true result instead |
|
75 | - $trueResult = $falseResult; |
|
76 | - $falseResult = "''"; |
|
77 | - } |
|
66 | + if (trim(end($rest), '"\'') === ':') { |
|
67 | + // remove the ':' if present |
|
68 | + array_pop($rest); |
|
69 | + } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { |
|
70 | + if ($falseResult === '?' || $falseResult === ':') { |
|
71 | + throw new CompilationException($compiler, |
|
72 | + 'Tif: incomplete tif statement, value missing after ' . $falseResult); |
|
73 | + } |
|
74 | + // there was in fact no false result provided, so we move it to be the true result instead |
|
75 | + $trueResult = $falseResult; |
|
76 | + $falseResult = "''"; |
|
77 | + } |
|
78 | 78 | |
79 | - // fetch true result if needed |
|
80 | - if (!isset($trueResult)) { |
|
81 | - $trueResult = array_pop($rest); |
|
82 | - // no true result provided so we use the expression arg |
|
83 | - if ($trueResult === '?') { |
|
84 | - $trueResult = true; |
|
85 | - } |
|
86 | - } |
|
79 | + // fetch true result if needed |
|
80 | + if (!isset($trueResult)) { |
|
81 | + $trueResult = array_pop($rest); |
|
82 | + // no true result provided so we use the expression arg |
|
83 | + if ($trueResult === '?') { |
|
84 | + $trueResult = true; |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - // remove the '?' if present |
|
89 | - if (trim(end($rest), '"\'') === '?') { |
|
90 | - array_pop($rest); |
|
91 | - } |
|
88 | + // remove the '?' if present |
|
89 | + if (trim(end($rest), '"\'') === '?') { |
|
90 | + array_pop($rest); |
|
91 | + } |
|
92 | 92 | |
93 | - // check params were correctly provided |
|
94 | - if (empty($rest) || $trueResult === null || $falseResult === null) { |
|
95 | - throw new CompilationException($compiler, |
|
96 | - 'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>'); |
|
97 | - } |
|
93 | + // check params were correctly provided |
|
94 | + if (empty($rest) || $trueResult === null || $falseResult === null) { |
|
95 | + throw new CompilationException($compiler, |
|
96 | + 'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>'); |
|
97 | + } |
|
98 | 98 | |
99 | - // parse condition |
|
100 | - $condition = PluginIf::replaceKeywords($rest, $tokens, $compiler); |
|
99 | + // parse condition |
|
100 | + $condition = PluginIf::replaceKeywords($rest, $tokens, $compiler); |
|
101 | 101 | |
102 | - return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', |
|
103 | - $condition) : $trueResult) . ' : ' . $falseResult . ')'; |
|
104 | - } |
|
102 | + return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', |
|
103 | + $condition) : $trueResult) . ' : ' . $falseResult . ')'; |
|
104 | + } |
|
105 | 105 | } |
106 | 106 | \ No newline at end of file |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | public static function compile(Compiler $compiler, array $rest, array $tokens) |
48 | 48 | { |
49 | 49 | // load if plugin |
50 | - if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
|
50 | + if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS.'PluginIf')) { |
|
51 | 51 | try { |
52 | 52 | $compiler->getCore()->getLoader()->loadPlugin('if'); |
53 | 53 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { |
70 | 70 | if ($falseResult === '?' || $falseResult === ':') { |
71 | 71 | throw new CompilationException($compiler, |
72 | - 'Tif: incomplete tif statement, value missing after ' . $falseResult); |
|
72 | + 'Tif: incomplete tif statement, value missing after '.$falseResult); |
|
73 | 73 | } |
74 | 74 | // there was in fact no false result provided, so we move it to be the true result instead |
75 | 75 | $trueResult = $falseResult; |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | // parse condition |
100 | 100 | $condition = PluginIf::replaceKeywords($rest, $tokens, $compiler); |
101 | 101 | |
102 | - return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', |
|
103 | - $condition) : $trueResult) . ' : ' . $falseResult . ')'; |
|
102 | + return '(('.implode(' ', $condition).') ? '.($trueResult === true ? implode(' ', |
|
103 | + $condition) : $trueResult).' : '.$falseResult.')'; |
|
104 | 104 | } |
105 | 105 | } |
106 | 106 | \ No newline at end of file |
@@ -50,8 +50,7 @@ |
||
50 | 50 | if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
51 | 51 | try { |
52 | 52 | $compiler->getCore()->getLoader()->loadPlugin('if'); |
53 | - } |
|
54 | - catch (Exception $e) { |
|
53 | + } catch (Exception $e) { |
|
55 | 54 | throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); |
56 | 55 | } |
57 | 56 | } |
@@ -29,31 +29,31 @@ |
||
29 | 29 | */ |
30 | 30 | interface ICompiler |
31 | 31 | { |
32 | - /** |
|
33 | - * Compiles the provided string down to php code. |
|
34 | - * |
|
35 | - * @param Core $core |
|
36 | - * @param ITemplate $template the template to compile |
|
37 | - * |
|
38 | - * @return string a compiled php code string |
|
39 | - */ |
|
40 | - public function compile(Core $core, ITemplate $template); |
|
32 | + /** |
|
33 | + * Compiles the provided string down to php code. |
|
34 | + * |
|
35 | + * @param Core $core |
|
36 | + * @param ITemplate $template the template to compile |
|
37 | + * |
|
38 | + * @return string a compiled php code string |
|
39 | + */ |
|
40 | + public function compile(Core $core, ITemplate $template); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Adds the custom plugins loaded into Dwoo to the compiler so it can load them. |
|
44 | - * |
|
45 | - * @see Core::addPlugin |
|
46 | - * |
|
47 | - * @param array $customPlugins an array of custom plugins |
|
48 | - */ |
|
49 | - public function setCustomPlugins(array $customPlugins); |
|
42 | + /** |
|
43 | + * Adds the custom plugins loaded into Dwoo to the compiler so it can load them. |
|
44 | + * |
|
45 | + * @see Core::addPlugin |
|
46 | + * |
|
47 | + * @param array $customPlugins an array of custom plugins |
|
48 | + */ |
|
49 | + public function setCustomPlugins(array $customPlugins); |
|
50 | 50 | |
51 | - /** |
|
52 | - * Sets the security policy object to enforce some php security settings. |
|
53 | - * use this if untrusted persons can modify templates, |
|
54 | - * set it on the Dwoo object as it will be passed onto the compiler automatically |
|
55 | - * |
|
56 | - * @param SecurityPolicy $policy the security policy object |
|
57 | - */ |
|
58 | - public function setSecurityPolicy(SecurityPolicy $policy = null); |
|
51 | + /** |
|
52 | + * Sets the security policy object to enforce some php security settings. |
|
53 | + * use this if untrusted persons can modify templates, |
|
54 | + * set it on the Dwoo object as it will be passed onto the compiler automatically |
|
55 | + * |
|
56 | + * @param SecurityPolicy $policy the security policy object |
|
57 | + */ |
|
58 | + public function setSecurityPolicy(SecurityPolicy $policy = null); |
|
59 | 59 | } |