static function getRunId() { if (!$GLOBALS['_run_id']) { $GLOBALS['_run_id'] = Strings::random(10); } return $GLOBALS['_run_id']; }
/** adds file and rules to ruleSets and parses all active rules in current file and former files @param path str file location string */ function matchRules($path, &$rules) { foreach ($rules as $ruleKey => &$rule) { if (!$rule) { # rule may have been flagged "once" continue; } unset($matched); if (!isset($rule['flags'])) { $flags = $rule[2] ? explode(',', $rule[2]) : array(); $rule['flags'] = array_fill_keys(array_values($flags), true); //parse flags for determining match string if ($rule['flags']['regex']) { $rule['matcher'] = \Grithin\Strings::pregDelimit($rule[0]); if ($rule['flags']['caseless']) { $rule['matcher'] .= 'i'; } } else { if ($rule['flags']['caseless']) { $rule['matcher'] = strtolower($rule[0]); } else { $rule['matcher'] = $rule[0]; } } } if ($rule['flags']['caseless']) { $subject = $this->caselessPath; } else { $subject = $this->path; } //test match if ($rule['flags']['regex']) { if (preg_match($rule['matcher'], $subject, $this->regexMatch)) { $matched = true; } } else { if ($rule['matcher'] == $subject) { $matched = true; } } if ($matched) { if ($this->debug) { Debug::log(['Matched Rule', $rule], ['title' => 'Route']); } $this->matchedRules[] = $rule; //++ apply replacement logic { if ($rule['flags']['regex']) { if (is_callable($rule[1])) { $this->matcher = $rule['matcher']; $bound = new Bound($rule[1], [$this]); } else { $bound = new Bound('\\Grithin\\Route::regexReplacer', [$rule[1]]); } $replacement = preg_replace_callback($rule['matcher'], $bound, $this->path, 1); } else { if (is_callable($rule[1])) { $this->matcher = $rule['matcher']; $replacement = call_user_func($rule[1], $this); } else { $replacement = $rule[1]; } } //handle redirects if ($rule['flags'][301]) { $httpRedirect = 301; } if ($rule['flags'][303]) { $httpRedirect = 303; } if ($rule['flags'][307]) { $httpRedirect = 307; } if ($httpRedirect) { if ($rule['flags']['params']) { $replacement = Http::appendsUrl(Http::parseQuery($_SERVER['QUERY_STRING']), $replacement); } Http::redirect($replacement, 'head', $httpRedirect); } //remake url with replacement $this->applyPath($replacement); $this->parsedTokens = []; $this->unparsedTokens = array_merge([''], $this->tokens); //++ } //++ apply parse flag { if ($rule['flags']['once']) { $rules[$ruleKey] = null; } elseif ($rule['flags']['file:last']) { $this->ruleSets[$path] = []; } elseif ($rule['flags']['last']) { $this->unparsedTokens = []; } //++ } return true; } } unset($rule); return false; }
static function escapeFilename($name) { return \Grithin\Strings::slashEscape(\Grithin\Strings::slashEscape($name)); }