1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Php7to5\Console; |
4
|
|
|
|
5
|
|
|
use Spatie\Php7to5\DirectoryConverter; |
6
|
|
|
use Spatie\Php7to5\Exceptions\InvalidArgument; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class ConvertCommand extends Command |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this->setName('convert') |
18
|
|
|
->setDescription('Convert PHP 7 code to PHP 5 code') |
19
|
|
|
->addArgument( |
20
|
|
|
'source', |
21
|
|
|
InputArgument::REQUIRED, |
22
|
|
|
'A PHP 7 file or a directory containing PHP 7 files' |
23
|
|
|
) |
24
|
|
|
->addArgument( |
25
|
|
|
'destination', |
26
|
|
|
InputArgument::REQUIRED, |
27
|
|
|
'The file or path where the PHP 5 code should be saved' |
28
|
|
|
) |
29
|
|
|
->addOption( |
30
|
|
|
'alsoCopyNonPhpFiles', |
31
|
|
|
'nonPhp', |
32
|
|
|
InputOption::VALUE_REQUIRED, |
33
|
|
|
'Should non php files be copied over as well', |
34
|
|
|
true |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
40
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
41
|
|
|
* |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$this->guardAgainstInvalidArguments($input); |
47
|
|
|
|
48
|
|
|
$source = $input->getArgument('source'); |
49
|
|
|
$destination = $input->getArgument('destination'); |
50
|
|
|
|
51
|
|
|
$output->writeln("Converting {$source} to {$destination}..."); |
52
|
|
|
$output->writeln(''); |
53
|
|
|
|
54
|
|
|
$converter = new DirectoryConverter($source); |
55
|
|
|
|
56
|
|
|
if (!$input->getOption('alsoCopyNonPhpFiles')) { |
57
|
|
|
$converter->doNotCopyNonPhpFiles(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$converter->savePhp5FilesTo($destination); |
61
|
|
|
|
62
|
|
|
$output->writeln('All Done!'); |
63
|
|
|
$output->writeln(''); |
64
|
|
|
|
65
|
|
|
return 0; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function guardAgainstInvalidArguments(InputInterface $input) |
69
|
|
|
{ |
70
|
|
|
if (!is_dir($input->getArgument('source'))) { |
71
|
|
|
throw InvalidArgument::directoryDoesNotExist($input->getArgument('source')); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|