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; }
/** * @param $file * @param $setupFileLines */ protected function writeSetupFile($file, $setupFileLines) { $header = $this->getSetupFileHeaderLines(); $yamlDump = Yaml::dump($setupFileLines, 100); $written = $this->filesystem->file_put_contents($file, $header . "\n\n" . $yamlDump); if ($written === false) { throw new RuntimeException('Could not write setup scaffold file to [' . $file . ']'); } }