PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/macro/src/compiler/lmbMacroFilterDictionary.class.php

http://github.com/limb-php-framework/limb
PHP | 103 lines | 71 code | 18 blank | 14 comment | 9 complexity | 51a130423d204a2b4d183d32695692b0 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * class lmbMacroFilterDictionary.
  11. *
  12. * @package macro
  13. * @version $Id$
  14. */
  15. class lmbMacroFilterDictionary
  16. {
  17. protected $info = array();
  18. protected $cache_dir;
  19. static protected $instance;
  20. static function instance()
  21. {
  22. if(self :: $instance)
  23. return self :: $instance;
  24. self :: $instance = new lmbMacroFilterDictionary();
  25. return self :: $instance;
  26. }
  27. function load(lmbMacroConfig $config)
  28. {
  29. $this->cache_dir = $config->cache_dir;
  30. if(!$config->forcescan && $this->_loadCache())
  31. return;
  32. //compatibility with PHP 5.1.6
  33. $filters_scan_dirs = $config->filters_scan_dirs;
  34. foreach($filters_scan_dirs as $dir)
  35. {
  36. foreach(lmb_glob($dir . '/*.filter.php') as $file)
  37. $this->registerFromFile($file);
  38. }
  39. $this->_saveCache();
  40. }
  41. protected function _loadCache()
  42. {
  43. $cache_file = $this->cache_dir . '/filters.cache';
  44. if(!file_exists($cache_file))
  45. return false;
  46. $info = @unserialize(file_get_contents($cache_file));
  47. if($info === false || !is_array($info))
  48. return false;
  49. $this->info = $info;
  50. return true;
  51. }
  52. protected function _saveCache()
  53. {
  54. $cache_file = $this->cache_dir . '/filters.cache';
  55. lmbFs :: safeWrite($cache_file, serialize($this->info));
  56. }
  57. function register($filter_info)
  58. {
  59. $names = array(strtolower($filter_info->getName()));
  60. $aliases = $filter_info->getAliases();
  61. if(count($aliases))
  62. {
  63. $aliases = array_map('strtolower', $aliases);
  64. $names = array_merge($names, $aliases);
  65. }
  66. foreach($names as $filter_name)
  67. {
  68. if(isset($this->info[$filter_name]))
  69. return;
  70. $this->info[$filter_name] = $filter_info;
  71. }
  72. }
  73. function registerFromFile($file)
  74. {
  75. $infos = lmbMacroAnnotationParser :: extractFromFile($file, 'lmbMacroFilterInfo');
  76. foreach($infos as $info)
  77. $this->register($info, $file);
  78. }
  79. function findFilterInfo($name)
  80. {
  81. $name = strtolower($name);
  82. if(isset($this->info[$name]))
  83. return $this->info[$name];
  84. }
  85. }