replaceFirst() public static method

Replace the first occurence of a string only. Behaves like str_replace, but replaces _only_ the _first_ occurence.
See also: http://stackoverflow.com/a/2606638
public static replaceFirst ( string $search, string $replace, string $subject ) : string
$search string
$replace string
$subject string
return string
Esempio n. 1
0
 /**
  * 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);
 }
Esempio n. 2
0
 /**
  * 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);
 }
Esempio n. 3
0
 /**
  * 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" . "&nbsp;&nbsp;* Ensure the {$platform} database is running\n" . "&nbsp;&nbsp;* Check the <code>database:</code> parameters are configured correctly in <code>app/config/config.yml</code>\n" . "&nbsp;&nbsp;&nbsp;&nbsp;* Database name is correct\n" . "&nbsp;&nbsp;&nbsp;&nbsp;* User name has access to the named database\n" . "&nbsp;&nbsp;&nbsp;&nbsp;* Password is correct\n";
         throw new LowlevelException($error);
     }
     // Resume normal error handling
     restore_error_handler();
 }
Esempio n. 4
0
 /**
  * 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;
 }
Esempio n. 6
0
 /**
  * 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;
 }
Esempio n. 7
0
 /**
  * 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);
 }
Esempio n. 8
0
 /**
  * {@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;
 }
Esempio n. 9
0
 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));
 }