PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Mage/Connect/Command/Install.php

https://gitlab.com/blingbang2016/shop
PHP | 448 lines | 319 code | 63 blank | 66 comment | 74 complexity | 77605d20bbe0fde7bfae95b19b4b18bb 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 Mage
  22. * @package Mage_Connect
  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. final class Mage_Connect_Command_Install
  27. extends Mage_Connect_Command
  28. {
  29. /**
  30. * Install action callback
  31. * @param string $command
  32. * @param array $options
  33. * @param array $params
  34. * @return void
  35. */
  36. public function doInstall($command, $options, $params, $objects = array())
  37. {
  38. $this->cleanupParams($params);
  39. $installFileMode = $command === 'install-file';
  40. try {
  41. $packager = $this->getPackager();
  42. $forceMode = isset($options['force']);
  43. $upgradeAllMode = $command == 'upgrade-all';
  44. $upgradeMode = $command == 'upgrade' || $command == 'upgrade-all';
  45. $noFilesInstall = isset($options['nofiles']);
  46. $withDepsMode = !isset($options['nodeps']);
  47. $ignoreModifiedMode = true || !isset($options['ignorelocalmodification']);
  48. $rest = $this->rest();
  49. $ftp = empty($options['ftp']) ? false : $options['ftp'];
  50. if($ftp) {
  51. list($cache, $config, $ftpObj) = $packager->getRemoteConf($ftp);
  52. } else {
  53. $config = $this->config();
  54. $cache = $this->getSconfig();
  55. }
  56. if($installFileMode) {
  57. if(count($params) < 1) {
  58. throw new Exception("Argument should be: filename");
  59. }
  60. $filename = $params[0];
  61. if(!@file_exists($filename)) {
  62. throw new Exception("File '{$filename}' not found");
  63. }
  64. if(!@is_readable($filename)) {
  65. throw new Exception("File '{$filename}' is not readable");
  66. }
  67. $package = new Mage_Connect_Package($filename);
  68. $package->validate();
  69. $errors = $package->getErrors();
  70. if(count($errors)) {
  71. throw new Exception("Package file is invalid\n".implode("\n", $errors));
  72. }
  73. $pChan = $package->getChannel();
  74. $pName = $package->getName();
  75. $pVer = $package->getVersion();
  76. if(!$cache->isChannel($pChan)) {
  77. throw new Exception("'{$pChan}' is not installed channel");
  78. }
  79. $conflicts = $cache->hasConflicts($pChan, $pName, $pVer);
  80. if(false !== $conflicts) {
  81. $conflicts = implode(", ",$conflicts);
  82. if($forceMode) {
  83. $this->doError($command, "Package {$pChan}/{$pName} {$pVer} conflicts with: ".$conflicts);
  84. } else {
  85. throw new Exception("Package {$pChan}/{$pName} {$pVer} conflicts with: ".$conflicts);
  86. }
  87. }
  88. $conflicts = $package->checkPhpDependencies();
  89. if(true !== $conflicts) {
  90. $confilcts = implode(",",$conflicts);
  91. $err = "Package {$pChan}/{$pName} {$pVer} depends on PHP extensions: ".$conflicts;
  92. if($forceMode) {
  93. $this->doError($command, $err);
  94. } else {
  95. throw new Exception($err);
  96. }
  97. }
  98. $conflicts = $package->checkPhpVersion();
  99. if(true !== $conflicts) {
  100. $err = "Package {$pChan}/{$pName} {$pVer}: ".$conflicts;
  101. if($forceMode) {
  102. $this->doError($command, $err);
  103. } else {
  104. throw new Exception($err);
  105. }
  106. }
  107. if(!$noFilesInstall) {
  108. if($ftp) {
  109. $packager->processInstallPackageFtp($package, $filename, $config, $ftpObj);
  110. } else {
  111. $packager->processInstallPackage($package, $filename, $config);
  112. }
  113. }
  114. $cache->addPackage($package);
  115. $installedDeps = array();
  116. $installedDepsAssoc = array();
  117. $installedDepsAssoc[] = array('channel'=>$pChan, 'name'=>$pName, 'version'=>$pVer);
  118. $installedDeps[] = array($pChan, $pName, $pVer);
  119. $title = isset($options['title']) ? $options['title'] : "Package installed: ";
  120. $out = array($command => array('data'=>$installedDeps, 'assoc'=>$installedDepsAssoc, 'title'=>$title));
  121. if($ftp) {
  122. $packager->writeToRemoteCache($cache, $ftpObj);
  123. @unlink($config->getFilename());
  124. }
  125. $this->ui()->output($out);
  126. return $out[$command]['data'];
  127. }
  128. if(!$upgradeAllMode) {
  129. if(count($params) < 2) {
  130. throw new Exception("Argument should be: channelName packageName");
  131. }
  132. $channel = $params[0];
  133. $package = $params[1];
  134. $argVersionMax = isset($params[2]) ? $params[2]: false;
  135. $argVersionMin = false;
  136. if($cache->isChannelName($channel)) {
  137. $uri = $cache->chanUrl($channel);
  138. } elseif($this->validator()->validateUrl($channel)) {
  139. $uri = $channel;
  140. } elseif($channel) {
  141. $uri = $config->protocol.'://'.$channel;
  142. } else {
  143. throw new Exception("'{$channel}' is not existant channel name / valid uri");
  144. }
  145. if($uri && !$cache->isChannel($uri)) {
  146. $rest->setChannel($uri);
  147. $data = $rest->getChannelInfo();
  148. $data->uri = $uri;
  149. $cache->addChannel($data->name, $uri);
  150. $this->ui()->output("Successfully added channel: ".$uri);
  151. }
  152. $channelName = $cache->chanName($channel);
  153. //var_dump($channelName);
  154. $packagesToInstall = $packager->getDependenciesList( $channelName, $package, $cache, $config, $argVersionMax, $argVersionMin, $withDepsMode);
  155. $packagesToInstall = $packagesToInstall['result'];
  156. //var_dump($packagesToInstall);
  157. } else {
  158. if(empty($params[0])) {
  159. $channels = $cache->getChannelNames();
  160. } else {
  161. $channel = $params[0];
  162. if(!$cache->isChannel($channel)) {
  163. throw new Exception("'{$channel}' is not existant channel name / valid uri");
  164. }
  165. $channels = $cache->chanName($channel);
  166. }
  167. $packagesToInstall = array();
  168. $neededToUpgrade = $packager->getUpgradesList($channels, $cache, $config);
  169. foreach($neededToUpgrade as $chan=>$packages) {
  170. foreach($packages as $name=>$data) {
  171. $versionTo = $data['to'];
  172. $tmp = $packager->getDependenciesList( $chan, $name, $cache, $config, $versionTo, $versionTo, $withDepsMode);
  173. if(count($tmp['result'])) {
  174. $packagesToInstall = array_merge($packagesToInstall, $tmp['result']);
  175. }
  176. }
  177. }
  178. }
  179. /**
  180. * Make installation
  181. */
  182. $installedDeps = array();
  183. $installedDepsAssoc = array();
  184. $keys = array();
  185. foreach($packagesToInstall as $package) {
  186. try {
  187. $pName = $package['name'];
  188. $pChan = $package['channel'];
  189. $pVer = $package['downloaded_version'];
  190. $rest->setChannel($cache->chanUrl($pChan));
  191. /**
  192. * Upgrade mode
  193. */
  194. if($upgradeMode && $cache->hasPackage($pChan, $pName, $pVer, $pVer)) {
  195. $this->ui()->output("Already installed: {$pChan}/{$pName} {$pVer}, skipping");
  196. continue;
  197. }
  198. $conflicts = $cache->hasConflicts($pChan, $pName, $pVer);
  199. if(false !== $conflicts) {
  200. $conflicts = implode(", ",$conflicts);
  201. if($forceMode) {
  202. $this->doError($command, "Package {$pChan}/{$pName} {$pVer} conflicts with: ".$conflicts);
  203. } else {
  204. throw new Exception("Package {$pChan}/{$pName} {$pVer} conflicts with: ".$conflicts);
  205. }
  206. }
  207. /**
  208. * Modifications
  209. */
  210. if ($upgradeMode && !$ignoreModifiedMode) {
  211. if($ftp) {
  212. $modifications = $packager->getRemoteModifiedFiles($pChan, $pName, $cache, $config, $ftp);
  213. } else {
  214. $modifications = $packager->getLocalModifiedFiles($pChan, $pName, $cache, $config);
  215. }
  216. if (count($modifications) > 0) {
  217. $this->ui()->output('Changed locally: ');
  218. foreach ($modifications as $row) {
  219. if(!$ftp) {
  220. $this->ui()->output($config->magento_root.DS.$row);
  221. } else {
  222. $this->ui()->output($row);
  223. }
  224. }
  225. /*$this->ui()->confirm('Do you want rewrite all files?');
  226. continue;*/
  227. }
  228. }
  229. $dir = $config->getChannelCacheDir($pChan);
  230. @mkdir($dir, 0777, true);
  231. $file = $dir.DIRECTORY_SEPARATOR.$pName."-".$pVer.".tgz";
  232. if(!@file_exists($file)) {
  233. $rest->downloadPackageFileOfRelease($pName, $pVer, $file);
  234. }
  235. $package = new Mage_Connect_Package($file);
  236. $conflicts = $package->checkPhpDependencies();
  237. if(true !== $conflicts) {
  238. $confilcts = implode(",",$conflicts);
  239. $err = "Package {$pChan}/{$pName} {$pVer} depends on PHP extensions: ".$conflicts;
  240. if($forceMode) {
  241. $this->doError($command, $err);
  242. } else {
  243. throw new Exception($err);
  244. }
  245. }
  246. $conflicts = $package->checkPhpVersion();
  247. if(true !== $conflicts) {
  248. $err = "Package {$pChan}/{$pName} {$pVer}: ".$conflicts;
  249. if($forceMode) {
  250. $this->doError($command, $err);
  251. } else {
  252. throw new Exception($err);
  253. }
  254. }
  255. if(!$noFilesInstall) {
  256. if($ftp) {
  257. $packager->processInstallPackageFtp($package, $file, $config, $ftpObj);
  258. } else {
  259. $packager->processInstallPackage($package, $file, $config);
  260. }
  261. }
  262. $cache->addPackage($package);
  263. $installedDepsAssoc[] = array('channel'=>$pChan, 'name'=>$pName, 'version'=>$pVer);
  264. $installedDeps[] = array($pChan, $pName, $pVer);
  265. } catch(Exception $e) {
  266. $this->doError($command, $e->getMessage());
  267. }
  268. }
  269. $title = isset($options['title']) ? $options['title'] : "Package installed: ";
  270. $out = array($command => array('data'=>$installedDeps, 'assoc'=>$installedDepsAssoc, 'title'=>$title));
  271. if($ftp) {
  272. $packager->writeToRemoteCache($cache, $ftpObj);
  273. @unlink($config->getFilename());
  274. }
  275. $this->ui()->output($out);
  276. return $out[$command]['data'];
  277. } catch (Exception $e) {
  278. if($ftp) {
  279. $packager->writeToRemoteCache($cache, $ftpObj);
  280. @unlink($config->getFilename());
  281. }
  282. return $this->doError($command, $e->getMessage());
  283. }
  284. }
  285. /**
  286. * Upgrade action callback
  287. * @param string $command
  288. * @param array $options
  289. * @param array $params
  290. * @return void
  291. */
  292. public function doUpgrade($command, $options, $params)
  293. {
  294. $options['title'] = "Package upgraded: ";
  295. return $this->doInstall($command, $options, $params);
  296. }
  297. /**
  298. * Updgrade action callback
  299. * @param string $command
  300. * @param array $options
  301. * @param array $params
  302. * @return void
  303. */
  304. public function doUpgradeAll($command, $options, $params)
  305. {
  306. $options['title'] = "Package upgraded: ";
  307. return $this->doInstall($command, $options, $params);
  308. }
  309. /**
  310. * Uninstall package callback
  311. * @param string $command
  312. * @param array $options
  313. * @param array $params
  314. * @return unknown_type
  315. */
  316. public function doUninstall($command, $options, $params)
  317. {
  318. $this->cleanupParams($params);
  319. //$this->splitPackageArgs($params);
  320. try {
  321. if(count($params) != 2) {
  322. throw new Exception("Argument count should be = 2");
  323. }
  324. $channel = $params[0];
  325. $package = $params[1];
  326. $packager = $this->getPackager();
  327. $withDepsMode = !isset($options['nodeps']);
  328. $forceMode = isset($options['force']);
  329. $ftp = empty($options['ftp']) ? false : $options['ftp'];
  330. if($ftp) {
  331. list($cache, $config, $ftpObj) = $packager->getRemoteConf($ftp);
  332. } else {
  333. $cache = $this->getSconfig();
  334. $config = $this->config();
  335. }
  336. $chan = $cache->getChannel($channel);
  337. $channel = $cache->chanName($channel);
  338. if(!$cache->hasPackage($channel, $package)) {
  339. throw new Exception("Package is not installed");
  340. }
  341. $deletedPackages = array();
  342. $list = $packager->getUninstallList($channel, $package, $cache, $config, $withDepsMode);
  343. foreach($list['list'] as $packageData) {
  344. try {
  345. $reqd = $cache->requiredByOtherPackages($packageData['channel'], $packageData['name'], $list['list']);
  346. if(count($reqd)) {
  347. $errMessage = "{$packageData['channel']}/{$packageData['name']} {$packageData['version']} is required by: ";
  348. $t = array();
  349. foreach($reqd as $r) {
  350. $t[] = $r['channel']."/".$r['name']. " ".$r['version'];
  351. }
  352. $errMessage .= implode(", ", $t);
  353. if($forceMode) {
  354. $this->ui()->output("Warning: ".$errMessage);
  355. } else {
  356. throw new Exception($errMessage);
  357. }
  358. }
  359. list($chan, $pack) = array($packageData['channel'], $packageData['name']);
  360. if($ftp) {
  361. $packager->processUninstallPackageFtp($chan, $pack, $cache, $config, $ftp);
  362. } else {
  363. $packager->processUninstallPackage($chan, $pack, $cache, $config);
  364. }
  365. $cache->deletePackage($chan, $pack);
  366. $deletedPackages[] = array($chan, $pack);
  367. } catch(Exception $e) {
  368. if($forceMode) {
  369. $this->doError($command, $e->getMessage());
  370. } else {
  371. throw new Exception($e->getMessage());
  372. }
  373. }
  374. }
  375. if($ftp) {
  376. $packager->writeToRemoteCache($cache, $ftpObj);
  377. @unlink($config->getFilename());
  378. }
  379. $out = array($command=>array('data'=>$deletedPackages, 'title'=>'Package deleted: '));
  380. $this->ui()->output($out);
  381. } catch (Exception $e) {
  382. return $this->doError($command, $e->getMessage());
  383. }
  384. }
  385. }