Skip to content

Commit cb6f501

Browse files
committed
ResultIterator: Class generation and Usage, update tests
1 parent 45a8c05 commit cb6f501

16 files changed

+260
-42
lines changed

src/Commands/AlteredConfiguration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function getDaoNamespace(): string
5656
return $this->configuration->getDaoNamespace();
5757
}
5858

59+
/**
60+
* @return string
61+
*/
62+
public function getResultIteratorNamespace(): string
63+
{
64+
return $this->configuration->getResultIteratorNamespace();
65+
}
66+
5967
/**
6068
* @return Connection
6169
*/

src/Configuration.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Configuration implements ConfigurationInterface
3333
* @var string
3434
*/
3535
private $daoNamespace;
36+
/** @var string */
37+
private $resultIteratorNamespace;
3638
/**
3739
* @var Connection
3840
*/
@@ -73,6 +75,7 @@ class Configuration implements ConfigurationInterface
7375
/**
7476
* @param string $beanNamespace The namespace hosting the beans
7577
* @param string $daoNamespace The namespace hosting the DAOs
78+
* @param string $resultIteratorNamespace The namespace hosting the ResultIterators
7679
* @param Connection $connection The connection to the database
7780
* @param NamingStrategyInterface|null $namingStrategy
7881
* @param Cache|null $cache The Doctrine cache to store database metadata
@@ -83,10 +86,22 @@ class Configuration implements ConfigurationInterface
8386
* @param CodeGeneratorListenerInterface[] $codeGeneratorListeners A list of listeners that can alter code generation of each bean/dao
8487
* @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException
8588
*/
86-
public function __construct(string $beanNamespace, string $daoNamespace, Connection $connection, NamingStrategyInterface $namingStrategy = null, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null, LoggerInterface $logger = null, array $generatorListeners = [], AnnotationParser $annotationParser = null, array $codeGeneratorListeners = [])
87-
{
89+
public function __construct(
90+
string $beanNamespace,
91+
string $daoNamespace,
92+
string $resultIteratorNamespace,
93+
Connection $connection,
94+
NamingStrategyInterface $namingStrategy = null,
95+
Cache $cache = null,
96+
SchemaAnalyzer $schemaAnalyzer = null,
97+
LoggerInterface $logger = null,
98+
array $generatorListeners = [],
99+
AnnotationParser $annotationParser = null,
100+
array $codeGeneratorListeners = []
101+
) {
88102
$this->beanNamespace = rtrim($beanNamespace, '\\');
89103
$this->daoNamespace = rtrim($daoNamespace, '\\');
104+
$this->resultIteratorNamespace = rtrim($resultIteratorNamespace, '\\');
90105
$this->connection = $connection;
91106
if ($cache !== null) {
92107
$this->cache = $cache;
@@ -122,6 +137,14 @@ public function getDaoNamespace(): string
122137
return $this->daoNamespace;
123138
}
124139

140+
/**
141+
* @return string
142+
*/
143+
public function getResultIteratorNamespace(): string
144+
{
145+
return $this->resultIteratorNamespace;
146+
}
147+
125148
/**
126149
* @return Connection
127150
*/

src/ConfigurationInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function getBeanNamespace(): string;
2626
*/
2727
public function getDaoNamespace(): string;
2828

29+
/**
30+
* @return string
31+
*/
32+
public function getResultIteratorNamespace(): string;
33+
2934
/**
3035
* @return Connection
3136
*/

src/ResultIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function __construct()
8484
*/
8585
public static function createResultIterator(QueryFactory $queryFactory, array $parameters, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, int $mode, LoggerInterface $logger): self
8686
{
87-
$iterator = new self();
87+
$iterator = new static();
8888
if ($mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) {
8989
throw new TDBMException("Unknown fetch mode: '".$mode."'");
9090
}

src/TDBMService.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,13 +1119,17 @@ private function exploreChildrenTablesRelationships(SchemaAnalyzer $schemaAnalyz
11191119
* @param string[] $additionalTablesFetch
11201120
* @param int|null $mode
11211121
* @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned
1122+
* @param string $resultIteratorClass The name of the resultIterator class to return
11221123
*
11231124
* @return ResultIterator An object representing an array of results
11241125
*
11251126
* @throws TDBMException
11261127
*/
1127-
public function findObjects(string $mainTable, $filter = null, array $parameters = array(), $orderString = null, array $additionalTablesFetch = array(), ?int $mode = null, string $className = null) : ResultIterator
1128+
public function findObjects(string $mainTable, $filter = null, array $parameters = array(), $orderString = null, array $additionalTablesFetch = array(), ?int $mode = null, string $className = null, string $resultIteratorClass = ResultIterator::class): ResultIterator
11281129
{
1130+
if (!is_a($resultIteratorClass, ResultIterator::class, true)) {
1131+
throw new TDBMInvalidArgumentException('$resultIteratorClass should be a `'. ResultIterator::class. '`. `' . $resultIteratorClass . '` provided.');
1132+
}
11291133
// $mainTable is not secured in MagicJoin, let's add a bit of security to avoid SQL injection.
11301134
if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $mainTable)) {
11311135
throw new TDBMException(sprintf("Invalid table name: '%s'", $mainTable));
@@ -1141,7 +1145,7 @@ public function findObjects(string $mainTable, $filter = null, array $parameters
11411145

11421146
$queryFactory = new FindObjectsQueryFactory($mainTable, $additionalTablesFetch, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer, $this->cache);
11431147

1144-
return ResultIterator::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1148+
return $resultIteratorClass::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
11451149
}
11461150

11471151
/**
@@ -1152,13 +1156,17 @@ public function findObjects(string $mainTable, $filter = null, array $parameters
11521156
* @param string|UncheckedOrderBy|null $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column)
11531157
* @param int $mode
11541158
* @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned
1159+
* @param string $resultIteratorClass The name of the resultIterator class to return
11551160
*
11561161
* @return ResultIterator An object representing an array of results
11571162
*
11581163
* @throws TDBMException
11591164
*/
1160-
public function findObjectsFromSql(string $mainTable, string $from, $filter = null, array $parameters = array(), $orderString = null, ?int $mode = null, string $className = null): ResultIterator
1165+
public function findObjectsFromSql(string $mainTable, string $from, $filter = null, array $parameters = array(), $orderString = null, ?int $mode = null, string $className = null, string $resultIteratorClass = ResultIterator::class): ResultIterator
11611166
{
1167+
if (!is_a($resultIteratorClass, ResultIterator::class, true)) {
1168+
throw new TDBMInvalidArgumentException('$resultIteratorClass should be a `'. ResultIterator::class. '`. `' . $resultIteratorClass . '` provided.');
1169+
}
11621170
// $mainTable is not secured in MagicJoin, let's add a bit of security to avoid SQL injection.
11631171
if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $mainTable)) {
11641172
throw new TDBMException(sprintf("Invalid table name: '%s'", $mainTable));
@@ -1174,7 +1182,7 @@ public function findObjectsFromSql(string $mainTable, string $from, $filter = nu
11741182

11751183
$queryFactory = new FindObjectsFromSqlQueryFactory($mainTable, $from, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer, $this->schemaAnalyzer, $this->cache, $this->cachePrefix);
11761184

1177-
return ResultIterator::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1185+
return $resultIteratorClass::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
11781186
}
11791187

11801188
/**
@@ -1322,13 +1330,17 @@ public function findObjectFromSql(string $mainTable, string $from, $filter = nul
13221330
* @param int|null $mode
13231331
* @param string|null $className
13241332
* @param string $sqlCount
1333+
* @param string $resultIteratorClass The name of the resultIterator class to return
13251334
*
13261335
* @return ResultIterator
13271336
*
13281337
* @throws TDBMException
13291338
*/
1330-
public function findObjectsFromRawSql(string $mainTable, string $sql, array $parameters = array(), ?int $mode = null, string $className = null, string $sqlCount = null): ResultIterator
1339+
public function findObjectsFromRawSql(string $mainTable, string $sql, array $parameters = array(), ?int $mode = null, string $className = null, string $sqlCount = null, string $resultIteratorClass = ResultIterator::class): ResultIterator
13311340
{
1341+
if (!is_a($resultIteratorClass, ResultIterator::class, true)) {
1342+
throw new TDBMInvalidArgumentException('$resultIteratorClass should be a `'. ResultIterator::class. '`. `' . $resultIteratorClass . '` provided.');
1343+
}
13321344
// $mainTable is not secured in MagicJoin, let's add a bit of security to avoid SQL injection.
13331345
if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $mainTable)) {
13341346
throw new TDBMException(sprintf("Invalid table name: '%s'", $mainTable));
@@ -1338,7 +1350,7 @@ public function findObjectsFromRawSql(string $mainTable, string $sql, array $par
13381350

13391351
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this, $this->tdbmSchemaAnalyzer->getSchema(), $mainTable, $sql, $sqlCount);
13401352

1341-
return ResultIterator::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1353+
return $resultIteratorClass::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
13421354
}
13431355

13441356
/**

0 commit comments

Comments
 (0)