Skip to content

Commit

Permalink
Merge pull request #6 from phpsu/task/add-shell-word
Browse files Browse the repository at this point in the history
add shell word
  • Loading branch information
ChrisB9 committed May 16, 2020
2 parents e1c3886 + 783c141 commit 7bf58da
Show file tree
Hide file tree
Showing 17 changed files with 736 additions and 127 deletions.
14 changes: 13 additions & 1 deletion src/Collection/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
use PHPSu\ShellCommandBuilder\ShellInterface;

abstract class AbstractCollection implements CollectionInterface
abstract class AbstractCollection implements ShellInterface
{
/** @var CollectionTuple|null */
protected $tuple;
Expand All @@ -23,6 +23,18 @@ protected function toTuple($command, string $join): CollectionTuple
return CollectionTuple::create($command, $join);
}

/**
* @return array<string|ShellInterface|array<mixed>>
* @throws ShellBuilderException
*/
public function __toArray(): array
{
if ($this->tuple === null) {
throw new ShellBuilderException('Tuple has not been set yet - collection cannot be parsed to array');
}
return $this->tuple->__toArray();
}

public function __toString(): string
{
return (string)$this->tuple;
Expand Down
10 changes: 0 additions & 10 deletions src/Collection/CollectionInterface.php

This file was deleted.

14 changes: 8 additions & 6 deletions src/Collection/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ final class Pipeline extends AbstractCollection
* @return $this
* @throws ShellBuilderException
*/
public function pipe($command): self
public static function pipe($command): self
{
$this->tuple = $this->toTuple($command, ControlOperator::PIPELINE);
return $this;
$pipeline = new self();
$pipeline->tuple = $pipeline->toTuple($command, ControlOperator::PIPELINE);
return $pipeline;
}

/**
* @param string|ShellInterface $command
* @return $this
* @throws ShellBuilderException
*/
public function pipeErrorForward($command): self
public static function pipeErrorForward($command): self
{
$this->tuple = $this->toTuple($command, ControlOperator::PIPELINE_WITH_STDERR_FORWARD);
return $this;
$pipeline = new self();
$pipeline->tuple = $pipeline->toTuple($command, ControlOperator::PIPELINE_WITH_STDERR_FORWARD);
return $pipeline;
}
}
35 changes: 20 additions & 15 deletions src/Collection/Redirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,35 @@ final class Redirection extends AbstractCollection
* @return $this
* @throws ShellBuilderException
*/
public function redirectOutput($value, bool $append): self
public static function redirectOutput($value, bool $append): self
{
$this->tuple = CollectionTuple::create($value, $append ? RedirectOperator::STDOUT_LEFT_APPEND : RedirectOperator::STDOUT_LEFT_INSERT);
return $this;
$redirect = new self();
$redirect->tuple = CollectionTuple::create($value, $append ? RedirectOperator::STDOUT_LEFT_APPEND : RedirectOperator::STDOUT_LEFT_INSERT);
return $redirect;
}

/**
* @param string|ShellInterface $value
* @return $this
* @throws ShellBuilderException
*/
public function redirectInput($value): self
public static function redirectInput($value): self
{
$this->tuple = CollectionTuple::create($value, RedirectOperator::STDIN_RIGHT);
return $this;
$redirect = new self();
$redirect->tuple = CollectionTuple::create($value, RedirectOperator::STDIN_RIGHT);
return $redirect;
}

/**
* @param string|ShellInterface $value
* @return $this
* @throws ShellBuilderException
*/
public function redirectError($value): self
public static function redirectError($value): self
{
$this->tuple = CollectionTuple::create($value, RedirectOperator::FILE_DESCRIPTOR_ERR . RedirectOperator::STDOUT_LEFT_INSERT);
return $this;
$redirect = new self();
$redirect->tuple = CollectionTuple::create($value, RedirectOperator::FILE_DESCRIPTOR_ERR . RedirectOperator::STDOUT_LEFT_INSERT);
return $redirect;
}

/**
Expand All @@ -50,15 +53,17 @@ public function redirectError($value): self
* @return $this
* @throws ShellBuilderException
*/
public function redirectBetweenFiles($value, bool $toLeft): self
public static function redirectBetweenFiles($value, bool $toLeft): self
{
$this->tuple = CollectionTuple::create($value, $toLeft ? RedirectOperator::REDIRECT_LEFT : RedirectOperator::REDIRECT_RIGHT);
return $this;
$redirect = new self();
$redirect->tuple = CollectionTuple::create($value, $toLeft ? RedirectOperator::REDIRECT_LEFT : RedirectOperator::REDIRECT_RIGHT);
return $redirect;
}

public function redirectErrorToOutput(): self
public static function redirectErrorToOutput(): self
{
$this->tuple = CollectionTuple::create('', RedirectOperator::ERR_TO_OUT_REDIRECT);
return $this;
$redirect = new self();
$redirect->tuple = CollectionTuple::create('', RedirectOperator::ERR_TO_OUT_REDIRECT);
return $redirect;
}
}
41 changes: 32 additions & 9 deletions src/Collection/ShellList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,58 @@
final class ShellList extends AbstractCollection
{
/**
* Returns something like: || echo "hello world"
*
* @param string|ShellInterface $command
* @return $this
* @throws ShellBuilderException
*/
public function addOr($command): self
public static function addOr($command): self
{
$this->tuple = $this->toTuple($command, ControlOperator::OR_OPERATOR);
return $this;
$list = new self();
$list->tuple = $list->toTuple($command, ControlOperator::OR_OPERATOR);
return $list;
}

/**
* Returns something like: && echo "hello world"
*
* @param string|ShellInterface $command
* @return $this
* @throws ShellBuilderException
*/
public function addAnd($command): self
public static function addAnd($command): self
{
$this->tuple = $this->toTuple($command, ControlOperator::AND_OPERATOR);
return $this;
$list = new self();
$list->tuple = $list->toTuple($command, ControlOperator::AND_OPERATOR);
return $list;
}

/**
* Returns something like: ; echo "hello world"
*
* @param string|ShellInterface $command
* @return $this
* @throws ShellBuilderException
*/
public function add($command): self
public static function add($command): self
{
$this->tuple = $this->toTuple($command, ControlOperator::COMMAND_DELIMITER);
return $this;
$list = new self();
$list->tuple = $list->toTuple($command, ControlOperator::COMMAND_DELIMITER);
return $list;
}

/**
* Returns something like: & echo "hello world"
*
* @param string|ShellInterface $command
* @return static
* @throws ShellBuilderException
*/
public static function async($command): self
{
$list = new self();
$list->tuple = $list->toTuple($command, ControlOperator::BASH_AMPERSAND);
return $list;
}
}
31 changes: 31 additions & 0 deletions src/Literal/ShellArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace PHPSu\ShellCommandBuilder\Literal;

use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
use PHPSu\ShellCommandBuilder\ShellInterface;

final class ShellArgument extends ShellWord
{
protected $isArgument = true;
protected $delimiter = '';

/**
* ShellArgument constructor.
* @param ShellInterface|string $argument
*/
public function __construct($argument)
{
parent::__construct('', $argument);
}

protected function validate(): void
{
if (is_string($this->value) && empty($this->value)) {
throw new ShellBuilderException('Argument cant be empty');
}
parent::validate();
}
}
24 changes: 24 additions & 0 deletions src/Literal/ShellEnvironmentVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace PHPSu\ShellCommandBuilder\Literal;

use PHPSu\ShellCommandBuilder\ShellInterface;

final class ShellEnvironmentVariable extends ShellWord
{
protected $isEnvironmentVariable = true;
protected $useAssignOperator = true;
protected $nameUpperCase = true;

/**
* ShellArgument constructor.
* @param string $option
* @param ShellInterface|string $value
*/
public function __construct(string $option, $value)
{
parent::__construct($option, $value);
}
}
20 changes: 20 additions & 0 deletions src/Literal/ShellExecutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PHPSu\ShellCommandBuilder\Literal;

final class ShellExecutable extends ShellWord
{
protected $isArgument = true;
protected $spaceAfterValue = false;
protected $isEscaped = false;
protected $delimiter = '';
protected $prefix = '';
protected $suffix = '';

public function __construct(string $executable)
{
parent::__construct($executable);
}
}
26 changes: 26 additions & 0 deletions src/Literal/ShellOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PHPSu\ShellCommandBuilder\Literal;

use PHPSu\ShellCommandBuilder\ShellInterface;

final class ShellOption extends ShellWord
{
protected $isOption = true;
protected $prefix = ShellWord::OPTION_CONTROL;

/**
* ShellArgument constructor.
* @param string $option
* @param ShellInterface|string $value
*/
public function __construct(string $option, $value = '')
{
if (is_string($value) && empty($value)) {
$this->delimiter = '';
}
parent::__construct($option, $value);
}
}
26 changes: 26 additions & 0 deletions src/Literal/ShellShortOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PHPSu\ShellCommandBuilder\Literal;

use PHPSu\ShellCommandBuilder\ShellInterface;

final class ShellShortOption extends ShellWord
{
protected $isShortOption = true;
protected $prefix = ShellWord::SHORT_OPTION_CONTROL;

/**
* ShellArgument constructor.
* @param string $option
* @param ShellInterface|string $value
*/
public function __construct(string $option, $value)
{
if (is_string($value) && empty($value)) {
$this->delimiter = '';
}
parent::__construct($option, $value);
}
}

0 comments on commit 7bf58da

Please sign in to comment.