|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Technodelight\ShellExec; |
| 4 | + |
| 5 | +class Command |
| 6 | +{ |
| 7 | + const TYPE_STANDALONE = 1; |
| 8 | + const TYPE_OPT = 2; |
| 9 | + const TYPE_LONGOPT = 3; |
| 10 | + const PREFIX_OPT = '-'; |
| 11 | + const PREFIX_LONGOPT = '--'; |
| 12 | + |
| 13 | + private $exec; |
| 14 | + private $args = []; |
| 15 | + private $squashOptions = false; |
| 16 | + |
| 17 | + public static function create($exec = null) |
| 18 | + { |
| 19 | + $command = new self; |
| 20 | + $command->exec = $exec; |
| 21 | + return $command; |
| 22 | + } |
| 23 | + |
| 24 | + public function withExec($exec) |
| 25 | + { |
| 26 | + $this->exec = $exec; |
| 27 | + return $this; |
| 28 | + } |
| 29 | + |
| 30 | + public function withArgument($argument) |
| 31 | + { |
| 32 | + $this->arg(self::TYPE_STANDALONE, $argument); |
| 33 | + return $this; |
| 34 | + } |
| 35 | + |
| 36 | + public function withOption($option, $value = null) |
| 37 | + { |
| 38 | + $this->arg( |
| 39 | + $this->isLongOpt($option) ? self::TYPE_LONGOPT : self::TYPE_OPT, |
| 40 | + $option, |
| 41 | + $value |
| 42 | + ); |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + public function withShortOption($option, $value = null) |
| 48 | + { |
| 49 | + $this->arg( |
| 50 | + self::TYPE_OPT, |
| 51 | + $option, |
| 52 | + $value |
| 53 | + ); |
| 54 | + |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + public function pipe(Command $command) |
| 59 | + { |
| 60 | + $this->arg(self::TYPE_STANDALONE, '| ' . $command); |
| 61 | + |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + public function withStdErrTo($destination) |
| 66 | + { |
| 67 | + $this->arg(self::TYPE_STANDALONE, '2> ' . $destination); |
| 68 | + |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + public function withStdOutTo($destination) |
| 73 | + { |
| 74 | + $this->arg(self::TYPE_STANDALONE, '> ' . $destination); |
| 75 | + |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + public function withStdErrToStdOut() |
| 80 | + { |
| 81 | + $this->arg(self::TYPE_STANDALONE, '2>&1'); |
| 82 | + |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + public function squashOptions() |
| 87 | + { |
| 88 | + $this->squashOptions = true; |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + public function __toString() |
| 93 | + { |
| 94 | + $parts = [$this->exec]; |
| 95 | + $args = $this->args; |
| 96 | + if ($this->squashOptions) { |
| 97 | + $opts = []; |
| 98 | + foreach ($args as $idx => $arg) { |
| 99 | + if ($arg['type'] == self::TYPE_OPT) { |
| 100 | + $opts[] = $this->render($arg, true); |
| 101 | + unset($args[$idx]); |
| 102 | + } |
| 103 | + } |
| 104 | + $parts[] = $this->render(['type' => self::TYPE_OPT, 'name' => join('', $opts), 'value' => null]); |
| 105 | + } |
| 106 | + |
| 107 | + foreach ($args as $arg) { |
| 108 | + $parts[] = $this->render($arg); |
| 109 | + } |
| 110 | + |
| 111 | + return join(' ', array_filter($parts)); |
| 112 | + } |
| 113 | + |
| 114 | + private function arg($type, $name, $value = null) |
| 115 | + { |
| 116 | + $this->args[] = [ |
| 117 | + 'type' => $type, |
| 118 | + 'name' => $name, |
| 119 | + 'value' => $value, |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + private function render(array $arg, $squashOptions = false) |
| 124 | + { |
| 125 | + if ($arg['type'] == self::TYPE_STANDALONE || ($squashOptions && !$arg['value'])) { |
| 126 | + return $arg['name']; |
| 127 | + } else { |
| 128 | + return sprintf( |
| 129 | + '%s%s%s', |
| 130 | + $arg['type'] == self::TYPE_LONGOPT ? self::PREFIX_LONGOPT : self::PREFIX_OPT, |
| 131 | + $arg['name'], |
| 132 | + $arg['value'] ? '=' . $arg['value'] : '' |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private function isLongOpt($option) |
| 138 | + { |
| 139 | + return strlen($option) > 1 ? true : false; |
| 140 | + } |
| 141 | +} |
0 commit comments