1. sfPluginConfiguration.class.php
  2. /** * sfPluginConfiguration represents a configuration for a symfony plugin. * * @package symfony * @subpackage config * @author Kris Wallsmith * @version SVN: $Id: sfPluginConfiguration.class.php 23822 2009-11-12 15:13:48Z Kris.Wallsmith $ */
  3. abstract class sfPluginConfiguration
  4. {
  5. protected
  6. $configuration = null,
  7. $dispatcher = null,
  8. $name = null,
  9. $rootDir = null;
  10. /**
  11. * Constructor.
  12. *
  13. * @param sfProjectConfiguration $configuration The project configuration
  14. * @param string $rootDir The plugin root directory
  15. * @param string $name The plugin name
  16. */
  17. public function __construct(sfProjectConfiguration $configuration, $rootDir = null, $name = null)
  18. {
  19. $this->configuration = $configuration;
  20. $this->dispatcher = $configuration->getEventDispatcher();
  21. $this->rootDir = null === $rootDir ? $this->guessRootDir() : realpath($rootDir);
  22. $this->name = null === $name ? $this->guessName() : $name;
  23. $this->setup();
  24. $this->configure();
  25. if (!$this->configuration instanceof sfApplicationConfiguration)
  26. {
  27. $this->initializeAutoload();
  28. $this->initialize();
  29. }
  30. }
  31. /**
  32. * Sets up the plugin.
  33. *
  34. * This method can be used when creating a base plugin configuration class for other plugins to extend.
  35. */
  36. public function setup()
  37. {
  38. }
  39. /**
  40. * Configures the plugin.
  41. *
  42. * This method is called before the plugin's classes have been added to sfAutoload.
  43. */
  44. public function configure()
  45. {
  46. }
  47. /**
  48. * Initializes the plugin.
  49. *
  50. * This method is called after the plugin's classes have been added to sfAutoload.
  51. *
  52. * @return boolean|null If false sfApplicationConfiguration will look for a config.php (maintains BC with symfony < 1.2)
  53. */
  54. public function initialize()
  55. {
  56. }
  57. /**
  58. * Returns the plugin root directory.
  59. *
  60. * @return string
  61. */
  62. public function getRootDir()
  63. {
  64. return $this->rootDir;
  65. }
  66. /**
  67. * Returns the plugin name.
  68. *
  69. * @return string
  70. */
  71. public function getName()
  72. {
  73. return $this->name;
  74. }
  75. /**
  76. * Initializes autoloading for the plugin.
  77. *
  78. * This method is called when a plugin is initialized in a project
  79. * configuration. Otherwise, autoload is handled in
  80. * {@link sfApplicationConfiguration} using {@link sfAutoload}.
  81. *
  82. * @see sfSimpleAutoload
  83. */
  84. public function initializeAutoload()
  85. {
  86. $autoload = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
  87. if (is_readable($file = $this->rootDir.'/config/autoload.yml'))
  88. {
  89. $this->configuration->getEventDispatcher()->connect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
  90. $autoload->loadConfiguration(array($file));
  91. $this->configuration->getEventDispatcher()->disconnect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
  92. }
  93. else
  94. {
  95. $autoload->addDirectory($this->rootDir.'/lib');
  96. }
  97. $autoload->register();
  98. }
  99. /**
  100. * Filters sfAutoload configuration values.
  101. *
  102. * @param sfEvent $event
  103. * @param array $config
  104. *
  105. * @return array
  106. */
  107. public function filterAutoloadConfig(sfEvent $event, array $config)
  108. {
  109. // use array_merge so config is added to the front of the autoload array
  110. if (!isset($config['autoload'][$this->name.'_lib']))
  111. {
  112. $config['autoload'] = array_merge(array(
  113. $this->name.'_lib' => array(
  114. 'path' => $this->rootDir.'/lib',
  115. 'recursive' => true,
  116. ),
  117. ), $config['autoload']);
  118. }
  119. if (!isset($config['autoload'][$this->name.'_module_libs']))
  120. {
  121. $config['autoload'] = array_merge(array(
  122. $this->name.'_module_libs' => array(
  123. 'path' => $this->rootDir.'/modules/*/lib',
  124. 'recursive' => true,
  125. 'prefix' => 1,
  126. ),
  127. ), $config['autoload']);
  128. }
  129. return $config;
  130. }
  131. /**
  132. * Connects the current plugin's tests to the "test:*" tasks.
  133. */
  134. public function connectTests()
  135. {
  136. $this->dispatcher->connect('task.test.filter_test_files', array($this, 'filterTestFiles'));
  137. }
  138. /**
  139. * Listens for the "task.test.filter_test_files" event and adds tests from the current plugin.
  140. *
  141. * @param sfEvent $event
  142. * @param array $files
  143. *
  144. * @return array An array of files with the appropriate tests from the current plugin merged in
  145. */
  146. public function filterTestFiles(sfEvent $event, $files)
  147. {
  148. $task = $event->getSubject();
  149. if ($task instanceof sfTestAllTask)
  150. {
  151. $directory = $this->rootDir.'/test';
  152. $names = array();
  153. }
  154. else if ($task instanceof sfTestFunctionalTask)
  155. {
  156. $directory = $this->rootDir.'/test/functional';
  157. $names = $event['arguments']['controller'];
  158. }
  159. else if ($task instanceof sfTestUnitTask)
  160. {
  161. $directory = $this->rootDir.'/test/unit';
  162. $names = $event['arguments']['name'];
  163. }
  164. if (!count($names))
  165. {
  166. $names = array('*');
  167. }
  168. foreach ($names as $name)
  169. {
  170. $finder = sfFinder::type('file')->follow_link()->name(basename($name).'Test.php');
  171. $files = array_merge($files, $finder->in($directory.'/'.dirname($name)));
  172. }
  173. return array_unique($files);
  174. }
  175. /**
  176. * Guesses the plugin root directory.
  177. *
  178. * @return string
  179. */
  180. protected function guessRootDir()
  181. {
  182. $r = new ReflectionClass(get_class($this));
  183. return realpath(dirname($r->getFilename()).'/..');
  184. }
  185. /**
  186. * Guesses the plugin name.
  187. *
  188. * @return string
  189. */
  190. protected function guessName()
  191. {
  192. return substr(get_class($this), 0, -13);
  193. }
  194. }

Debug toolbar