PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/project/library/Oxy/Compiler/Abstract.php

http://oxybase.googlecode.com/
PHP | 402 lines | 177 code | 53 blank | 172 comment | 7 complexity | e37f38928124374c54dda511cd7847cd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once 'Oxy/Compiler/Text/Filter/Manager.php';
  3. abstract class Oxy_Compiler_Abstract
  4. {
  5. /**
  6. * Directory which contents will be compiled into one file
  7. *
  8. * @var String
  9. */
  10. private $str_compile_dir;
  11. /**
  12. * Compile directory path offset
  13. *
  14. * @var String
  15. */
  16. private $str_compile_dir_offset;
  17. /**
  18. * Base compile dir, which is used as a first part of Namespace name
  19. *
  20. * @var string
  21. */
  22. private $str_base_compile_dir;
  23. /**
  24. * Name of a compiled file
  25. * @var String
  26. */
  27. private $str_compiled_filename;
  28. /**
  29. * Variable which holds data in memory until it's time to write data to file
  30. * @var String
  31. */
  32. private $str_code_chunk;
  33. /**
  34. * Max size of a text in memory
  35. * @var Integer
  36. */
  37. private $int_code_chunk_limit;
  38. /**
  39. * Filter manager object that will take care of text processing
  40. * @var Text_Filter_Manager
  41. */
  42. private $obj_filters_manager;
  43. /**
  44. * List of whitelisted modules.
  45. *
  46. * @var array
  47. */
  48. private $arr_module_whitelist = array();
  49. /**
  50. * List of blacklisted modules
  51. *
  52. * @var array
  53. */
  54. private $arr_module_blacklist = array();
  55. /**
  56. * Check if whitelist mode started - at least one module is in whitelist
  57. *
  58. * @return boolean
  59. */
  60. protected function isWhitelistMode()
  61. {
  62. return (count($this->arr_module_whitelist) > 0);
  63. }
  64. /**
  65. * Check if blacklist mode started - at least one module is in blacklist
  66. *
  67. * @return boolean
  68. */
  69. protected function isBlacklistMode()
  70. {
  71. return (count($this->arr_module_blacklist) > 0);
  72. }
  73. /**
  74. * Add module to whitelist.
  75. *
  76. * @param string|array $mix_module_name Module name or an array of module names
  77. * @return none
  78. */
  79. public function addWhitelistItem($mix_module_name)
  80. {
  81. if (is_string($mix_module_name))
  82. {
  83. $mix_module_name = array($mix_module_name);
  84. }
  85. elseif (!is_array($mix_module_name))
  86. {
  87. throw new Exception('Add whitelist module: string or array expected');
  88. }
  89. $arr_module_names = array();
  90. foreach ($mix_module_name as $str_module_name)
  91. {
  92. $arr_module_names[$str_module_name] = $str_module_name;
  93. }
  94. $this->arr_module_whitelist = array_merge($this->arr_module_whitelist, $arr_module_names);
  95. }
  96. /**
  97. * Add module to blacklist.
  98. *
  99. * @param string|array $mix_module_name Module name or an array of module names
  100. * @return none
  101. */
  102. public function addBlacklistItem($mix_module_name)
  103. {
  104. if (is_string($mix_module_name))
  105. {
  106. $mix_module_name = array($mix_module_name);
  107. }
  108. elseif (!is_array($mix_module_name))
  109. {
  110. throw new Exception('Add whitelist module: string or array expected');
  111. }
  112. $arr_module_names = array();
  113. foreach ($mix_module_name as $str_module_name)
  114. {
  115. $arr_module_names[$str_module_name] = $str_module_name;
  116. }
  117. $this->arr_module_blacklist = array_merge($this->arr_module_whitelist, $arr_module_names);
  118. }
  119. /**
  120. * Get whitelisted modules
  121. *
  122. * @return array
  123. */
  124. public function getWhitelist()
  125. {
  126. return $this->arr_module_whitelist;
  127. }
  128. /**
  129. * Get blacklisted modules
  130. *
  131. * @return array
  132. */
  133. public function getBlacklist()
  134. {
  135. return $this->arr_module_blacklist;
  136. }
  137. /**
  138. *
  139. * @param String $str_root_directory Directory which contents will be compiled
  140. */
  141. public function __construct($str_root_directory)
  142. {
  143. $int_current_memory_limit = (int)ini_get('memory_limit'); // Memory limit in Mb
  144. $this->setMaxChunkSize(1024 * 1024 * $int_current_memory_limit * 0.5);
  145. $this->obj_filters_manager = new Oxy_Compiler_Text_Filter_Manager();
  146. $arr_path_parts = explode('/', $str_root_directory);
  147. $this->str_base_compile_dir = array_pop($arr_path_parts);
  148. $this->str_compile_dir_offset = ($arr_path_parts) ? implode('/', $arr_path_parts) : '';
  149. $this->createAutoCompiledFilename($this->str_base_compile_dir);
  150. $this->setCompileDirectory($str_root_directory);
  151. }
  152. /**
  153. * Creates automatic compiled file name based on specified root directory name
  154. * @param $str_root_directory Directory, which contents will be compiled, name
  155. */
  156. private function createAutoCompiledFilename($str_root_directory)
  157. {
  158. $str_filename = str_replace(array('/', ' ', '-', '_', '.'), '', $str_root_directory);
  159. $str_filename = strtolower($str_filename);
  160. $str_filename = $str_filename . '.compiled.php';
  161. $this->setCompiledFilename($str_filename);
  162. }
  163. /**
  164. * Get text that currently exists in memory
  165. * @return String
  166. */
  167. private function getCodeChunk()
  168. {
  169. return $this->str_code_chunk;
  170. }
  171. /**
  172. * Resets text in memory with a text provided
  173. * @param String $str_code_chunk String Text to set to memory
  174. */
  175. private function setCodeChunk($str_code_chunk)
  176. {
  177. $this->str_code_chunk = $str_code_chunk;
  178. }
  179. /**
  180. * Get the size of text that is currently in the memory
  181. * @return Integer Text size
  182. */
  183. private function getCodeChunkSize()
  184. {
  185. return strlen($this->str_code_chunk);
  186. }
  187. /**
  188. * Add text to memory
  189. * @param String $str_code_chunk Text to add to memory
  190. */
  191. private function addCodeChunk($str_code_chunk)
  192. {
  193. $this->str_code_chunk .= $str_code_chunk;
  194. }
  195. /**
  196. * Do text processing with a filters specified in registerFilters method or by specifying needed filter as parameter.
  197. * @param String $str_contents Text to be processed
  198. * @param Array $arr_filters Array of filters that will be applied to text
  199. * @return String Processed contents
  200. */
  201. protected function processContents($str_contents, $arr_filters = null)
  202. {
  203. // If filters were not specified inline, then process text with filters set as a main filters
  204. if ($arr_filters === null)
  205. {
  206. return $this->obj_filters_manager->process($str_contents);
  207. }
  208. // ... if the filters were specified inline, process text using them
  209. elseif (Text_Filter_Manager::filtersListValid($arr_filters))
  210. {
  211. $obj_filters_manager = new Text_Filter_Manager();
  212. $obj_filters_manager->addFilters($arr_filters);
  213. return $obj_filters_manager->process($str_contents);
  214. }
  215. // ... otherwise don't process text
  216. else
  217. {
  218. return $str_contents;
  219. }
  220. }
  221. /**
  222. * Writes text to file
  223. * @param String $str_contents Text to write
  224. * @param Boolean $bl_append Should the text be appended (true) or should it overwrite existing contents (false)
  225. * @param Boolean $bl_do_text_processing Should the text be processed
  226. */
  227. protected function writeToFile($str_contents, $bl_append = true, $bl_do_text_processing = false)
  228. {
  229. $int_flags = $bl_append ? FILE_APPEND : 0;
  230. $str_contents = $bl_do_text_processing ? $this->processCode($str_contents) : $str_contents;
  231. file_put_contents($this->getCompiledFilename(), $str_contents, $int_flags);
  232. }
  233. /**
  234. * Forces to write out text that is in the memory to the file
  235. */
  236. protected function flushContents()
  237. {
  238. $this->writeToFile($this->getCodeChunk());
  239. }
  240. /**
  241. * Add code to compiled file or memory, depending on whether it's possible to put it on memory or not
  242. * @param String $str_code Text to add
  243. * @param Boolean $bl_do_filtering Should text filtering be done on provided text
  244. */
  245. protected function addCode($str_code, $bl_do_filtering = true)
  246. {
  247. // Do contents processing if required
  248. if ($bl_do_filtering)
  249. {
  250. $str_code = $this->processContents($str_code);
  251. }
  252. // Write code that exists in memory to file, if we have reached memory limit
  253. if ($this->getCodeChunkSize() + strlen($str_code) > $this->int_code_chunk_limit)
  254. {
  255. $this->writeToFile($this->getCodeChunk());
  256. $this->setCodeChunk($str_code);
  257. }
  258. // ... otherwise just add text into memory
  259. else
  260. {
  261. $this->addCodeChunk($str_code);
  262. }
  263. }
  264. /**
  265. * Set maximum text chunk size
  266. * @param Integer $int_max_size Max text size in memory
  267. */
  268. public function setMaxChunkSize($int_max_size)
  269. {
  270. $this->int_code_chunk_limit = (int) $int_max_size;
  271. }
  272. /**
  273. * Set compiled file name
  274. * @param String $str_filename File name
  275. */
  276. public function setCompiledFilename($str_filename)
  277. {
  278. $this->str_compiled_filename = $str_filename;
  279. }
  280. /**
  281. * Set directory, which contents will be be compiled, name
  282. * @param String $str_root_directory Directory name
  283. */
  284. public function setCompileDirectory($str_root_directory)
  285. {
  286. $this->str_compile_dir = $str_root_directory;
  287. $this->createAutoCompiledFilename($str_root_directory);
  288. }
  289. /**
  290. * Get directory, which contents will be compiled, base name
  291. *
  292. * @return string
  293. */
  294. public function getCompileDirectoryBase()
  295. {
  296. return ($this->str_base_compile_dir == '') ? '' : $this->str_base_compile_dir;
  297. }
  298. /**
  299. * Get directory, which contents will be compiled, path offset
  300. *
  301. * @return string
  302. */
  303. public function getCompileDirectoryOffset()
  304. {
  305. return ($this->str_compile_dir_offset == '') ? '' : $this->str_compile_dir_offset . '/';
  306. }
  307. /**
  308. * Get directory, which contents will be compiled, name
  309. * @return String
  310. */
  311. public function getCompileDirectory()
  312. {
  313. return $this->str_compile_dir;
  314. }
  315. /**
  316. * Registers filters that will be invoked during code processing
  317. * @param Array $arr_filters List of filter objects that complies to Text_Filter_Interface
  318. */
  319. public function registerFilters($arr_filters)
  320. {
  321. $this->obj_filters_manager->addFilters($arr_filters);
  322. }
  323. /**
  324. * Get compiled file name
  325. * @return String
  326. */
  327. public function getCompiledFilename()
  328. {
  329. return $this->str_compiled_filename;
  330. }
  331. /**
  332. * Main method that will compile all file into one
  333. */
  334. abstract public function compile($bl_compress_contents = false);
  335. /**
  336. * Check if given class is whitelisted
  337. *
  338. * @param string $str_class_name Class name to check
  339. * @return boolean Is whitelisted
  340. */
  341. abstract protected function isWhitelisted($str_item_name);
  342. /**
  343. * Check if given class is blacklisted
  344. *
  345. * @param string $str_class_name Class name to check
  346. * @return boolean Is blacklisted
  347. */
  348. abstract protected function isBlacklisted($str_item_name);
  349. }