1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kpicaza\GenBundle\Command; |
4
|
|
|
|
5
|
|
|
use Kpicaza\GenBundle\Generator\GenFormGenerator; |
6
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineFormCommand; |
7
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\Validators; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class FormGenCommand. |
15
|
|
|
*/ |
16
|
|
|
class FormGenCommand extends GenerateDoctrineFormCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @see Command |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this |
24
|
|
|
->setName('gen:generate:form') |
25
|
|
|
->setDescription('Generates a form type class based on a Doctrine entity') |
26
|
|
|
->setDefinition(array( |
27
|
|
|
new InputArgument('entity', InputArgument::REQUIRED, 'The entity class name to initialize (shortcut notation)'), |
28
|
|
|
)) |
29
|
|
|
->setHelp(<<<EOT |
30
|
|
|
The <info>%command.name%</info> command generates a form class based on a Doctrine entity. |
31
|
|
|
|
32
|
|
|
<info>php %command.full_name% AcmeBlogBundle:Post</info> |
33
|
|
|
|
34
|
|
|
Every generated file is based on a template. There are default templates but they can be overriden by placing custom templates in one of the following locations, by order of priority: |
35
|
|
|
|
36
|
|
|
<info>BUNDLE_PATH/Resources/SensioGeneratorBundle/skeleton/form |
37
|
|
|
APP_PATH/Resources/SensioGeneratorBundle/skeleton/form</info> |
38
|
|
|
|
39
|
|
|
You can check https://github.com/sensio/SensioGeneratorBundle/tree/master/Resources/skeleton |
40
|
|
|
in order to know the file structure of the skeleton |
41
|
|
|
EOT |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @see Command |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$entity = Validators::validateEntityName($input->getArgument('entity')); |
51
|
|
|
list($bundle, $entity) = $this->parseShortcutNotation($entity); |
52
|
|
|
|
53
|
|
|
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle).'\\'.$entity; |
54
|
|
|
$metadata = $this->getEntityMetadata($entityClass); |
55
|
|
|
$bundle = $this->getApplication()->getKernel()->getBundle($bundle); |
|
|
|
|
56
|
|
|
$generator = $this->getGenerator($bundle); |
57
|
|
|
|
58
|
|
|
$generator->generate($bundle, $entity, $metadata[0]); |
59
|
|
|
|
60
|
|
|
$output->writeln(sprintf( |
61
|
|
|
'The new %s.php class file has been created under %s.', |
62
|
|
|
$generator->getClassName(), |
63
|
|
|
$generator->getClassPath() |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function createGenerator() |
68
|
|
|
{ |
69
|
|
|
return new GenFormGenerator($this->getContainer()->get('filesystem')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getGenerator(BundleInterface $bundle = null) |
73
|
|
|
{ |
74
|
|
|
$formGenerator = $this->createGenerator(); |
75
|
|
|
$formGenerator->setSkeletonDirs($this->getSkeletonDirs($bundle)); |
76
|
|
|
|
77
|
|
|
return $formGenerator; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
protected function getSkeletonDirs(BundleInterface $bundle = null) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$skeletonDirs = parent::getSkeletonDirs($bundle); |
83
|
|
|
|
84
|
|
|
if (isset($bundle) && is_dir($dir = $bundle->getPath().'/Resources/skeleton')) { |
85
|
|
|
$skeletonDirs[] = $dir; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (is_dir($dir = $this->getContainer()->get('kernel')->getRootdir().'/Resources/skeleton')) { |
89
|
|
|
$skeletonDirs[] = $dir; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$kernel = $this->getApplication()->getKernel(); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources/skeleton'); |
95
|
|
|
$skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources'); |
96
|
|
|
|
97
|
|
|
return array_reverse($skeletonDirs); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.