MySQLDump::write PHP Method

write() public method

Writes dump to logical file.
public write ( $handle = NULL ) : void
return void
    public function write($handle = NULL)
    {
        if ($handle === NULL) {
            $handle = fopen('php://output', 'wb');
        } elseif (!is_resource($handle) || get_resource_type($handle) !== 'stream') {
            throw new Exception('Argument must be stream resource.');
        }
        $tables = $views = array();
        $res = $this->connection->query('SHOW FULL TABLES');
        while ($row = $res->fetch_row()) {
            if ($row[1] === 'VIEW') {
                $views[] = $row[0];
            } else {
                $tables[] = $row[0];
            }
        }
        $res->close();
        $tables = array_merge($tables, $views);
        // views must be last
        $this->connection->query('LOCK TABLES `' . implode('` READ, `', $tables) . '` READ');
        $db = $this->connection->query('SELECT DATABASE()')->fetch_row();
        fwrite($handle, "-- Created at " . date('j.n.Y G:i') . " using David Grudl MySQL Dump Utility\n" . (isset($_SERVER['HTTP_HOST']) ? "-- Host: {$_SERVER['HTTP_HOST']}\n" : '') . "-- MySQL Server: " . $this->connection->server_info . "\n" . "-- Database: " . $db[0] . "\n" . "\n" . "SET NAMES utf8;\n" . "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\n" . "SET FOREIGN_KEY_CHECKS=0;\n");
        foreach ($tables as $table) {
            $this->dumpTable($handle, $table);
        }
        fwrite($handle, "-- THE END\n");
        $this->connection->query('UNLOCK TABLES');
    }

Usage Example

Ejemplo n.º 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $host = $input->getOption('host');
     $user = $input->getOption('user');
     $pass = $input->getOption('pass');
     $dbName = $input->getArgument('name');
     try {
         $this->dump = $this->pdoFactory->makeDump($host, $user, $pass, $dbName);
     } catch (\PDOException $e) {
         throw new RuntimeException('Error while connecting to database [' . $dbName . ']: ' . $e->getMessage());
     }
     if (false === $this->dump) {
         $output->writeln('<error>Something went wrong with the dump component instance.</error>');
         return false;
     }
     \Codeception\Configuration::config();
     if (!empty($input->getOption('dump-file'))) {
         $dumpFile = $input->getOption('dump-file');
     } else {
         $dumpFile = codecept_data_dir($input->getArgument('snapshot') . '.sql');
     }
     $output->writeln('<info>Dump file will be written to [' . $dumpFile . ']</info>');
     if (!empty($input->getOption('dist-dump-file'))) {
         $distDumpFile = $input->getOption('dist-dump-file');
     } else {
         $distDumpFile = codecept_data_dir($input->getArgument('snapshot') . '.dist.sql');
     }
     $output->writeln('<info>Distribution version of dump file will be written to [' . $distDumpFile . ']</info>');
     $skipTables = $input->getOption('skip-tables');
     if (!empty($skipTables)) {
         $tables = explode(',', $skipTables);
         foreach ($tables as $table) {
             $this->dump->tables[$table] = \MySQLDump::NONE;
         }
     }
     $memory = fopen('php://memory', 'w');
     $this->dump->write($memory);
     rewind($memory);
     $dumpContents = stream_get_contents($memory);
     if (!$this->filesystem->file_put_contents($dumpFile, $dumpContents)) {
         $output->writeln('<error>Could not write dump to [' . $dumpFile . ']</error>');
         return false;
     }
     $output->writeln('<info>Dump file written to [' . $dumpFile . ']</info>');
     $localUrl = $input->getOption('local-url');
     $distUrl = $input->getOption('dist-url');
     $localDomain = rtrim(preg_replace('~http(s)*:\\/\\/(www\\.)*~', '', $localUrl), '/');
     $distDomain = rtrim(preg_replace('~http(s)*:\\/\\/(www\\.)*~', '', $distUrl), '/');
     $distDumpContents = str_replace($localDomain, $distDomain, $dumpContents);
     if (!$this->filesystem->file_put_contents($distDumpFile, $distDumpContents)) {
         $output->writeln('<error>Could not write dist dump to [' . $distDumpFile . ']</error>');
         return false;
     }
     $output->writeln('<info>Distribution version of dump file written to [' . $distDumpFile . ']</info>');
     $output->writeln('<comment>Any occurrence of [' . $localDomain . '] in it was replaced with [' . $distDomain . ']</comment>');
     parent::execute($input, $output);
     return true;
 }
All Usage Examples Of MySQLDump::write