sfPluginConfiguration.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfPluginConfiguration represents a configuration for a symfony plugin.
  11. *
  12. * @package symfony
  13. * @subpackage config
  14. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  15. * @version SVN: $Id: sfPluginConfiguration.class.php 13196 2008-11-20 18:23:18Z Kris.Wallsmith $
  16. */
  17. abstract class sfPluginConfiguration
  18. {
  19. protected
  20. $configuration = null,
  21. $dispatcher = null,
  22. $name = null,
  23. $rootDir = null;
  24. /**
  25. * Constructor.
  26. *
  27. * @param sfProjectConfiguration $configuration
  28. * @param string $rootDir The plugin root directory
  29. * @param string $name The plugin name
  30. */
  31. public function __construct(sfProjectConfiguration $configuration, $rootDir = null, $name = null)
  32. {
  33. $this->configuration = $configuration;
  34. $this->dispatcher = $configuration->getEventDispatcher();
  35. $this->rootDir = is_null($rootDir) ? $this->guessRootDir() : realpath($rootDir);
  36. $this->name = is_null($name) ? $this->guessName() : $name;
  37. $this->setup();
  38. $this->configure();
  39. if (!$this->configuration instanceof sfApplicationConfiguration)
  40. {
  41. $this->initializeAutoload();
  42. $this->initialize();
  43. }
  44. }
  45. /**
  46. * Sets up the plugin.
  47. *
  48. * This method can be used when creating a base plugin configuration class for other plugins to extend.
  49. */
  50. public function setup()
  51. {
  52. }
  53. /**
  54. * Configures the plugin.
  55. *
  56. * This method is called before the plugin's classes have been added to sfAutoload.
  57. */
  58. public function configure()
  59. {
  60. }
  61. /**
  62. * Initializes the plugin.
  63. *
  64. * This method is called after the plugin's classes have been added to sfAutoload.
  65. *
  66. * @return boolean|null If false sfApplicationConfiguration will look for a config.php (maintains BC with symfony < 1.2)
  67. */
  68. public function initialize()
  69. {
  70. }
  71. /**
  72. * Returns the plugin root directory.
  73. *
  74. * @return string
  75. */
  76. public function getRootDir()
  77. {
  78. return $this->rootDir;
  79. }
  80. /**
  81. * Returns the plugin name.
  82. *
  83. * @return string
  84. */
  85. public function getName()
  86. {
  87. return $this->name;
  88. }
  89. /**
  90. * Initializes autoloading for the plugin.
  91. *
  92. * This method is called when a plugin is initialized in a project
  93. * configuration. Otherwise, autoload is handled in
  94. * {@link sfApplicationConfiguration} using {@link sfAutoload}.
  95. *
  96. * @see sfSimpleAutoload
  97. */
  98. public function initializeAutoload()
  99. {
  100. $autoload = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
  101. if (is_readable($file = $this->rootDir.'/config/autoload.yml'))
  102. {
  103. $this->configuration->getEventDispatcher()->connect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
  104. $config = new sfAutoloadConfigHandler();
  105. $mappings = $config->evaluate(array($file));
  106. foreach ($mappings as $class => $file)
  107. {
  108. $autoload->setClassPath($class, $file);
  109. }
  110. }
  111. else
  112. {
  113. $autoload->addDirectory($this->rootDir.'/lib');
  114. }
  115. $autoload->register();
  116. }
  117. /**
  118. * Filters sfAutoload configuration values.
  119. *
  120. * @param array $config
  121. *
  122. * @return array
  123. */
  124. public function filterAutoloadConfig(sfEvent $event, array $config)
  125. {
  126. // use array_merge so config is added to the front of the autoload array
  127. if (!isset($config['autoload'][$this->name.'_lib']))
  128. {
  129. $config['autoload'] = array_merge(array(
  130. $this->name.'_lib' => array(
  131. 'path' => $this->rootDir.'/lib',
  132. 'recursive' => true,
  133. ),
  134. ), $config['autoload']);
  135. }
  136. if (!isset($config['autoload'][$this->name.'_module_libs']))
  137. {
  138. $config['autoload'] = array_merge(array(
  139. $this->name.'_module_libs' => array(
  140. 'path' => $this->rootDir.'/modules/*/lib',
  141. 'recursive' => true,
  142. 'prefix' => 1,
  143. ),
  144. ), $config['autoload']);
  145. }
  146. return $config;
  147. }
  148. /**
  149. * Guesses the plugin root directory.
  150. *
  151. * @return string
  152. */
  153. protected function guessRootDir()
  154. {
  155. $r = new ReflectionClass(get_class($this));
  156. return realpath(dirname($r->getFilename()).'/..');
  157. }
  158. /**
  159. * Guesses the plugin name.
  160. *
  161. * @return string
  162. */
  163. protected function guessName()
  164. {
  165. return substr(get_class($this), 0, -13);
  166. }
  167. }