/** * This function is to replace PHP's extremely buggy realpath(). * * Both $path and $basePath can be relative or absolute. * If an absolute path/basePath is provided, then the return path will be absolute. * Vise-versa, if both paths are relative, then the return path will be relative. * * Note: It will remove ending slashes. * * @param string $path The original path, can be relative etc. * @param string $basePath Default: current working directory * @throws Exception\PathException If path is trying to move up more directories than the base path has. * @return string The resolved path, it might not exist. * @since 1.11.0 */ public static function truePath($path, $basePath = null) { $basePath = $basePath ?: getcwd(); // attempts to detect if path is relative in which case, add cwd $basePath = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $basePath); if (!static::isAbsolute($path)) { $basePath = rtrim($basePath, DIRECTORY_SEPARATOR); $path = $basePath . DIRECTORY_SEPARATOR . $path; } $abs = Str::startsWith($path, '/'); // resolve path parts (single dot, double dot and double delimiters) $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $part) { if ('.' == $part) { continue; } if ('..' == $part) { if (empty($absolutes)) { throw new PathException('Cannot move up another directory'); } array_pop($absolutes); } else { $absolutes[] = $part; } } $path = implode(DIRECTORY_SEPARATOR, $absolutes); $path = $abs ? DIRECTORY_SEPARATOR . $path : $path; return $path; }
public function test_does_not_startsWith() { $this->assertFalse(Str::startsWith("blah", "q")); $this->assertFalse(Str::startsWith("blah", "blaq")); $this->assertFalse(Str::startsWith("blah", "qbla")); $this->assertFalse(Str::startsWith("blah", "lah")); $this->assertFalse(Str::startsWith("BLah", "lah")); }
/** * This uses default class/method if not provided * * {@inheritdoc} */ public function match($pattern, $to = null) { if (!$this->defaultControllerClass) { return parent::match($pattern, $to); } if ($to === null && $this->defaultControllerMethod) { $to = [$this->defaultControllerClass, $this->defaultControllerMethod]; } elseif (is_string($to) && Str::startsWith($to, '::')) { $to = [$this->defaultControllerClass, substr($to, 2)]; } return parent::match($pattern, $to); }
/** @inheritdoc */ public static function getValue($section, $key, $default = null, $allowEmpty = false) { $value = parent::getValue($section, $key, $default, $allowEmpty); if (!is_string($value) || !Str::startsWith($value, '%') || !Str::endsWith($value, '%')) { return $value; } $env = substr($value, 1, strlen($value) - 2); if (!static::$environments->containsKey($env)) { $location = static::$envName . " > " . ($section ? "{$section} > " : "") . "{$key}"; throw new ConfigException("Config file does not contain the environment: \"{$env}\" requested by {$location}"); } static::$config = static::$environments->get($env); $value = static::getValue($section, $key, $default, $allowEmpty); static::$config = static::$environments->get(static::$envName); return $value; }
private function convertNamespaceToPattern($pattern) { if (Str::startsWith($pattern, '/')) { // $pattern = substr($pattern, 1); // $pos = strrpos($pattern, '/'); // list ($str1, $str2) = $this->splitStringAt($pattern, $pos); // $str2 = '/' . $str2; // $pattern = '/' . str_replace('\\', '\/', $str1) . $str2; } else { $pattern = str_replace('\\', '/', $pattern); } return $pattern; }
public function getGroup($startsWith) { return $this->filter(function ($key) use($startsWith) { return Str::startsWith($key, $startsWith); }); }