Exemplo n.º 1
0
 /**
  * Translates a path alias into an actual path.
  *
  * The translation is done according to the following procedure:
  *
  * 1. If the given alias does not start with '@', it is returned back without change;
  * 2. Otherwise, look for the longest registered alias that matches the beginning part
  *    of the given alias. If it exists, replace the matching part of the given alias with
  *    the corresponding registered path.
  * 3. Throw an exception or return false, depending on the `$throwException` parameter.
  *
  * For example, by default '@rock' is registered as the alias to the Rock framework directory,
  * say '/path/to/rock'. The alias '@rock/web' would then be translated into '/path/to/rock/web'.
  *
  * If you have registered two aliases '@foo' and '@foo/bar'. Then translating '@foo/bar/config'
  * would replace the part '@foo/bar' (instead of '@foo') with the corresponding registered path.
  * This is because the longest alias takes precedence.
  *
  * However, if the alias to be translated is '@foo/barbar/config', then '@foo' will be replaced
  * instead of '@foo/bar', because '/' serves as the boundary character.
  *
  * Note, this method does not check if the returned path exists or not.
  *
  * @param string $alias the alias to be translated.
  * @param array $placeholders
  * @param boolean $throwException whether to throw an exception if the given alias is invalid.
  *                                If this is false and an invalid alias is given, false will be returned by this method.
  * @throws \Exception if the alias is invalid while $throwException is true.
  * @return string|boolean the path corresponding to the alias, false if the root alias is not previously registered.
  * @see setAlias()
  */
 public static function getAlias($alias, array $placeholders = [], $throwException = true)
 {
     if (strncmp($alias, '@', 1)) {
         // not an alias
         return $alias;
     }
     $delimiter = ObjectHelper::isNamespace($alias) ? '\\' : '/';
     $pos = strpos($alias, $delimiter);
     $root = $pos === false ? $alias : substr($alias, 0, $pos);
     if (isset(static::$aliases[$root])) {
         if (is_string(static::$aliases[$root])) {
             $result = $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos);
             return StringHelper::replace($result, $placeholders, false);
         } else {
             foreach (static::$aliases[$root] as $name => $path) {
                 if (strpos($alias . $delimiter, $name . $delimiter) === 0) {
                     $result = $path . substr($alias, strlen($name));
                     return StringHelper::replace($result, $placeholders, false);
                 }
             }
         }
     }
     if ($throwException) {
         throw new \Exception("Invalid path alias: {$alias}");
     } else {
         return false;
     }
 }
Exemplo n.º 2
0
 protected function defaultFormatOptions()
 {
     return ['F' => function (DateTime $datetime) {
         return $datetime->getLocale()->getMonth($datetime->format('n') - 1);
     }, 'M' => function (DateTime $datetime) {
         return $datetime->getLocale()->getShortMonth($datetime->format('n') - 1);
     }, 'l' => function (DateTime $datetime) {
         return $datetime->getLocale()->getWeekDay($datetime->format('N') - 1);
     }, 'D' => function (DateTime $datetime) {
         return $datetime->getLocale()->getShortWeekDay($datetime->format('N') - 1);
     }, 'ago' => function (DateTime $datetime) {
         $diff = $datetime->diff(new DateTime());
         $locale = $datetime->getLocale();
         if ($diff->y >= 1) {
             $number = $diff->y;
             $names = $locale->getYearNames();
         } elseif ($diff->total_months >= 1) {
             $number = $diff->total_months;
             $names = $locale->getMonthNames();
         } elseif ($diff->total_days >= 7) {
             $number = $diff->total_weeks;
             $names = $locale->getWeekNames();
         } elseif ($diff->total_days >= 1) {
             $number = $diff->total_days;
             $names = $locale->getDayNames();
         } elseif ($diff->total_hours >= 1) {
             $number = $diff->total_hours;
             $names = $locale->getHourNames();
         } elseif ($diff->total_minutes >= 1) {
             $number = $diff->total_minutes;
             $names = $locale->getMinuteNames();
         } else {
             $number = $diff->total_seconds;
             $names = $locale->getSecondNames();
         }
         $options = $locale->getOptions();
         if (isset($options['ago'])) {
             return StringHelper::replace($options['ago'], ['number' => $number, 'name' => Inflector::plural($number, $names)]);
         }
         return $number . ' ' . Inflector::plural($number, $names) . ' ago';
     }];
 }
Exemplo n.º 3
0
 protected function translateInternal($keys, array $placeholders = [])
 {
     if (!isset(static::$data[$this->locale][$this->category])) {
         static::$data[$this->locale][$this->category] = [];
     }
     $result = ArrayHelper::getValue(static::$data[$this->locale][$this->category], $keys);
     if (!isset($result)) {
         if ($this->throwException) {
             $keys = is_array($keys) ? implode('][', $keys) : $keys;
             throw new i18nException(i18nException::UNKNOWN_I18N, ['name' => "{$this->category}[{$keys}]"]);
         }
         return null;
     }
     return StringHelper::replace($result, $placeholders, $this->removeBraces);
 }
Exemplo n.º 4
0
 protected function replace($message)
 {
     if (empty($this->placeholders)) {
         return $message;
     }
     return StringHelper::replace($message, $this->placeholders);
 }
Exemplo n.º 5
0
Arquivo: Log.php Projeto: romeoz/rock
 /**
  * Adds a log record at the `EMERGENCY` level.
  *
  * This method allows for compatibility with common interfaces.
  *
  * @param  string $message log message
  * @param  array $placeholders placeholders for replacement
  * @return bool Whether the record has been processed
  */
 protected function emergInternal($message, array $placeholders = [])
 {
     return $this->logger->emerg(StringHelper::replace($message, $placeholders, false), $placeholders);
 }
Exemplo n.º 6
0
 /**
  * Converts an exception into a simple string.
  *
  * @param \Exception $exception the exception being converted
  * @param bool $asStack
  * @return string the string representation of the exception.
  */
 public static function convertExceptionToString(\Exception $exception, $asStack = ROCK_DEBUG)
 {
     $trace = $exception->getTrace();
     $placeholders = ['class' => isset($trace[0]['class']) ? $trace[0]['class'] : '', 'method' => isset($trace[0]['function']) ? $trace[0]['function'] : '', 'message' => $asStack ? $exception->getMessage() . ' ' . $exception->getTraceAsString() : $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine()];
     return StringHelper::replace(static::$format, $placeholders, false);
 }
Exemplo n.º 7
0
 private function prepareAliases(array $aliases)
 {
     foreach ($aliases as &$alias) {
         $placeholders = ['self_scheme' => $this->request->getScheme(), 'self_path' => $this->request->getUrlWithoutArgs()];
         foreach ($this->request->rawGet() ?: [] as $name => $placeholder) {
             $placeholders["self_query_{$name}"] = $placeholder;
         }
         $alias = StringHelper::replace($alias, $placeholders, false);
     }
     return $aliases;
 }
Exemplo n.º 8
0
 /**
  * Replacing placeholders to URL-data.
  * @param array $placeholders
  * @return static
  */
 public function replace(array $placeholders = [])
 {
     if (empty($placeholders)) {
         return $this;
     }
     $callback = function ($value) use($placeholders) {
         return StringHelper::replace($value, $placeholders, false);
     };
     $this->data = ArrayHelper::map($this->data, $callback, true);
     return $this;
 }
Exemplo n.º 9
0
 protected function searchByPattern($pattern, $error = true, $is = self::TYPE_FILE)
 {
     foreach (parent::listContents('', true) as $data) {
         if (isset($is) && $data['type'] !== $is) {
             continue;
         }
         if (preg_match($pattern, $data['path'])) {
             return $data['path'];
         }
     }
     if ($error === true) {
         $this->errors[] = StringHelper::replace(FileException::UNKNOWN_FILE, ['path' => $pattern]);
     }
     return null;
 }