4
4
use DirectoryIterator ;
5
5
use Gt \Database \Connection \Driver ;
6
6
use Gt \Database \Database ;
7
+ use SplFileInfo ;
7
8
8
9
class QueryCollectionFactory {
9
10
protected Driver $ driver ;
@@ -19,15 +20,9 @@ public function __construct(Driver $driver) {
19
20
20
21
public function create (string $ name ):QueryCollection {
21
22
if (!isset ($ this ->queryCollectionCache [$ name ])) {
22
- $ directoryPath = $ this ->locateDirectory ($ name );
23
-
24
- if (is_null ($ directoryPath )) {
25
- throw new QueryCollectionNotFoundException ($ name );
26
- }
27
-
28
- $ this ->queryCollectionCache [$ name ] = new QueryCollection (
29
- $ directoryPath ,
30
- $ this ->driver
23
+ $ this ->queryCollectionCache [$ name ] = $ this ->findQueryCollection (
24
+ $ name ,
25
+ $ this ->driver ,
31
26
);
32
27
}
33
28
@@ -74,13 +69,13 @@ protected function recurseLocateDirectory(
74
69
throw new BaseQueryPathDoesNotExistException ($ basePath );
75
70
}
76
71
72
+ /** @var SplFileInfo $fileInfo */
77
73
foreach (new DirectoryIterator ($ basePath ) as $ fileInfo ) {
78
- if ($ fileInfo ->isDot ()
79
- || !$ fileInfo ->isDir ()) {
74
+ if ($ fileInfo ->isDot ()) {
80
75
continue ;
81
76
}
82
77
83
- $ basename = $ fileInfo ->getBasename ();
78
+ $ basename = $ fileInfo ->getBasename (" .php " );
84
79
if (strtolower ($ part ) === strtolower ($ basename )) {
85
80
$ realPath = $ fileInfo ->getRealPath ();
86
81
@@ -101,4 +96,30 @@ protected function recurseLocateDirectory(
101
96
protected function getDefaultBasePath ():string {
102
97
return getcwd ();
103
98
}
99
+
100
+ private function findQueryCollection (
101
+ string $ name ,
102
+ Driver $ driver ,
103
+ ):QueryCollection {
104
+ $ path = $ this ->locateDirectory ($ name );
105
+
106
+ if ($ path && is_dir ($ path )) {
107
+ $ this ->queryCollectionCache [$ name ] = new QueryCollectionDirectory (
108
+ $ path ,
109
+ $ driver ,
110
+ );
111
+ }
112
+ elseif ($ path && is_file ($ path )) {
113
+ $ this ->queryCollectionCache [$ name ] = new QueryCollectionClass (
114
+ $ path ,
115
+ $ driver ,
116
+ );
117
+ }
118
+ else {
119
+ throw new QueryCollectionNotFoundException ($ name );
120
+ }
121
+
122
+ return $ this ->queryCollectionCache [$ name ];
123
+ }
124
+
104
125
}
0 commit comments