N98\Util\Console\Helper\DatabaseHelper::detectDbSettings PHP Method

detectDbSettings() public method

public detectDbSettings ( Symfony\Component\Console\Output\OutputInterface $output )
$output Symfony\Component\Console\Output\OutputInterface
    public function detectDbSettings(OutputInterface $output)
    {
        if (null !== $this->dbSettings) {
            return;
        }
        $application = $this->getApplication();
        $application->detectMagento();
        $configFile = $application->getMagentoRootFolder() . '/app/etc/local.xml';
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
            $output->writeln(sprintf('<debug>Loading database configuration from file <info>%s</info></debug>', $configFile));
        }
        try {
            $this->dbSettings = new DbSettings($configFile);
        } catch (InvalidArgumentException $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            throw new RuntimeException('Failed to load database settings from config file', 0, $e);
        }
    }

Usage Example

Example #1
0
 /**
  * PHP alternative to dump database without exec
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @throws \Exception
  */
 private function createBackupWithoutExec(InputInterface $input, OutputInterface $output)
 {
     $magerun = $this->getMagerun();
     $filePath = $this->getFilePath($input);
     $stripOptions = $input->getOption('strip') ?: '@development';
     // Exec must be unavailable so use PHP alternative (match output)
     $dbHelper = new DatabaseHelper();
     $dbHelper->setHelperSet($magerun->getHelperSet());
     $dbHelper->detectDbSettings(new NullOutput());
     $magerunConfig = $magerun->getConfig();
     $stripTables = $dbHelper->resolveTables(explode(' ', $stripOptions), $dbHelper->getTableDefinitions($magerunConfig['commands']['N98\\Magento\\Command\\Database\\DumpCommand']));
     $output->writeln(array('', $magerun->getHelperSet()->get('formatter')->formatBlock('Dump MySQL Database (without exec)', 'bg=blue;fg=white', true), ''));
     $dbSettings = $dbHelper->getDbSettings();
     $username = (string) $dbSettings['username'];
     $password = (string) $dbSettings['password'];
     $dbName = (string) $dbSettings['dbname'];
     try {
         $output->writeln('<comment>No-data export for: <info>' . implode(' ', $stripTables) . '</info></comment>');
         $output->writeln('<comment>Start dumping database <info>' . $dbSettings['dbname'] . '</info> to file <info>' . $filePath . '</info>');
         // Dump Structure for tables that we are not to receive data from
         $dumpStructure = new Mysqldump(sprintf('%s;dbname=%s', $dbHelper->dsn(), $dbName), $username, $password, array('include-tables' => $stripTables, 'no-data' => true));
         $dumpStructure->start($filePath . '.structure');
         $dump = new Mysqldump(sprintf('%s;dbname=%s', $dbHelper->dsn(), $dbName), $username, $password, array('exclude-tables' => $stripTables));
         $dump->start($filePath . '.data');
         // Now merge two files
         $fhData = fopen($filePath . '.data', 'a+');
         $fhStructure = fopen($filePath . '.structure', 'r');
         if ($fhData && $fhStructure) {
             while (!feof($fhStructure)) {
                 fwrite($fhData, fgets($fhStructure, 4096));
             }
         }
         fclose($fhStructure);
         // Gzip
         rewind($fhData);
         $zfh = gzopen($filePath, 'wb');
         while (!feof($fhData)) {
             gzwrite($zfh, fgets($fhData, 4096));
         }
         gzclose($zfh);
         fclose($fhData);
     } catch (\Exception $e) {
         throw new \Exception("Unable to export database.");
     }
 }
All Usage Examples Of N98\Util\Console\Helper\DatabaseHelper::detectDbSettings