Total Complexity | 177 |
Total Lines | 1292 |
Duplicated Lines | 0 % |
Changes | 80 | ||
Bugs | 15 | Features | 8 |
Complex classes like Mysqldump often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mysqldump, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Mysqldump |
||
32 | { |
||
33 | |||
34 | // Same as mysqldump. |
||
35 | const MAXLINESIZE = 1000000; |
||
36 | |||
37 | // List of available compression methods as constants. |
||
38 | const GZIP = 'Gzip'; |
||
39 | const BZIP2 = 'Bzip2'; |
||
40 | const NONE = 'None'; |
||
41 | const GZIPSTREAM = 'Gzipstream'; |
||
42 | |||
43 | // List of available connection strings. |
||
44 | const UTF8 = 'utf8'; |
||
45 | const UTF8MB4 = 'utf8mb4'; |
||
46 | |||
47 | /** |
||
48 | * Database username. |
||
49 | * @var string |
||
50 | */ |
||
51 | public $user; |
||
52 | |||
53 | /** |
||
54 | * Database password. |
||
55 | * @var string |
||
56 | */ |
||
57 | public $pass; |
||
58 | |||
59 | /** |
||
60 | * Connection string for PDO. |
||
61 | * @var string |
||
62 | */ |
||
63 | public $dsn; |
||
64 | |||
65 | /** |
||
66 | * Destination filename, defaults to stdout. |
||
67 | * @var string |
||
68 | */ |
||
69 | public $fileName = 'php://stdout'; |
||
70 | |||
71 | // Internal stuff. |
||
72 | private $tables = array(); |
||
73 | private $views = array(); |
||
74 | private $triggers = array(); |
||
75 | private $procedures = array(); |
||
76 | private $functions = array(); |
||
77 | private $events = array(); |
||
78 | private $dbHandler = null; |
||
79 | private $dbType = ""; |
||
80 | private $compressManager; |
||
81 | private $typeAdapter; |
||
82 | private $dumpSettings = array(); |
||
83 | private $pdoSettings = array(); |
||
84 | private $version; |
||
85 | private $tableColumnTypes = array(); |
||
86 | private $transformTableRowCallable; |
||
87 | private $transformColumnValueCallable; |
||
88 | private $infoCallable; |
||
89 | |||
90 | /** |
||
91 | * Database name, parsed from dsn. |
||
92 | * @var string |
||
93 | */ |
||
94 | private $dbName; |
||
95 | |||
96 | /** |
||
97 | * Host name, parsed from dsn. |
||
98 | * @var string |
||
99 | */ |
||
100 | private $host; |
||
101 | |||
102 | /** |
||
103 | * Dsn string parsed as an array. |
||
104 | * @var array |
||
105 | */ |
||
106 | private $dsnArray = array(); |
||
107 | |||
108 | /** |
||
109 | * Keyed on table name, with the value as the conditions. |
||
110 | * e.g. - 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH' |
||
111 | * |
||
112 | * @var array |
||
113 | */ |
||
114 | private $tableWheres = array(); |
||
115 | private $tableLimits = array(); |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Constructor of Mysqldump. Note that in the case of an SQLite database |
||
120 | * connection, the filename must be in the $db parameter. |
||
121 | * |
||
122 | * @param string $dsn PDO DSN connection string |
||
123 | * @param string $user SQL account username |
||
124 | * @param string $pass SQL account password |
||
125 | * @param array $dumpSettings SQL database settings |
||
126 | * @param array $pdoSettings PDO configured attributes |
||
127 | */ |
||
128 | public function __construct( |
||
129 | $dsn = '', |
||
130 | $user = '', |
||
131 | $pass = '', |
||
132 | $dumpSettings = array(), |
||
133 | $pdoSettings = array() |
||
134 | ) { |
||
135 | $dumpSettingsDefault = array( |
||
136 | 'include-tables' => array(), |
||
137 | 'exclude-tables' => array(), |
||
138 | 'compress' => Mysqldump::NONE, |
||
139 | 'init_commands' => array(), |
||
140 | 'no-data' => array(), |
||
141 | 'reset-auto-increment' => false, |
||
142 | 'add-drop-database' => false, |
||
143 | 'add-drop-table' => false, |
||
144 | 'add-drop-trigger' => true, |
||
145 | 'add-locks' => true, |
||
146 | 'complete-insert' => false, |
||
147 | 'databases' => false, |
||
148 | 'default-character-set' => Mysqldump::UTF8, |
||
149 | 'disable-keys' => true, |
||
150 | 'extended-insert' => true, |
||
151 | 'events' => false, |
||
152 | 'hex-blob' => true, /* faster than escaped content */ |
||
153 | 'insert-ignore' => false, |
||
154 | 'net_buffer_length' => self::MAXLINESIZE, |
||
155 | 'no-autocommit' => true, |
||
156 | 'no-create-info' => false, |
||
157 | 'lock-tables' => true, |
||
158 | 'routines' => false, |
||
159 | 'single-transaction' => true, |
||
160 | 'skip-triggers' => false, |
||
161 | 'skip-tz-utc' => false, |
||
162 | 'skip-comments' => false, |
||
163 | 'skip-dump-date' => false, |
||
164 | 'skip-definer' => false, |
||
165 | 'where' => '', |
||
166 | /* deprecated */ |
||
167 | 'disable-foreign-keys-check' => true |
||
168 | ); |
||
169 | |||
170 | $pdoSettingsDefault = array( |
||
171 | PDO::ATTR_PERSISTENT => true, |
||
172 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
173 | ); |
||
174 | |||
175 | $this->user = $user; |
||
176 | $this->pass = $pass; |
||
177 | $this->parseDsn($dsn); |
||
178 | |||
179 | // This drops MYSQL dependency, only use the constant if it's defined. |
||
180 | if ("mysql" === $this->dbType) { |
||
181 | $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
||
182 | } |
||
183 | |||
184 | $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
||
185 | $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
||
186 | $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
||
187 | |||
188 | if (false === $this->dumpSettings['skip-tz-utc']) { |
||
189 | $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
||
190 | } |
||
191 | |||
192 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
||
193 | if (count($diff) > 0) { |
||
194 | throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
||
195 | } |
||
196 | |||
197 | if (!is_array($this->dumpSettings['include-tables']) || |
||
198 | !is_array($this->dumpSettings['exclude-tables'])) { |
||
199 | throw new Exception("Include-tables and exclude-tables should be arrays"); |
||
200 | } |
||
201 | |||
202 | // Dump the same views as tables, mimic mysqldump behaviour |
||
203 | $this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
||
204 | |||
205 | // Create a new compressManager to manage compressed output |
||
206 | $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Destructor of Mysqldump. Unsets dbHandlers and database objects. |
||
211 | */ |
||
212 | public function __destruct() |
||
213 | { |
||
214 | $this->dbHandler = null; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Custom array_replace_recursive to be used if PHP < 5.3 |
||
219 | * Replaces elements from passed arrays into the first array recursively. |
||
220 | * |
||
221 | * @param array $array1 The array in which elements are replaced |
||
222 | * @param array $array2 The array from which elements will be extracted |
||
223 | * |
||
224 | * @return array Returns an array, or NULL if an error occurs. |
||
225 | */ |
||
226 | public static function array_replace_recursive($array1, $array2) |
||
227 | { |
||
228 | if (function_exists('array_replace_recursive')) { |
||
229 | return array_replace_recursive($array1, $array2); |
||
230 | } |
||
231 | |||
232 | foreach ($array2 as $key => $value) { |
||
233 | if (is_array($value)) { |
||
234 | $array1[$key] = self::array_replace_recursive($array1[$key], $value); |
||
235 | } else { |
||
236 | $array1[$key] = $value; |
||
237 | } |
||
238 | } |
||
239 | return $array1; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Keyed by table name, with the value as the conditions: |
||
244 | * e.g. 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH AND deleted=0' |
||
245 | * |
||
246 | * @param array $tableWheres |
||
247 | */ |
||
248 | public function setTableWheres(array $tableWheres) |
||
249 | { |
||
250 | $this->tableWheres = $tableWheres; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param $tableName |
||
255 | * |
||
256 | * @return boolean|mixed |
||
257 | */ |
||
258 | public function getTableWhere($tableName) |
||
259 | { |
||
260 | if (!empty($this->tableWheres[$tableName])) { |
||
261 | return $this->tableWheres[$tableName]; |
||
262 | } elseif ($this->dumpSettings['where']) { |
||
263 | return $this->dumpSettings['where']; |
||
264 | } |
||
265 | |||
266 | return false; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Keyed by table name, with the value as the numeric limit: |
||
271 | * e.g. 'users' => 3000 |
||
272 | * |
||
273 | * @param array $tableLimits |
||
274 | */ |
||
275 | public function setTableLimits(array $tableLimits) |
||
276 | { |
||
277 | $this->tableLimits = $tableLimits; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Returns the LIMIT for the table. Must be numeric to be returned. |
||
282 | * @param $tableName |
||
283 | * @return boolean |
||
284 | */ |
||
285 | public function getTableLimit($tableName) |
||
286 | { |
||
287 | if (empty($this->tableLimits[$tableName])) { |
||
288 | return false; |
||
289 | } |
||
290 | |||
291 | $limit = $this->tableLimits[$tableName]; |
||
292 | if (!is_numeric($limit)) { |
||
293 | return false; |
||
294 | } |
||
295 | |||
296 | return $limit; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Parse DSN string and extract dbname value |
||
301 | * Several examples of a DSN string |
||
302 | * mysql:host=localhost;dbname=testdb |
||
303 | * mysql:host=localhost;port=3307;dbname=testdb |
||
304 | * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
305 | * |
||
306 | * @param string $dsn dsn string to parse |
||
307 | * @return boolean |
||
308 | */ |
||
309 | private function parseDsn($dsn) |
||
310 | { |
||
311 | if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) { |
||
312 | throw new Exception("Empty DSN string"); |
||
313 | } |
||
314 | |||
315 | $this->dsn = $dsn; |
||
316 | $this->dbType = strtolower(substr($dsn, 0, $pos)); // always returns a string |
||
317 | |||
318 | if (empty($this->dbType)) { |
||
319 | throw new Exception("Missing database type from DSN string"); |
||
320 | } |
||
321 | |||
322 | $dsn = substr($dsn, $pos + 1); |
||
323 | |||
324 | foreach (explode(";", $dsn) as $kvp) { |
||
325 | $kvpArr = explode("=", $kvp); |
||
326 | $this->dsnArray[strtolower($kvpArr[0])] = $kvpArr[1]; |
||
327 | } |
||
328 | |||
329 | if (empty($this->dsnArray['host']) && |
||
330 | empty($this->dsnArray['unix_socket'])) { |
||
331 | throw new Exception("Missing host from DSN string"); |
||
332 | } |
||
333 | $this->host = (!empty($this->dsnArray['host'])) ? |
||
334 | $this->dsnArray['host'] : $this->dsnArray['unix_socket']; |
||
335 | |||
336 | if (empty($this->dsnArray['dbname'])) { |
||
337 | throw new Exception("Missing database name from DSN string"); |
||
338 | } |
||
339 | |||
340 | $this->dbName = $this->dsnArray['dbname']; |
||
341 | |||
342 | return true; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Connect with PDO. |
||
347 | * |
||
348 | * @return null |
||
349 | */ |
||
350 | private function connect() |
||
351 | { |
||
352 | // Connecting with PDO. |
||
353 | try { |
||
354 | switch ($this->dbType) { |
||
355 | case 'sqlite': |
||
356 | $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
||
357 | break; |
||
358 | case 'mysql': |
||
359 | case 'pgsql': |
||
360 | case 'dblib': |
||
361 | $this->dbHandler = @new PDO( |
||
362 | $this->dsn, |
||
363 | $this->user, |
||
364 | $this->pass, |
||
365 | $this->pdoSettings |
||
366 | ); |
||
367 | // Execute init commands once connected |
||
368 | foreach ($this->dumpSettings['init_commands'] as $stmt) { |
||
369 | $this->dbHandler->exec($stmt); |
||
370 | } |
||
371 | // Store server version |
||
372 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
373 | break; |
||
374 | default: |
||
375 | throw new Exception("Unsupported database type (".$this->dbType.")"); |
||
376 | } |
||
377 | } catch (PDOException $e) { |
||
378 | throw new Exception( |
||
379 | "Connection to ".$this->dbType." failed with message: ". |
||
380 | $e->getMessage() |
||
381 | ); |
||
382 | } |
||
383 | |||
384 | if (is_null($this->dbHandler)) { |
||
385 | throw new Exception("Connection to ".$this->dbType."failed"); |
||
386 | } |
||
387 | |||
388 | $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
389 | $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Primary function, triggers dumping. |
||
394 | * |
||
395 | * @param string $filename Name of file to write sql dump to |
||
396 | * @return null |
||
397 | * @throws \Exception |
||
398 | */ |
||
399 | public function start($filename = '') |
||
400 | { |
||
401 | // Output file can be redefined here |
||
402 | if (!empty($filename)) { |
||
403 | $this->fileName = $filename; |
||
404 | } |
||
405 | |||
406 | // Connect to database |
||
407 | $this->connect(); |
||
408 | |||
409 | // Create output file |
||
410 | $this->compressManager->open($this->fileName); |
||
411 | |||
412 | // Write some basic info to output file |
||
413 | $this->compressManager->write($this->getDumpFileHeader()); |
||
414 | |||
415 | // Store server settings and use sanner defaults to dump |
||
416 | $this->compressManager->write( |
||
417 | $this->typeAdapter->backup_parameters() |
||
418 | ); |
||
419 | |||
420 | if ($this->dumpSettings['databases']) { |
||
421 | $this->compressManager->write( |
||
422 | $this->typeAdapter->getDatabaseHeader($this->dbName) |
||
423 | ); |
||
424 | if ($this->dumpSettings['add-drop-database']) { |
||
425 | $this->compressManager->write( |
||
426 | $this->typeAdapter->add_drop_database($this->dbName) |
||
427 | ); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | // Get table, view, trigger, procedures, functions and events structures from |
||
432 | // database. |
||
433 | $this->getDatabaseStructureTables(); |
||
434 | $this->getDatabaseStructureViews(); |
||
435 | $this->getDatabaseStructureTriggers(); |
||
436 | $this->getDatabaseStructureProcedures(); |
||
437 | $this->getDatabaseStructureFunctions(); |
||
438 | $this->getDatabaseStructureEvents(); |
||
439 | |||
440 | if ($this->dumpSettings['databases']) { |
||
441 | $this->compressManager->write( |
||
442 | $this->typeAdapter->databases($this->dbName) |
||
443 | ); |
||
444 | } |
||
445 | |||
446 | // If there still are some tables/views in include-tables array, |
||
447 | // that means that some tables or views weren't found. |
||
448 | // Give proper error and exit. |
||
449 | // This check will be removed once include-tables supports regexps. |
||
450 | if (0 < count($this->dumpSettings['include-tables'])) { |
||
451 | $name = implode(",", $this->dumpSettings['include-tables']); |
||
452 | throw new Exception("Table (".$name.") not found in database"); |
||
453 | } |
||
454 | |||
455 | $this->exportTables(); |
||
456 | $this->exportTriggers(); |
||
457 | $this->exportFunctions(); |
||
458 | $this->exportProcedures(); |
||
459 | $this->exportViews(); |
||
460 | $this->exportEvents(); |
||
461 | |||
462 | // Restore saved parameters. |
||
463 | $this->compressManager->write( |
||
464 | $this->typeAdapter->restore_parameters() |
||
465 | ); |
||
466 | // Write some stats to output file. |
||
467 | $this->compressManager->write($this->getDumpFileFooter()); |
||
468 | // Close output file. |
||
469 | $this->compressManager->close(); |
||
470 | |||
471 | return; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Returns header for dump file. |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | private function getDumpFileHeader() |
||
480 | { |
||
481 | $header = ''; |
||
482 | if (!$this->dumpSettings['skip-comments']) { |
||
483 | // Some info about software, source and time |
||
484 | $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
||
485 | "--".PHP_EOL. |
||
486 | "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
||
487 | "-- ------------------------------------------------------".PHP_EOL; |
||
488 | |||
489 | if (!empty($this->version)) { |
||
490 | $header .= "-- Server version \t".$this->version.PHP_EOL; |
||
491 | } |
||
492 | |||
493 | if (!$this->dumpSettings['skip-dump-date']) { |
||
494 | $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
||
495 | } |
||
496 | } |
||
497 | return $header; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Returns footer for dump file. |
||
502 | * |
||
503 | * @return string |
||
504 | */ |
||
505 | private function getDumpFileFooter() |
||
506 | { |
||
507 | $footer = ''; |
||
508 | if (!$this->dumpSettings['skip-comments']) { |
||
509 | $footer .= '-- Dump completed'; |
||
510 | if (!$this->dumpSettings['skip-dump-date']) { |
||
511 | $footer .= ' on: '.date('r'); |
||
512 | } |
||
513 | $footer .= PHP_EOL; |
||
514 | } |
||
515 | |||
516 | return $footer; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Reads table names from database. |
||
521 | * Fills $this->tables array so they will be dumped later. |
||
522 | * |
||
523 | * @return null |
||
524 | */ |
||
525 | private function getDatabaseStructureTables() |
||
526 | { |
||
527 | // Listing all tables from database |
||
528 | if (empty($this->dumpSettings['include-tables'])) { |
||
529 | // include all tables for now, blacklisting happens later |
||
530 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
531 | array_push($this->tables, current($row)); |
||
532 | } |
||
533 | } else { |
||
534 | // include only the tables mentioned in include-tables |
||
535 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
536 | if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
||
537 | array_push($this->tables, current($row)); |
||
538 | $elem = array_search( |
||
539 | current($row), |
||
540 | $this->dumpSettings['include-tables'] |
||
541 | ); |
||
542 | unset($this->dumpSettings['include-tables'][$elem]); |
||
543 | } |
||
544 | } |
||
545 | } |
||
546 | return; |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Reads view names from database. |
||
551 | * Fills $this->tables array so they will be dumped later. |
||
552 | * |
||
553 | * @return null |
||
554 | */ |
||
555 | private function getDatabaseStructureViews() |
||
556 | { |
||
557 | // Listing all views from database |
||
558 | if (empty($this->dumpSettings['include-views'])) { |
||
559 | // include all views for now, blacklisting happens later |
||
560 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
561 | array_push($this->views, current($row)); |
||
562 | } |
||
563 | } else { |
||
564 | // include only the tables mentioned in include-tables |
||
565 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
566 | if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
||
567 | array_push($this->views, current($row)); |
||
568 | $elem = array_search( |
||
569 | current($row), |
||
570 | $this->dumpSettings['include-views'] |
||
571 | ); |
||
572 | unset($this->dumpSettings['include-views'][$elem]); |
||
573 | } |
||
574 | } |
||
575 | } |
||
576 | return; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * Reads trigger names from database. |
||
581 | * Fills $this->tables array so they will be dumped later. |
||
582 | * |
||
583 | * @return null |
||
584 | */ |
||
585 | private function getDatabaseStructureTriggers() |
||
586 | { |
||
587 | // Listing all triggers from database |
||
588 | if (false === $this->dumpSettings['skip-triggers']) { |
||
589 | foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { |
||
590 | array_push($this->triggers, $row['Trigger']); |
||
591 | } |
||
592 | } |
||
593 | return; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * Reads procedure names from database. |
||
598 | * Fills $this->tables array so they will be dumped later. |
||
599 | * |
||
600 | * @return null |
||
601 | */ |
||
602 | private function getDatabaseStructureProcedures() |
||
603 | { |
||
604 | // Listing all procedures from database |
||
605 | if ($this->dumpSettings['routines']) { |
||
606 | foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { |
||
607 | array_push($this->procedures, $row['procedure_name']); |
||
608 | } |
||
609 | } |
||
610 | return; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Reads functions names from database. |
||
615 | * Fills $this->tables array so they will be dumped later. |
||
616 | * |
||
617 | * @return null |
||
618 | */ |
||
619 | private function getDatabaseStructureFunctions() |
||
620 | { |
||
621 | // Listing all functions from database |
||
622 | if ($this->dumpSettings['routines']) { |
||
623 | foreach ($this->dbHandler->query($this->typeAdapter->show_functions($this->dbName)) as $row) { |
||
624 | array_push($this->functions, $row['function_name']); |
||
625 | } |
||
626 | } |
||
627 | return; |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * Reads event names from database. |
||
632 | * Fills $this->tables array so they will be dumped later. |
||
633 | * |
||
634 | * @return null |
||
635 | */ |
||
636 | private function getDatabaseStructureEvents() |
||
637 | { |
||
638 | // Listing all events from database |
||
639 | if ($this->dumpSettings['events']) { |
||
640 | foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { |
||
641 | array_push($this->events, $row['event_name']); |
||
642 | } |
||
643 | } |
||
644 | return; |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Compare if $table name matches with a definition inside $arr |
||
649 | * @param $table string |
||
650 | * @param $arr array with strings or patterns |
||
651 | * @return boolean |
||
652 | */ |
||
653 | private function matches($table, $arr) |
||
654 | { |
||
655 | $match = false; |
||
656 | |||
657 | foreach ($arr as $pattern) { |
||
658 | if ('/' != $pattern[0]) { |
||
659 | continue; |
||
660 | } |
||
661 | if (1 == preg_match($pattern, $table)) { |
||
662 | $match = true; |
||
663 | } |
||
664 | } |
||
665 | |||
666 | return in_array($table, $arr) || $match; |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * Exports all the tables selected from database |
||
671 | * |
||
672 | * @return null |
||
673 | */ |
||
674 | private function exportTables() |
||
675 | { |
||
676 | // Exporting tables one by one |
||
677 | foreach ($this->tables as $table) { |
||
678 | if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { |
||
679 | continue; |
||
680 | } |
||
681 | $this->getTableStructure($table); |
||
682 | if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger |
||
683 | $this->listValues($table); |
||
684 | } elseif (true === $this->dumpSettings['no-data'] |
||
685 | || $this->matches($table, $this->dumpSettings['no-data'])) { |
||
686 | continue; |
||
687 | } else { |
||
688 | $this->listValues($table); |
||
689 | } |
||
690 | } |
||
691 | } |
||
692 | |||
693 | /** |
||
694 | * Exports all the views found in database |
||
695 | * |
||
696 | * @return null |
||
697 | */ |
||
698 | private function exportViews() |
||
699 | { |
||
700 | if (false === $this->dumpSettings['no-create-info']) { |
||
701 | // Exporting views one by one |
||
702 | foreach ($this->views as $view) { |
||
703 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
704 | continue; |
||
705 | } |
||
706 | $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); |
||
707 | $this->getViewStructureTable($view); |
||
708 | } |
||
709 | foreach ($this->views as $view) { |
||
710 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
711 | continue; |
||
712 | } |
||
713 | $this->getViewStructureView($view); |
||
714 | } |
||
715 | } |
||
716 | } |
||
717 | |||
718 | /** |
||
719 | * Exports all the triggers found in database |
||
720 | * |
||
721 | * @return null |
||
722 | */ |
||
723 | private function exportTriggers() |
||
724 | { |
||
725 | // Exporting triggers one by one |
||
726 | foreach ($this->triggers as $trigger) { |
||
727 | $this->getTriggerStructure($trigger); |
||
728 | } |
||
729 | |||
730 | } |
||
731 | |||
732 | /** |
||
733 | * Exports all the procedures found in database |
||
734 | * |
||
735 | * @return null |
||
736 | */ |
||
737 | private function exportProcedures() |
||
738 | { |
||
739 | // Exporting triggers one by one |
||
740 | foreach ($this->procedures as $procedure) { |
||
741 | $this->getProcedureStructure($procedure); |
||
742 | } |
||
743 | } |
||
744 | |||
745 | /** |
||
746 | * Exports all the functions found in database |
||
747 | * |
||
748 | * @return null |
||
749 | */ |
||
750 | private function exportFunctions() |
||
751 | { |
||
752 | // Exporting triggers one by one |
||
753 | foreach ($this->functions as $function) { |
||
754 | $this->getFunctionStructure($function); |
||
755 | } |
||
756 | } |
||
757 | |||
758 | /** |
||
759 | * Exports all the events found in database |
||
760 | * |
||
761 | * @return null |
||
762 | */ |
||
763 | private function exportEvents() |
||
764 | { |
||
765 | // Exporting triggers one by one |
||
766 | foreach ($this->events as $event) { |
||
767 | $this->getEventStructure($event); |
||
768 | } |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * Table structure extractor |
||
773 | * |
||
774 | * @todo move specific mysql code to typeAdapter |
||
775 | * @param string $tableName Name of table to export |
||
776 | * @return null |
||
777 | */ |
||
778 | private function getTableStructure($tableName) |
||
779 | { |
||
780 | if (!$this->dumpSettings['no-create-info']) { |
||
781 | $ret = ''; |
||
782 | if (!$this->dumpSettings['skip-comments']) { |
||
783 | $ret = "--".PHP_EOL. |
||
784 | "-- Table structure for table `$tableName`".PHP_EOL. |
||
785 | "--".PHP_EOL.PHP_EOL; |
||
786 | } |
||
787 | $stmt = $this->typeAdapter->show_create_table($tableName); |
||
788 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
789 | $this->compressManager->write($ret); |
||
790 | if ($this->dumpSettings['add-drop-table']) { |
||
791 | $this->compressManager->write( |
||
792 | $this->typeAdapter->drop_table($tableName) |
||
793 | ); |
||
794 | } |
||
795 | $this->compressManager->write( |
||
796 | $this->typeAdapter->create_table($r) |
||
797 | ); |
||
798 | break; |
||
799 | } |
||
800 | } |
||
801 | $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); |
||
802 | return; |
||
803 | } |
||
804 | |||
805 | /** |
||
806 | * Store column types to create data dumps and for Stand-In tables |
||
807 | * |
||
808 | * @param string $tableName Name of table to export |
||
809 | * @return array type column types detailed |
||
810 | */ |
||
811 | |||
812 | private function getTableColumnTypes($tableName) |
||
813 | { |
||
814 | $columnTypes = array(); |
||
815 | $columns = $this->dbHandler->query( |
||
816 | $this->typeAdapter->show_columns($tableName) |
||
817 | ); |
||
818 | $columns->setFetchMode(PDO::FETCH_ASSOC); |
||
819 | |||
820 | foreach ($columns as $key => $col) { |
||
821 | $types = $this->typeAdapter->parseColumnType($col); |
||
822 | $columnTypes[$col['Field']] = array( |
||
823 | 'is_numeric'=> $types['is_numeric'], |
||
824 | 'is_blob' => $types['is_blob'], |
||
825 | 'type' => $types['type'], |
||
826 | 'type_sql' => $col['Type'], |
||
827 | 'is_virtual' => $types['is_virtual'] |
||
828 | ); |
||
829 | } |
||
830 | |||
831 | return $columnTypes; |
||
832 | } |
||
833 | |||
834 | /** |
||
835 | * View structure extractor, create table (avoids cyclic references) |
||
836 | * |
||
837 | * @todo move mysql specific code to typeAdapter |
||
838 | * @param string $viewName Name of view to export |
||
839 | * @return null |
||
840 | */ |
||
841 | private function getViewStructureTable($viewName) |
||
842 | { |
||
843 | if (!$this->dumpSettings['skip-comments']) { |
||
844 | $ret = "--".PHP_EOL. |
||
845 | "-- Stand-In structure for view `${viewName}`".PHP_EOL. |
||
846 | "--".PHP_EOL.PHP_EOL; |
||
847 | $this->compressManager->write($ret); |
||
848 | } |
||
849 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
850 | |||
851 | // create views as tables, to resolve dependencies |
||
852 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
853 | if ($this->dumpSettings['add-drop-table']) { |
||
854 | $this->compressManager->write( |
||
855 | $this->typeAdapter->drop_view($viewName) |
||
856 | ); |
||
857 | } |
||
858 | |||
859 | $this->compressManager->write( |
||
860 | $this->createStandInTable($viewName) |
||
861 | ); |
||
862 | break; |
||
863 | } |
||
864 | } |
||
865 | |||
866 | /** |
||
867 | * Write a create table statement for the table Stand-In, show create |
||
868 | * table would return a create algorithm when used on a view |
||
869 | * |
||
870 | * @param string $viewName Name of view to export |
||
871 | * @return string create statement |
||
872 | */ |
||
873 | public function createStandInTable($viewName) |
||
874 | { |
||
875 | $ret = array(); |
||
876 | foreach ($this->tableColumnTypes[$viewName] as $k => $v) { |
||
877 | $ret[] = "`${k}` ${v['type_sql']}"; |
||
878 | } |
||
879 | $ret = implode(PHP_EOL.",", $ret); |
||
880 | |||
881 | $ret = "CREATE TABLE IF NOT EXISTS `$viewName` (". |
||
882 | PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; |
||
883 | |||
884 | return $ret; |
||
885 | } |
||
886 | |||
887 | /** |
||
888 | * View structure extractor, create view |
||
889 | * |
||
890 | * @todo move mysql specific code to typeAdapter |
||
891 | * @param string $viewName Name of view to export |
||
892 | * @return null |
||
893 | */ |
||
894 | private function getViewStructureView($viewName) |
||
895 | { |
||
896 | if (!$this->dumpSettings['skip-comments']) { |
||
897 | $ret = "--".PHP_EOL. |
||
898 | "-- View structure for view `${viewName}`".PHP_EOL. |
||
899 | "--".PHP_EOL.PHP_EOL; |
||
900 | $this->compressManager->write($ret); |
||
901 | } |
||
902 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
903 | |||
904 | // create views, to resolve dependencies |
||
905 | // replacing tables with views |
||
906 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
907 | // because we must replace table with view, we should delete it |
||
908 | $this->compressManager->write( |
||
909 | $this->typeAdapter->drop_view($viewName) |
||
910 | ); |
||
911 | $this->compressManager->write( |
||
912 | $this->typeAdapter->create_view($r) |
||
913 | ); |
||
914 | break; |
||
915 | } |
||
916 | } |
||
917 | |||
918 | /** |
||
919 | * Trigger structure extractor |
||
920 | * |
||
921 | * @param string $triggerName Name of trigger to export |
||
922 | * @return null |
||
923 | */ |
||
924 | private function getTriggerStructure($triggerName) |
||
925 | { |
||
926 | $stmt = $this->typeAdapter->show_create_trigger($triggerName); |
||
927 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
928 | if ($this->dumpSettings['add-drop-trigger']) { |
||
929 | $this->compressManager->write( |
||
930 | $this->typeAdapter->add_drop_trigger($triggerName) |
||
931 | ); |
||
932 | } |
||
933 | $this->compressManager->write( |
||
934 | $this->typeAdapter->create_trigger($r) |
||
935 | ); |
||
936 | return; |
||
937 | } |
||
938 | } |
||
939 | |||
940 | /** |
||
941 | * Procedure structure extractor |
||
942 | * |
||
943 | * @param string $procedureName Name of procedure to export |
||
944 | * @return null |
||
945 | */ |
||
946 | private function getProcedureStructure($procedureName) |
||
947 | { |
||
948 | if (!$this->dumpSettings['skip-comments']) { |
||
949 | $ret = "--".PHP_EOL. |
||
950 | "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
||
951 | "--".PHP_EOL.PHP_EOL; |
||
952 | $this->compressManager->write($ret); |
||
953 | } |
||
954 | $stmt = $this->typeAdapter->show_create_procedure($procedureName); |
||
955 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
956 | $this->compressManager->write( |
||
957 | $this->typeAdapter->create_procedure($r) |
||
958 | ); |
||
959 | return; |
||
960 | } |
||
961 | } |
||
962 | |||
963 | /** |
||
964 | * Function structure extractor |
||
965 | * |
||
966 | * @param string $functionName Name of function to export |
||
967 | * @return null |
||
968 | */ |
||
969 | private function getFunctionStructure($functionName) |
||
970 | { |
||
971 | if (!$this->dumpSettings['skip-comments']) { |
||
972 | $ret = "--".PHP_EOL. |
||
973 | "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
||
974 | "--".PHP_EOL.PHP_EOL; |
||
975 | $this->compressManager->write($ret); |
||
976 | } |
||
977 | $stmt = $this->typeAdapter->show_create_function($functionName); |
||
978 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
979 | $this->compressManager->write( |
||
980 | $this->typeAdapter->create_function($r) |
||
981 | ); |
||
982 | return; |
||
983 | } |
||
984 | } |
||
985 | |||
986 | /** |
||
987 | * Event structure extractor |
||
988 | * |
||
989 | * @param string $eventName Name of event to export |
||
990 | * @return null |
||
991 | */ |
||
992 | private function getEventStructure($eventName) |
||
993 | { |
||
994 | if (!$this->dumpSettings['skip-comments']) { |
||
995 | $ret = "--".PHP_EOL. |
||
996 | "-- Dumping events for database '".$this->dbName."'".PHP_EOL. |
||
997 | "--".PHP_EOL.PHP_EOL; |
||
998 | $this->compressManager->write($ret); |
||
999 | } |
||
1000 | $stmt = $this->typeAdapter->show_create_event($eventName); |
||
1001 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
1002 | $this->compressManager->write( |
||
1003 | $this->typeAdapter->create_event($r) |
||
1004 | ); |
||
1005 | return; |
||
1006 | } |
||
1007 | } |
||
1008 | |||
1009 | /** |
||
1010 | * Prepare values for output |
||
1011 | * |
||
1012 | * @param string $tableName Name of table which contains rows |
||
1013 | * @param array $row Associative array of column names and values to be |
||
1014 | * quoted |
||
1015 | * |
||
1016 | * @return array |
||
1017 | */ |
||
1018 | private function prepareColumnValues($tableName, array $row) |
||
1019 | { |
||
1020 | $ret = array(); |
||
1021 | $columnTypes = $this->tableColumnTypes[$tableName]; |
||
1022 | |||
1023 | if ($this->transformTableRowCallable) { |
||
1024 | $row = call_user_func($this->transformTableRowCallable, $tableName, $row); |
||
1025 | } |
||
1026 | |||
1027 | foreach ($row as $colName => $colValue) { |
||
1028 | if ($this->transformColumnValueCallable) { |
||
1029 | $colValue = call_user_func($this->transformColumnValueCallable, $tableName, $colName, $colValue, $row); |
||
1030 | } |
||
1031 | |||
1032 | $ret[] = $this->escape($colValue, $columnTypes[$colName]); |
||
1033 | } |
||
1034 | |||
1035 | return $ret; |
||
1036 | } |
||
1037 | |||
1038 | /** |
||
1039 | * Escape values with quotes when needed |
||
1040 | * |
||
1041 | * @param string $tableName Name of table which contains rows |
||
1042 | * @param array $row Associative array of column names and values to be quoted |
||
1043 | * |
||
1044 | * @return string |
||
1045 | */ |
||
1046 | private function escape($colValue, $colType) |
||
1047 | { |
||
1048 | if (is_null($colValue)) { |
||
1049 | return "NULL"; |
||
1050 | } elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) { |
||
1051 | if ($colType['type'] == 'bit' || !empty($colValue)) { |
||
1052 | return "0x${colValue}"; |
||
1053 | } else { |
||
1054 | return "''"; |
||
1055 | } |
||
1056 | } elseif ($colType['is_numeric']) { |
||
1057 | return $colValue; |
||
1058 | } |
||
1059 | |||
1060 | return $this->dbHandler->quote($colValue); |
||
1061 | } |
||
1062 | |||
1063 | /** |
||
1064 | * Set a callable that will be used to transform table rows |
||
1065 | * |
||
1066 | * @param callable $callable |
||
1067 | * |
||
1068 | * @return void |
||
1069 | */ |
||
1070 | public function setTransformTableRowHook($callable) |
||
1071 | { |
||
1072 | $this->transformTableRowCallable = $callable; |
||
1073 | } |
||
1074 | |||
1075 | /** |
||
1076 | * Set a callable that will be used to transform column values |
||
1077 | * |
||
1078 | * @param callable $callable |
||
1079 | * |
||
1080 | * @return void |
||
1081 | * |
||
1082 | * @deprecated Use setTransformTableRowHook instead for better performance |
||
1083 | */ |
||
1084 | public function setTransformColumnValueHook($callable) |
||
1085 | { |
||
1086 | $this->transformColumnValueCallable = $callable; |
||
1087 | } |
||
1088 | |||
1089 | /** |
||
1090 | * Set a callable that will be used to report dump information |
||
1091 | * |
||
1092 | * @param callable $callable |
||
1093 | * |
||
1094 | * @return void |
||
1095 | */ |
||
1096 | public function setInfoHook($callable) |
||
1097 | { |
||
1098 | $this->infoCallable = $callable; |
||
1099 | } |
||
1100 | |||
1101 | /** |
||
1102 | * Table rows extractor |
||
1103 | * |
||
1104 | * @param string $tableName Name of table to export |
||
1105 | * |
||
1106 | * @return null |
||
1107 | */ |
||
1108 | private function listValues($tableName) |
||
1178 | } |
||
1179 | } |
||
1180 | |||
1181 | /** |
||
1182 | * Table rows extractor, append information prior to dump |
||
1183 | * |
||
1184 | * @param string $tableName Name of table to export |
||
1185 | * |
||
1186 | * @return null |
||
1187 | */ |
||
1188 | public function prepareListValues($tableName) |
||
1189 | { |
||
1190 | if (!$this->dumpSettings['skip-comments']) { |
||
1191 | $this->compressManager->write( |
||
1192 | "--".PHP_EOL. |
||
1193 | "-- Dumping data for table `$tableName`".PHP_EOL. |
||
1194 | "--".PHP_EOL.PHP_EOL |
||
1195 | ); |
||
1196 | } |
||
1197 | |||
1198 | if ($this->dumpSettings['single-transaction']) { |
||
1199 | $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
||
1200 | $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
||
1201 | } |
||
1202 | |||
1203 | if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { |
||
1204 | $this->typeAdapter->lock_table($tableName); |
||
1205 | } |
||
1206 | |||
1207 | if ($this->dumpSettings['add-locks']) { |
||
1208 | $this->compressManager->write( |
||
1209 | $this->typeAdapter->start_add_lock_table($tableName) |
||
1210 | ); |
||
1211 | } |
||
1212 | |||
1213 | if ($this->dumpSettings['disable-keys']) { |
||
1214 | $this->compressManager->write( |
||
1215 | $this->typeAdapter->start_add_disable_keys($tableName) |
||
1216 | ); |
||
1217 | } |
||
1218 | |||
1219 | // Disable autocommit for faster reload |
||
1220 | if ($this->dumpSettings['no-autocommit']) { |
||
1221 | $this->compressManager->write( |
||
1222 | $this->typeAdapter->start_disable_autocommit() |
||
1223 | ); |
||
1224 | } |
||
1225 | |||
1226 | return; |
||
1227 | } |
||
1228 | |||
1229 | /** |
||
1230 | * Table rows extractor, close locks and commits after dump |
||
1231 | * |
||
1232 | * @param string $tableName Name of table to export. |
||
1233 | * @param integer $count Number of rows inserted. |
||
1234 | * |
||
1235 | * @return void |
||
1236 | */ |
||
1237 | public function endListValues($tableName, $count = 0) |
||
1238 | { |
||
1239 | if ($this->dumpSettings['disable-keys']) { |
||
1240 | $this->compressManager->write( |
||
1241 | $this->typeAdapter->end_add_disable_keys($tableName) |
||
1242 | ); |
||
1243 | } |
||
1244 | |||
1245 | if ($this->dumpSettings['add-locks']) { |
||
1246 | $this->compressManager->write( |
||
1247 | $this->typeAdapter->end_add_lock_table($tableName) |
||
1248 | ); |
||
1249 | } |
||
1250 | |||
1251 | if ($this->dumpSettings['single-transaction']) { |
||
1252 | $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
||
1253 | } |
||
1254 | |||
1255 | if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { |
||
1256 | $this->typeAdapter->unlock_table($tableName); |
||
1257 | } |
||
1258 | |||
1259 | // Commit to enable autocommit |
||
1260 | if ($this->dumpSettings['no-autocommit']) { |
||
1261 | $this->compressManager->write( |
||
1262 | $this->typeAdapter->end_disable_autocommit() |
||
1263 | ); |
||
1264 | } |
||
1265 | |||
1266 | $this->compressManager->write(PHP_EOL); |
||
1267 | |||
1268 | if (!$this->dumpSettings['skip-comments']) { |
||
1269 | $this->compressManager->write( |
||
1270 | "-- Dumped table `".$tableName."` with $count row(s)".PHP_EOL. |
||
1271 | '--'.PHP_EOL.PHP_EOL |
||
1272 | ); |
||
1273 | } |
||
1274 | |||
1275 | return; |
||
1276 | } |
||
1277 | |||
1278 | /** |
||
1279 | * Build SQL List of all columns on current table which will be used for selecting |
||
1280 | * |
||
1281 | * @param string $tableName Name of table to get columns |
||
1282 | * |
||
1283 | * @return array SQL sentence with columns for select |
||
1284 | */ |
||
1285 | public function getColumnStmt($tableName) |
||
1286 | { |
||
1287 | $colStmt = array(); |
||
1288 | foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
||
1289 | if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
||
1290 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
||
1291 | } elseif ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
||
1292 | $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
||
1293 | } elseif ($colType['is_virtual']) { |
||
1294 | $this->dumpSettings['complete-insert'] = true; |
||
1295 | continue; |
||
1296 | } else { |
||
1297 | $colStmt[] = "`${colName}`"; |
||
1298 | } |
||
1299 | } |
||
1300 | |||
1301 | return $colStmt; |
||
1302 | } |
||
1303 | |||
1304 | /** |
||
1305 | * Build SQL List of all columns on current table which will be used for inserting |
||
1306 | * |
||
1307 | * @param string $tableName Name of table to get columns |
||
1308 | * |
||
1309 | * @return array columns for sql sentence for insert |
||
1310 | */ |
||
1311 | public function getColumnNames($tableName) |
||
1323 | } |
||
1324 | } |
||
1325 | |||
1326 | /** |
||
1327 | * Enum with all available compression methods |
||
1328 | * |
||
1329 | */ |
||
1330 | abstract class CompressMethod |
||
1331 | { |
||
1332 | public static $enums = array( |
||
1333 | Mysqldump::NONE, |
||
1334 | Mysqldump::GZIP, |
||
1335 | Mysqldump::BZIP2, |
||
1336 | Mysqldump::GZIPSTREAM, |
||
1337 | ); |
||
1338 | |||
1339 | /** |
||
1340 | * @param string $c |
||
1341 | * @return boolean |
||
1342 | */ |
||
1343 | public static function isValid($c) |
||
1344 | { |
||
1345 | return in_array($c, self::$enums); |
||
1346 | } |
||
1347 | } |
||
1348 | |||
1349 | abstract class CompressManagerFactory |
||
1350 | { |
||
1351 | /** |
||
1352 | * @param string $c |
||
1353 | * @return CompressBzip2|CompressGzip|CompressNone |
||
1354 | */ |
||
1355 | public static function create($c) |
||
1356 | { |
||
1357 | $c = ucfirst(strtolower($c)); |
||
1358 | if (!CompressMethod::isValid($c)) { |
||
1359 | throw new Exception("Compression method ($c) is not defined yet"); |
||
1360 | } |
||
1361 | |||
1362 | $method = __NAMESPACE__."\\"."Compress".$c; |
||
1363 | |||
1364 | return new $method; |
||
1365 | } |
||
2337 |