DmitryDulepov\Realurl\Decoder\UrlDecoder::decodePath PHP Method

decodePath() protected method

Decodes the path.
protected decodePath ( array &$pathSegments ) : integer
$pathSegments array
return integer
    protected function decodePath(array &$pathSegments)
    {
        $savedRemainingPathSegments = array();
        $currentPid = 0;
        $remainingPathSegments = $pathSegments;
        $savedResult = NULL;
        $result = $this->searchPathInCache($remainingPathSegments);
        $allPathsAreExpired = $this->isExpiredPath && $result && !$this->expiredPath;
        if ($allPathsAreExpired) {
            // Special case: all paths are expired. We will try to unexpire the actual entry.
            $savedRemainingPathSegments = $remainingPathSegments;
            $remainingPathSegments = $pathSegments;
            $savedResult = $result;
            $result = NULL;
        }
        if (is_null($result)) {
            $result = $this->decodePathByOverride($remainingPathSegments);
            if (!is_null($result)) {
                $currentPid = $result->getPageId();
            }
        }
        if (is_null($result) || count($remainingPathSegments) > 0) {
            // Here we are if one of the following is true:
            // - nothing is in the cache
            // - there is an entry in the cache for the partial path
            // We see what it is:
            // - if a postVar exists for the next segment, it is a full path
            // - if no path segments left, we found the path
            // - otherwise we have to search
            reset($pathSegments);
            if (!$this->isPostVar(current($pathSegments))) {
                if ($result) {
                    $processedPathSegments = array_diff($pathSegments, $remainingPathSegments);
                    $currentPid = $result->getPageId();
                } else {
                    $processedPathSegments = array();
                    $currentPid = $this->rootPageId;
                }
                $currentMountPointPid = 0;
                while ($currentPid !== 0 && count($remainingPathSegments) > 0) {
                    $segment = array_shift($remainingPathSegments);
                    if ($segment === '') {
                        array_unshift($remainingPathSegments, $segment);
                        break;
                    }
                    $saveToCache = true;
                    $nextResult = $this->searchPages($currentPid, $segment, $saveToCache);
                    if ($nextResult) {
                        $result = $nextResult;
                        if ($this->mountPointStartPid !== $currentMountPointPid) {
                            $currentPid = $currentMountPointPid = $this->mountPointStartPid;
                        } else {
                            $currentPid = $result->getPageId();
                        }
                        $processedPathSegments[] = $segment;
                        $result->setPagePath(implode('/', $processedPathSegments));
                        if ($saveToCache) {
                            // Path is valid so far, so we cache it
                            $this->putToPathCache($result);
                        }
                    } elseif ($this->isPostVar($segment, $currentPid)) {
                        // Not decoded, looks like a postVarSet. Put it back.
                        array_unshift($remainingPathSegments, $segment);
                        break;
                    } else {
                        // Not decoded, not a postVarSet, could be a fixedPostVar. Still put back and hope for the best!
                        array_unshift($remainingPathSegments, $segment);
                        break;
                    }
                }
            } elseif ($currentPid === 0) {
                // Found a postVar on the rootPage
                $currentPid = $this->rootPageId;
            }
        }
        if ($allPathsAreExpired && !$result) {
            // We could not resolve the new path, use the expired one :(
            $result = $savedResult;
            $remainingPathSegments = $savedRemainingPathSegments;
        }
        if ($result && $this->expiredPath) {
            $startPosition = (int) strpos($this->speakingUri, $this->expiredPath);
            if ($startPosition !== FALSE) {
                $newUrl = substr($this->speakingUri, 0, $startPosition) . $result->getPagePath() . substr($this->speakingUri, $startPosition + strlen($this->expiredPath));
                @ob_end_clean();
                header(self::REDIRECT_STATUS_HEADER);
                header(self::REDIRECT_INFO_HEADER . ': redirect for expired page path');
                header('Location: ' . GeneralUtility::locationHeaderUrl($newUrl));
                die;
            }
        }
        if ($result || (int) $currentPid === (int) $this->rootPageId) {
            $pathSegments = $remainingPathSegments;
        } else {
            $this->throw404('Cannot decode "' . implode('/', $pathSegments) . '"');
        }
        return $result ? $result->getPageId() : ((int) $currentPid === (int) $this->rootPageId ? $currentPid : 0);
    }