sfPearEnvironment.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. // Remove E_STRICT from error_reporting
  10. error_reporting(error_reporting() & ~E_STRICT);
  11. date_default_timezone_set('UTC');
  12. require_once 'PEAR.php';
  13. require_once 'PEAR/Config.php';
  14. require_once 'PEAR/Registry.php';
  15. require_once 'PEAR/Command.php';
  16. require_once 'PEAR/PackageFile/v2/rw.php';
  17. require_once 'PEAR/Dependency2.php';
  18. require_once 'PEAR/Installer.php';
  19. /**
  20. * sfPearEnvironment represents a PEAR environment.
  21. *
  22. * @package symfony
  23. * @subpackage plugin
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  25. * @version SVN: $Id: sfPearEnvironment.class.php 19914 2009-07-06 10:06:16Z fabien $
  26. */
  27. class sfPearEnvironment
  28. {
  29. protected
  30. $dispatcher = null,
  31. $config = null,
  32. $registry = null,
  33. $rest = null,
  34. $frontend = null,
  35. $options = array();
  36. /**
  37. * Constructs a new sfPluginManager.
  38. *
  39. * @param sfEventDispatcher $dispatcher An event dispatcher instance
  40. * @param array $options An array of options
  41. */
  42. public function __construct(sfEventDispatcher $dispatcher, $options)
  43. {
  44. $this->initialize($dispatcher, $options);
  45. }
  46. /**
  47. * Initializes this sfPluginManager instance.
  48. *
  49. * Available options:
  50. *
  51. * * plugin_dir: The directory where to put plugins
  52. * * cache_dir: The local PEAR cache directory
  53. * * rest_base_class: The base class for REST calls (default to sfPearRest)
  54. * (mainly used for testing)
  55. * * downloader_base_class: The base class for downloads (default to sfPearDownloader)
  56. * (mainly used for testing)
  57. *
  58. * @param sfEventDispatcher $dispatcher An event dispatcher instance
  59. * @param array $options An array of options
  60. */
  61. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  62. {
  63. $this->dispatcher = $dispatcher;
  64. // initialize options
  65. if (!isset($options['plugin_dir']))
  66. {
  67. throw new sfConfigurationException('You must provide a "plugin_dir" option.');
  68. }
  69. if (!isset($options['cache_dir']))
  70. {
  71. throw new sfConfigurationException('You must provide a "cache_dir" option.');
  72. }
  73. if (!is_dir($options['cache_dir']))
  74. {
  75. mkdir($options['cache_dir'], 0777, true);
  76. }
  77. if (!isset($options['rest_base_class']))
  78. {
  79. $options['rest_base_class'] = 'sfPearRest';
  80. }
  81. if (!isset($options['downloader_base_class']))
  82. {
  83. $options['downloader_base_class'] = 'sfPearDownloader';
  84. }
  85. $this->options = $options;
  86. // initialize some PEAR objects
  87. $this->initializeConfiguration($options['plugin_dir'], $options['cache_dir']);
  88. $this->initializeRegistry();
  89. $this->initializeFrontend();
  90. // initializes the REST object
  91. $this->rest = new sfPearRestPlugin($this->config, array('base_class' => $options['rest_base_class']));
  92. $this->rest->setChannel($this->config->get('default_channel'));
  93. }
  94. /**
  95. * Returns a configuration value.
  96. *
  97. * @param string $name The configuration name
  98. *
  99. * @return mixed The configuration value
  100. */
  101. public function getOption($name)
  102. {
  103. return isset($this->options[$name]) ? $this->options[$name] : null;
  104. }
  105. /**
  106. * Returns whether configuration name exists.
  107. *
  108. * @param string $name The configuration name
  109. *
  110. * @return boolean True if configuration name exists
  111. */
  112. public function hasOption($name)
  113. {
  114. return isset($this->options[$name]);
  115. }
  116. /**
  117. * Sets a configuration value.
  118. *
  119. * @param string $name The configuration name
  120. * @param mixed $value The configuration value
  121. */
  122. public function setOption($name, $value)
  123. {
  124. $this->options[$name] = $value;
  125. }
  126. /**
  127. * Returns the PEAR Rest instance.
  128. *
  129. * @return object The PEAR Rest instance
  130. */
  131. public function getRest()
  132. {
  133. return $this->rest;
  134. }
  135. /**
  136. * Returns the PEAR Config instance.
  137. *
  138. * @return object The PEAR Config instance
  139. */
  140. public function getConfig()
  141. {
  142. return $this->config;
  143. }
  144. /**
  145. * Returns the PEAR Frontend instance.
  146. *
  147. * @return object The PEAR Frontend instance
  148. */
  149. public function getFrontend()
  150. {
  151. return $this->frontend;
  152. }
  153. /**
  154. * Returns the PEAR Registry instance.
  155. *
  156. * @return object The PEAR Registry instance
  157. */
  158. public function getRegistry()
  159. {
  160. return $this->registry;
  161. }
  162. /**
  163. * Registers a PEAR channel.
  164. *
  165. * @param string $channel The channel name
  166. * @param Boolean $isDefault true if this is the default PEAR channel, false otherwise
  167. */
  168. public function registerChannel($channel, $isDefault = false)
  169. {
  170. $this->config->set('auto_discover', true);
  171. if (!$this->registry->channelExists($channel, true))
  172. {
  173. $class = $this->options['downloader_base_class'];
  174. $downloader = new $class($this->frontend, array(), $this->config);
  175. if (!$downloader->discover($channel))
  176. {
  177. throw new sfPluginException(sprintf('Unable to register channel "%s"', $channel));
  178. }
  179. }
  180. if ($isDefault)
  181. {
  182. $this->config->set('default_channel', $channel);
  183. $this->rest->setChannel($channel);
  184. }
  185. }
  186. /**
  187. * Initializes the PEAR Frontend instance.
  188. */
  189. protected function initializeFrontend()
  190. {
  191. $this->frontend = PEAR_Frontend::singleton('sfPearFrontendPlugin');
  192. if (PEAR::isError($this->frontend))
  193. {
  194. throw new sfPluginException(sprintf('Unable to initialize PEAR Frontend object: %s', $this->frontend->getMessage()));
  195. }
  196. $this->frontend->setEventDispatcher($this->dispatcher);
  197. }
  198. /**
  199. * Initializes the PEAR Registry instance.
  200. */
  201. protected function initializeRegistry()
  202. {
  203. $this->registry = $this->config->getRegistry();
  204. if (PEAR::isError($this->registry))
  205. {
  206. throw new sfPluginException(sprintf('Unable to initialize PEAR registry: %s', $this->registry->getMessage()));
  207. }
  208. }
  209. /**
  210. * Registers the PEAR Configuration instance.
  211. *
  212. * @param string $pluginDir The plugin path
  213. * @param string $cacheDir The cache path
  214. */
  215. public function initializeConfiguration($pluginDir, $cacheDir)
  216. {
  217. $this->config = PEAR_Config::singleton();
  218. // change the configuration for use
  219. $this->config->set('php_dir', $pluginDir);
  220. $this->config->set('data_dir', $pluginDir);
  221. $this->config->set('test_dir', $pluginDir);
  222. $this->config->set('doc_dir', $pluginDir);
  223. $this->config->set('bin_dir', $pluginDir);
  224. if($this->hasOption('preferred_state'))
  225. {
  226. $this->config->set('preferred_state', $this->getOption('preferred_state'));
  227. }
  228. // change the PEAR temp dirs
  229. $this->config->set('cache_dir', $cacheDir);
  230. $this->config->set('download_dir', $cacheDir);
  231. $this->config->set('temp_dir', $cacheDir);
  232. $this->config->set('verbose', 1);
  233. }
  234. }