You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use yii\base\Object;
  10. /**
  11. * CompositeUrlRule is the base class for URL rule classes that consist of multiple simpler rules.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @since 2.0
  15. */
  16. abstract class CompositeUrlRule extends Object implements UrlRuleInterface
  17. {
  18. /**
  19. * @var UrlRuleInterface[] the URL rules contained in this composite rule.
  20. * This property is set in [[init()]] by the return value of [[createRules()]].
  21. */
  22. protected $rules = [];
  23. /**
  24. * Creates the URL rules that should be contained within this composite rule.
  25. * @return UrlRuleInterface[] the URL rules
  26. */
  27. abstract protected function createRules();
  28. /**
  29. * @inheritdoc
  30. */
  31. public function init()
  32. {
  33. parent::init();
  34. $this->rules = $this->createRules();
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function parseRequest($manager, $request)
  40. {
  41. foreach ($this->rules as $rule) {
  42. /* @var $rule \yii\web\UrlRule */
  43. if (($result = $rule->parseRequest($manager, $request)) !== false) {
  44. Yii::trace("Request parsed with URL rule: {$rule->name}", __METHOD__);
  45. return $result;
  46. }
  47. }
  48. return false;
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function createUrl($manager, $route, $params)
  54. {
  55. foreach ($this->rules as $rule) {
  56. /* @var $rule \yii\web\UrlRule */
  57. if (($url = $rule->createUrl($manager, $route, $params)) !== false) {
  58. return $url;
  59. }
  60. }
  61. return false;
  62. }
  63. }