PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/downloader/lib/Magento/Connect/Frontend/CLI.php

https://github.com/jonathanselander/magento2
PHP | 458 lines | 273 code | 43 blank | 142 comment | 23 complexity | b6b0d4d0f1d19e7af39b8e79a308a5cc MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Unlicense
  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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package Magento_Connect
  23. * @copyright Copyright (c) 2013 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * CLI Frontend implementation
  28. *
  29. * @category Magento
  30. * @package Magento_Connect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. namespace Magento\Connect\Frontend;
  34. class CLI
  35. extends \Magento\Connect\Frontend
  36. {
  37. /**
  38. * Collected output
  39. * @var array
  40. */
  41. protected $_output = array();
  42. /**
  43. * Output error
  44. * @param string $command
  45. * @param string $message
  46. * @return void
  47. */
  48. public function doError($command, $message)
  49. {
  50. parent::doError($command, $message);
  51. $this->writeln("Error: ");
  52. $this->writeln("$command: $message");
  53. }
  54. /**
  55. * Output config help
  56. * @param array $data
  57. * @return void
  58. */
  59. public function outputConfigHelp($data)
  60. {
  61. foreach($data['data'] as $k=>$v) {
  62. if(is_scalar($v)) {
  63. $this->writeln($v);
  64. } elseif(is_array($v)) {
  65. $this->writeln(implode(": ", $v));
  66. }
  67. }
  68. }
  69. /**
  70. * Output info
  71. * @param array $data
  72. * @return void
  73. */
  74. public function outputRemoteInfo($data)
  75. {
  76. if(!is_array($data['releases'])) {
  77. return;
  78. }
  79. foreach ($data['releases'] as $r) {
  80. $this->writeln(implode(" ", $r));
  81. }
  82. }
  83. public function detectMethodByType($type)
  84. {
  85. $defaultMethod = "output";
  86. $methodMap = array(
  87. 'list-upgrades'=> 'outputUpgrades',
  88. 'list-available' => 'outputChannelsPackages',
  89. 'list-installed' => 'writeInstalledList',
  90. 'package-dependencies' => 'outputPackageDeps',
  91. 'package-prepare' => 'outputPackagePrepare',
  92. 'list-files' => 'outputPackageContents',
  93. 'config-help' => 'outputConfigHelp',
  94. 'info' => 'outputRemoteInfo',
  95. 'config-show' => 'outputConfig',
  96. 'install' => 'outputInstallResult',
  97. 'install-file' => 'outputInstallResult',
  98. 'upgrade' => 'outputInstallResult',
  99. 'upgrade-all' => 'outputInstallResult',
  100. 'uninstall' => 'outputDeleted',
  101. 'list-channels' => 'outputListChannels',
  102. );
  103. if(isset($methodMap[$type])) {
  104. return $methodMap[$type];
  105. }
  106. return $defaultMethod;
  107. }
  108. public function outputDeleted($data)
  109. {
  110. if(!count($data['data'])) {
  111. return;
  112. }
  113. $this->writeln($data['title']);
  114. foreach($data['data'] as $row) {
  115. $this->writeln("$row[0]/$row[1]");
  116. }
  117. }
  118. public function outputListChannels($data)
  119. {
  120. $this->writeln($data['title']);
  121. $channels =& $data['data'][\Magento\Connect\Singleconfig::K_CHAN];
  122. foreach($channels as $name => $v) {
  123. $this->writeln("$name: {$v[\Magento\Connect\Singleconfig::K_URI]}");
  124. }
  125. $aliases =& $data['data'][\Magento\Connect\Singleconfig::K_CHAN_ALIAS];
  126. if(count($aliases)) {
  127. $this->writeln();
  128. $this->writeln($data['title_aliases']);
  129. foreach($aliases as $k=>$v) {
  130. $this->writeln("$k => $v");
  131. }
  132. }
  133. }
  134. /**
  135. * Output install result
  136. * @param array $data
  137. * @return void
  138. */
  139. public function outputInstallResult($data)
  140. {
  141. if(isset($data['title'])) {
  142. $title = trim($data['title'])." ";
  143. } else {
  144. $title = '';
  145. }
  146. foreach($data['assoc'] as $row) {
  147. $this->printf("%s%s/%s %s\n", $title, $row['channel'], $row['name'], $row['version']);
  148. }
  149. }
  150. /**
  151. * Ouptut package contents
  152. * @param array $data
  153. * @return void
  154. */
  155. public function outputPackageContents($data)
  156. {
  157. $this->writeln($data['title']);
  158. foreach($data['data'] as $file) {
  159. $this->writeln($file);
  160. }
  161. }
  162. /**
  163. * Output package dependencies
  164. * @param $data
  165. * @return void
  166. */
  167. public function outputPackageDeps($data)
  168. {
  169. $title = $data['title'];
  170. $this->writeln($title);
  171. foreach($data['data'] as $package) {
  172. $this->printf("%-20s %-20s %-20s %-20s\n", $package['channel'], $package['name'], $package['min'], $package['max']);
  173. }
  174. }
  175. /**
  176. * Output package prepare
  177. * @param $data
  178. * @return void
  179. */
  180. public function outputPackagePrepare($data)
  181. {
  182. $title = $data['title'];
  183. $this->writeln($title);
  184. foreach($data['data'] as $package) {
  185. $this->printf("%-20s %-20s %-20s %-20s\n", $package['channel'], $package['name'], $package['version'], $package['install_state']);
  186. }
  187. }
  188. /**
  189. * Ouptut channel packages
  190. * @param $data
  191. * @return unknown_type
  192. */
  193. public function outputChannelsPackages($data)
  194. {
  195. foreach($data['data'] as $channelInfo) {
  196. $title =& $channelInfo['title'];
  197. $packages =& $channelInfo['packages'];
  198. $this->writeln($title);
  199. foreach($packages as $name=>$package) {
  200. $releases =& $package['releases'];
  201. $tmp = array();
  202. foreach($releases as $ver=>$state) {
  203. $tmp[] = "$ver $state";
  204. }
  205. $tmp = implode(',',$tmp);
  206. $this->writeln($name.": ".$tmp);
  207. }
  208. }
  209. }
  210. /**
  211. * Make output
  212. *
  213. * @param array $data
  214. * @return void
  215. */
  216. public function output($data)
  217. {
  218. $capture = $this->isCapture();
  219. if($capture) {
  220. $this->_output[] = $data;
  221. return;
  222. }
  223. if(is_array($data)) {
  224. foreach($data as $type=>$params) {
  225. $method = $this->detectMethodByType($type);
  226. if($method) {
  227. $this->$method($params);
  228. } else {
  229. $this->writeln(__METHOD__." handler not found for {$type}");
  230. }
  231. }
  232. } else {
  233. $this->writeln($data);
  234. }
  235. }
  236. /**
  237. * Detailed package info
  238. * @param \Magento\Connect\Package $package
  239. * @return void
  240. */
  241. public function outputPackage($package)
  242. {
  243. $fields = array(
  244. 'Name'=>'name',
  245. 'Version'=>'version',
  246. 'Stability'=>'stability',
  247. 'Description' => 'description',
  248. 'Date' => 'date',
  249. 'Authors' => 'authors',
  250. );
  251. foreach($fields as $title => $fld) {
  252. $method = "get".ucfirst($fld);
  253. $data = $package->$method();
  254. if(empty($data)) {
  255. continue;
  256. }
  257. $this->write($title.": ");
  258. if(is_array($data)) {
  259. $this->write(print_r($data,true));
  260. } else {
  261. $this->write($data);
  262. }
  263. $this->writeln('');
  264. }
  265. }
  266. /**
  267. * Write channels list
  268. * @param array $data
  269. * @return void
  270. */
  271. public function writeChannelsList($data)
  272. {
  273. $this->writeln("Channels available: ");
  274. $this->writeln("===================");
  275. $out = $data['byName'];
  276. ksort($out);
  277. foreach($out as $k=>$v) {
  278. $this->printf ("%-20s %-20s\n", $k, $v);
  279. }
  280. }
  281. /**
  282. * Write installed list
  283. * @param array $data
  284. * @return void
  285. */
  286. public function writeInstalledList($data)
  287. {
  288. $totalCount = 0;
  289. foreach($data['data'] as $channel=>$packages) {
  290. $title = sprintf($data['channel-title'], $channel);
  291. $c = count($packages);
  292. $totalCount += $c;
  293. if(!$c) {
  294. continue;
  295. }
  296. $this->writeln($title);
  297. foreach($packages as $name=>$row) {
  298. $this->printf("%-20s %-20s\n", $name, $row['version']." ".$row['stability']);
  299. }
  300. }
  301. if($totalCount === 0) {
  302. $this->writeln("No installed packages");
  303. }
  304. }
  305. /**
  306. * Output commands list
  307. * @param array $data
  308. * @return void
  309. */
  310. public function outputCommandList($data)
  311. {
  312. $this->writeln("Connect commands available:");
  313. $this->writeln("===========================");
  314. foreach ($data as $k=>$v) {
  315. $this->printf ("%-20s %-20s\n", $k, $v['summary']);
  316. }
  317. }
  318. /**
  319. * Output config
  320. * @param array $data
  321. * @return void
  322. */
  323. public function outputConfig($data)
  324. {
  325. foreach($data['data'] as $name=>$row) {
  326. $value = $row['value'] === '' ? "<not set>" : strval($row['value']);
  327. $this->printf("%-30s %-20s %-20s\n", $row['prompt'], $name, $value);
  328. }
  329. }
  330. /**
  331. * Output config variable
  332. * @param string $key
  333. * @param string $value
  334. * @return void
  335. */
  336. public function outputConfigVariable($key, $value)
  337. {
  338. if($value === '') {
  339. $value = '<not set>';
  340. }
  341. $this->writeln("Config variable '{$key}': {$value}");
  342. }
  343. /**
  344. * Write data and "\n" afterwards
  345. * @param string $data
  346. * @return void
  347. */
  348. public function writeln($data = '')
  349. {
  350. $this->write($data."\n");
  351. }
  352. /**
  353. * get output, clear if needed
  354. *
  355. * @param bool $clearPrevoius optional, true by default
  356. * @return array
  357. */
  358. public function getOutput($clearPrevious = true)
  359. {
  360. $out = $this->_output;
  361. if($clearPrevious) {
  362. $this->_output = array();
  363. }
  364. return $out;
  365. }
  366. /**
  367. * Write data to console
  368. * @param string $data
  369. * @return void
  370. */
  371. public function write($data)
  372. {
  373. if($this->isSilent()) {
  374. return;
  375. }
  376. print $data;
  377. }
  378. /**
  379. * Output printf-stlye formatted string and args
  380. * @return void
  381. */
  382. public function printf()
  383. {
  384. $args = func_get_args();
  385. $this->write(call_user_func_array('sprintf', $args));
  386. }
  387. /**
  388. * Readline from console
  389. * @return string
  390. */
  391. public function readln()
  392. {
  393. $out = "";
  394. $key = fgetc(STDIN);
  395. while ($key!="\n") {
  396. $out.= $key;
  397. $key = fread(STDIN, 1);
  398. }
  399. return $out;
  400. }
  401. /**
  402. * Output upgrades
  403. * @param array $data
  404. * @return void
  405. */
  406. public function outputUpgrades($data)
  407. {
  408. foreach($data['data'] as $chan => $packages) {
  409. $this->writeln("Updates for ".$chan.": ");
  410. foreach($packages as $name => $data) {
  411. $this->writeln(" $name: {$data['from']} => {$data['to']}");
  412. }
  413. }
  414. }
  415. }