PageRenderTime 80ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/downloader/lib/Mage/Connect/Command.php

https://bitbucket.org/mengqing/magento-mirror
PHP | 443 lines | 358 code | 10 blank | 75 comment | 3 complexity | 76e9d496c2f8f7cbecd4ff7a0ef4b52c 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 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Connect Command abstract class. It cannot instantiate directly
  28. *
  29. * @category Mage
  30. * @package Mage_Connect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Connect_Command
  34. {
  35. /**
  36. * All commands list
  37. * @var array
  38. */
  39. protected static $_commandsAll = array();
  40. /**
  41. * Commands list hash (key=class)
  42. * @var array
  43. */
  44. protected static $_commandsByClass = array();
  45. /**
  46. * Frontend object
  47. * @var Mage_Connect_Fro
  48. */
  49. protected static $_frontend = null;
  50. /**
  51. * Connect Config instance
  52. * @var Mage_Connect_Config
  53. */
  54. protected static $_config = null;
  55. /**
  56. * Validator instance
  57. *
  58. * @var Mage_Connect_Validator
  59. */
  60. protected static $_validator = null;
  61. /**
  62. * Rest instance
  63. *
  64. * @var Mage_Connect_Rest
  65. */
  66. protected static $_rest = null;
  67. /**
  68. * Cache config instance
  69. *
  70. * @var Mage_Connect_Singleconfig
  71. */
  72. protected static $_sconfig = null;
  73. /**
  74. * Called class name
  75. *
  76. * @var string
  77. */
  78. protected $_class;
  79. /**
  80. * Packager instance
  81. *
  82. * @var Mage_Connect_Packager
  83. */
  84. protected static $_packager = null;
  85. protected static $_return = array();
  86. /**
  87. * Constructor
  88. */
  89. public function __construct()
  90. {
  91. $class = $this->_class = get_class($this);
  92. if(__CLASS__ == $class) {
  93. throw new Exception("You shouldn't instantiate {$class} directly!");
  94. }
  95. $this->commandsInfo = self::$_commandsByClass[$class];
  96. }
  97. /**
  98. * Get command info (static)
  99. *
  100. * @param string $name command name
  101. * @return array|boolean
  102. */
  103. public static function commandInfo($name)
  104. {
  105. $name = strtolower($name);
  106. if(!isset(self::$_commandsAll[$name])) {
  107. return false;
  108. }
  109. return self::$_commandsAll[$name];
  110. }
  111. /**
  112. * Get command info for current command object
  113. *
  114. * @param string $name
  115. * @return array|boolean
  116. */
  117. public function getCommandInfo($name)
  118. {
  119. if(!isset(self::$_commandsByClass[$this->_class][$name])) {
  120. return false;
  121. }
  122. return self::$_commandsByClass[$this->_class][$name];
  123. }
  124. /**
  125. * Run command
  126. *
  127. * @param string $command
  128. * @param string $options
  129. * @param string $params
  130. * @throws Exception if there's no needed method
  131. * @return mixed
  132. */
  133. public function run($command, $options, $params)
  134. {
  135. $data = $this->getCommandInfo($command);
  136. $method = $data['function'];
  137. if(! method_exists($this, $method)) {
  138. throw new Exception("$method does't exist in class ".$this->_class);
  139. }
  140. return $this->$method($command, $options, $params);
  141. }
  142. /**
  143. * Static functions
  144. */
  145. /**
  146. * Static
  147. * @param $commandName
  148. * @return unknown_type
  149. */
  150. public static function getInstance($commandName)
  151. {
  152. if(!isset(self::$_commandsAll[$commandName])) {
  153. throw new UnexpectedValueException("Cannot find command $commandName");
  154. }
  155. $currentCommand = self::$_commandsAll[$commandName];
  156. return new $currentCommand['class']();
  157. }
  158. /**
  159. * Cache config setter
  160. *
  161. * @static
  162. * @param Mage_Connect_Singleconfig $obj
  163. * @return null
  164. */
  165. public static function setSconfig($obj)
  166. {
  167. self::$_sconfig = $obj;
  168. }
  169. /**
  170. * Cache config getter
  171. *
  172. * @return Mage_Connect_Singleconfig
  173. */
  174. public function getSconfig()
  175. {
  176. return self::$_sconfig;
  177. }
  178. /**
  179. * Sets frontend object for all commands
  180. *
  181. * @param Mage_Connect_Frontend $obj
  182. * @return null
  183. */
  184. public static function setFrontendObject($obj)
  185. {
  186. self::$_frontend = $obj;
  187. }
  188. /**
  189. * Set config object for all commands
  190. *
  191. * @param Mage_Connect_Config $obj
  192. * @return null
  193. */
  194. public static function setConfigObject($obj)
  195. {
  196. self::$_config = $obj;
  197. }
  198. /**
  199. * Non-static getter for config
  200. *
  201. * @return Mage_Connect_Config
  202. */
  203. public function config()
  204. {
  205. return self::$_config;
  206. }
  207. /**
  208. * Non-static getter for UI
  209. *
  210. * @return Mage_Connect_Frontend
  211. */
  212. public function ui()
  213. {
  214. return self::$_frontend;
  215. }
  216. /**
  217. * Get validator object
  218. *
  219. * @return Mage_Connect_Validator
  220. */
  221. public function validator()
  222. {
  223. if(is_null(self::$_validator)) {
  224. self::$_validator = new Mage_Connect_Validator();
  225. }
  226. return self::$_validator;
  227. }
  228. /**
  229. * Get rest object
  230. *
  231. * @return Mage_Connect_Rest
  232. */
  233. public function rest()
  234. {
  235. if(is_null(self::$_rest)) {
  236. self::$_rest = new Mage_Connect_Rest(self::config()->protocol);
  237. }
  238. return self::$_rest;
  239. }
  240. /**
  241. * Get commands list sorted
  242. *
  243. * @return array
  244. */
  245. public static function getCommands()
  246. {
  247. if(!count(self::$_commandsAll)) {
  248. self::registerCommands();
  249. }
  250. ksort(self::$_commandsAll);
  251. return self::$_commandsAll;
  252. }
  253. /**
  254. * Get Getopt args from command definitions
  255. * and parse them
  256. *
  257. * @param $command
  258. * @return array
  259. */
  260. public static function getGetoptArgs($command)
  261. {
  262. $commandInfo = self::commandInfo($command);
  263. $short_args = '';
  264. $long_args = array();
  265. if (empty($commandInfo) || empty($commandInfo['options'])) {
  266. return;
  267. }
  268. reset($commandInfo['options']);
  269. while (list($option, $info) = each($commandInfo['options'])) {
  270. $larg = $sarg = '';
  271. if (isset($info['arg'])) {
  272. if ($info['arg']{0} == '(') {
  273. $larg = '==';
  274. $sarg = '::';
  275. } else {
  276. $larg = '=';
  277. $sarg = ':';
  278. }
  279. }
  280. if (isset($info['shortopt'])) {
  281. $short_args .= $info['shortopt'] . $sarg;
  282. }
  283. $long_args[] = $option . $larg;
  284. }
  285. return array($short_args, $long_args);
  286. }
  287. /**
  288. * Try to register commands automatically
  289. *
  290. * @return null
  291. */
  292. public static function registerCommands()
  293. {
  294. $pathCommands = dirname(__FILE__).DIRECTORY_SEPARATOR.basename(__FILE__, ".php");
  295. $f = new DirectoryIterator($pathCommands);
  296. foreach($f as $file) {
  297. /** @var $file DirectoryIterator */
  298. if (! $file->isFile()) {
  299. continue;
  300. }
  301. $pattern = preg_match("/(.*)_Header\.php/imsu", $file->getFilename(), $matches);
  302. if(! $pattern) {
  303. continue;
  304. }
  305. include($file->getPathname());
  306. if(! isset($commands)) {
  307. continue;
  308. }
  309. $class = __CLASS__."_".$matches[1];
  310. foreach ($commands as $k=>$v) {
  311. $commands[$k]['class'] = $class;
  312. self::$_commandsAll[$k] = $commands[$k];
  313. }
  314. self::$_commandsByClass[$class] = $commands;
  315. }
  316. }
  317. /**
  318. * Add Error message
  319. *
  320. * @param string $command
  321. * @param string $message
  322. * @return null
  323. */
  324. public function doError($command, $message)
  325. {
  326. return $this->ui()->doError($command, $message);
  327. }
  328. /**
  329. * Set command return
  330. *
  331. * @param string $key
  332. * @param mixed $val
  333. * @return null
  334. */
  335. public static function setReturn($key, $val)
  336. {
  337. self::$_return[$key] = $val;
  338. }
  339. /**
  340. * Get command return
  341. *
  342. * @param $key
  343. * @param $clear
  344. * @return mixed
  345. */
  346. public static function getReturn($key, $clear = true)
  347. {
  348. if(isset(self::$_return[$key])) {
  349. $out = self::$_return[$key];
  350. if($clear) {
  351. unset(self::$_return[$key]);
  352. }
  353. return $out;
  354. }
  355. return null;
  356. }
  357. /**
  358. * Cleanup command params from empty strings
  359. *
  360. * @param array $params by reference
  361. */
  362. public function cleanupParams(array & $params)
  363. {
  364. $newParams = array();
  365. if(!count($params)) {
  366. return;
  367. }
  368. foreach($params as $v) {
  369. if(is_string($v)) {
  370. $v = trim($v);
  371. if(!strlen($v)) {
  372. continue;
  373. }
  374. }
  375. $newParams[] = $v;
  376. }
  377. $params = $newParams;
  378. }
  379. /**
  380. * Splits first command argument: channel/package
  381. * to two arguments if found in top of array
  382. *
  383. * @param array $params
  384. */
  385. public function splitPackageArgs(array & $params)
  386. {
  387. if(!count($params) || !isset($params[0])) {
  388. return;
  389. }
  390. if($this->validator()->validateUrl($params[0])) {
  391. return;
  392. }
  393. if(preg_match("@([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)@ims", $params[0], $subs)) {
  394. $params[0] = $subs[2];
  395. array_unshift($params, $subs[1]);
  396. }
  397. }
  398. /**
  399. * Get packager instance
  400. *
  401. * @return Mage_Connect_Packager
  402. */
  403. public function getPackager()
  404. {
  405. if(!self::$_packager) {
  406. self::$_packager = new Mage_Connect_Packager();
  407. }
  408. return self::$_packager;
  409. }
  410. }