PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/Pear.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 297 lines | 200 code | 45 blank | 52 comment | 26 complexity | d2aa295eb2b960fa3ab9a031a1e24c7b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Pear
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Pear package routines
  28. * *
  29. * @category Varien
  30. * @package Varien_Pear
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. // Looks like PEAR is being developed without E_NOTICE (1.7.0RC1)
  34. error_reporting(E_ALL & ~E_NOTICE);
  35. // just a shortcut
  36. if (!defined('DS')) {
  37. define('DS', DIRECTORY_SEPARATOR);
  38. }
  39. // add PEAR lib in include_path if needed
  40. $_includePath = get_include_path();
  41. $_pearDir = dirname(dirname(dirname(__FILE__))) . DS . 'downloader' . DS . 'pearlib';
  42. if (!getenv('PHP_PEAR_INSTALL_DIR')) {
  43. putenv('PHP_PEAR_INSTALL_DIR=' . $_pearDir);
  44. }
  45. $_pearPhpDir = $_pearDir . DS . 'php';
  46. if (strpos($_includePath, $_pearPhpDir) === false) {
  47. if (substr($_includePath, 0, 2) === '.' . PATH_SEPARATOR) {
  48. $_includePath = '.' . PATH_SEPARATOR . $_pearPhpDir . PATH_SEPARATOR . substr($_includePath, 2);
  49. } else {
  50. $_includePath = $_pearPhpDir . PATH_SEPARATOR . $_includePath;
  51. }
  52. set_include_path($_includePath);
  53. }
  54. // include necessary PEAR libs
  55. require_once $_pearPhpDir."/PEAR.php";
  56. require_once $_pearPhpDir."/PEAR/Frontend.php";
  57. require_once $_pearPhpDir."/PEAR/Registry.php";
  58. require_once $_pearPhpDir."/PEAR/Config.php";
  59. require_once $_pearPhpDir."/PEAR/Command.php";
  60. require_once $_pearPhpDir."/PEAR/Exception.php";
  61. require_once dirname(__FILE__)."/Pear/Frontend.php";
  62. require_once dirname(__FILE__)."/Pear/Package.php";
  63. class Varien_Pear
  64. {
  65. protected $_config;
  66. protected $_registry;
  67. protected $_frontend;
  68. protected $_cmdCache = array();
  69. static protected $_instance;
  70. static public $reloadOnRegistryUpdate = true;
  71. public function __construct()
  72. {
  73. $this->getConfig();
  74. }
  75. public function getInstance()
  76. {
  77. if (!self::$_instance) {
  78. self::$_instance = new self;
  79. }
  80. return self::$_instance;
  81. }
  82. public function isSystemPackage($pkg)
  83. {
  84. return in_array($pkg, array('Archive_Tar', 'Console_Getopt', 'PEAR', 'Structures_Graph'));
  85. }
  86. public function getBaseDir()
  87. {
  88. return dirname(dirname(dirname(__FILE__)));
  89. }
  90. public function getPearDir()
  91. {
  92. return $this->getBaseDir().DS.'downloader'.DS.'pearlib';
  93. }
  94. public function getConfig()
  95. {
  96. if (!$this->_config) {
  97. $pear_dir = $this->getPearDir();
  98. $config = PEAR_Config::singleton($pear_dir.DS.'pear.ini', '-');
  99. $config->set('auto_discover', 1);
  100. $config->set('cache_ttl', 60);
  101. #$config->set('preferred_state', 'beta');
  102. $config->set('bin_dir', $pear_dir);
  103. $config->set('php_dir', $pear_dir.DS.'php');
  104. $config->set('download_dir', $pear_dir.DS.'download');
  105. $config->set('temp_dir', $pear_dir.DS.'temp');
  106. $config->set('data_dir', $pear_dir.DS.'data');
  107. $config->set('cache_dir', $pear_dir.DS.'cache');
  108. $config->set('test_dir', $pear_dir.DS.'tests');
  109. $config->set('doc_dir', $pear_dir.DS.'docs');
  110. $mageDir = $config->get('mage_dir');
  111. foreach ($config->getKeys() as $key) {
  112. if (!(substr($key, 0, 5)==='mage_' && substr($key, -4)==='_dir')) {
  113. continue;
  114. }
  115. $config->set($key, preg_replace('#^'.preg_quote($mageDir).'#', $this->getBaseDir(), $config->get($key)));
  116. #echo $key.' : '.$config->get($key).'<br>';
  117. }
  118. $reg = $this->getRegistry();
  119. $config->setRegistry($reg);
  120. PEAR_DependencyDB::singleton($config, $pear_dir.DS.'php'.DS.'.depdb');
  121. PEAR_Frontend::setFrontendObject($this->getFrontend());
  122. PEAR_Command::registerCommands(false, $pear_dir.DS.'php'.DS.'PEAR'.DS.'Command'.DS);
  123. $this->_config = $config;
  124. }
  125. return $this->_config;
  126. }
  127. public function getMagentoChannels()
  128. {
  129. return array('connect.magentocommerce.com/core', 'connect.magentocommerce.com/community');
  130. }
  131. public function getRegistry($redirectOnChange=true)
  132. {
  133. if (!$this->_registry) {
  134. $this->_registry = new Varien_Pear_Registry($this->getPearDir().DS.'php');
  135. $changed = false;
  136. foreach ($this->getMagentoChannels() as $channel) {
  137. if (!$this->getRegistry()->channelExists($channel)) {
  138. $this->run('channel-discover', array(), array($channel));
  139. $changed = true;
  140. }
  141. }
  142. if ($changed) {
  143. $this->_registry = new Varien_Pear_Registry($this->getPearDir().DS.'php');
  144. }
  145. // if ($changed && self::$reloadOnRegistryUpdate && empty($_GET['pear_registry'])) {
  146. // echo "TEST:";
  147. // echo self::$reloadOnRegistryUpdate;
  148. // //TODO:refresh registry in memory to reflect discovered channels without redirect
  149. // #header("Location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'].'&pear_registry=1');
  150. // exit;
  151. // }
  152. }
  153. return $this->_registry;
  154. }
  155. public function getFrontend()
  156. {
  157. if (!$this->_frontend) {
  158. $this->_frontend = new Varien_Pear_Frontend;
  159. }
  160. return $this->_frontend;
  161. }
  162. public function getLog()
  163. {
  164. return $this->getFrontend()->getLog();
  165. }
  166. public function getOutput()
  167. {
  168. return $this->getFrontend()->getOutput();
  169. }
  170. public function run($command, $options=array(), $params=array())
  171. {
  172. @set_time_limit(0);
  173. @ini_set('memory_limit', '256M');
  174. if (empty($this->_cmdCache[$command])) {
  175. $cmd = PEAR_Command::factory($command, $this->getConfig());
  176. if ($cmd instanceof PEAR_Error) {
  177. return $cmd;
  178. }
  179. $this->_cmdCache[$command] = $cmd;
  180. } else {
  181. $cmd = $this->_cmdCache[$command];
  182. }
  183. #$cmd = PEAR_Command::factory($command, $this->getConfig());
  184. $result = $cmd->run($command, $options, $params);
  185. return $result;
  186. }
  187. public function setRemoteConfig($uri) #$host, $user, $password, $path='', $port=null)
  188. {
  189. #$uri = 'ftp://' . $user . ':' . $password . '@' . $host . (is_numeric($port) ? ':' . $port : '') . '/' . trim($path, '/') . '/';
  190. $this->run('config-set', array(), array('remote_config', $uri));
  191. return $this;
  192. }
  193. /**
  194. * Run PEAR command with html output console style
  195. *
  196. * @param array|Varien_Object $runParams command, options, params,
  197. * comment, success_callback, failure_callback
  198. */
  199. public function runHtmlConsole($runParams)
  200. {
  201. ob_implicit_flush();
  202. $fe = $this->getFrontend();
  203. $oldLogStream = $fe->getLogStream();
  204. $fe->setLogStream('stdout');
  205. if ($runParams instanceof Varien_Object) {
  206. $run = $runParams;
  207. } elseif (is_array($runParams)) {
  208. $run = new Varien_Object($runParams);
  209. } elseif (is_string($runParams)) {
  210. $run = new Varien_Object(array('title'=>$runParams));
  211. } else {
  212. throw Varien_Exception("Invalid run parameters");
  213. }
  214. ?>
  215. <html><head><style type="text/css">
  216. body { margin:0px; padding:3px; background:black; color:white; }
  217. pre { font:normal 11px Courier New, serif; color:#2EC029; }
  218. </style></head><body>
  219. <?php
  220. echo "<pre>".$run->getComment();
  221. if ($command = $run->getCommand()) {
  222. $result = $this->run($command, $run->getOptions(), $run->getParams());
  223. if ($result instanceof PEAR_Error) {
  224. echo "\r\n\r\nPEAR ERROR: ".$result->getMessage();
  225. }
  226. echo '</pre><script type="text/javascript">';
  227. if ($result instanceof PEAR_Error) {
  228. if ($callback = $run->getFailureCallback()) {
  229. call_user_func_array($callback, array($result));
  230. }
  231. } else {
  232. if ($callback = $run->getSuccessCallback()) {
  233. call_user_func_array($callback, array($result));
  234. }
  235. }
  236. echo '</script>';
  237. } else {
  238. $result = false;
  239. echo '</pre>';
  240. }
  241. ?>
  242. <script type="text/javascript">
  243. if (!auto_scroll) {
  244. var auto_scroll = window.setInterval("document.body.scrollTop+=2", 10);
  245. }
  246. </script>
  247. </body></html>
  248. <?php
  249. $fe->setLogStream($oldLogStream);
  250. return $result;
  251. }
  252. }