1212namespace FSi \Component \DataSource \Driver \Doctrine \DBAL ;
1313
1414use Closure ;
15+ use Composer \InstalledVersions ;
16+ use Doctrine \DBAL \Connection ;
1517use Doctrine \DBAL \Query \QueryBuilder ;
1618use FSi \Component \DataSource \Driver \Doctrine \DBAL \Event \PostGetResult ;
1719use FSi \Component \DataSource \Driver \Doctrine \DBAL \Event \PreGetResult ;
2325use Psr \EventDispatcher \EventDispatcherInterface ;
2426
2527use function sprintf ;
26- use function strpos ;
2728
2829/**
29- * @template T
30- * @template-extends AbstractDriver<T>
30+ * @template-extends AbstractDriver<array<string,mixed>>
3131 */
3232final class DBALDriver extends AbstractDriver
3333{
@@ -37,26 +37,30 @@ final class DBALDriver extends AbstractDriver
3737 * @var string|Closure|null
3838 */
3939 private $ indexField ;
40+ private ?Connection $ connection ;
4041
4142 /**
4243 * @param EventDispatcherInterface $eventDispatcher
4344 * @param array<FieldTypeInterface> $fieldTypes
4445 * @param QueryBuilder $queryBuilder
4546 * @param string $alias
4647 * @param string|Closure|null $indexField
48+ * @param Connection|null $connection
4749 */
4850 public function __construct (
4951 EventDispatcherInterface $ eventDispatcher ,
5052 array $ fieldTypes ,
5153 QueryBuilder $ queryBuilder ,
5254 string $ alias ,
53- $ indexField = null
55+ string |Closure |null $ indexField = null ,
56+ ?Connection $ connection = null
5457 ) {
5558 parent ::__construct ($ eventDispatcher , $ fieldTypes );
5659
5760 $ this ->initialQuery = $ queryBuilder ;
5861 $ this ->alias = $ alias ;
5962 $ this ->indexField = $ indexField ;
63+ $ this ->connection = $ connection ;
6064 }
6165
6266 /**
@@ -67,7 +71,7 @@ public function getQueryFieldName(FieldInterface $field): string
6771 {
6872 $ name = $ field ->getOption ('field ' );
6973
70- if (true === $ field ->getOption ('auto_alias ' ) && false === strpos ($ name , ". " )) {
74+ if (true === $ field ->getOption ('auto_alias ' ) && false === str_contains ($ name , ". " )) {
7175 $ name = "{$ this ->alias }. {$ name }" ;
7276 }
7377
@@ -103,7 +107,18 @@ public function getResult(array $fields, ?int $first, ?int $max): Result
103107 $ query ->setFirstResult ($ first );
104108 }
105109
106- $ result = new Paginator ($ query );
110+ if (false === InstalledVersions::getVersion ('doctrine/dbal ' ) >= '4.0.0 ' ) {
111+ $ result = new Paginator ($ query );
112+ } else {
113+ if (null === $ this ->connection ) {
114+ throw new DBALDriverException (
115+ sprintf ('Connection is required for "%s" driver. ' , self ::class)
116+ );
117+ }
118+
119+ $ result = new Paginator4 ($ this ->connection , $ query );
120+ }
121+
107122 if (null !== $ this ->indexField ) {
108123 $ result = new DBALResult ($ result , $ this ->indexField );
109124 }
0 commit comments