00001 <?php
00002
00003 error_reporting(E_ALL & ~E_NOTICE);
00004
00005
00006 if (!defined('DS')) {
00007 define('DS', DIRECTORY_SEPARATOR);
00008 }
00009
00010
00011 $_includePath = get_include_path();
00012 $_pearDir = dirname(dirname(dirname(__FILE__))) . DS . 'downloader' . DS . 'pearlib';
00013 if (!getenv('PHP_PEAR_INSTALL_DIR')) {
00014 putenv('PHP_PEAR_INSTALL_DIR=' . $_pearDir);
00015 }
00016 $_pearPhpDir = $_pearDir . DS . 'php';
00017 if (strpos($_includePath, $_pearPhpDir) === false) {
00018 if (substr($_includePath, 0, 2) === '.' . PATH_SEPARATOR) {
00019 $_includePath = '.' . PATH_SEPARATOR . $_pearPhpDir . PATH_SEPARATOR . substr($_includePath, 2);
00020 } else {
00021 $_includePath = $_pearPhpDir . PATH_SEPARATOR . $_includePath;
00022 }
00023 set_include_path($_includePath);
00024 }
00025
00026
00027 require_once $_pearPhpDir."/PEAR.php";
00028 require_once $_pearPhpDir."/PEAR/Frontend.php";
00029 require_once $_pearPhpDir."/PEAR/Registry.php";
00030 require_once $_pearPhpDir."/PEAR/Config.php";
00031 require_once $_pearPhpDir."/PEAR/Command.php";
00032 require_once $_pearPhpDir."/PEAR/Exception.php";
00033
00034 require_once dirname(__FILE__)."/Pear/Frontend.php";
00035 require_once dirname(__FILE__)."/Pear/Package.php";
00036
00037 class Varien_Pear
00038 {
00039 protected $_config;
00040
00041 protected $_registry;
00042
00043 protected $_frontend;
00044
00045 protected $_cmdCache = array();
00046
00047 static protected $_instance;
00048
00049 static public $reloadOnRegistryUpdate = true;
00050
00051 public function __construct()
00052 {
00053 $this->getConfig();
00054 }
00055
00056 public function getInstance()
00057 {
00058 if (!self::$_instance) {
00059 self::$_instance = new self;
00060 }
00061 return self::$_instance;
00062 }
00063
00064 public function isSystemPackage($pkg)
00065 {
00066 return in_array($pkg, array('Archive_Tar', 'Console_Getopt', 'PEAR', 'Structures_Graph'));
00067 }
00068
00069 public function getBaseDir()
00070 {
00071 return dirname(dirname(dirname(__FILE__)));
00072 }
00073
00074 public function getPearDir()
00075 {
00076 return $this->getBaseDir().DS.'downloader'.DS.'pearlib';
00077 }
00078
00079 public function getConfig()
00080 {
00081 if (!$this->_config) {
00082 $pear_dir = $this->getPearDir();
00083
00084 $config = PEAR_Config::singleton($pear_dir.DS.'pear.ini', '-');
00085
00086 $config->set('auto_discover', 1);
00087 $config->set('cache_ttl', 60);
00088 #$config->set('preferred_state', 'beta');
00089
00090 $config->set('bin_dir', $pear_dir);
00091 $config->set('php_dir', $pear_dir.DS.'php');
00092 $config->set('download_dir', $pear_dir.DS.'download');
00093 $config->set('temp_dir', $pear_dir.DS.'temp');
00094 $config->set('data_dir', $pear_dir.DS.'data');
00095 $config->set('cache_dir', $pear_dir.DS.'cache');
00096 $config->set('test_dir', $pear_dir.DS.'tests');
00097 $config->set('doc_dir', $pear_dir.DS.'docs');
00098
00099 $mageDir = $config->get('mage_dir');
00100
00101 foreach ($config->getKeys() as $key) {
00102 if (!(substr($key, 0, 5)==='mage_' && substr($key, -4)==='_dir')) {
00103 continue;
00104 }
00105 $config->set($key, preg_replace('#^'.preg_quote($mageDir).'#', $this->getBaseDir(), $config->get($key)));
00106 #echo $key.' : '.$config->get($key).'<br>';
00107 }
00108
00109 $reg = $this->getRegistry();
00110 $config->setRegistry($reg);
00111
00112 PEAR_DependencyDB::singleton($config, $pear_dir.DS.'php'.DS.'.depdb');
00113
00114 PEAR_Frontend::setFrontendObject($this->getFrontend());
00115
00116 PEAR_Command::registerCommands(false, $pear_dir.DS.'php'.DS.'PEAR'.DS.'Command'.DS);
00117
00118 $this->_config = $config;
00119 }
00120 return $this->_config;
00121 }
00122
00123 public function getMagentoChannels()
00124 {
00125 return array('connect.magentocommerce.com/core', 'connect.magentocommerce.com/community');
00126 }
00127
00128 public function getRegistry($redirectOnChange=true)
00129 {
00130 if (!$this->_registry) {
00131 $this->_registry = new Varien_Pear_Registry($this->getPearDir().DS.'php');
00132
00133 $changed = false;
00134 foreach ($this->getMagentoChannels() as $channel) {
00135 if (!$this->getRegistry()->channelExists($channel)) {
00136 $this->run('channel-discover', array(), array($channel));
00137 $changed = true;
00138 }
00139 }
00140
00141 if ($changed) {
00142 $this->_registry = new Varien_Pear_Registry($this->getPearDir().DS.'php');
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 }
00152 return $this->_registry;
00153 }
00154
00155 public function getFrontend()
00156 {
00157 if (!$this->_frontend) {
00158 $this->_frontend = new Varien_Pear_Frontend;
00159 }
00160 return $this->_frontend;
00161 }
00162
00163 public function getLog()
00164 {
00165 return $this->getFrontend()->getLog();
00166 }
00167
00168 public function getOutput()
00169 {
00170 return $this->getFrontend()->getOutput();
00171 }
00172
00173 public function run($command, $options=array(), $params=array())
00174 {
00175 @set_time_limit(0);
00176 @ini_set('memory_limit', '256M');
00177
00178 if (empty($this->_cmdCache[$command])) {
00179 $cmd = PEAR_Command::factory($command, $this->getConfig());
00180 if ($cmd instanceof PEAR_Error) {
00181 return $cmd;
00182 }
00183 $this->_cmdCache[$command] = $cmd;
00184 } else {
00185 $cmd = $this->_cmdCache[$command];
00186 }
00187 #$cmd = PEAR_Command::factory($command, $this->getConfig());
00188 $result = $cmd->run($command, $options, $params);
00189 return $result;
00190 }
00191
00192 public function setRemoteConfig($uri) #$host, $user, $password, $path='', $port=null)
00193 {
00194 #$uri = 'ftp://' . $user . ':' . $password . '@' . $host . (is_numeric($port) ? ':' . $port : '') . '/' . trim($path, '/') . '/';
00195 $this->run('config-set', array(), array('remote_config', $uri));
00196 return $this;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 public function runHtmlConsole($runParams)
00206 {
00207 ob_implicit_flush();
00208
00209 $fe = $this->getFrontend();
00210 $oldLogStream = $fe->getLogStream();
00211 $fe->setLogStream('stdout');
00212
00213 if ($runParams instanceof Varien_Object) {
00214 $run = $runParams;
00215 } elseif (is_array($runParams)) {
00216 $run = new Varien_Object($runParams);
00217 } elseif (is_string($runParams)) {
00218 $run = new Varien_Object(array('title'=>$runParams));
00219 } else {
00220 throw Varien_Exception("Invalid run parameters");
00221 }
00222 ?>
00223 <html><head><style type="text/css">
00224 body { margin:0px; padding:3px; background:black; color:white; }
00225 pre { font:normal 11px Courier New, serif; color:#2EC029; }
00226 </style></head><body>
00227 <?php
00228 echo "<pre>".$run->getComment();
00229
00230 if ($command = $run->getCommand()) {
00231 $result = $this->run($command, $run->getOptions(), $run->getParams());
00232
00233 if ($result instanceof PEAR_Error) {
00234 echo "\r\n\r\nPEAR ERROR: ".$result->getMessage();
00235 }
00236 echo '</pre><script type="text/javascript">';
00237 if ($result instanceof PEAR_Error) {
00238 if ($callback = $run->getFailureCallback()) {
00239 call_user_func_array($callback, array($result));
00240 }
00241 } else {
00242 if ($callback = $run->getSuccessCallback()) {
00243 call_user_func_array($callback, array($result));
00244 }
00245 }
00246 echo '</script>';
00247 } else {
00248 $result = false;
00249
00250 echo '</pre>';
00251 }
00252 ?>
00253 <script type="text/javascript">
00254 if (!auto_scroll) {
00255 var auto_scroll = window.setInterval("document.body.scrollTop+=2", 10);
00256 }
00257 </script>
00258 </body></html>
00259 <?php
00260 $fe->setLogStream($oldLogStream);
00261
00262 return $result;
00263 }
00264 }