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