1 | <?php |
||
13 | class Snapshot |
||
14 | { |
||
15 | /** @var \Illuminate\Filesystem\FilesystemAdapter */ |
||
16 | public $disk; |
||
17 | |||
18 | /** @var string */ |
||
19 | public $fileName; |
||
20 | |||
21 | /** @var string */ |
||
22 | public $name; |
||
23 | |||
24 | /** @var string */ |
||
25 | public $compressionExtension = null; |
||
26 | |||
27 | public function __construct(Disk $disk, string $fileName) |
||
42 | |||
43 | public function load(string $connectionName = null) |
||
63 | |||
64 | public function loadStream(string $connectionName = null) |
||
65 | { |
||
66 | event(new LoadingSnapshot($this)); |
||
67 | |||
68 | if ($connectionName !== null) { |
||
69 | DB::setDefaultConnection($connectionName); |
||
70 | } |
||
71 | |||
72 | $this->dropAllCurrentTables(); |
||
73 | |||
74 | if ($this->compressionExtension === 'gz') { |
||
75 | $dumpFilePath = $this->decompress(); |
||
76 | } else { |
||
77 | $dumpFilePath = $this->disk->path($this->fileName); |
||
78 | } |
||
79 | |||
80 | $this->streamFileIntoDB($dumpFilePath, $connectionName); |
||
81 | |||
82 | event(new LoadedSnapshot($this)); |
||
83 | } |
||
84 | |||
85 | public function streamFileIntoDB($path, string $connectionName = null) |
||
86 | { |
||
87 | if ($connectionName !== null) { |
||
88 | DB::setDefaultConnection($connectionName); |
||
89 | } |
||
90 | |||
91 | $tmpLine = ''; |
||
92 | $lines = file($path); |
||
93 | $errors = []; |
||
94 | |||
95 | foreach ($lines as $line) { |
||
96 | |||
97 | // Skip it if line is a comment |
||
98 | if (substr($line, 0, 2) == '--' || trim($line) == '') { |
||
99 | continue; |
||
100 | } |
||
101 | |||
102 | // Add this line to the current segment |
||
103 | $tmpLine .= $line; |
||
104 | |||
105 | // If the line ends with a semicolon, it is the end of the query - run it |
||
106 | if (substr(trim($line), -1, 1) == ';') { |
||
107 | try { |
||
108 | DB::connection($connectionName)->unprepared($tmpLine); |
||
109 | } catch (\Exception $e) { |
||
110 | $errors[] = [ |
||
111 | 'query' => $tmpLine, |
||
112 | 'message' => $e->getMessage(), |
||
113 | ]; |
||
114 | } |
||
115 | |||
116 | $tmpLine = ''; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | if (empty($errors)) { |
||
121 | return true; |
||
122 | } |
||
123 | |||
124 | return $errors; |
||
125 | } |
||
126 | |||
127 | public function decompress() |
||
160 | |||
161 | public function delete() |
||
169 | |||
170 | public function size(): int |
||
174 | |||
175 | public function createdAt(): Carbon |
||
179 | |||
180 | protected function dropAllCurrentTables() |
||
188 | } |
||
189 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.