@@ -96,71 +96,107 @@ protected function shouldIgnoreLine(string $line): bool
96
96
97
97
protected function loadStream (?string $ connectionName = null ): void
98
98
{
99
- $ directory = (new TemporaryDirectory (config ('db-snapshots.temporary_directory_path ' )))->create ();
99
+ $ temporaryDirectory = (new TemporaryDirectory (config ('db-snapshots.temporary_directory_path ' )))->create ();
100
100
101
+ $ this ->configureFilesystemDisk ($ temporaryDirectory ->path ());
102
+
103
+ $ localDisk = $ this ->filesystemFactory ->disk (self ::class);
104
+
105
+ try {
106
+ $ this ->processStream ($ localDisk , $ connectionName );
107
+ } finally {
108
+ $ temporaryDirectory ->delete ();
109
+ }
110
+ }
111
+
112
+ private function configureFilesystemDisk (string $ path ): void
113
+ {
101
114
config ([
102
115
'filesystems.disks. ' . self ::class => [
103
116
'driver ' => 'local ' ,
104
- 'root ' => $ directory -> path () ,
117
+ 'root ' => $ path ,
105
118
'throw ' => false ,
106
- ]
119
+ ],
107
120
]);
121
+ }
108
122
109
- $ localDisk = $ this ->filesystemFactory ->disk (self ::class);
123
+ private function processStream ($ localDisk , ?string $ connectionName ): void
124
+ {
125
+ $ this ->copyStreamToLocalDisk ($ localDisk );
126
+
127
+ $ stream = $ this ->openStream ($ localDisk );
110
128
111
129
try {
112
- LazyCollection:: make ( function () use ( $ localDisk ) {
113
- $ localDisk -> writeStream ( $ this -> fileName , $ this -> disk -> readStream ( $ this -> fileName ));
114
-
115
- $ stream = $ this -> compressionExtension === ' gz '
116
- ? gzopen ( $ localDisk -> path ( $ this -> fileName ), ' r ' )
117
- : $ localDisk -> readStream ( $ this -> fileName );
118
-
119
- $ statement = '' ;
120
- while (! feof ( $ stream )) {
121
- $ chunk = $ this -> compressionExtension === ' gz '
122
- ? gzread ( $ stream , self :: STREAM_BUFFER_SIZE )
123
- : fread ( $ stream , self :: STREAM_BUFFER_SIZE );
124
-
125
- $ lines = explode ( "\n" , $ chunk );
126
- foreach ( $ lines as $ idx => $ line ) {
127
- if ( $ this -> shouldIgnoreLine ( $ line )) {
128
- continue ;
129
- }
130
-
131
- $ statement .= $ line ;
132
-
133
- // Carry-over the last line to the next chunk since it
134
- // is possible that this chunk finished mid-line right on
135
- // a semi-colon.
136
- if ( count ( $ lines ) == $ idx + 1 ) {
137
- break ;
138
- }
139
-
140
- if ( str_ends_with ( trim ( $ statement ), ' ; ' )) {
141
- yield $ statement ;
142
- $ statement = '' ;
143
- }
144
- }
130
+ $ this -> processStatements ( $ stream , $ connectionName );
131
+ } finally {
132
+ $ this -> closeStream ( $ stream );
133
+ }
134
+ }
135
+
136
+ private function copyStreamToLocalDisk ( $ localDisk ): void
137
+ {
138
+ $ localDisk -> writeStream ( $ this -> fileName , $ this -> disk -> readStream ( $ this -> fileName ));
139
+ }
140
+
141
+ private function openStream ( $ localDisk )
142
+ {
143
+ return $ this -> compressionExtension === ' gz '
144
+ ? gzopen ( $ localDisk -> path ( $ this -> fileName ), ' r ' )
145
+ : $ localDisk -> readStream ( $ this -> fileName );
146
+ }
147
+
148
+ private function closeStream ( $ stream ): void
149
+ {
150
+ $ this -> compressionExtension === ' gz ' ? gzclose ( $ stream ) : fclose ( $ stream );
151
+ }
152
+
153
+ private function processStatements ( $ stream , ? string $ connectionName ): void
154
+ {
155
+ $ statement = '' ;
156
+ while (! feof ( $ stream )) {
157
+ $ chunk = $ this -> readChunk ( $ stream );
158
+ $ lines = explode ( "\n" , $ chunk );
159
+
160
+ foreach ( $ lines as $ idx => $ line ) {
161
+ if ( $ this -> shouldIgnoreLine ( $ line )) {
162
+ continue ;
145
163
}
146
164
147
- if (str_ends_with (trim ($ statement ), '; ' )) {
148
- yield $ statement ;
165
+ $ statement .= $ line ;
166
+
167
+ if ($ this ->isLastLineOfChunk ($ lines , $ idx )) {
168
+ break ;
149
169
}
150
170
151
- if ($ this ->compressionExtension === 'gz ' ) {
152
- gzclose ($ stream );
153
- } else {
154
- fclose ($ stream );
171
+ if ($ this ->isCompleteStatement ($ statement )) {
172
+ DB ::connection ($ connectionName )->unprepared ($ statement );
173
+ $ statement = '' ;
155
174
}
156
- })-> each ( function ( string $ statement ) use ( $ connectionName ) {
157
- DB :: connection ( $ connectionName )-> unprepared ( $ statement );
158
- });
159
- } finally {
160
- $ directory -> delete ( );
175
+ }
176
+ }
177
+
178
+ if ( $ this -> isCompleteStatement ( $ statement )) {
179
+ DB :: connection ( $ connectionName )-> unprepared ( $ statement );
161
180
}
162
181
}
163
182
183
+ private function readChunk ($ stream ): string
184
+ {
185
+ return $ this ->compressionExtension === 'gz '
186
+ ? gzread ($ stream , self ::STREAM_BUFFER_SIZE )
187
+ : fread ($ stream , self ::STREAM_BUFFER_SIZE );
188
+ }
189
+
190
+ private function isLastLineOfChunk (array $ lines , int $ idx ): bool
191
+ {
192
+ return count ($ lines ) === $ idx + 1 ;
193
+ }
194
+
195
+ private function isCompleteStatement (string $ statement ): bool
196
+ {
197
+ return str_ends_with (trim ($ statement ), '; ' );
198
+ }
199
+
164
200
public function delete (): void
165
201
{
166
202
event (new DeletingSnapshot ($ this ));
0 commit comments