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