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