Skip to content

Commit 65e5feb

Browse files
authored
Merge pull request #147 from Kharhamel/daoFactoryLazyload
WIP: edited the daoFactory generation code to lazyload from the container
2 parents 3affca3 + 3af998f commit 65e5feb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"ramsey/uuid": "^3.7",
3636
"doctrine/annotations": "^1.6",
3737
"zendframework/zend-code": "^3.3.1",
38+
"psr/container": "^1",
3839
"ext-PDO": "*",
3940
"ext-json": "*",
4041
"ext-hash": "*",

src/Utils/TDBMDaoGenerator.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ private function generateFactory(array $tableList) : void
299299
300300
namespace {$daoNamespace}\\Generated;
301301
302+
use Psr\Container\ContainerInterface;
302303
";
303304
foreach ($tableList as $table) {
304305
$tableName = $table->getName();
@@ -313,6 +314,14 @@ private function generateFactory(array $tableList) : void
313314
*/
314315
class $daoFactoryClassName
315316
{
317+
private \$container;
318+
319+
public function __construct(ContainerInterface \$container)
320+
{
321+
\$this->container = \$container;
322+
}
323+
324+
316325
";
317326

318327
foreach ($tableList as $table) {
@@ -321,7 +330,7 @@ class $daoFactoryClassName
321330
$daoInstanceName = self::toVariableName($daoClassName);
322331

323332
$str .= ' /**
324-
* @var '.$daoClassName.'
333+
* @var '.$daoClassName.'|null
325334
*/
326335
private $'.$daoInstanceName.';
327336
@@ -332,11 +341,15 @@ class $daoFactoryClassName
332341
*/
333342
public function get'.$daoClassName.'() : '.$daoClassName.'
334343
{
344+
if (!$this->'.$daoInstanceName.') {
345+
$this->'.$daoInstanceName.' = $this->container->get('.$daoClassName.'::class);
346+
}
347+
335348
return $this->'.$daoInstanceName.';
336349
}
337350
338351
/**
339-
* Sets the instance of the '.$daoClassName.' class that will be returned by the factory getter.
352+
* Override the instance of the '.$daoClassName.' class that will be returned by the factory getter.
340353
*
341354
* @param '.$daoClassName.' $'.$daoInstanceName.'
342355
*/

0 commit comments

Comments
 (0)