Commit 507a6f10 by Qiang Xue

Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules

parent 800fb947
......@@ -16,6 +16,7 @@ Yii Framework 2 Change Log
- Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
- Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue)
......
......@@ -133,14 +133,7 @@ class UrlManager extends Component
public function init()
{
parent::init();
$this->compileRules();
}
/**
* Parses the URL rules.
*/
protected function compileRules()
{
if (!$this->enablePrettyUrl || empty($this->rules)) {
return;
}
......@@ -152,15 +145,46 @@ class UrlManager extends Component
$hash = md5(json_encode($this->rules));
if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) {
$this->rules = $data[0];
return;
} else {
$this->rules = $this->buildRules($this->rules);
$this->cache->set($cacheKey, [$this->rules, $hash]);
}
} else {
$this->rules = $this->buildRules($this->rules);
}
}
$rules = [];
/**
* Adds additional URL rules.
* This method will call [[buildRules()]] to parse the given rule declarations and then append or insert
* them to the existing [[rules]].
* @param array $rules the new rules to be added. Each array element represents a single rule declaration.
* Please refer to [[rules]] for the acceptable rule format.
* @param boolean $append whether to add the new rules by appending them to the end of the existing rules.
*/
public function addRules($rules, $append = true)
{
$rules = $this->buildRules($rules);
if ($append) {
$this->rules = array_merge($this->rules, $rules);
} else {
$this->rules = array_merge($rules, $this->rules);
}
}
/**
* Builds URL rule objects from the given rule declarations.
* @param array $rules the rule declarations. Each array element represents a single rule declaration.
* Please refer to [[rules]] for the acceptable rule formats.
* @return UrlRuleInterface[] the rule objects built from the given rule declarations
* @throws InvalidConfigException if a rule declaration is invalid
*/
protected function buildRules($rules)
{
$compiledRules = [];
$verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
foreach ($this->rules as $key => $rule) {
if (!is_array($rule)) {
foreach ($rules as $key => $rule) {
if (is_string($rule)) {
$rule = ['route' => $rule];
if (preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches)) {
$rule['verb'] = explode(',', $matches[1]);
......@@ -169,17 +193,15 @@ class UrlManager extends Component
}
$rule['pattern'] = $key;
}
$rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
if (is_array($rule)) {
$rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
}
if (!$rule instanceof UrlRuleInterface) {
throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.');
}
$rules[] = $rule;
}
$this->rules = $rules;
if (isset($cacheKey, $hash)) {
$this->cache->set($cacheKey, [$this->rules, $hash]);
$compiledRules[] = $rule;
}
return $compiledRules;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment