Skip to content

Commit

Permalink
merge: pull request #136 from rollun-com/db_table_creation_error
Browse files Browse the repository at this point in the history
fix DbTable: creation error 'Uncaught RuntimeException: Dependency with name dbTable not found in container.'
  • Loading branch information
misha-rollun authored Jan 26, 2022
2 parents 0555470 + fff8ab6 commit 5135e91
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
23 changes: 11 additions & 12 deletions src/DataStore/src/DataStore/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use rollun\datastore\DataStore\ConditionBuilder\SqlConditionBuilder;
use rollun\datastore\Rql\RqlQuery;
use rollun\datastore\TableGateway\DbSql\MultiInsertSql;
Expand All @@ -31,14 +32,14 @@
*/
class DbTable extends DataStoreAbstract
{
const LOG_METHOD = 'method';
const LOG_TABLE = 'table';
const LOG_TIME = 'time';
const LOG_REQUEST = 'request';
const LOG_RESPONSE = 'response';
const LOG_ROLLBACK = 'rollbackTransaction';
const LOG_SQL = 'sql';
const LOG_COUNT = 'count';
public const LOG_METHOD = 'method';
public const LOG_TABLE = 'table';
public const LOG_TIME = 'time';
public const LOG_REQUEST = 'request';
public const LOG_RESPONSE = 'response';
public const LOG_ROLLBACK = 'rollbackTransaction';
public const LOG_SQL = 'sql';
public const LOG_COUNT = 'count';

/**
* @var TableGateway
Expand Down Expand Up @@ -67,13 +68,11 @@ class DbTable extends DataStoreAbstract
public function __construct(
TableGateway $dbTable,
bool $writeLogs = false,
LoggerInterface $loggerService = null
?LoggerInterface $loggerService = null
) {
$this->dbTable = $dbTable;
$this->writeLogs = $writeLogs;
InsideConstruct::init([
'loggerService' => LoggerInterface::class,
]);
$this->loggerService = $loggerService ?? new NullLogger();
}

protected function getSqlQueryBuilder()
Expand Down
24 changes: 13 additions & 11 deletions src/DataStore/src/DataStore/Factory/DbTableAbstractFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @copyright Copyright © 2014 Rollun LC (http://rollun.com/)
* @license LICENSE.md New BSD License
Expand All @@ -7,9 +8,11 @@
namespace rollun\datastore\DataStore\Factory;

use Interop\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use rollun\datastore\DataStore\DataStoreException;
use rollun\datastore\DataStore\DbTable;
use Zend\Db\TableGateway\TableGateway;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;

/**
* Create and return an instance of the DataStore which based on DbTable
Expand Down Expand Up @@ -37,9 +40,9 @@
*/
class DbTableAbstractFactory extends DataStoreAbstractFactory
{
const KEY_TABLE_NAME = 'tableName';
const KEY_TABLE_GATEWAY = 'tableGateway';
const KEY_DB_ADAPTER = 'dbAdapter';
public const KEY_TABLE_NAME = 'tableName';
public const KEY_TABLE_GATEWAY = 'tableGateway';
public const KEY_DB_ADAPTER = 'dbAdapter';

public static $KEY_DATASTORE_CLASS = DbTable::class;

Expand All @@ -64,18 +67,17 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$serviceConfig = $config[self::KEY_DATASTORE][$requestedName];
$requestedClassName = $serviceConfig[self::KEY_CLASS];
$tableGateway = $this->getTableGateway($container, $serviceConfig, $requestedName);
$writeLogs = false;
if (isset($serviceConfig[DataStoreAbstractFactory::KEY_WRITE_LOGS])) {
$writeLogs = $serviceConfig[DataStoreAbstractFactory::KEY_WRITE_LOGS];
$writeLogs = $serviceConfig[DataStoreAbstractFactory::KEY_WRITE_LOGS] ?? false;

if (!is_bool($writeLogs)) {
throw new ServiceNotCreatedException(
"$requestedName datastore config error: " . self::KEY_WRITE_LOGS . ' should be bool value.'
);
}

$this::$KEY_IN_CREATE = 0;

if ($writeLogs) {
return new $requestedClassName($tableGateway, $writeLogs);
}

return new $requestedClassName($tableGateway);
return new $requestedClassName($tableGateway, $writeLogs, $container->get(LoggerInterface::class));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/DataStore/src/DataStore/SerializedDbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class SerializedDbTable extends DbTable
{
protected $tableName;

public function __construct(TableGateway $dbTable, bool $writeLogs = false)
public function __construct(TableGateway $dbTable, bool $writeLogs = false, ?LoggerInterface $loggerService = null)
{
parent::__construct($dbTable, $writeLogs);
parent::__construct($dbTable, $writeLogs, $loggerService);
$this->tableName = $this->dbTable->getTable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use rollun\datastore\DataStore\SerializedDbTable;
use rollun\datastore\TableGateway\SqlQueryBuilder;
use rollun\datastore\TableGateway\TableManagerMysql;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function testSerializableWithoutConfigs()
$container->setService($this->tableName, $tableGateway);
InsideConstruct::setContainer($container);

$this->object = new SerializedDbTable($tableGateway);
$this->object = $this->makeSerializedDbTable($tableGateway);
$this->assertEquals($this->object, unserialize(serialize($this->object)));
}

Expand All @@ -104,4 +105,9 @@ public function testSerializableWithConfigs()
$this->object = $this->getContainer()->get('dbDataStoreSerialized');
$this->assertEquals($this->object, unserialize(serialize($this->object)));
}

private function makeSerializedDbTable(TableGateway $tableGateway)
{
return new SerializedDbTable($tableGateway, false, $this->getContainer()->get(LoggerInterface::class));
}
}

0 comments on commit 5135e91

Please sign in to comment.