/** * 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 render(Environment $twig) { $templates = ArrayCollection::create($this->template)->map(function ($template) { return Str::endsWith($template, '.twig') ? $template : $template . '.twig'; }); $this->setContent($twig->resolveTemplate($templates->toArray())->render($this->variables->toArray())); $this->rendered = true; }
protected static function convertToType($value) { if (is_numeric($value)) { if (Str::contains($value, '.')) { return floatval($value); } return intval($value); } return $value; }
/** * 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 getBool($section, $key, $default = null) { static::setConfig(); if (!static::hasKey($section, $key, $default === null)) { return $default; } $value = static::doGetValue($section, $key); if (static::$configFileType === 'ini' && is_string($value)) { // booleans from ini are converted to 1|0 return Str::equals($value, "true", false) || Str::equals($value, "1"); } return (bool) $value; }
/** @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; }
protected function matchTubeNames($tubesSearch, OutputInterface $output) { $matchedTubes = new ArrayCollection(); $queue = $this->getQueue(); $error = false; foreach ((array) $tubesSearch as $tubeSearch) { $matched = $queue->tubes()->filter(function (Tube $tube) use($tubeSearch) { return Str::contains($tube->name(), $tubeSearch, false); }); if ($matched->isEmpty()) { $output->writeln("<warn>No tubes matched to: {$tubeSearch}</warn>"); $error = true; } $matchedTubes->merge($matched); } return array($matchedTubes, $error); }
/** * Additional functionality: * One or more strings can be passed in to match to tube name * * @param string|string[]|callable|null $p * @return static */ public function filter($p) { if (is_string($p)) { $p = array($p); } if (is_array($p)) { $terms = $p; $p = function (Tube $tube) use($terms) { foreach ($terms as $term) { if (Str::contains($tube->name(), $term, false)) { return true; } } return false; }; } return parent::filter($p); }
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); }); }
/** * Returns the class name without the namespace. * * If the class does not exist false is returned. * @param string|object $cls object or fully qualified class name * @return string|false */ public static function className($cls) { if (is_string($cls) && !class_exists($cls)) { return false; } elseif (is_object($cls)) { $cls = get_class($cls); } return Str::splitLast($cls, "\\"); }
/** * Get a collection of {@see WorkerInfo}'s that implement {@see WorkerInterface} * @param null|string|array $filter [optional] worker(s) filter * @return WorkerInfo[]|ArrayCollection */ public function getWorkers($filter = null) { $processes = $this->processor->grepForPids(sprintf('runner \\"%s\\"', $this->getWorkerDir())); return $this->getWorkerInfoList()->map(function (WorkerInfo $worker) use($processes) { foreach ($processes as $process) { if ($worker->getFullyQualifiedName() === $process[0]) { $worker->addPid($process[1]); } } return $worker; })->filter(function (WorkerInfo $worker) use($filter) { if (!$filter) { return true; } if (!is_array($filter)) { $filter = array($filter); } foreach ($filter as $f) { if (Str::contains($worker->getName(), $f, false)) { return true; } } return false; }); }
protected function executeManagerFunction(InputInterface $input, OutputInterface $output, WorkerManager $manager, $workers) { $number = intval(Str::removeFirst($input->getOption('number'), '=')); $manager->startWorkers($workers, $number); }
/** * Determines whether or not the query is a SELECT INTO type query * @param string $query * @return boolean */ private function isSelectIntoQuery($query) { return Str::containsInsensitive($query, 'into outfile') || Str::containsInsensitive($query, 'into dumpfile'); }
public static function getTubeName() { return Str::className(static::className()); }
public function test_class_name_does_not_exist() { $this->assertFalse(Str::className('ClassDoesNotExist')); }