Run the compressor command and get the output

@param string $cmd The command to run.
@param string $content The content to run through the command.
@return The result of the command.
@throws RuntimeException
425176
protected function _runCmd($cmd, $content, $environment = null)
    {
        $Process = new AssetProcess();
        $Process->environment($environment);
        $Process->command($cmd)->run($content);

        if ($Process->error()) {
            throw new RuntimeException($Process->error());
        }
        return $Process->output();
    }
1082289
public function executeCommand($cmd, $timeout = 300)
    {
        $php = escapeshellarg($this->getPhp());
        $process = new Process($php.' '.$this->root_dir.'console '.$cmd, $this->root_dir.'/../', null, null, $timeout);
        $process->run(function ($type, $buffer) {
            echo $buffer;
        });

        if (!$process->isSuccessful()) {
            throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', $cmd));
        }
    }
58357
public static function run($cmd)
    {
        self::initEvn();

        $param = is_array($cmd) && count($cmd) == 2 ? $cmd[1] : $cmd;
        switch ($param) {
            case 'start':
                self::start();
                break;
            case 'stop':
                self::stop();
                break;
            case 'reload':
                self::reload();
                break;
            case 'status':
                self::getStatus(true);
                break;
            case 'add-task':
                if (func_num_args() < 1) {
                    self::message('please input task name');
                    break;
                }
                $args = func_get_args();
                $frequency = isset($args[2]) ? intval($args[2]) : 60;
                self::addTask($args[1], $frequency);
                break;
            case 'rm-task':
                if (func_num_args() < 1) {
                    self::message('please input task name');
                    break;
                }
                $args = func_get_args();
                self::rmTask($args[1]);
                break;
            default:
                self::message('Usage: xxx.php cml.cmd DaemonWorker::run {start|stop|status|addtask|rmtask}');
                break;
        }
    }
573101
public function write($content = '')
    {
        if ($this->rotate()) {
            if (empty($this->file_mtime) && file_exists($this->file_path)) {
                $this->file_mtime = filemtime($this->file_path);
            }
            $f = @fopen($this->file_path, "ab");
            if ($f) {
                fputs($f, str_replace('<', '&lt;', $content));
                fclose($f);
                $this->file_mtime = filemtime($this->file_path);
                return true;
            } else {
                throw new \RuntimeException(
                    sprintf('Can not write in file "%s"!', $this->file_path)
                );
            }
        } else {
            throw new \RuntimeException(
                sprintf('Can not write and/or rotate file "%s"!', $this->file_path)
            );
        }
    }
1318431
public function exec($cmd): ProcessStatus
    {
        $cmd = sprintf('%s %s', $this->phpBinaryPath, $cmd . ' &');
        if (PHP_OS === 'WINNT') {
            $cmd = str_replace('/', '\\', $cmd);
            $cmd = str_replace('\\', '\\\\', $cmd);
        }
        if (PHP_SAPI !== 'cli') {
//            define('STDIN', fopen('php://stdin', 'r'));
            define('STDOUT', fopen('php://stdout', 'w'));
            define('STDERR', fopen('php://stderr', 'w'));
        }
        $pipes = [['pipe', 'r'], STDOUT, STDERR];
        $pipesOpened = [];
        Log::log($cmd);
        $resource = proc_open($cmd, $pipes, $pipesOpened, null, null, ['bypass_shell' => true]);
        if (false === $resource) {
            throw new \RuntimeException('Cannot be run ' . $cmd);
        }
        return new ProcessStatus($resource, $pipesOpened);
    }
487521
public function write(AssetTarget $build, $content)
    {
        $ext = $build->ext();
        $path = $build->outputDir();

        if (!is_writable($path)) {
            throw new RuntimeException('Cannot write cache file. Unable to write to ' . $path);
        }
        $filename = $this->buildFileName($build);
        $success = file_put_contents($path . DIRECTORY_SEPARATOR . $filename, $content) !== false;
        $this->finalize($build);
        return $success;
    }
682166
protected function _runCmd( $cmd )
    {
        if ( !empty( $cmd ) )
        {
            if ( is_array( $cmd ) )
            {
                $cmd = implode( ' && ', $cmd );
            }
            if ( $this->_isWindows() )
            {
                if ( function_exists( 'popen' ) && function_exists( 'pclose' ) )
                {
                    pclose( popen( 'start / B ' . $cmd, 'r' ) );
                }
                else
                {
                    if ( $this->messaging === true )
                    {
                        $this->error( 'Windows php.exe should support \'popen\'|\'pclose\' commands to proper functionality' );
                    }
                }
            }
            else
            {
                if ( function_exists( 'exec' ) )
                {
                    exec( $cmd );
                }
                else
                {
                    if ( $this->messaging === true )
                    {
                        $this->error( 'linux php-cli should support \'exec\' command to proper functionality' );
                    }
                }
            }
        }
        else
        {
            $this->error( 'Command is empty. Check Requirements first.' );
        }
    }
1367541
protected function instantiateCommand($cmd)
    {
        if (!is_array($cmd)) {
            $cmd = [$cmd];
        }

        if (count($cmd)) {
            if (!is_numeric(current(array_keys($cmd)))) {
                $cmd = [$cmd];
            }
        }

        $command = new Command\SequenceCommand();

        foreach ($cmd as $c) {
            $cc = $this->instantiateSingleCommand($c);
            if ($cc instanceof CommandInterface) {
                $command->add($cc);
            } elseif (null !== $cc) {
                throw new \RuntimeException('Instantiated command is not a valid command', 16);
            }
        }

        return $command;
    }
46571
public function runRemoteCommand($cmd, $jail, $timeout = 120)
    {
        $user = $this->getEnvOption('user', $this->getCurrentUser());
        $sudo = $this->getEnvOption('sudo', false);
        $host = $this->getHostName();
        $sshConfig = $this->getSSHConfig();

        $cmdDelegate = $cmd;
        if ($sudo === true) {
            $cmdDelegate = sprintf('sudo %s', $cmd);
        }

        $hostPath = rtrim($this->getEnvOption('host_path'), '/');
        if ($jail && $this->getReleaseId() !== null) {
            $cmdDelegate = sprintf('cd %s/releases/%s && %s', $hostPath, $this->getReleaseId(), $cmdDelegate);
        } elseif ($jail) {
            $cmdDelegate = sprintf('cd %s && %s', $hostPath, $cmdDelegate);
        }

        $cmdRemote = str_replace('"', '\"', $cmdDelegate);
        $cmdLocal = sprintf('ssh -p %d %s %s@%s "%s"', $sshConfig['port'], $sshConfig['flags'], $user, $host, $cmdRemote);

        return $this->runLocalCommand($cmdLocal, $timeout);
    }
1242469
protected function composerRunCommand($command)
    {
        $this->info("Run \"$command\".");

        $output = [];
        exec($command, $output, $returnStatusCode);

        if ($returnStatusCode !== 0) {
            throw RuntimeException::commandExecutionFailed($command, $returnStatusCode);
        }

        $this->info("\"$command\" was successfully ran.");
    }
264934
public static String execForStr(Charset charset, String... cmds) throws IORuntimeException {
		return getResult(exec(cmds), charset);
	}
563797
private function run(string $cmd, string $cwd = null): bool
    {
        $process = new Process($cmd, $cwd);

        $process->setTimeout(300);

        if ($process->isTty()) {
            $process->setTty(true);
        }

        try {
            $output = $this->app->make(OutputInterface::class);
        } catch (Throwable $e) {
            $output = null;
        }

        if ($output) {
            $output->write("\n");
            $process->run(
                function ($type, $line) use ($output) {
                    $output->write($line);
                }
            );
        } else {
            $process->run();
        }

        return $process->isSuccessful();
    }
933424
private function saveContent($file, $content)
    {
        if (file_put_contents($file, $content, LOCK_EX) === false) {
            throw new RuntimeException('Plugin Manager is not able to save ' . $file . '.'); // @codeCoverageIgnore
        }
    }
129588
public static function loadFromString(string $content, string $password = NULL): Pkcs12 {
        if(!openssl_pkcs12_read($content, $result, $password)) {
            throw new RuntimeException(OpenSSL::getLastError());
        }

        $pkcs12 = new Pkcs12();

        if(array_key_exists('cert', $result)) {
            $certificate = Certificate::createFromString($result['cert']);
            $pkcs12->setCertificate($certificate);
        }

        if(array_key_exists('pkey', $result)) {
            $privateKey = PrivateKey::loadFromString($result['pkey']);
            $pkcs12->setPrivateKey($privateKey);
        }

        if(array_key_exists('extracerts', $result)) {
            $chain = new Chain($result['extracerts']);
            $pkcs12->setChain($chain);
        }

        return $pkcs12;
    }
524039
protected function instantiateSingleCommand($cmd)
    {
        $command = null;

        if (!is_array($cmd)) {
            $cmd = ['cmd' => $cmd];
        }

        if (!isset($cmd['cmd'])) {
            $cmd['cmd'] = null;
        }

        if (isset($cmd['cmd'])) {
            $cmd['cmd'] = trim($cmd['cmd']);
            if (0 < preg_match('/^\?([^\:]+)\:(.+)$/', $cmd['cmd'], $matches)) {
                $cmd['if'] = trim($matches[1]);
                $cmd['cmd'] = trim($matches[2]);
            }
            if ('!' === substr($cmd['cmd'], 0, 1)) {
                $cmd['command'] = trim(substr($cmd['cmd'], 1));
            } elseif ('@' === substr($cmd['cmd'], 0, 1)) {
                list ($cmdName) = explode(' ', trim(substr($cmd['cmd'], 1)), 2);
                if (is_file('bin/dev-'.$cmdName)) {
                    $cmd['command'] = sprintf('bin/dev-%s', trim(substr($cmd['cmd'], 1)));
                } else {
                    $cmd['command'] = sprintf('bin/dev %s', trim(substr($cmd['cmd'], 1)));
                }
            } elseif ('#' === substr($cmd['cmd'], 0, 1)) {
                $cmd['class'] = Command\ShellScriptCommand::class;
                $scriptName = trim(substr($cmd['cmd'], 1));
                $description = null;
                if (false !== strpos($scriptName, ',')) {
                    list($scriptName, $description) = explode(',', $scriptName, 2);
                }
                $cmd['args'] = [$scriptName];
                $cmd['description'] = $description;
            } else {
                $cmd['class'] = trim($cmd['cmd']);
            }
            unset($cmd['cmd']);
        }

        if (isset($cmd['command'])) {
            $command = new Command\ExecCommand($cmd['command']);
        } elseif (isset($cmd['class'])) {
            if (!class_exists($cmd['class'])) {
                throw new \RuntimeException(sprintf("Unknown class '%s'", $cmd['class']), 51);
            }
            $r       = new \ReflectionClass($cmd['class']);
            $command = $r->newInstanceArgs((isset($cmd['args']) && is_array($cmd['args'])) ? $cmd['args'] : []);
        }

        if (!is_object($command)) {
            throw new \RuntimeException(sprintf("Unable to instantiate command '%s'", json_encode($cmd)), 50);
        }

        if (isset($cmd['description'])) {
            $command->setDescription($cmd['description']);
        }

        if (isset($cmd['if'])) {
            $command = new Command\ConditionedCommand($command, trim($cmd['if']), $this->getExpressionService());
        }

        return $command;
    }
35265
public function run(Command $command, OutputFormatter $formatter = null) : Result
    {
        $cmd = $this->processor->run($command->getCommand(), $command->getAcceptableExitCodes());

        if (!$cmd->isSuccessful()) {
            throw new RuntimeException(
                'Command failed:' . PHP_EOL
                . '  exit-code: ' . $cmd->getCode() . PHP_EOL
                . '  message:   ' . $cmd->getStdErr() . PHP_EOL,
                $cmd->getCode()
            );
        }

        $formatted = $formatter !== null ? $formatter->format($cmd->getStdOutAsArray()) : [];
        $result    = new Result($cmd, $formatted);

        return $result;
    }
1027356
protected function _compress($content, $ext) {
		$compress = $this->getConfig('compress');
		if ($compress === true) {
			// Native compressor only supports HTML right now
			if ($ext === 'html') {
				$Compressor = new Compressor();
				$content = $Compressor->compress($content);
			}
		} elseif (is_callable($compress)) {
			$content = $compress($content, $ext);
		} elseif ($compress) {
			$content = call_user_func($compress, $content, $ext);
		}

		return $content;
	}