Skip to content

Commit

Permalink
Fixes yiisoft#13134: Added logging URL rules (bashkarev)
Browse files Browse the repository at this point in the history
  • Loading branch information
bashkarev authored and samdark committed Jan 27, 2017
1 parent f20c017 commit be4ebdd
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 7 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Yii Framework 2 Change Log
- Enh #13266: Added `yii\validators\EachValidator::$stopOnFirstError` allowing addition of more than one error (klimov-paul)
- Enh #13268: Added logging of memory usage (bashkarev)
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
- Enh #13134: Added logging URL rules (bashkarev)
- Enh: Refactored `yii\web\ErrorAction` to make it reusable (silverfire)
- Enh: Added support for field `yii\console\controllers\BaseMigrateController::$migrationNamespaces` setup from CLI (schmunk42)
- Bug #13287: Fixed translating "and" separator in `UniqueValidator` error message (jetexe)
Expand Down
12 changes: 9 additions & 3 deletions framework/rest/UrlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,15 @@ public function parseRequest($manager, $request)
if (strpos($pathInfo, $urlName) !== false) {
foreach ($rules as $rule) {
/* @var $rule \yii\web\UrlRule */
if (($result = $rule->parseRequest($manager, $request)) !== false) {
Yii::trace("Request parsed with URL rule: {$rule->name}", __METHOD__);

$result = $rule->parseRequest($manager, $request);
if (YII_DEBUG) {
Yii::trace([
'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
'match' => $result !== false,
'parent' => self::className()
], __METHOD__);
}
if ($result !== false) {
return $result;
}
}
Expand Down
12 changes: 9 additions & 3 deletions framework/web/CompositeUrlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ public function parseRequest($manager, $request)
{
foreach ($this->rules as $rule) {
/* @var $rule \yii\web\UrlRule */
if (($result = $rule->parseRequest($manager, $request)) !== false) {
Yii::trace("Request parsed with URL rule: {$rule->name}", __METHOD__);

$result = $rule->parseRequest($manager, $request);
if (YII_DEBUG) {
Yii::trace([
'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
'match' => $result !== false,
'parent' => self::className()
], __METHOD__);
}
if ($result !== false) {
return $result;
}
}
Expand Down
10 changes: 9 additions & 1 deletion framework/web/UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,15 @@ public function parseRequest($request)
if ($this->enablePrettyUrl) {
/* @var $rule UrlRule */
foreach ($this->rules as $rule) {
if (($result = $rule->parseRequest($this, $request)) !== false) {
$result = $rule->parseRequest($this, $request);
if (YII_DEBUG) {
Yii::trace([
'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
'match' => $result !== false,
'parent' => null
], __METHOD__);
}
if ($result !== false) {
return $result;
}
}
Expand Down
20 changes: 20 additions & 0 deletions framework/web/UrlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ class UrlRule extends Object implements UrlRuleInterface
*/
private $_routeParams = [];

/**
* @return string
* @since 2.0.11
*/
public function __toString()
{
$str = '';
if ($this->verb !== null) {
$str .= implode(',', $this->verb) . ' ';
}
if ($this->host !== null && strrpos($this->name, $this->host) === false) {
$str .= $this->host . '/';
}
$str .= $this->name;

if ($str === '') {
return '/';
}
return $str;
}

/**
* Initializes this rule.
Expand Down
71 changes: 71 additions & 0 deletions tests/framework/web/UrlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ public function testParseRequestWithUrlRuleCustomNormalizer()
$this->assertEquals(['post/index', ['page' => 1, 'tag' => 'a']], $result);
}

public function testToString()
{
$suites = $this->getTestsForToString();
foreach ($suites as $i => $suite) {
list ($name, $config, $test) = $suite;
$rule = new UrlRule($config);
$this->assertEquals($rule->__toString(), $test, "Test#$i: $name");
}
}

protected function getTestsForCreateUrl()
{
// structure of each test
Expand Down Expand Up @@ -1029,4 +1039,65 @@ protected function getTestsForParseRequest()
],
];
}

protected function getTestsForToString()
{
return [
[
'empty pattern',
[
'pattern' => '',
'route' => 'post/index',
],
'/'
],
[
'multiple params with special chars',
[
'pattern' => 'post/<page-number:\d+>/<per_page:\d+>/<author.login>',
'route' => 'post/index',
],
'post/<page-number:\d+>/<per_page:\d+>/<author.login>'
],
[
'with host info',
[
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
'host' => 'http://<lang:en|fr>.example.com',
],
'http://<lang:en|fr>.example.com/post/<page:\d+>/<tag>'
],
[
'with host info in pattern',
[
'pattern' => 'http://<lang:en|fr>.example.com/post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
'http://<lang:en|fr>.example.com/post/<page:\d+>/<tag>'
],
[
'with verb',
[
'verb' => ['POST'],
'pattern' => 'post/<id:\d+>',
'route' => 'post/index'
],
'POST post/<id:\d+>'
],
[
'with verbs',
[
'verb' => ['PUT', 'POST'],
'pattern' => 'post/<id:\d+>',
'route' => 'post/index'
],
'PUT,POST post/<id:\d+>'
],


];
}
}

0 comments on commit be4ebdd

Please sign in to comment.