PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/macro/src/compiler/lmbMacroTagDictionary.class.php

http://github.com/limb-php-framework/limb
PHP | 125 lines | 36 code | 8 blank | 81 comment | 3 complexity | 87d4da1417aa3a16e209b159aaf9c295 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 lmbMacroTagDictionary.
  11. *
  12. * @package macro
  13. * @version $Id$
  14. */
  15. class lmbMacroTagDictionary
  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 lmbMacroTagDictionary();
  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. $real_scan_dirs = array();
  33. //compatibility with PHP 5.1.6
  34. $tag_scan_dirs = $config->tags_scan_dirs;
  35. foreach($tag_scan_dirs as $dir)
  36. {
  37. foreach($this->_getThisAndImmediateDirectories($dir) as $item)
  38. $real_scan_dirs[] = $item;
  39. }
  40. foreach($real_scan_dirs as $scan_dir)
  41. {
  42. foreach(lmb_glob($scan_dir . '/*.tag.php') as $file)
  43. $this->registerFromFile($file);
  44. }
  45. $this->_saveCache();
  46. }
  47. function _getThisAndImmediateDirectories($dir)
  48. {
  49. $dirs = array();
  50. foreach(lmb_glob("$dir/*") as $item) {
  51. if($item != '.' && $item != '..' && is_dir($item))
  52. $dirs[] = $item;
  53. }
  54. $dirs[] = $dir;
  55. return $dirs;
  56. }
  57. protected function _loadCache()
  58. {
  59. $cache_file = $this->cache_dir . '/tags.cache';
  60. if(!file_exists($cache_file))
  61. return false;
  62. $info = @unserialize(file_get_contents($cache_file));
  63. if($info === false || !is_array($info))
  64. return false;
  65. $this->info = $info;
  66. return true;
  67. }
  68. protected function _saveCache()
  69. {
  70. lmb_require('limb/fs/src/lmbFs.class.php');
  71. $cache_file = $this->cache_dir . '/tags.cache';
  72. lmbFs :: safeWrite($cache_file, serialize($this->info));
  73. }
  74. function register($tag_info)
  75. {
  76. $names = array(strtolower($tag_info->getTag()));
  77. $aliases = $tag_info->getAliases();
  78. if(count($aliases))
  79. {
  80. $aliases = array_map('strtolower', $aliases);
  81. $names = array_merge($names, $aliases);
  82. }
  83. foreach($names as $tag_name)
  84. {
  85. if(isset($this->info[$tag_name]))
  86. return;
  87. $this->info[$tag_name] = $tag_info;
  88. }
  89. }
  90. function registerFromFile($file)
  91. {
  92. $infos = lmbMacroAnnotationParser :: extractFromFile($file, 'lmbMacroTagInfo');
  93. foreach($infos as $info)
  94. $this->register($info, $file);
  95. }
  96. function findTagInfo($tag)
  97. {
  98. $tag = strtolower($tag);
  99. if(isset($this->info[$tag]))
  100. return $this->info[$tag];
  101. }
  102. }