Pimcore\Model\Staticroute::assemble PHP Method

assemble() public method

public assemble ( array $urlOptions = [], boolean $reset = false, boolean $encode = true ) : mixed | string
$urlOptions array
$reset boolean
$encode boolean
return mixed | string
    public function assemble(array $urlOptions = [], $reset = false, $encode = true)
    {
        // get request parameters
        $blockedRequestParams = ["controller", "action", "module", "document"];
        $front = \Zend_Controller_Front::getInstance();
        // allow blocked params if we use it as variables
        $variables = explode(",", $this->getVariables());
        foreach ($variables as $name) {
            $pos = array_search($name, $blockedRequestParams);
            if ($pos !== false) {
                unset($blockedRequestParams[$pos]);
            }
        }
        if ($reset) {
            $requestParameters = [];
        } else {
            $requestParameters = $front->getRequest()->getParams();
            // remove blocked parameters from request
            foreach ($blockedRequestParams as $key) {
                if (array_key_exists($key, $requestParameters)) {
                    unset($requestParameters[$key]);
                }
            }
        }
        $defaultValues = $this->getDefaultsArray();
        // apply values (controller,action,module, ... ) from previous match if applicable (only when )
        if ($reset) {
            if (self::$_currentRoute && self::$_currentRoute->getName() == $this->getName()) {
                $defaultValues = array_merge($defaultValues, self::$_currentRoute->_values);
            }
        }
        // merge with defaults
        $urlParams = array_merge($defaultValues, $requestParameters, $urlOptions);
        $parametersInReversePattern = [];
        $parametersGet = [];
        $url = $this->getReverse();
        $forbiddenCharacters = ["#", ":", "?"];
        // check for named variables
        uksort($urlParams, function ($a, $b) {
            // order by key length, longer key have priority
            // (%abcd prior %ab, so that %ab doesn't replace %ab in [%ab]cd)
            return strlen($b) - strlen($a);
        });
        $tmpReversePattern = $this->getReverse();
        foreach ($urlParams as $key => $param) {
            if (strpos($tmpReversePattern, "%" . $key) !== false) {
                $parametersInReversePattern[$key] = $param;
                // we need to replace the found variable to that it cannot match again a placeholder
                // eg. %abcd prior %ab if %abcd matches already %ab shouldn't match again on the same placeholder
                $tmpReversePattern = str_replace("%" . $key, "---", $tmpReversePattern);
            } else {
                // only append the get parameters if there are defined in $urlOptions
                // or if they are defined in $_GET an $reset is false
                if (array_key_exists($key, $urlOptions) || !$reset && array_key_exists($key, $_GET)) {
                    $parametersGet[$key] = $param;
                }
            }
        }
        $urlEncodeEscapeCharacters = "~|urlen" . md5(microtime()) . "code|~";
        // replace named variables
        uksort($parametersInReversePattern, function ($a, $b) {
            // order by key length, longer key have priority
            // (%abcd prior %ab, so that %ab doesn't replace %ab in [%ab]cd)
            return strlen($b) - strlen($a);
        });
        foreach ($parametersInReversePattern as $key => $value) {
            $value = str_replace($forbiddenCharacters, "", $value);
            if (strlen($value) > 0) {
                if ($encode) {
                    $value = urlencode_ignore_slash($value);
                }
                $value = str_replace("%", $urlEncodeEscapeCharacters, $value);
                $url = str_replace("%" . $key, $value, $url);
            }
        }
        // remove optional parts
        $url = preg_replace("/\\{([^\\}]+)?%[^\\}]+\\}/", "", $url);
        $url = str_replace(["{", "}"], "", $url);
        // optional get parameters
        if (!empty($parametersGet)) {
            if ($encode) {
                $getParams = array_urlencode($parametersGet);
            } else {
                $getParams = array_toquerystring($parametersGet);
            }
            $url .= "?" . $getParams;
        }
        // convert tmp urlencode escape char back to real escape char
        $url = str_replace($urlEncodeEscapeCharacters, "%", $url);
        $results = \Pimcore::getEventManager()->trigger("frontend.path.staticroute", $this, ["frontendPath" => $url, "params" => $urlParams, "reset" => $reset, "encode" => $encode]);
        if ($results->count()) {
            $url = $results->last();
        }
        return $url;
    }