Skip to content

Commit

Permalink
Merge pull request #3 from moufmouf/daoFactory
Browse files Browse the repository at this point in the history
Adding a code generator to modify DaoFactory
  • Loading branch information
moufmouf authored Aug 2, 2019
2 parents 41f1ff9 + eb1190d commit 32dd838
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Resources/config/container/tdbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<service id="TheCodingMachine\TDBM\Configuration" class="TheCodingMachine\TDBM\Configuration">
<argument></argument> <!-- will be filled in with tdbm.bean_namespace dynamically -->
<argument></argument> <!-- will be filled in with tdbm.dao_namespace dynamically -->
<argument key="$codeGeneratorListeners" type="collection">
<argument type="service" id="TheCodingMachine\TDBM\Bundle\Utils\SymfonyCodeGeneratorListener"/>
</argument>
</service>

<service id="TheCodingMachine\TDBM\ConfigurationInterface" alias="TheCodingMachine\TDBM\Configuration">
Expand Down Expand Up @@ -40,6 +43,8 @@
<service id="tdbm.SchemaManager" class="Doctrine\DBAL\Schema\AbstractSchemaManager">
<factory service="doctrine.dbal.default_connection" method="getSchemaManager" />
</service>

<service id="TheCodingMachine\TDBM\Bundle\Utils\SymfonyCodeGeneratorListener" />
</services>

</container>
38 changes: 38 additions & 0 deletions Tests/Utils/SymfonyCodeGeneratorListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace TheCodingMachine\TDBM\Bundle\Utils;

use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use TheCodingMachine\TDBM\Configuration;
use TheCodingMachine\TDBM\Utils\BeanDescriptor;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\FileGenerator;

class SymfonyCodeGeneratorListenerTest extends TestCase
{

public function testOnDaoFactoryGenerated()
{
$file = new FileGenerator();
$class = new ClassGenerator("Foo");
$file->setClass($class);

$codeGeneratorListener = new SymfonyCodeGeneratorListener();

$beanDescriptor = $this->createMock(BeanDescriptor::class);
$beanDescriptor->method('getDaoClassName')->willReturn('FooDao');
$configuration = $this->createMock(Configuration::class);
$configuration->method('getDaoNamespace')->willReturn('App\\Dao');

$file = $codeGeneratorListener->onDaoFactoryGenerated($file, [$beanDescriptor], $configuration);

$this->assertContains(ServiceSubscriberInterface::class, $file->getClass()->getImplementedInterfaces());
$this->assertContains(<<<CODE
return [
'App\\\\Dao\\\\FooDao' => 'App\\\\Dao\\\\FooDao',
];
CODE
, $file->getClass()->getMethod('getSubscribedServices')->getBody());
}
}
53 changes: 53 additions & 0 deletions Utils/SymfonyCodeGeneratorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php


namespace TheCodingMachine\TDBM\Bundle\Utils;


use Symfony\Contracts\Service\ServiceSubscriberInterface;
use TheCodingMachine\TDBM\ConfigurationInterface;
use TheCodingMachine\TDBM\Utils\BaseCodeGeneratorListener;
use TheCodingMachine\TDBM\Utils\BeanDescriptor;
use Zend\Code\Generator\DocBlock\Tag\ReturnTag;
use Zend\Code\Generator\DocBlockGenerator;
use Zend\Code\Generator\FileGenerator;
use Zend\Code\Generator\MethodGenerator;
use function var_export;

class SymfonyCodeGeneratorListener extends BaseCodeGeneratorListener
{
/**
* @param BeanDescriptor[] $beanDescriptors
*/
public function onDaoFactoryGenerated(FileGenerator $fileGenerator, array $beanDescriptors, ConfigurationInterface $configuration): ?FileGenerator
{
$class = $fileGenerator->getClass();
$class->setImplementedInterfaces([ ServiceSubscriberInterface::class ]);

$getterBody = "return [\n";
foreach ($beanDescriptors as $beanDescriptor) {
$varExportClassName = var_export($configuration->getDaoNamespace().'\\'.$beanDescriptor->getDaoClassName(), true);
$getterBody .= " $varExportClassName => $varExportClassName,\n";
}
$getterBody .= "];\n";

$method = new MethodGenerator(
'getSubscribedServices',
[],
MethodGenerator::FLAG_PUBLIC,
$getterBody
);
$method->setStatic(true);
$method->setDocBlock(new DocBlockGenerator(
null,
null,
[
new ReturnTag([ 'array<string,string>' ])
]
));
$method->setReturnType('array');
$class->addMethodFromGenerator($method);

return $fileGenerator;
}
}

0 comments on commit 32dd838

Please sign in to comment.