Advertisement
Guest User

Untitled

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