示例#1
0
 /**
  * {@inheritdoc}
  */
 public function execute(CommandParams $params, CommandExecutionContext $executionContext)
 {
     $fileName = $params->getFirstArgument();
     if (!$executionContext->hasFileInWorkingDirectory($fileName)) {
         throw new ExecutionFailedException("File '{$fileName}' does not exist.");
     }
     $contents = $executionContext->getContentsOfFileInWorkingDirectory($fileName);
     switch ($params->getSecondArgument()) {
         case 'line':
             $lines = explode("\n", $contents);
             $range = $params->getThirdArgument();
             if (is_numeric($range)) {
                 return implode("\n", array_slice($lines, $range - 1, 1));
             }
             $matches = [];
             if (!preg_match('|(\\d+)-(\\d+)|', $range, $matches)) {
                 throw new ExecutionFailedException("Line definition '{$range}' is not valid. Must be in format N or N-N.");
             }
             $from = min([$matches[1], $matches[2]]);
             $to = max([$matches[1], $matches[2]]);
             return implode("\n", array_slice($lines, $from - 1, $to - $from + 1));
         default:
             return $contents;
     }
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function execute(CommandParams $params, CommandExecutionContext $executionContext)
 {
     $fileName = $params->getFirstArgument();
     if (!$executionContext->hasFileInWorkingDirectory($fileName)) {
         throw new ExecutionFailedException("File '{$fileName}' does not exist.");
     }
     $path = $executionContext->getPathOfFileInWorkingDirectory($fileName);
     $cmd = sprintf('php %s 2>&1', escapeshellarg($path));
     $output = shell_exec($cmd);
     return $this->wrapOutput($output);
 }