Skip to content

Commit 3159e4b

Browse files
committed
Refactoring
1 parent 382b308 commit 3159e4b

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

spec/Command/ScriptCommandSpec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace spec\PhpZone\Shell\Command;
44

55
use PhpSpec\ObjectBehavior;
6-
use PhpZone\Shell\Process\ProcessFactory;
76
use Prophecy\Argument;
7+
use Symfony\Component\Process\Process;
88

99
class ScriptCommandSpec extends ObjectBehavior
1010
{
11-
public function let(ProcessFactory $processFactory)
11+
public function let(Process $process)
1212
{
13-
$this->beConstructedWith('command:test', array('ls'), 'test description', $processFactory);
13+
$this->beConstructedWith('command:test', 'test description', array($process));
1414
}
1515

1616
public function it_is_initializable()

src/Command/ScriptCommand.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PhpZone\Shell\Command;
44

5-
use PhpZone\Shell\Process\ProcessFactory;
65
use Symfony\Component\Console\Command\Command;
76
use Symfony\Component\Console\Input\InputInterface;
87
use Symfony\Component\Console\Output\OutputInterface;
@@ -11,29 +10,20 @@
1110
class ScriptCommand extends Command
1211
{
1312
/** @var Process[] */
14-
private $processes = array();
13+
private $processes;
1514

1615
/**
1716
* @param string $name
18-
* @param array $script
19-
* @param string $description
20-
* @param ProcessFactory $processFactory
17+
* @param null|string $description
18+
* @param array $processes
2119
*/
22-
public function __construct($name, array $script, $description, ProcessFactory $processFactory)
20+
public function __construct($name, $description, array $processes)
2321
{
24-
$this->generateProcesses($processFactory, $script);
2522
$this->setDescription($description);
2623

27-
parent::__construct($name);
28-
}
29-
30-
private function generateProcesses(ProcessFactory $processFactory, array $script)
31-
{
32-
foreach ($script as $command) {
33-
$process = $processFactory->createByCommand($command);
24+
$this->processes = $processes;
3425

35-
$this->processes[] = $process;
36-
}
26+
parent::__construct($name);
3727
}
3828

3929
protected function execute(InputInterface $input, OutputInterface $output)

src/Shell.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
1010
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
1111
use Symfony\Component\OptionsResolver\OptionsResolver;
12+
use Symfony\Component\Process\Process;
1213

1314
class Shell implements Extension
1415
{
@@ -71,12 +72,32 @@ private function generateCommandDefinition($commandName, array $commandOptions)
7172
{
7273
$commandOptions = $this->optionsResolver->resolve($commandOptions);
7374

75+
$processes = $this->generateProcesses($commandOptions['script']);
76+
7477
$definition = new Definition('PhpZone\Shell\Command\ScriptCommand');
7578
$definition->setArguments(
76-
array($commandName, $commandOptions['script'], $commandOptions['description'], $this->processFactory)
79+
array($commandName, $commandOptions['description'], $processes)
7780
);
7881
$definition->addTag('command');
7982

8083
return $definition;
8184
}
85+
86+
/**
87+
* @param array $script
88+
*
89+
* @return Process[]
90+
*/
91+
private function generateProcesses(array $script)
92+
{
93+
$processes = array();
94+
95+
foreach ($script as $command) {
96+
$process = $this->processFactory->createByCommand($command);
97+
98+
$processes[] = $process;
99+
}
100+
101+
return $processes;
102+
}
82103
}

0 commit comments

Comments
 (0)