[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/finder/Shell/ -> Command.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Finder\Shell;
  13  
  14  @trigger_error('The '.__NAMESPACE__.'\Command class is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
  15  
  16  /**
  17   * @author Jean-François Simon <contact@jfsimon.fr>
  18   *
  19   * @deprecated since 2.8, to be removed in 3.0.
  20   */
  21  class Command
  22  {
  23      private $parent;
  24      private $bits = array();
  25      private $labels = array();
  26  
  27      /**
  28       * @var \Closure|null
  29       */
  30      private $errorHandler;
  31  
  32      public function __construct(Command $parent = null)
  33      {
  34          $this->parent = $parent;
  35      }
  36  
  37      /**
  38       * Returns command as string.
  39       *
  40       * @return string
  41       */
  42      public function __toString()
  43      {
  44          return $this->join();
  45      }
  46  
  47      /**
  48       * Creates a new Command instance.
  49       *
  50       * @return self
  51       */
  52      public static function create(Command $parent = null)
  53      {
  54          return new self($parent);
  55      }
  56  
  57      /**
  58       * Escapes special chars from input.
  59       *
  60       * @param string $input A string to escape
  61       *
  62       * @return string The escaped string
  63       */
  64      public static function escape($input)
  65      {
  66          return escapeshellcmd($input);
  67      }
  68  
  69      /**
  70       * Quotes input.
  71       *
  72       * @param string $input An argument string
  73       *
  74       * @return string The quoted string
  75       */
  76      public static function quote($input)
  77      {
  78          return escapeshellarg($input);
  79      }
  80  
  81      /**
  82       * Appends a string or a Command instance.
  83       *
  84       * @param string|Command $bit
  85       *
  86       * @return $this
  87       */
  88      public function add($bit)
  89      {
  90          $this->bits[] = $bit;
  91  
  92          return $this;
  93      }
  94  
  95      /**
  96       * Prepends a string or a command instance.
  97       *
  98       * @param string|Command $bit
  99       *
 100       * @return $this
 101       */
 102      public function top($bit)
 103      {
 104          array_unshift($this->bits, $bit);
 105  
 106          foreach ($this->labels as $label => $index) {
 107              ++$this->labels[$label];
 108          }
 109  
 110          return $this;
 111      }
 112  
 113      /**
 114       * Appends an argument, will be quoted.
 115       *
 116       * @param string $arg
 117       *
 118       * @return $this
 119       */
 120      public function arg($arg)
 121      {
 122          $this->bits[] = self::quote($arg);
 123  
 124          return $this;
 125      }
 126  
 127      /**
 128       * Appends escaped special command chars.
 129       *
 130       * @param string $esc
 131       *
 132       * @return $this
 133       */
 134      public function cmd($esc)
 135      {
 136          $this->bits[] = self::escape($esc);
 137  
 138          return $this;
 139      }
 140  
 141      /**
 142       * Inserts a labeled command to feed later.
 143       *
 144       * @param string $label The unique label
 145       *
 146       * @return self|string
 147       *
 148       * @throws \RuntimeException If label already exists
 149       */
 150      public function ins($label)
 151      {
 152          if (isset($this->labels[$label])) {
 153              throw new \RuntimeException(sprintf('Label "%s" already exists.', $label));
 154          }
 155  
 156          $this->bits[] = self::create($this);
 157          $this->labels[$label] = \count($this->bits) - 1;
 158  
 159          return $this->bits[$this->labels[$label]];
 160      }
 161  
 162      /**
 163       * Retrieves a previously labeled command.
 164       *
 165       * @param string $label
 166       *
 167       * @return self|string
 168       *
 169       * @throws \RuntimeException
 170       */
 171      public function get($label)
 172      {
 173          if (!isset($this->labels[$label])) {
 174              throw new \RuntimeException(sprintf('Label "%s" does not exist.', $label));
 175          }
 176  
 177          return $this->bits[$this->labels[$label]];
 178      }
 179  
 180      /**
 181       * Returns parent command (if any).
 182       *
 183       * @return self
 184       *
 185       * @throws \RuntimeException If command has no parent
 186       */
 187      public function end()
 188      {
 189          if (null === $this->parent) {
 190              throw new \RuntimeException('Calling end on root command doesn\'t make sense.');
 191          }
 192  
 193          return $this->parent;
 194      }
 195  
 196      /**
 197       * Counts bits stored in command.
 198       *
 199       * @return int The bits count
 200       */
 201      public function length()
 202      {
 203          return \count($this->bits);
 204      }
 205  
 206      /**
 207       * @return $this
 208       */
 209      public function setErrorHandler(\Closure $errorHandler)
 210      {
 211          $this->errorHandler = $errorHandler;
 212  
 213          return $this;
 214      }
 215  
 216      /**
 217       * @return \Closure|null
 218       */
 219      public function getErrorHandler()
 220      {
 221          return $this->errorHandler;
 222      }
 223  
 224      /**
 225       * Executes current command.
 226       *
 227       * @return array The command result
 228       *
 229       * @throws \RuntimeException
 230       */
 231      public function execute()
 232      {
 233          if (null === $errorHandler = $this->errorHandler) {
 234              exec($this->join(), $output);
 235          } else {
 236              $process = proc_open($this->join(), array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
 237              $output = preg_split('~(\r\n|\r|\n)~', stream_get_contents($pipes[1]), -1, PREG_SPLIT_NO_EMPTY);
 238  
 239              if ($error = stream_get_contents($pipes[2])) {
 240                  $errorHandler($error);
 241              }
 242  
 243              proc_close($process);
 244          }
 245  
 246          return $output ?: array();
 247      }
 248  
 249      /**
 250       * Joins bits.
 251       *
 252       * @return string
 253       */
 254      public function join()
 255      {
 256          return implode(' ', array_filter(
 257              array_map(function ($bit) {
 258                  return $bit instanceof Command ? $bit->join() : ($bit ?: null);
 259              }, $this->bits),
 260              function ($bit) { return null !== $bit; }
 261          ));
 262      }
 263  
 264      /**
 265       * Insert a string or a Command instance before the bit at given position $index (index starts from 0).
 266       *
 267       * @param string|Command $bit
 268       * @param int            $index
 269       *
 270       * @return $this
 271       */
 272      public function addAtIndex($bit, $index)
 273      {
 274          array_splice($this->bits, $index, 0, $bit instanceof self ? array($bit) : $bit);
 275  
 276          return $this;
 277      }
 278  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1