Completed
Push — master ( f209dc...9d2c7e )
by Jan Philipp
9s
created

ProcessExecutor::executeTemplateRendering()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\ScriptRuntime;
5
6
use Shopware\Psh\Listing\Script;
7
use Symfony\Component\Process\Process;
8
9
/**
10
 * Execute a command in a separate process
11
 */
12
class ProcessExecutor
13
{
14
    /**
15
     * @var ProcessEnvironment
16
     */
17
    private $environment;
18
19
    /**
20
     * @var TemplateEngine
21
     */
22
    private $templateEngine;
23
24
    /**
25
     * @var Logger
26
     */
27
    private $logger;
28
29
    /**
30
     * @var string
31
     */
32
    private $applicationDirectory;
33
34
    /**
35
     * ProcessExecutor constructor.
36
     * @param ProcessEnvironment $environment
37
     * @param TemplateEngine $templateEngine
38
     * @param Logger $logger
39
     * @param string $applicationDirectory
40
     */
41
    public function __construct(
42
        ProcessEnvironment $environment,
43
        TemplateEngine $templateEngine,
44
        Logger $logger,
45
        string $applicationDirectory
46
    ) {
47
        $this->environment = $environment;
48
        $this->templateEngine = $templateEngine;
49
        $this->logger = $logger;
50
        $this->applicationDirectory = $applicationDirectory;
51
    }
52
53
    /**
54
     * @param Script $script
55
     * @param Command[] $commands
56
     */
57
    public function execute(Script $script, array $commands)
58
    {
59
        $this->logger->startScript($script);
60
61
        $this->executeTemplateRendering();
62
63
        foreach ($commands as $index => $command) {
64
            $parsedCommand = $this->getParsedShellCommand($command);
65
66
            $this->logger->logCommandStart(
67
                $parsedCommand,
68
                $command->getLineNumber(),
69
                $command->isIgnoreError(),
70
                $index,
71
                count($commands)
72
            );
73
74
            $process = $this->environment->createProcess($parsedCommand);
75
76
            $this->setUpProcess($command, $process);
77
            $this->runProcess($process);
78
            $this->testProcessResultValid($command, $process);
79
        }
80
81
        $this->logger->finishScript($script);
82
    }
83
84
    /**
85
     * @param Command $command
86
     * @return string
87
     */
88
    protected function getParsedShellCommand(Command $command): string
89
    {
90
        $rawShellCommand = $command->getShellCommand();
91
92
        $parsedCommand = $this->templateEngine->render(
93
            $rawShellCommand,
94
            $this->environment->getAllValues()
95
        );
96
97
        return $parsedCommand;
98
    }
99
100
    /**
101
     * @param Process $process
102
     */
103
    protected function setUpProcess(Command $command, Process $process)
104
    {
105
        $process->setWorkingDirectory($this->applicationDirectory);
106
        $process->setTimeout(0);
107
        $process->setTty($command->isTTy());
108
    }
109
110
    /**
111
     * @param Process $process
112
     */
113
    protected function runProcess(Process $process)
114
    {
115
        $process->run(function ($type, $response) {
116
            if (Process::ERR === $type) {
117
                $this->logger->err($response);
118
            } else {
119
                $this->logger->out($response);
120
            }
121
        });
122
    }
123
124
    /**
125
     * @param Command $command
126
     * @param Process $process
127
     */
128
    protected function testProcessResultValid(Command $command, Process $process)
129
    {
130
        if (!$command->isIgnoreError() && !$process->isSuccessful()) {
131
            throw new ExecutionErrorException('Command exited with Error');
132
        }
133
    }
134
135
    private function executeTemplateRendering()
136
    {
137
        foreach ($this->environment->getTemplates() as $template) {
138
            $renderedTemplateDestination = $this->templateEngine
139
                ->render($template->getDestination(), $this->environment->getAllValues());
140
141
            $template->setDestination($renderedTemplateDestination);
142
143
            $renderedTemplateContent = $this->templateEngine
144
                ->render($template->getContent(), $this->environment->getAllValues());
145
146
            $template->setContents($renderedTemplateContent);
147
        }
148
    }
149
}
150