|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
11
|
|
|
* THE SOFTWARE. |
|
12
|
|
|
* |
|
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
14
|
|
|
* and is licensed under the MIT license. |
|
15
|
|
|
* |
|
16
|
|
|
* Copyright (c) 2018 Yuuki Takezawa |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Ytake\Lom\Console; |
|
20
|
|
|
|
|
21
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
25
|
|
|
use Symfony\Component\Finder\Finder; |
|
26
|
|
|
use TokenReflection\Broker; |
|
27
|
|
|
use TokenReflection\Broker\Backend\Memory; |
|
28
|
|
|
use Ytake\Lom\AnnotationRegister; |
|
29
|
|
|
use Ytake\Lom\Lom; |
|
30
|
|
|
use Ytake\Lom\Printer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class GenerateCommand. |
|
34
|
|
|
* |
|
35
|
|
|
* @author yuuki.takezawa<[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class GenerateCommand extends Command |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var string command name */ |
|
40
|
|
|
protected $command = 'generate-code'; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string command description */ |
|
43
|
|
|
protected $description = 'generate code with a simple set of annotations'; |
|
44
|
|
|
|
|
45
|
|
|
/** @var Lom */ |
|
46
|
|
|
protected $lom; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Lom $lom |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Lom $lom) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct(); |
|
54
|
|
|
$this->lom = $lom; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function arguments() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->addArgument('dir', InputArgument::REQUIRED, 'specify your source directory'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param InputInterface $input |
|
64
|
|
|
* @param OutputInterface $output |
|
65
|
|
|
* |
|
66
|
|
|
* @return int|null|void |
|
67
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
|
68
|
|
|
* @throws \ReflectionException |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->finder($input->getArgument('dir')); |
|
73
|
|
|
$output->write('<info>generated</info>'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $directory |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
|
80
|
|
|
* @throws \ReflectionException |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function finder(string $directory) |
|
83
|
|
|
{ |
|
84
|
|
|
$finder = new Finder(); |
|
85
|
|
|
$broker = new Broker(new Memory()); |
|
86
|
|
|
$annotationReader = new AnnotationRegister(); |
|
87
|
|
|
$printer = new Printer(new Standard()); |
|
88
|
|
|
/** @var \Symfony\Component\Finder\SplFileInfo $file */ |
|
89
|
|
|
foreach ($finder->files()->in($directory) as $file) { |
|
90
|
|
|
/** @var \TokenReflection\ReflectionFileNamespace $namespace */ |
|
91
|
|
|
foreach ($broker->processFile($file, true)->getNamespaces() as $namespace) { |
|
92
|
|
|
/** @var \TokenReflection\ReflectionClass $class */ |
|
93
|
|
|
foreach ($namespace->getClasses() as $class) { |
|
94
|
|
|
$printer->setStatement( |
|
95
|
|
|
$this->lom->register($annotationReader) |
|
96
|
|
|
->target($class->getName()) |
|
97
|
|
|
->parseCode() |
|
98
|
|
|
)->putFile($file->getPathname()); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|