PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/dwoo-old/Dwoo/Loader.php

https://github.com/jonathanslenders/balancirk-website
PHP | 147 lines | 76 code | 11 blank | 60 comment | 17 complexity | 68ca4bfe6d49748c9a9ebff0bfad92c1 MD5 | raw file
  1. <?php
  2. /**
  3. * handles plugin loading and caching of plugins names/paths relationships
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * In no event will the authors be held liable for any damages arising from the use of this software.
  7. *
  8. * @author Jordi Boggiano <j.boggiano@seld.be>
  9. * @copyright Copyright (c) 2008, Jordi Boggiano
  10. * @license http://dwoo.org/LICENSE Modified BSD License
  11. * @link http://dwoo.org/
  12. * @version 1.1.0
  13. * @date 2009-07-18
  14. * @package Dwoo
  15. */
  16. class Dwoo_Loader implements Dwoo_ILoader
  17. {
  18. /**
  19. * stores the plugin directories
  20. *
  21. * @see addDirectory
  22. * @var array
  23. */
  24. protected $paths = array();
  25. /**
  26. * stores the plugins names/paths relationships
  27. * don't edit this on your own, use addDirectory
  28. *
  29. * @see addDirectory
  30. * @var array
  31. */
  32. protected $classPath = array();
  33. /**
  34. * path where class paths cache files are written
  35. *
  36. * @var string
  37. */
  38. protected $cacheDir;
  39. protected $corePluginDir;
  40. public function __construct($cacheDir)
  41. {
  42. $this->corePluginDir = DWOO_DIRECTORY . 'plugins';
  43. $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  44. // include class paths or rebuild paths if the cache file isn't there
  45. $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php';
  46. if (file_exists($cacheFile)) {
  47. $classpath = file_get_contents($cacheFile);
  48. $this->classPath = unserialize($classpath) + $this->classPath;
  49. } else {
  50. $this->rebuildClassPathCache($this->corePluginDir, $cacheFile);
  51. }
  52. }
  53. /**
  54. * rebuilds class paths, scans the given directory recursively and saves all paths in the given file
  55. *
  56. * @param string $path the plugin path to scan
  57. * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten
  58. */
  59. protected function rebuildClassPathCache($path, $cacheFile)
  60. {
  61. if ($cacheFile!==false) {
  62. $tmp = $this->classPath;
  63. $this->classPath = array();
  64. }
  65. // iterates over all files/folders
  66. $list = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*');
  67. if (is_array($list)) {
  68. foreach ($list as $f) {
  69. if (is_dir($f)) {
  70. $this->rebuildClassPathCache($f, false);
  71. } else {
  72. $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f;
  73. }
  74. }
  75. }
  76. // save in file if it's the first call (not recursed)
  77. if ($cacheFile!==false) {
  78. if (!file_put_contents($cacheFile, serialize($this->classPath))) {
  79. throw new Dwoo_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()');
  80. }
  81. $this->classPath += $tmp;
  82. }
  83. }
  84. /**
  85. * loads a plugin file
  86. *
  87. * @param string $class the plugin name, without the Dwoo_Plugin_ prefix
  88. * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it has just been added, defaults to true
  89. */
  90. public function loadPlugin($class, $forceRehash = true)
  91. {
  92. // a new class was added or the include failed so we rebuild the cache
  93. if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) {
  94. if ($forceRehash) {
  95. $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php');
  96. foreach ($this->paths as $path=>$file) {
  97. $this->rebuildClassPathCache($path, $file);
  98. }
  99. if (isset($this->classPath[$class])) {
  100. include $this->classPath[$class];
  101. } else {
  102. throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE);
  103. }
  104. } else {
  105. throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE);
  106. }
  107. }
  108. }
  109. /**
  110. * adds a plugin directory, the plugins found in the new plugin directory
  111. * will take precedence over the other directories (including the default
  112. * dwoo plugin directory), you can use this for example to override plugins
  113. * in a specific directory for a specific application while keeping all your
  114. * usual plugins in the same place for all applications.
  115. *
  116. * TOCOM don't forget that php functions overrides are not rehashed so you
  117. * need to clear the classpath caches by hand when adding those
  118. *
  119. * @param string $pluginDirectory the plugin path to scan
  120. */
  121. public function addDirectory($pluginDirectory)
  122. {
  123. $pluginDir = realpath($pluginDirectory);
  124. if (!$pluginDir) {
  125. throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory);
  126. }
  127. $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo::RELEASE_TAG.'.php';
  128. $this->paths[$pluginDir] = $cacheFile;
  129. if (file_exists($cacheFile)) {
  130. $classpath = file_get_contents($cacheFile);
  131. $this->classPath = unserialize($classpath) + $this->classPath;
  132. } else {
  133. $this->rebuildClassPathCache($pluginDir, $cacheFile);
  134. }
  135. }
  136. }