-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from moufmouf/daoFactory
Adding a code generator to modify DaoFactory
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |