PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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