Airship\Hangar\Command::getCommandStatic PHP Method

getCommandStatic() public static method

Return a command (statically callable)
public static getCommandStatic ( string $name, boolean $cache = true ) : Command
$name string
$cache boolean
return Command
    public static function getCommandStatic($name, $cache = true)
    {
        $_name = '\\Airship\\Hangar\\Commands\\' . \ucfirst($name);
        if (!empty(self::$cache[$name])) {
            return self::$cache[$name];
        }
        if ($cache) {
            self::$cache[$name] = new $_name();
            return self::$cache[$name];
        }
        return new $_name();
    }

Usage Example

Example #1
0
    $argc = 2;
}
// Create a little cache for the Help command, if applicable. Doesn't contain objects.
$commands = [];
foreach (\glob(__DIR__ . '/Commands/*.php') as $file) {
    // Let's build a queue of all the file names
    // Grab the filename from the Commands directory:
    $className = \preg_replace('#.*/([A-Za-z0-9_]+)\\.php$#', '$1', $file);
    $index = \strtolower($className);
    // Append to $commands array
    $commands[$index] = $className;
    if ($argv[1] !== 'help') {
        // If this is the command the user passed...
        if ($index === $argv[1]) {
            // Instantiate this object
            $exec = Command::getCommandStatic($className);
            // Store the relevant storage devices in the command, in case they're needed
            $exec->storeConfig($config);
            // Execute it, passing the extra parameters to the command's fire() method
            try {
                $exec->fire(\array_values(\array_slice($argv, 2)));
            } catch (\Exception $e) {
                echo $e->getMessage(), "\n";
                $code = $e->getCode();
                exit($code > 0 ? $code : 255);
            }
            $exec->saveConfig();
            exit(0);
        }
    }
}