mirror of https://github.com/hauke68/Magallanes
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
<?php |
|
/* |
|
* This file is part of the Magallanes package. |
|
* |
|
* (c) Andrés Montañez <andres@andresmontanez.com> |
|
* |
|
* For the full copyright and license information, please view the LICENSE |
|
* file that was distributed with this source code. |
|
*/ |
|
|
|
namespace Mage\Command; |
|
|
|
use Mage\Config; |
|
use Mage\Autoload; |
|
|
|
use Exception; |
|
|
|
/** |
|
* Loads a Magallanes Command. |
|
* |
|
* @author Andrés Montañez <andres@andresmontanez.com> |
|
*/ |
|
class Factory |
|
{ |
|
/** |
|
* Gets an instance of a Command. |
|
* |
|
* @param string $commandName |
|
* @param Config $config |
|
* @return AbstractCommand |
|
* @throws Exception |
|
*/ |
|
public static function get($commandName, Config $config) |
|
{ |
|
$instance = null; |
|
$commandName = ucwords(str_replace('-', ' ', $commandName)); |
|
$commandName = str_replace(' ', '', $commandName); |
|
|
|
$commandName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $commandName))); |
|
$className = 'Mage\\Command\\BuiltIn\\' . $commandName . 'Command'; |
|
/** @var AbstractCommand $instance */ |
|
$instance = new $className; |
|
if (!is_a($instance, "Mage\Command\AbstractCommand")) { |
|
throw new Exception('The command ' . $commandName . ' must be an instance of Mage\Command\AbstractCommand.'); |
|
} |
|
|
|
$instance->setConfig($config); |
|
|
|
return $instance; |
|
} |
|
} |