/** * Event fired on database connection failure. * * @param FailedConnectionEvent $args * * @throws BootException */ public function failConnect(FailedConnectionEvent $args) { $e = $args->getException(); $this->logger->debug($e->getMessage(), ['event' => 'exception', 'exception' => $e]); /* * Using Driver here since Platform may try to connect * to the database, which has failed since we are here. */ $platform = $args->getDriver()->getName(); $platform = Str::replaceFirst('pdo_', '', $platform); $response = $this->exceptionController->databaseConnect($platform, $e); throw new BootException($e->getMessage(), $e->getCode(), $e, $response); }
/** * Event fired on database connection failure. * * @param FailedConnectionEvent $args * * @throws LowLevelDatabaseException */ public function failConnect(FailedConnectionEvent $args) { $e = $args->getException(); $this->logger->debug($e->getMessage(), ['event' => 'exception', 'exception' => $e]); // Trap double exceptions set_exception_handler(function () { }); /* * Using Driver here since Platform may try to connect * to the database, which has failed since we are here. */ $platform = $args->getDriver()->getName(); $platform = Str::replaceFirst('pdo_', '', $platform); throw LowLevelDatabaseException::failedConnect($platform, $e); }
/** * Set up the DBAL connection now to check for a proper connection to the database. * * @throws LowlevelException */ protected function checkDatabaseConnection() { // [SECURITY]: If we get an error trying to connect to database, we throw a new // LowLevelException with general information to avoid leaking connection information. try { $this['db']->connect(); // A ConnectionException or DriverException could be thrown, we'll catch DBALException to be safe. } catch (DBALException $e) { // Trap double exceptions caused by throwing a new LowlevelException set_exception_handler(array('\\Bolt\\Exception\\LowlevelException', 'nullHandler')); /* * Using Driver here since Platform may try to connect * to the database, which has failed since we are here. */ $platform = $this['db']->getDriver()->getName(); $platform = Str::replaceFirst('pdo_', '', $platform); $error = "Bolt could not connect to the configured database.\n\n" . "Things to check:\n" . " * Ensure the {$platform} database is running\n" . " * Check the <code>database:</code> parameters are configured correctly in <code>app/config/config.yml</code>\n" . " * Database name is correct\n" . " * User name has access to the named database\n" . " * Password is correct\n"; throw new LowlevelException($error); } // Resume normal error handling restore_error_handler(); }
/** * Parse and fine-tune the database configuration. * * @param array $options * * @return array */ protected function parseDatabase(array $options) { // Make sure prefix ends with underscore if (substr($options['prefix'], strlen($options['prefix']) - 1) !== '_') { $options['prefix'] .= '_'; } // Parse master connection parameters $master = $this->parseConnectionParams($options); // Merge master connection into options $options = array_replace($options, $master); // Add platform specific random functions $driver = Str::replaceFirst('pdo_', '', $options['driver']); if ($driver === 'sqlite') { $options['driver'] = 'pdo_sqlite'; $options['randomfunction'] = 'RANDOM()'; } elseif (in_array($driver, ['mysql', 'mysqli'])) { $options['driver'] = 'pdo_mysql'; $options['randomfunction'] = 'RAND()'; } elseif (in_array($driver, ['pgsql', 'postgres', 'postgresql'])) { $options['driver'] = 'pdo_pgsql'; $options['randomfunction'] = 'RANDOM()'; } // Specify the wrapper class for the connection $options['wrapperClass'] = '\\Bolt\\Storage\\Database\\Connection'; // Parse SQLite separately since it has to figure out database path if ($driver === 'sqlite') { return $this->parseSqliteOptions($options); } // If no slaves return with single connection if (empty($options['slaves'])) { return $options; } // Specify we want a master slave connection $options['wrapperClass'] = '\\Bolt\\Storage\\Database\\MasterSlaveConnection'; // Add master connection where MasterSlaveConnection looks for it. $options['master'] = $master; // Parse each slave connection parameters foreach ($options['slaves'] as $name => $slave) { $options['slaves'][$name] = $this->parseConnectionParams($slave, $master); } return $options; }
/** * Helper function to insert some HTML into the body section of an HTML * page, right before the </body> tag. * * @param string $tag * @param string $html * * @return string */ static function insertEndOfBody($tag, $html) { // first, attempt to insert it before the </body> tag, matching indentation. if (preg_match("~([ \t]*)</body~mi", $html, $matches)) { // Try to insert it just before </head> $replacement = sprintf("%s\t%s\n%s", $matches[1], $tag, $matches[0]); $html = Str::replaceFirst($matches[0], $replacement, $html); } else { // Since we're serving tag soup, just append it. $html .= $tag . "\n"; } return $html; }
/** * Helper function to insert some HTML after the last javascript include. * First in the head section, but if there is no script in the head, place * it anywhere. * * @param string $tag * @param string $html * @param bool $insidehead * * @return string */ public function insertAfterJs($tag, $html, $insidehead = true) { // Set $context: only the part until </head>, or entire document. if ($insidehead) { $pos = strpos($html, "</head>"); $context = substr($html, 0, $pos); } else { $context = $html; } // then, attempt to insert it after the last <script> tag within context, matching indentation. if (preg_match_all("~^([ \t]*)(.*)</script>~mi", $context, $matches)) { // matches[0] has some elements, the last index is -1, because zero indexed. $last = count($matches[0]) - 1; $replacement = sprintf("%s\n%s%s", $matches[0][$last], $matches[1][$last], $tag); $html = Str::replaceFirst($matches[0][$last], $replacement, $html); } elseif ($insidehead) { // Second attempt: entire document $html = $this->insertAfterJs($tag, $html, false); } else { // Just insert it at the end of the head section. $html = $this->insertEndOfHead($tag, $html); } return $html; }
/** * Helper function to insert some HTML after the last javascript include. * First in the head section, but if there is no script in the head, place * it anywhere. * * @param AssetInterface $asset * @param string $rawHtml * @param boolean $insidehead * * @return string */ protected function jsTagsAfter($asset, $rawHtml, $insidehead = false) { if ($insidehead) { $pos = strpos($rawHtml, '</head>'); $context = substr($rawHtml, 0, $pos); } else { $context = $rawHtml; } // This match tag is a unique case if ($matches = $this->getMatches($context, '(.*)</script>', false, true)) { // Attempt to insert it after the last <script> tag within context, matching indentation. $last = count($matches[0]) - 1; $replacement = sprintf("%s\n%s%s", $matches[0][$last], $matches[1][$last], (string) $asset); return Str::replaceFirst($matches[0][$last], $replacement, $rawHtml); } elseif ($insidehead) { // Second attempt: entire document return $this->jsTagsAfter($asset, $rawHtml, false); } return $this->headTagEnd($asset, $rawHtml); }
/** * {@inheritdoc} */ public final function getVendor() { if ($this->vendor === null) { $namespace = $this->getNamespace(); $name = Str::replaceFirst('Bolt\\Extension\\', '', $namespace); $pos = strpos($name, '\\'); $this->vendor = $pos === false ? $name : substr($name, 0, $pos); } return $this->vendor; }
public function testReplaceFirst() { $input = "this is a test string this is a test string"; $this->assertEquals("one is a test string this is a test string", Str::replaceFirst('this', 'one', $input)); }