Skip to content

Commit 88f017e

Browse files
committed
Merge branch 'master' into 1.x
2 parents 721e7fc + 949e00f commit 88f017e

26 files changed

+1064
-487
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ before_script:
1818
script:
1919
- composer cs
2020
- composer test -- --coverage-clover=build/logs/clover.xml --coverage-text
21-
- ./vendor/bin/test-reporter
21+
- 'if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./vendor/bin/test-reporter; fi'

Vagrantfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Vagrant.configure("2") do |config|
44
config.vm.provision "shell", inline: <<-SHELL
55
add-apt-repository ppa:ondrej/php
66
apt-get update
7-
apt-get install -y php7.1 php7.1-xml php7.1-mbstring php7.1-zip composer
7+
apt-get install -y php7.1 php7.1-xml php7.1-mbstring php7.1-zip php7.1-curl php7.1-xdebug composer
8+
9+
# Switch to /vagrant and install packages
10+
cd /vagrant && composer install
811
SHELL
912
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace SystemCtl\Command;
4+
5+
use SystemCtl\Exception\CommandFailedException;
6+
7+
/**
8+
* Interface CommandDispatcherInterface
9+
*
10+
* @package SystemCtl\Command
11+
*/
12+
interface CommandDispatcherInterface
13+
{
14+
/**
15+
* Timeout after which the dispatcher failes the execution
16+
*
17+
* @param int $timeout
18+
*
19+
* @return CommandDispatcherInterface
20+
*/
21+
public function setTimeout(int $timeout): CommandDispatcherInterface;
22+
23+
/**
24+
* Set basic binary to dispatch
25+
*
26+
* @param string $binary
27+
*
28+
* @return CommandDispatcherInterface
29+
*/
30+
public function setBinary(string $binary): CommandDispatcherInterface;
31+
32+
/**
33+
* Dispatch given commands against implementers logic and creating a new command
34+
* to read results
35+
*
36+
* @param array $commands
37+
*
38+
* @return CommandInterface
39+
* @throws CommandFailedException
40+
*/
41+
public function dispatch(...$commands): CommandInterface;
42+
}

src/Command/CommandInterface.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
4+
namespace SystemCtl\Command;
5+
6+
use SystemCtl\Exception\CommandFailedException;
7+
8+
/**
9+
* Interface CommandInterface
10+
*
11+
* @package SystemCtl\Command
12+
* @author icanhazstring <[email protected]>
13+
*/
14+
interface CommandInterface
15+
{
16+
/**
17+
* @return CommandInterface
18+
* @throws CommandFailedException
19+
*/
20+
public function run(): CommandInterface;
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getOutput(): string;
26+
27+
/**
28+
* @return bool
29+
*/
30+
public function isSuccessful(): bool;
31+
}

src/Command/SymfonyCommand.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace SystemCtl\Command;
4+
5+
use Symfony\Component\Process\Process;
6+
use SystemCtl\Exception\CommandFailedException;
7+
8+
/**
9+
* Class SymfonyCommand
10+
*
11+
* @package SystemCtl\Command
12+
* @author icanhazstring <[email protected]>
13+
*/
14+
class SymfonyCommand implements CommandInterface
15+
{
16+
/** @var Process */
17+
private $process;
18+
19+
public function __construct(Process $process)
20+
{
21+
$this->process = $process;
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function getOutput(): string
28+
{
29+
return $this->process->getOutput();
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function isSuccessful(): bool
36+
{
37+
return $this->process->isSuccessful();
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function run(): CommandInterface
44+
{
45+
$this->process->run();
46+
47+
if (!$this->process->isSuccessful()) {
48+
throw new CommandFailedException($this->process->getErrorOutput());
49+
}
50+
51+
return $this;
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace SystemCtl\Command;
4+
5+
use Symfony\Component\Process\ProcessBuilder;
6+
7+
/**
8+
* Class SymfonyCommandDispatcher
9+
*
10+
* @package SystemCtl\Command
11+
* @author icanhazstring <[email protected]>
12+
*/
13+
class SymfonyCommandDispatcher implements CommandDispatcherInterface
14+
{
15+
private $binary;
16+
private $timetout;
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function setBinary(string $binary): CommandDispatcherInterface
22+
{
23+
$this->binary = $binary;
24+
25+
return $this;
26+
}
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function setTimeout(int $timeout): CommandDispatcherInterface
32+
{
33+
$this->timetout = $timeout;
34+
35+
return $this;
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function dispatch(...$commands): CommandInterface
42+
{
43+
$processBuilder = new ProcessBuilder();
44+
$processBuilder->setPrefix($this->binary);
45+
$processBuilder->setTimeout($this->timetout);
46+
$processBuilder->setArguments($commands);
47+
48+
$process = new SymfonyCommand($processBuilder->getProcess());
49+
50+
return $process->run();
51+
}
52+
}

src/Exception/CommandFailedException.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,10 @@
33
namespace SystemCtl\Exception;
44

55
/**
6-
* CommandFailedException
7-
*
8-
* @method static CommandFailedException fromService(string $unitName, string $command)
9-
* @method static CommandFailedException fromTimer(string $unitName, string $command)
6+
* Class CommandFailedException
107
*
118
* @package SystemCtl\Exception
12-
*
13-
* @author icanhazstring <[email protected]>
149
*/
1510
class CommandFailedException extends \Exception
1611
{
17-
public static function __callStatic($name, $arguments)
18-
{
19-
preg_match('/from(?<unit>.*)/', $name, $match);
20-
21-
$unit = strtolower($match['unit']);
22-
23-
$unitName = $arguments[0];
24-
$command = $arguments[1];
25-
26-
return new self("Failed to {$command} {$unit} {$unitName}");
27-
}
2812
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace SystemCtl\Exception;
4+
5+
use Throwable;
6+
7+
/**
8+
* Interface ExceptionInterface
9+
*
10+
* @package SystemCtl\Exception
11+
*/
12+
interface ExceptionInterface extends Throwable
13+
{
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace SystemCtl\Exception;
4+
5+
use RuntimeException;
6+
7+
/**
8+
* Class UnitNotFoundException
9+
*
10+
* @package SystemCtl\Exception
11+
*/
12+
class UnitNotFoundException extends RuntimeException implements ExceptionInterface
13+
{
14+
/**
15+
* @param string $type
16+
* @param string $name
17+
*
18+
* @return UnitNotFoundException
19+
*/
20+
public static function create(string $type, string $name): self
21+
{
22+
return new self(
23+
sprintf('Could not find %s "%s"', ucfirst($type), $name)
24+
);
25+
}
26+
}

src/Exception/UnitTypeNotSupportedException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace SystemCtl\Exception;
44

5+
/**
6+
* Class UnitTypeNotSupportedException
7+
*
8+
* @package SystemCtl\Exception
9+
*/
510
class UnitTypeNotSupportedException extends \Exception
611
{
712
}

0 commit comments

Comments
 (0)