1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pgs\RestfonyBundle\Generator; |
4
|
|
|
|
5
|
|
|
use Pgs\RestfonyBundle\Generator\Helper\BundleStructureHelper; |
6
|
|
|
use Sensio\Bundle\GeneratorBundle\Generator\Generator; |
7
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
8
|
|
|
|
9
|
|
|
class DoctrineManagerGenerator extends Generator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var BundleStructureHelper |
13
|
|
|
*/ |
14
|
|
|
protected $helper; |
15
|
|
|
|
16
|
2 |
|
public function __construct(BundleInterface $bundle, $entity) |
17
|
|
|
{ |
18
|
2 |
|
$this->helper = new BundleStructureHelper($bundle, $entity); |
19
|
2 |
|
} |
20
|
|
|
|
21
|
1 |
|
protected function canWriteToFile($forceOverwrite) |
22
|
|
|
{ |
23
|
1 |
|
return !file_exists($this->helper->getManagerFullFilename()) || $forceOverwrite; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Generates the entity form class if it does not exist. |
28
|
|
|
* |
29
|
|
|
* @param bool $forceOverwrite |
30
|
|
|
*/ |
31
|
2 |
|
public function generate($forceOverwrite = false) |
32
|
|
|
{ |
33
|
2 |
View Code Duplication |
if (!$this->canWriteToFile($forceOverwrite)) { |
|
|
|
|
34
|
1 |
|
throw new \RuntimeException(sprintf( |
35
|
1 |
|
'Unable to generate the %s manager class as it already exists under the file: %s', |
36
|
1 |
|
$this->helper->getEntityClass() . 'Type', |
|
|
|
|
37
|
1 |
|
$this->helper->getManagerFullFilename() |
|
|
|
|
38
|
|
|
)); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$this->renderFile( |
42
|
1 |
|
'manager/manager.php.twig', |
43
|
1 |
|
$this->helper->getManagerFullFilename(), |
|
|
|
|
44
|
|
|
[ |
45
|
1 |
|
'repository_class' => $this->helper->getRepositoryFullClass(), |
|
|
|
|
46
|
1 |
|
'class' => $this->helper->getManagerClass(), |
|
|
|
|
47
|
1 |
|
'namespace' => $this->helper->getManagerNamespace() |
|
|
|
|
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
|
51
|
1 |
|
$this->renderFile( |
52
|
1 |
|
'manager/interface.php.twig', |
53
|
1 |
|
$this->helper->getManagerFullFilename($this->helper->getManagerClass() . 'Interface.php'), |
|
|
|
|
54
|
|
|
[ |
55
|
1 |
|
'bundle_namespace' => $this->helper->getBundleNamespace(), |
56
|
1 |
|
'entity_class' => $this->helper->getEntityClass(), |
|
|
|
|
57
|
1 |
|
'class' => $this->helper->getManagerClass(), |
|
|
|
|
58
|
1 |
|
'namespace' => $this->helper->getManagerNamespace() |
|
|
|
|
59
|
|
|
] |
60
|
|
|
); |
61
|
1 |
|
} |
62
|
|
|
} |
63
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: