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