1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JanPiet\PhpTranspilerApp; |
4
|
|
|
|
5
|
|
|
use JanPiet\PhpTranspiler\Feature\AnonymousClassFeature; |
6
|
|
|
use JanPiet\PhpTranspiler\Feature\NullCoalescingOperatorFeature; |
7
|
|
|
use JanPiet\PhpTranspiler\Feature\ReturnTypeFeature; |
8
|
|
|
use JanPiet\PhpTranspiler\Feature\SpaceshipOperatorFeature; |
9
|
|
|
use JanPiet\PhpTranspiler\Feature\StrictTypesFeature; |
10
|
|
|
use JanPiet\PhpTranspiler\Feature\TypeHintFeature; |
11
|
|
|
use JanPiet\PhpTranspiler\Transpiler; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\Finder\Finder; |
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Example command for testing purposes. |
22
|
|
|
*/ |
23
|
|
|
class TranspileCommand extends Command |
24
|
|
|
{ |
25
|
1 |
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
1 |
|
->setName('transpile') |
29
|
1 |
|
->setDescription('Transpile a source file to a destination') |
30
|
1 |
|
->addArgument('source', InputArgument::REQUIRED, 'Where is your PHP7 source?') |
31
|
1 |
|
->addArgument('destination', InputArgument::REQUIRED, 'Where should the PHP5.6 compatible files be put?') |
32
|
|
|
; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
1 |
|
$transpiler = new Transpiler(); |
38
|
|
|
|
39
|
1 |
|
$filesystem = new Filesystem(); |
40
|
1 |
|
$finder = new Finder(); |
41
|
1 |
|
$finder->files()->in($input->getArgument('source')); |
42
|
|
|
|
43
|
|
|
/** @var SplFileInfo $file */ |
44
|
1 |
|
foreach ($finder as $file) { |
45
|
1 |
|
if ($file->getExtension() !== 'php') { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$transpiled = $transpiler->transpileFeature( |
50
|
1 |
|
$file->getContents(), |
51
|
1 |
|
new AnonymousClassFeature(), |
52
|
1 |
|
new NullCoalescingOperatorFeature(), |
53
|
1 |
|
new ReturnTypeFeature(), |
54
|
1 |
|
new SpaceshipOperatorFeature(), |
55
|
1 |
|
new TypeHintFeature(), |
56
|
1 |
|
new StrictTypesFeature() |
57
|
|
|
); |
58
|
|
|
|
59
|
1 |
|
$filesystem->dumpFile($input->getArgument('destination') . '/' . $file->getRelativePath() . '/' . $file->getFilename(), $transpiled); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
} |
63
|
|
|
|