CompositeUrlRule.php 1.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

use Yii;
use yii\base\Object;

/**
14
 * CompositeUrlRule is the base class for URL rule classes that consist of multiple simpler rules.
15 16 17 18 19 20
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
abstract class CompositeUrlRule extends Object implements UrlRuleInterface
{
21 22 23 24 25
    /**
     * @var UrlRuleInterface[] the URL rules contained in this composite rule.
     * This property is set in [[init()]] by the return value of [[createRules()]].
     */
    protected $rules = [];
26

27

28 29 30 31 32
    /**
     * Creates the URL rules that should be contained within this composite rule.
     * @return UrlRuleInterface[] the URL rules
     */
    abstract protected function createRules();
33

34 35 36 37 38 39 40 41
    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        $this->rules = $this->createRules();
    }
42

43 44 45 46 47 48
    /**
     * @inheritdoc
     */
    public function parseRequest($manager, $request)
    {
        foreach ($this->rules as $rule) {
49
            /* @var $rule \yii\web\UrlRule */
50 51
            if (($result = $rule->parseRequest($manager, $request)) !== false) {
                Yii::trace("Request parsed with URL rule: {$rule->name}", __METHOD__);
52

53 54 55
                return $result;
            }
        }
56

57 58 59 60 61 62 63 64 65
        return false;
    }

    /**
     * @inheritdoc
     */
    public function createUrl($manager, $route, $params)
    {
        foreach ($this->rules as $rule) {
66
            /* @var $rule \yii\web\UrlRule */
67 68 69 70 71 72 73
            if (($url = $rule->createUrl($manager, $route, $params)) !== false) {
                return $url;
            }
        }

        return false;
    }
74
}