1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kpicaza\GenBundle\Command; |
13
|
|
|
|
14
|
|
|
use Kpicaza\GenBundle\Generator\GenRepositoryGenerator; |
15
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCommand; |
16
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\Validators; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\Console\Command\Command; |
22
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Generates a form type class for a given Doctrine entity. |
26
|
|
|
* |
27
|
|
|
* @author Fabien Potencier <[email protected]> |
28
|
|
|
* @author Hugo Hamon <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class RepositoryGenCommand extends GenerateDoctrineCommand |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @see Command |
34
|
|
|
*/ |
35
|
|
|
protected function configure() |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->setName('gen:generate:repository-pattern') |
39
|
|
|
->setAliases(array('generate:doctrine:repository-pattern')) |
40
|
|
|
->setDescription('Generates a repository pattern classes based on a Doctrine entity') |
41
|
|
|
->setDefinition(array( |
42
|
|
|
new InputArgument('entity', InputArgument::REQUIRED, 'The entity class name to initialize (shortcut notation)'), |
43
|
|
|
new InputOption('overwrite', '', InputOption::VALUE_NONE, 'Overwrite any existing class when generating the Repository pattern contents'), |
44
|
|
|
)) |
45
|
|
|
->setHelp(<<<EOT |
46
|
|
|
The <info>%command.name%</info> command generates a repository pattern classes based on a Doctrine entity. |
47
|
|
|
|
48
|
|
|
<info>php %command.full_name% AcmeBlogBundle:Post</info> |
49
|
|
|
|
50
|
|
|
EOT |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @see Command |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$entity = Validators::validateEntityName($input->getArgument('entity')); |
60
|
|
|
list($bundle, $entity) = $this->parseShortcutNotation($entity); |
61
|
|
|
|
62
|
|
|
$forceOverwrite = $input->getOption('overwrite'); |
63
|
|
|
|
64
|
|
|
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle) . '\\' . $entity; |
65
|
|
|
$metadata = $this->getEntityMetadata($entityClass); |
66
|
|
|
$bundle = $this->getApplication()->getKernel()->getBundle($bundle); |
|
|
|
|
67
|
|
|
$generator = $this->getGenerator($bundle); |
68
|
|
|
|
69
|
|
|
$generator->generate($bundle, $entity, $metadata[0], 'yml', $forceOverwrite); |
70
|
|
|
|
71
|
|
|
$output->writeln( |
72
|
|
|
'The new Repository pattern implementation has been created under Model and Repository folders.' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function createGenerator() |
77
|
|
|
{ |
78
|
|
|
return new GenRepositoryGenerator( |
79
|
|
|
$this->getContainer()->get('filesystem'), |
80
|
|
|
$this->getContainer()->getParameter('kernel.root_dir') |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getGenerator(BundleInterface $bundle = null) |
85
|
|
|
{ |
86
|
|
|
$repositoryGenerator = $this->createGenerator(); |
87
|
|
|
$repositoryGenerator->setSkeletonDirs($this->getSkeletonDirs($bundle)); |
88
|
|
|
|
89
|
|
|
return $repositoryGenerator; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
protected function getSkeletonDirs(BundleInterface $bundle = null) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$skeletonDirs = parent::getSkeletonDirs($bundle); |
95
|
|
|
|
96
|
|
|
if (isset($bundle) && is_dir($dir = $bundle->getPath() . '/Resources/skeleton')) { |
97
|
|
|
$skeletonDirs[] = $dir; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (is_dir($dir = $this->getContainer()->get('kernel')->getRootdir() . '/Resources/skeleton')) { |
101
|
|
|
$skeletonDirs[] = $dir; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$kernel = $this->getApplication()->getKernel(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources/skeleton'); |
107
|
|
|
$skeletonDirs[] = $kernel->locateResource('@KpicazaGenBundle/Resources'); |
108
|
|
|
|
109
|
|
|
return array_reverse($skeletonDirs); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.