1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Executor; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface; |
7
|
|
|
use PhpUnitGen\Exception\ExecutorException; |
8
|
|
|
use PhpUnitGen\Executor\ExecutorInterface\ExecutorInterface; |
9
|
|
|
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface; |
10
|
|
|
use PhpUnitGen\Validator\ValidatorInterface\FileValidatorInterface; |
11
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class FileExecutor. |
15
|
|
|
* |
16
|
|
|
* @author Paul Thébaud <[email protected]>. |
17
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
19
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
20
|
|
|
* @since Class available since Release 2.0.0. |
21
|
|
|
*/ |
22
|
|
|
class FileExecutor implements FileExecutorInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ConsoleConfigInterface $config The configuration to use. |
26
|
|
|
*/ |
27
|
|
|
private $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var StyleInterface $output The output to display message. |
31
|
|
|
*/ |
32
|
|
|
private $output; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ExecutorInterface $executor The executor for php code. |
36
|
|
|
*/ |
37
|
|
|
private $executor; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var FilesystemInterface $fileSystem The file system to use. |
41
|
|
|
*/ |
42
|
|
|
private $fileSystem; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var FileValidatorInterface $fileValidator The file validator to know which files we need to parse. |
46
|
|
|
*/ |
47
|
|
|
private $fileValidator; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* DirectoryParser constructor. |
51
|
|
|
* |
52
|
|
|
* @param ConsoleConfigInterface $config A config instance. |
53
|
|
|
* @param StyleInterface $output An output to display message. |
54
|
|
|
* @param ExecutorInterface $executor A PhpUnitGen executor. |
55
|
|
|
* @param FilesystemInterface $fileSystem A file system instance. |
56
|
|
|
* @param FileValidatorInterface $fileValidator A file validator. |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
ConsoleConfigInterface $config, |
60
|
|
|
StyleInterface $output, |
61
|
|
|
ExecutorInterface $executor, |
62
|
|
|
FilesystemInterface $fileSystem, |
63
|
|
|
FileValidatorInterface $fileValidator |
64
|
|
|
) { |
65
|
|
|
$this->config = $config; |
66
|
|
|
$this->output = $output; |
67
|
|
|
$this->executor = $executor; |
68
|
|
|
$this->fileSystem = $fileSystem; |
69
|
|
|
$this->fileValidator = $fileValidator; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function execute(string $sourcePath, string $targetPath): void |
76
|
|
|
{ |
77
|
|
|
if (! $this->fileValidator->validate($sourcePath)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->checkTargetPath($targetPath); |
82
|
|
|
|
83
|
|
|
/** @scrutinizer ignore-type Because file readability has already been checked. */ |
84
|
|
|
$content = $this->fileSystem->read($sourcePath); |
85
|
|
|
$code = $this->executor->execute($content); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->fileSystem->write($targetPath, $code); |
88
|
|
|
|
89
|
|
|
// Output that a file is parsed |
90
|
|
|
$this->output->text(sprintf('Parsing file "%s" completed.', $sourcePath)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if an old file exists. If overwrite option is activated, delete it, else, throw an exception. |
95
|
|
|
* |
96
|
|
|
* @param string $targetPath The target file path. |
97
|
|
|
* |
98
|
|
|
* @throws ExecutorException If overwrite option is deactivated and file exists. |
99
|
|
|
*/ |
100
|
|
|
public function checkTargetPath(string $targetPath): void |
101
|
|
|
{ |
102
|
|
|
$targetPathExists = $this->fileSystem->has($targetPath); |
103
|
|
|
|
104
|
|
|
if ($targetPathExists && ! $this->config->hasOverwrite()) { |
105
|
|
|
throw new ExecutorException(sprintf('The target file "%s" already exists.', $targetPath)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($targetPathExists) { |
109
|
|
|
$this->fileSystem->delete($targetPath); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|