1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yoghi\Bundle\MaddaBundle\Command; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Adapter\Local; |
6
|
|
|
use League\Flysystem\Filesystem; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
use Yoghi\Bundle\MaddaBundle\Finder\Finder; |
16
|
|
|
use Yoghi\Bundle\MaddaBundle\Generator\DDDGenerator; |
17
|
|
|
use Yoghi\Bundle\MaddaBundle\Model\Reader; |
18
|
|
|
|
19
|
|
|
class GenerateModelCommand extends Command |
20
|
|
|
{ |
21
|
|
|
private $logger; |
22
|
|
|
private $errors; |
23
|
|
|
|
24
|
|
|
protected function configure() |
25
|
|
|
{ |
26
|
|
|
$this |
27
|
|
|
->setName('generate:model') |
28
|
|
|
->setDescription('Genera tutto il modello a partire da un file yml') |
29
|
|
|
->addArgument('directory', InputArgument::REQUIRED, 'Directory sorgente') |
30
|
|
|
->addArgument('outputdirectory', InputArgument::REQUIRED, 'Directory output delle classi generate') |
31
|
|
|
->addOption('clean', null, InputOption::VALUE_OPTIONAL, 'Option clean output directory') |
32
|
|
|
// ->addArgument('argument', InputArgument::OPTIONAL, 'Argument description') |
|
|
|
|
33
|
|
|
// ->addOption('option', null, InputOption::VALUE_NONE, 'Option description') |
|
|
|
|
34
|
|
|
; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// protected function generateClasses(Local $fullPathFile, Local $directoryOutput, $io) |
|
|
|
|
38
|
|
|
// { |
39
|
|
|
// $adapter = new Local($directoryOutput); |
|
|
|
|
40
|
|
|
// $filesystem = new Filesystem($adapter); |
|
|
|
|
41
|
|
|
// $io->section('Analisi di '.$baseDirectory.'/'.$fileName); |
|
|
|
|
42
|
|
|
// |
43
|
|
|
// $rym = new Reader(); |
|
|
|
|
44
|
|
|
// $rym->readYaml($baseDirectory, $fileName); |
|
|
|
|
45
|
|
|
// $specList = $rym->getProperties(); |
|
|
|
|
46
|
|
|
// } |
47
|
|
|
|
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
// throw new \Exception('boo'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$directory = realpath($input->getArgument('directory')); |
53
|
|
|
$directoryOutput = $input->getArgument('outputdirectory'); |
54
|
|
|
|
55
|
|
|
/* @var $logger Psr\Log\LoggerInterface */ |
56
|
|
|
$this->logger = $this->getContainer()->get('logger'); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$io = new SymfonyStyle($input, $output); |
|
|
|
|
59
|
|
|
$io->title('DDD Model Generation'); |
60
|
|
|
|
61
|
|
|
$clean = $input->hasOption('clean'); |
62
|
|
|
if ($clean) { |
63
|
|
|
$io->section('Clean output directoty'); |
64
|
|
|
$fs = new \Symfony\Component\Filesystem\Filesystem(); |
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$fs->remove($directoryOutput); |
67
|
|
|
} catch (IOExceptionInterface $e) { |
|
|
|
|
68
|
|
|
$io->error($e->getMessage()); |
69
|
|
|
} |
70
|
|
|
$io->text('clean of '.$directoryOutput.' completed'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (is_dir($directory)) { |
74
|
|
|
$finder = new Finder(); |
75
|
|
|
$finder->search($directory); |
|
|
|
|
76
|
|
|
foreach ($finder->getFindedFiles() as $file) { |
77
|
|
|
if (pathinfo($file, PATHINFO_FILENAME) == 'model.yml') { |
78
|
|
|
$io->text('Analizzo model.yml in '.pathinfo($file, PATHINFO_DIRNAME)); |
79
|
|
|
$dddGenerator = new DDDGenerator(); |
80
|
|
|
$dddGenerator->setLogger($this->logger); |
81
|
|
|
$dddGenerator->analyze($file); |
82
|
|
|
$dddGenerator->generate($directoryOutput); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$io->section('Php-Cs-Fixer on generated files'); |
87
|
|
|
|
88
|
|
|
$fixer = new \Symfony\CS\Console\Command\FixCommand(); |
89
|
|
|
|
90
|
|
|
$input = new ArrayInput([ |
91
|
|
|
'path' => $directoryOutput, |
92
|
|
|
'--level' => 'psr2', |
93
|
|
|
'--fixers' => 'eof_ending,strict_param,short_array_syntax,trailing_spaces,indentation,line_after_namespace,php_closing_tag', |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$output = new BufferedOutput(); |
97
|
|
|
$fixer->run($input, $output); |
98
|
|
|
$content = $output->fetch(); |
99
|
|
|
|
100
|
|
|
$io->text($content); |
101
|
|
|
|
102
|
|
|
if (count($this->errors) == 0) { |
103
|
|
|
$io->success('Completed generation'); |
104
|
|
|
} else { |
105
|
|
|
$io->error($this->errors); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
$io->caution('Directory '.$directory.' not valid'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// PER I WARNING RECUPERABILI |
112
|
|
|
//$io->note('Generate Class'); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.