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

Debug toolbar