PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

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