sfPearEnvironment.class.php 7.0 KB

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