/** * Returns the absolute path for the given template file path based on the given base directory. * * In case the given template file path starts with a slash ("/"), the template file is considered * to be relative to the module directory. * Otherwise the given template file path is considered to be relative to the given base path. * * @param string $templateFilePath Path of the template file to return the absolute path for * @param string $basePath Base path used to determine the absolute path for the given template file * @return string */ public static function getAbsoluteTemplateFilePath($templateFilePath, $basePath) { // determine template directory $templateDirectory = StringUtil::startsWith($templateFilePath, '/') ? ABLERON_MODULE_DIR : FileUtil::normalizePath($basePath) . '/'; // return absolute template file path return $templateDirectory . $templateFilePath; }
/** * Cuts off the path to the Ableron root directory in case the given path * is an Ableron path. * * Additionally normalizes the given path. * * @param string $path The path to get relative representation for * @return string */ public static function getRelativeAbleronPath(string $path) { // normalize path $path = self::normalizePath($path); // check whether we have an Ableron path if (StringUtil::startsWith($path, ABLERON_ROOT_DIR . '/')) { return StringUtil::getSubstring($path, StringUtil::getLength(ABLERON_ROOT_DIR)); } // given path is not an Ableron path so simply return given path return $path; }
/** * @see \Ableron\Core\Router\Route\RouteInterface::match() */ public function match(Uri $moduleRequestUri, HttpRequest $request) { // prepare module request URI $moduleRequestUriNormalized = $this->routeParameters['isCaseSensitive'] ? $moduleRequestUri->getPath() : StringUtil::toLowerCase($moduleRequestUri->getPath()); // handle "exactMath" if ($this->routeParameters['exactMatch'] !== null) { $exactMatchPattern = $this->routeParameters['isCaseSensitive'] ? $this->routeParameters['exactMatch'] : StringUtil::toLowerCase($this->routeParameters['exactMatch']); return $exactMatchPattern === $moduleRequestUriNormalized ? $this : null; } // handle "startsWith" if ($this->routeParameters['startsWith'] !== null) { $startsWithPattern = $this->routeParameters['isCaseSensitive'] ? $this->routeParameters['startsWith'] : StringUtil::toLowerCase($this->routeParameters['startsWith']); return StringUtil::startsWith($moduleRequestUriNormalized, $startsWithPattern) ? $this : null; } // no match return null; }
/** * Sets up this object by parsing the given string representation of a URI. * * Throws an exception in case the given string could not be parsed correctly. * * @param string $uri The URI to parse * @throws \Ableron\Core\Exception\SystemException * @return void */ private function parseUri($uri) { if (($uriComponents = parse_url(StringUtil::startsWith($uri, '//') ? 'http:' . $uri : $uri)) !== false) { // set scheme if (isset($uriComponents['scheme']) && !StringUtil::startsWith($uri, '//')) { $this->setScheme($uriComponents['scheme']); } // set user info $isUsernameSet = isset($uriComponents['user']) && $uriComponents['user'] !== ''; $isPasswordSet = isset($uriComponents['pass']) && $uriComponents['pass'] !== ''; if ($isUsernameSet || $isPasswordSet) { $this->setUserInfo(($isUsernameSet ? $uriComponents['user'] : '') . ($isPasswordSet ? ':' . $uriComponents['pass'] : '')); } // set host if (isset($uriComponents['host'])) { $this->setHost($uriComponents['host']); } // set port if (isset($uriComponents['port'])) { $this->setPort($uriComponents['port']); } // set path if (isset($uriComponents['path'])) { $this->setPath($uriComponents['path']); } // set fragment if (isset($uriComponents['fragment'])) { $this->setFragment($uriComponents['fragment']); } elseif (StringUtil::endsWith($uri, '#')) { $this->setFragment('', true); } // set query (it is necessary to set the query after the fragment as we need the fragment part here) if (isset($uriComponents['query'])) { $this->setQuery($uriComponents['query']); } elseif (StringUtil::endsWith($uri, '?' . ($this->fragment !== null ? '#' . $this->fragment : ''))) { $this->setQuery('', true); } } else { throw new SystemException(sprintf('Unable to parse URI: %s', $uri), 0, E_USER_NOTICE, __FILE__, __LINE__); } }
/** * Initializes this template tag using the given template tag string. * * Throws an exception in case the given string is not a valid template tag string. * * @param string $templateTagString The template tag string to parse * @throws \Ableron\Core\Exception\SystemException * @return void */ private function initFromTemplateTagString($templateTagString) { // extract information from given template tag if (!preg_match('#^(?<tagDeclaration>/?\\w+)(\\s+(?<argumentString>.*))?$#s', $templateTagString, $tagParts)) { throw new SystemException(sprintf('Unable to init TemplateTag from string "%s" - Given string is not a valid template tag string!', $templateTagString), 0, E_USER_ERROR, __FILE__, __LINE__); } $this->fullTag = $templateTagString; $this->isClosingTag = StringUtil::startsWith($tagParts['tagDeclaration'], '/'); $this->tagName = $this->isClosingTag ? StringUtil::getSubstring($tagParts['tagDeclaration'], 1) : $tagParts['tagDeclaration']; $this->argumentString = isset($tagParts['argumentString']) ? $tagParts['argumentString'] : null; $this->arguments = $this->parseArgumentString($this->argumentString); }
/** * Removes the given enclosing characters from the start and the end of the given input string * in case the string is enclosed by the given characters. * * E.g. removeEnclosingCharacter('{{foo}}', '{{', '}}') will return 'foo'; * * Returns the input string without any modification in case the given input string * is not enclosed by the given characters. * * @param string $inputString The input string to remove the enclosing characters from * @param string $enclosingStartCharactersToRemove The start characters to remove from the start of the string if string is enclosed by the given characters * @param string $enclosingEndCharactersToRemove The end characters to remove from the end of the string if string is enclosed by the given characters * @return string */ public static function removeEnclosingCharacters(string $inputString, string $enclosingStartCharactersToRemove, string $enclosingEndCharactersToRemove) { // return input string with removed given enclosing characters if (StringUtil::startsWith($inputString, $enclosingStartCharactersToRemove) && StringUtil::endsWith($inputString, $enclosingEndCharactersToRemove)) { return StringUtil::getSubstring($inputString, self::getLength($enclosingStartCharactersToRemove), -self::getLength($enclosingEndCharactersToRemove)); } // return unmodified input string if not enclosed by the given characters return $inputString; }
/** * Extracts all blocks from the given template and returns them. * * @param string $template The template to extract the blocks from * @return array */ private function extractBlocks($template) { // init required variables $blocks = array(); $insideFirstLevelBlock = false; $firstLevelBlockTag = null; $firstLevelBlockContentStartIndex = 0; // parse given template for ($i = 0, $templateLength = StringUtil::getLength($template); $i < $templateLength; $i++) { // check whether current character is the left tag delimiter if (StringUtil::getSubstring($template, $i, 1) == '{') { // get remaining template $remainingTemplate = StringUtil::getSubstring($template, $i); // check whether very first element in the remaining template is an opening or closing block tag $isOpeningBlockTag = preg_match('#^{(block\\s[^}]+)}#', $remainingTemplate, $openingBlockTagMatch); $isClosingBlockTag = StringUtil::startsWith($remainingTemplate, '{/block}'); if ($isOpeningBlockTag && !$insideFirstLevelBlock) { $insideFirstLevelBlock = true; $firstLevelBlockTag = new TemplateTag($openingBlockTagMatch[1]); $firstLevelBlockContentStartIndex = $i + StringUtil::getLength($openingBlockTagMatch[0]); $blocks[$this->getBlockName($firstLevelBlockTag)]['childBlocks'] = array(); } elseif ($isOpeningBlockTag && $insideFirstLevelBlock) { preg_match('#^{(block\\s[^}]+)}(.*?){/block}#s', $remainingTemplate, $blockMatch); $i += StringUtil::getLength($blockMatch[0]) - 1; $secondLevelBlockTag = new TemplateTag($blockMatch[1]); $blocks[$this->getBlockName($firstLevelBlockTag)]['childBlocks'][$this->getBlockName($secondLevelBlockTag)] = array('blockTag' => $secondLevelBlockTag, 'blockContent' => trim($blockMatch[2])); } elseif ($isClosingBlockTag && $insideFirstLevelBlock) { $insideFirstLevelBlock = false; $blocks[$this->getBlockName($firstLevelBlockTag)]['blockTag'] = $firstLevelBlockTag; $blocks[$this->getBlockName($firstLevelBlockTag)]['blockContent'] = trim(StringUtil::getSubstring($template, $firstLevelBlockContentStartIndex, $i - $firstLevelBlockContentStartIndex)); } } } // return extracted blocks return $blocks; }
/** * Returns the headers of the active request as an array of key-value pairs * extracted from the $_SERVER superglobal. * * @return array */ private function parseRequestHeaders() { $headers = array(); foreach ($_SERVER as $key => $value) { if (StringUtil::startsWith($key, 'HTTP_')) { $headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value; } } return $headers; }