mikehaertl\shellcommand\Command::addArg PHP Метод

addArg() публичный Метод

public addArg ( string $key, string | array | null $value = null, boolean | null $escape = null ) : static
$key string the argument key to add e.g. `--feature` or `--name=`. If the key does not end with and `=`, the $value will be separated by a space, if any. Keys are not escaped unless $value is null and $escape is `true`.
$value string | array | null the optional argument value which will get escaped if $escapeArgs is true. An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', array('val1','val2'))` which will create the option `--exclude 'val1' 'val2'`.
$escape boolean | null if set, this overrides the $escapeArgs setting and enforces escaping/no escaping
Результат static for method chaining
    public function addArg($key, $value = null, $escape = null)
    {
        $doEscape = $escape !== null ? $escape : $this->escapeArgs;
        $useLocale = $doEscape && $this->locale !== null;
        if ($useLocale) {
            $locale = setlocale(LC_CTYPE, 0);
            // Returns current locale setting
            setlocale(LC_CTYPE, $this->locale);
        }
        if ($value === null) {
            // Only escape single arguments if explicitely requested
            $this->_args[] = $escape ? escapeshellarg($key) : $key;
        } else {
            $separator = substr($key, -1) === '=' ? '' : ' ';
            if (is_array($value)) {
                $params = array();
                foreach ($value as $v) {
                    $params[] = $doEscape ? escapeshellarg($v) : $v;
                }
                $this->_args[] = $key . $separator . implode(' ', $params);
            } else {
                $this->_args[] = $key . $separator . ($doEscape ? escapeshellarg($value) : $value);
            }
        }
        if ($useLocale) {
            setlocale(LC_CTYPE, $locale);
        }
        return $this;
    }

Usage Example

Пример #1
0
 public function up()
 {
     preg_match('/host=([^;]*)/', $this->db->dsn, $hostMatches);
     $hostName = $hostMatches[1];
     preg_match('/dbname=([^;]*)/', $this->db->dsn, $databaseMatches);
     $databaseName = $databaseMatches[1];
     preg_match('/port=([^;]*)/', $this->db->dsn, $portMatches);
     if (isset($portMatches[1])) {
         $port = $portMatches[1];
     } else {
         $port = "3306";
     }
     $command = new Command($this->mysqlExecutable);
     $command->addArg('-h', $hostName);
     $command->addArg('-P', $port);
     $command->addArg('-u', $this->db->username);
     $command->addArg('--password=', $this->db->password);
     $cmd = $command->getExecCommand() . " \"{$databaseName}\" < \"{$this->file}\"";
     #echo "    ".$cmd . "\n"; // TODO echo only with --verbose
     exec($cmd, $output, $return);
     if ($return !== 0) {
         //var_dump($output, $return);
         return false;
     } else {
         return true;
     }
 }
All Usage Examples Of mikehaertl\shellcommand\Command::addArg