1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Sergey Glagolev <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.components.url
|
8
|
|
|
*
|
9
|
|
|
* <pre>
|
10
|
|
|
* 'productCategory' => array('product/category', 'class' => 'DBRule', 'pattern' => '<category:\w+>', 'models' => array('category' => 'ProductCategory')),
|
11
|
|
|
* </pre>
|
12
|
|
|
*/
|
13
|
|
|
class DBRule extends FUrlRule
|
14
|
|
|
{
|
15
|
|
|
public $models = array();
|
16
|
|
|
|
17
|
3 |
|
public function __construct($route, $pattern)
|
18
|
|
|
{
|
19
|
3 |
|
if( is_array($route) )
|
20
|
3 |
|
{
|
21
|
3 |
|
foreach(array('models') as $name)
|
22
|
|
|
{
|
23
|
3 |
|
if( isset($route[$name]) )
|
24
|
3 |
|
$this->$name = $route[$name];
|
25
|
3 |
|
}
|
26
|
3 |
|
}
|
27
|
|
|
|
28
|
3 |
|
parent::__construct($route, $pattern);
|
29
|
3 |
|
}
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* @param FUrlManager $manager
|
33
|
|
|
* @param CHttpRequest $request
|
34
|
|
|
* @param string $pathInfo
|
35
|
|
|
* @param string $rawPathInfo
|
36
|
|
|
*
|
37
|
|
|
* @return bool|string
|
38
|
|
|
*/
|
39
|
1 |
|
public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
|
40
|
|
|
{
|
41
|
1 |
|
$manager->defaultParams = array();
|
42
|
|
|
|
43
|
1 |
|
if( ($pathInfo = $this->preparePathInfo($manager, $request, $pathInfo, $rawPathInfo)) === false )
|
44
|
1 |
|
{
|
45
|
|
|
return false;
|
46
|
|
|
}
|
47
|
|
|
|
48
|
1 |
View Code Duplication |
if( !empty($this->defaultParams) && !preg_match($this->pattern, $pathInfo, $matches) )
|
|
|
|
|
49
|
1 |
|
{
|
50
|
|
|
$pathInfo .= implode('/', $this->defaultParams).'/';
|
51
|
|
|
$manager->defaultParams = $this->defaultParams;
|
52
|
|
|
}
|
53
|
|
|
|
54
|
1 |
|
if( preg_match($this->pattern, $pathInfo, $matches) )
|
55
|
1 |
|
{
|
56
|
1 |
|
$result = true;
|
57
|
1 |
|
$builder = new CDbCommandBuilder(Yii::app()->db->getSchema());
|
58
|
|
|
|
59
|
1 |
|
foreach($this->models as $key => $class)
|
60
|
|
|
{
|
61
|
|
|
/**
|
62
|
|
|
* @var FActiveRecord $class
|
63
|
|
|
*/
|
64
|
1 |
|
$criteria = new CDbCriteria(array('limit' => 1));
|
65
|
1 |
|
$criteria->compare('url', $matches[$key]);
|
66
|
1 |
|
$command = $builder->createFindCommand($class::model()->tableName(), $criteria);
|
67
|
|
|
|
68
|
1 |
|
if( !$command->queryAll() )
|
69
|
1 |
|
{
|
70
|
|
|
$result = false;
|
71
|
|
|
break;
|
72
|
|
|
}
|
73
|
1 |
|
}
|
74
|
|
|
|
75
|
|
|
if( $result )
|
76
|
1 |
|
{
|
77
|
1 |
|
$manager->rule = $this;
|
78
|
1 |
|
return $this->getRoute($manager, $pathInfo, $matches);
|
79
|
|
|
}
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
return false;
|
83
|
|
|
}
|
84
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.