PageRenderTime 76ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Mage/Connect/Frontend/CLI.php

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