1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Php7to5\Console; |
4
|
|
|
|
5
|
|
|
use Spatie\Php7to5\Converter; |
6
|
|
|
use Spatie\Php7to5\DirectoryConverter; |
7
|
|
|
use Spatie\Php7to5\Exceptions\InvalidParameter; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
|
14
|
|
|
class ConvertCommand extends Command |
15
|
|
|
{ |
16
|
|
|
protected function configure() |
17
|
|
|
{ |
18
|
|
|
$this->setName('convert') |
19
|
|
|
->setDescription('Convert PHP 7 code to PHP 5 code') |
20
|
|
|
->addArgument( |
21
|
|
|
'source', |
22
|
|
|
InputArgument::REQUIRED, |
23
|
|
|
'A PHP 7 file or a directory containing PHP 7 files' |
24
|
|
|
) |
25
|
|
|
->addArgument( |
26
|
|
|
'destination', |
27
|
|
|
InputArgument::REQUIRED, |
28
|
|
|
'The file or path where the PHP 5 code should be saved' |
29
|
|
|
) |
30
|
|
|
->addOption( |
31
|
|
|
'extension', |
32
|
|
|
'e', |
33
|
|
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
34
|
|
|
'PHP extensions', |
35
|
|
|
['php'] |
36
|
|
|
) |
37
|
|
|
->addOption( |
38
|
|
|
'exclude', |
39
|
|
|
null, |
40
|
|
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
41
|
|
|
'Exclude path' |
42
|
|
|
) |
43
|
|
|
->addOption( |
44
|
|
|
'copy-all', |
45
|
|
|
null, |
46
|
|
|
InputOption::VALUE_NONE, |
47
|
|
|
'If set, will copy all files in a directory, not only php' |
48
|
|
|
) |
49
|
|
|
->addOption( |
50
|
|
|
'overwrite', |
51
|
|
|
null, |
52
|
|
|
InputOption::VALUE_NONE, |
53
|
|
|
'If set, will overwrite existing destination file or directory' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
59
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
60
|
|
|
* |
61
|
|
|
* @return int |
62
|
|
|
* |
63
|
|
|
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter |
64
|
|
|
*/ |
65
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
66
|
|
|
{ |
67
|
|
|
$output->writeln("<info>Start converting {$input->getArgument('source')}</info>"); |
68
|
|
|
|
69
|
|
|
$source = $input->getArgument('source'); |
70
|
|
|
|
71
|
|
|
if (!file_exists($source)) { |
72
|
|
|
throw InvalidParameter::sourceDoesNotExist($source); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (is_file($source)) { |
76
|
|
|
$this->convertFile($input); |
77
|
|
|
} |
78
|
|
|
if (is_dir($source)) { |
79
|
|
|
$this->convertPHPFilesInDirectory($input, $output); |
80
|
|
|
} |
81
|
|
|
$output->writeln('<info>All done!</info>'); |
82
|
|
|
|
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function convertFile(InputInterface $input) |
87
|
|
|
{ |
88
|
|
|
$converter = new Converter($input->getArgument('source')); |
89
|
|
|
$destination = $input->getArgument('destination'); |
90
|
|
|
|
91
|
|
|
if (file_exists($destination) && !$input->getOption('overwrite')) { |
92
|
|
|
throw InvalidParameter::destinationExist(); |
93
|
|
|
} |
94
|
|
|
$converter->saveAsPhp5($destination); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function convertPHPFilesInDirectory(InputInterface $input, OutputInterface $output) |
98
|
|
|
{ |
99
|
|
|
$source = $input->getArgument('source'); |
100
|
|
|
$destination = $input->getArgument('destination'); |
101
|
|
|
$extensions = $input->getOption('extension'); |
102
|
|
|
$excludes = $input->getOption('exclude'); |
103
|
|
|
$converter = new DirectoryConverter($source, $extensions, $excludes); |
104
|
|
|
|
105
|
|
|
$this->isDestinationASourceDirectory($source, $destination); |
106
|
|
|
$this->isDestinationDifferentThanSource($source, $destination); |
107
|
|
|
|
108
|
|
|
if (!$input->getOption('copy-all')) { |
109
|
|
|
$converter->doNotCopyNonPhpFiles(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (file_exists($destination) && !$input->getOption('overwrite')) { |
113
|
|
|
throw InvalidParameter::destinationExist(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$converter->setLogger($output); |
117
|
|
|
$converter->savePhp5FilesTo($destination); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $source |
122
|
|
|
* @param string $destination |
123
|
|
|
* |
124
|
|
|
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter |
125
|
|
|
*/ |
126
|
|
|
protected function isDestinationASourceDirectory($source, $destination) |
127
|
|
|
{ |
128
|
|
|
$this->isEqual($source, $destination); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $source |
133
|
|
|
* @param string $destination |
134
|
|
|
* |
135
|
|
|
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter |
136
|
|
|
*/ |
137
|
|
|
protected function isDestinationDifferentThanSource($source, $destination) |
138
|
|
|
{ |
139
|
|
|
$path_parts = pathinfo($destination); |
140
|
|
|
$this->isEqual($source, $path_parts['dirname']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $source |
145
|
|
|
* @param string $destination |
146
|
|
|
* |
147
|
|
|
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter |
148
|
|
|
*/ |
149
|
|
|
protected function isEqual($source, $destination) |
150
|
|
|
{ |
151
|
|
|
if (!ends_with($destination, DIRECTORY_SEPARATOR)) { |
152
|
|
|
$destination = $destination.DIRECTORY_SEPARATOR; |
153
|
|
|
} |
154
|
|
|
if (!ends_with($source, DIRECTORY_SEPARATOR)) { |
155
|
|
|
$source = $source.DIRECTORY_SEPARATOR; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($destination === $source) { |
159
|
|
|
throw InvalidParameter::destinationDirectoryIsSource(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|