magento-ce /src/downloader/lib/Mage/Connect/Command.php

Language PHP Lines 464
MD5 Hash 6d3eff81db6d7890ed8acf616d0d11a5 Estimated Cost $5,353 (why?)
Repository https://bitbucket.org/mkrasuski/magento-ce.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Connect
 * @copyright  Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Connect Command abstract class. It cannot instantiate directly
 *
 * @category    Mage
 * @package     Mage_Connect
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Connect_Command
{
    /**
     * All commands list
     * @var array
     */
    protected static $_commandsAll = array();

    /**
     * Commands list hash (key=class)
     * @var array
     */
    protected static $_commandsByClass = array();

    /**
     * Frontend object
     * @var Mage_Connect_Fro
     */
    protected static $_frontend = null;

    /**
     * Connect Config instance
     * @var Mage_Connect_Config
     */
    protected static $_config = null;
    /**
     * Validator instance
     *
     * @var Mage_Connect_Validator
     */
    protected static $_validator = null;

    /**
     * Backup instance
     *
     * @var Mage_Connect_Backup
     */
    protected static $_backup = null;

    /**
     * Rest instance
     *
     * @var Mage_Connect_Rest
     */
    protected static $_rest = null;

    /**
     * Cache config instance
     *
     * @var Mage_Connect_Singleconfig
     */
    protected static $_sconfig = null;

    /**
     * Called class name
     *
     * @var string
     */
    protected $_class;

    /**
     * Packager instance
     *
     * @var Mage_Connect_Packager
     */
    protected static $_packager = null;

    protected static $_return = array();

    /**
     * Constructor
     */
    public function __construct()
    {
        $class = $this->_class = get_class($this);
        if(__CLASS__ == $class) {
            throw new Exception("You shouldn't instantiate {$class} directly!");
        }
        $this->commandsInfo = self::$_commandsByClass[$class];
    }

    /**
     * Get command info (static)
     *
     * @param string $name command name
     * @return array|boolean
     */
    public static function commandInfo($name)
    {
        $name = strtolower($name);
        if(!isset(self::$_commandsAll[$name])) {
            return false;
        }
        return self::$_commandsAll[$name];
    }

    /**
     * Get command info for current command object
     *
     * @param string $name
     * @return array|boolean
     */
    public function getCommandInfo($name)
    {
        if(!isset(self::$_commandsByClass[$this->_class][$name])) {
            return false;
        }
        return self::$_commandsByClass[$this->_class][$name];
    }

    /**
     * Run command
     *
     * @param string $command
     * @param string $options
     * @param string $params
     * @throws Exception if there's no needed method
     * @return mixed
     */
    public function run($command, $options, $params)
    {
        $data = $this->getCommandInfo($command);
        $method = $data['function'];
        if(! method_exists($this, $method)) {
            throw new Exception("$method does't exist in class ".$this->_class);
        }
        return $this->$method($command, $options, $params);
    }

    /**
     * Static functions
     */

    /**
     * Static
     * @param $commandName
     * @return unknown_type
     */
    public static function getInstance($commandName)
    {
        if(!isset(self::$_commandsAll[$commandName])) {
            throw new UnexpectedValueException("Cannot find command $commandName");
        }
        $currentCommand = self::$_commandsAll[$commandName];
        return new $currentCommand['class']();
    }

    /**
     * Cache config setter
     *
     * @static
     * @param Mage_Connect_Singleconfig $obj
     * @return null
     */
    public static function setSconfig($obj)
    {
        self::$_sconfig = $obj;
    }

    /**
     * Cache config getter
     *
     * @return Mage_Connect_Singleconfig
     */
    public function getSconfig()
    {
        return self::$_sconfig;
    }

    /**
     * Sets frontend object for all commands
     *
     * @param Mage_Connect_Frontend $obj
     * @return null
     */
    public static function setFrontendObject($obj)
    {
        self::$_frontend = $obj;
    }

    /**
     * Set config object for all commands
     *
     * @param Mage_Connect_Config $obj
     * @return null
     */
    public static function setConfigObject($obj)
    {
        self::$_config = $obj;
    }

    /**
     * Non-static getter for config
     *
     * @return Mage_Connect_Config
     */
    public function config()
    {
        return self::$_config;
    }

    /**
     * Non-static getter for UI
     *
     * @return Mage_Connect_Frontend
     */
    public function ui()
    {
        return self::$_frontend;
    }

    /**
     * Get validator object
     *
     * @return Mage_Connect_Validator
     */
    public function validator()
    {
        if(is_null(self::$_validator)) {
            self::$_validator = new Mage_Connect_Validator();
        }
        return self::$_validator;
    }

    /**
     * Get backup object
     *
     * @return Mage_Connect_Backup
     */
    public function backup()
    {
        if(is_null(self::$_backup)) {
            self::$_backup = new Mage_Connect_Backup();
        }
        return self::$_backup;
    }

    /**
     * Get rest object
     *
     * @return Mage_Connect_Rest
     */
    public function rest()
    {
        if(is_null(self::$_rest)) {
            self::$_rest = Mage_Connect_Rest_Builder::getAdapter(self::config()->protocol);
        }
        return self::$_rest;
    }

    /**
     * Get commands list sorted
     *
     * @return array
     */
    public static function getCommands()
    {
        if(!count(self::$_commandsAll)) {
            self::registerCommands();
        }
        ksort(self::$_commandsAll);
        return self::$_commandsAll;
    }

    /**
     * Get Getopt args from command definitions
     * and parse them
     *
     * @param $command
     * @return array
     */
    public static function getGetoptArgs($command)
    {
        $commandInfo = self::commandInfo($command);
        $short_args = '';
        $long_args = array();
        if (empty($commandInfo) || empty($commandInfo['options'])) {
            return;
        }
        reset($commandInfo['options']);
        while (list($option, $info) = each($commandInfo['options'])) {
            $larg = $sarg = '';
            if (isset($info['arg'])) {
                if ($info['arg']{0} == '(') {
                    $larg = '==';
                    $sarg = '::';
                } else {
                    $larg = '=';
                    $sarg = ':';
                }
            }
            if (isset($info['shortopt'])) {
                $short_args .= $info['shortopt'] . $sarg;
            }
            $long_args[] = $option . $larg;
        }
        return array($short_args, $long_args);
    }

    /**
     * Try to register commands automatically
     *
     * @return null
     */
    public static function registerCommands()
    {
        $pathCommands = dirname(__FILE__).DIRECTORY_SEPARATOR.basename(__FILE__, ".php");
        $f = new DirectoryIterator($pathCommands);
        foreach($f as $file) {
            /** @var $file DirectoryIterator */
            if (! $file->isFile()) {
                continue;
            }
            $pattern = preg_match("/(.*)_Header\.php/imsu", $file->getFilename(), $matches);
            if(! $pattern) {
                continue;
            }
            include($file->getPathname());
            if(! isset($commands)) {
                continue;
            }
            $class = __CLASS__."_".$matches[1];
            foreach ($commands as $k=>$v) {
                $commands[$k]['class'] = $class;
                self::$_commandsAll[$k] = $commands[$k];
            }
            self::$_commandsByClass[$class] = $commands;
        }
    }

    /**
     * Add Error message
     *
     * @param string $command
     * @param string $message
     * @return null
     */
    public function doError($command, $message)
    {
        return $this->ui()->doError($command, $message);
    }

    /**
     * Set command return
     *
     * @param string $key
     * @param mixed $val
     * @return null
     */
    public static function setReturn($key, $val)
    {
        self::$_return[$key] = $val;
    }

    /**
     * Get command return
     *
     * @param $key
     * @param $clear
     * @return mixed
     */
    public static function getReturn($key, $clear = true)
    {
        if(isset(self::$_return[$key])) {
            $out = self::$_return[$key];
            if($clear) {
                unset(self::$_return[$key]);
            }
            return $out;
        }
        return null;
    }

    /**
     * Cleanup command params from empty strings
     *
     * @param array $params by reference
     */
    public function cleanupParams(array & $params)
    {
        $newParams = array();
        if(!count($params)) {
            return;
        }
        foreach($params as $v) {
            if(is_string($v)) {
                $v = trim($v);
                if(!strlen($v)) {
                    continue;
                }
            }
            $newParams[] = $v;
        }
        $params = $newParams;
    }

    /**
     * Splits first command argument: channel/package
     * to two arguments if found in top of array
     *
     * @param array $params
     */
    public function splitPackageArgs(array & $params)
    {
        if(!count($params) || !isset($params[0])) {
            return;
        }
        if($this->validator()->validateUrl($params[0])) {
            return;
        }
        if(preg_match("@([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)@ims", $params[0], $subs)) {
            $params[0] = $subs[2];
            array_unshift($params, $subs[1]);
        }
    }


    /**
     * Get packager instance
     *
     * @return Mage_Connect_Packager
     */
    public function getPackager()
    {
        if(!self::$_packager) {
            self::$_packager = new Mage_Connect_Packager();
        }
        return self::$_packager;
    }
}
Back to Top