Skip to content

Commit 32dd838

Browse files
authored
Merge pull request #3 from moufmouf/daoFactory
Adding a code generator to modify DaoFactory
2 parents 41f1ff9 + eb1190d commit 32dd838

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Resources/config/container/tdbm.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<service id="TheCodingMachine\TDBM\Configuration" class="TheCodingMachine\TDBM\Configuration">
1313
<argument></argument> <!-- will be filled in with tdbm.bean_namespace dynamically -->
1414
<argument></argument> <!-- will be filled in with tdbm.dao_namespace dynamically -->
15+
<argument key="$codeGeneratorListeners" type="collection">
16+
<argument type="service" id="TheCodingMachine\TDBM\Bundle\Utils\SymfonyCodeGeneratorListener"/>
17+
</argument>
1518
</service>
1619

1720
<service id="TheCodingMachine\TDBM\ConfigurationInterface" alias="TheCodingMachine\TDBM\Configuration">
@@ -40,6 +43,8 @@
4043
<service id="tdbm.SchemaManager" class="Doctrine\DBAL\Schema\AbstractSchemaManager">
4144
<factory service="doctrine.dbal.default_connection" method="getSchemaManager" />
4245
</service>
46+
47+
<service id="TheCodingMachine\TDBM\Bundle\Utils\SymfonyCodeGeneratorListener" />
4348
</services>
4449

4550
</container>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace TheCodingMachine\TDBM\Bundle\Utils;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
7+
use TheCodingMachine\TDBM\Configuration;
8+
use TheCodingMachine\TDBM\Utils\BeanDescriptor;
9+
use Zend\Code\Generator\ClassGenerator;
10+
use Zend\Code\Generator\FileGenerator;
11+
12+
class SymfonyCodeGeneratorListenerTest extends TestCase
13+
{
14+
15+
public function testOnDaoFactoryGenerated()
16+
{
17+
$file = new FileGenerator();
18+
$class = new ClassGenerator("Foo");
19+
$file->setClass($class);
20+
21+
$codeGeneratorListener = new SymfonyCodeGeneratorListener();
22+
23+
$beanDescriptor = $this->createMock(BeanDescriptor::class);
24+
$beanDescriptor->method('getDaoClassName')->willReturn('FooDao');
25+
$configuration = $this->createMock(Configuration::class);
26+
$configuration->method('getDaoNamespace')->willReturn('App\\Dao');
27+
28+
$file = $codeGeneratorListener->onDaoFactoryGenerated($file, [$beanDescriptor], $configuration);
29+
30+
$this->assertContains(ServiceSubscriberInterface::class, $file->getClass()->getImplementedInterfaces());
31+
$this->assertContains(<<<CODE
32+
return [
33+
'App\\\\Dao\\\\FooDao' => 'App\\\\Dao\\\\FooDao',
34+
];
35+
CODE
36+
, $file->getClass()->getMethod('getSubscribedServices')->getBody());
37+
}
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\TDBM\Bundle\Utils;
5+
6+
7+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
8+
use TheCodingMachine\TDBM\ConfigurationInterface;
9+
use TheCodingMachine\TDBM\Utils\BaseCodeGeneratorListener;
10+
use TheCodingMachine\TDBM\Utils\BeanDescriptor;
11+
use Zend\Code\Generator\DocBlock\Tag\ReturnTag;
12+
use Zend\Code\Generator\DocBlockGenerator;
13+
use Zend\Code\Generator\FileGenerator;
14+
use Zend\Code\Generator\MethodGenerator;
15+
use function var_export;
16+
17+
class SymfonyCodeGeneratorListener extends BaseCodeGeneratorListener
18+
{
19+
/**
20+
* @param BeanDescriptor[] $beanDescriptors
21+
*/
22+
public function onDaoFactoryGenerated(FileGenerator $fileGenerator, array $beanDescriptors, ConfigurationInterface $configuration): ?FileGenerator
23+
{
24+
$class = $fileGenerator->getClass();
25+
$class->setImplementedInterfaces([ ServiceSubscriberInterface::class ]);
26+
27+
$getterBody = "return [\n";
28+
foreach ($beanDescriptors as $beanDescriptor) {
29+
$varExportClassName = var_export($configuration->getDaoNamespace().'\\'.$beanDescriptor->getDaoClassName(), true);
30+
$getterBody .= " $varExportClassName => $varExportClassName,\n";
31+
}
32+
$getterBody .= "];\n";
33+
34+
$method = new MethodGenerator(
35+
'getSubscribedServices',
36+
[],
37+
MethodGenerator::FLAG_PUBLIC,
38+
$getterBody
39+
);
40+
$method->setStatic(true);
41+
$method->setDocBlock(new DocBlockGenerator(
42+
null,
43+
null,
44+
[
45+
new ReturnTag([ 'array<string,string>' ])
46+
]
47+
));
48+
$method->setReturnType('array');
49+
$class->addMethodFromGenerator($method);
50+
51+
return $fileGenerator;
52+
}
53+
}

0 commit comments

Comments
 (0)