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.models.xml
|
8
|
|
|
*/
|
9
|
|
|
abstract class AbstractXml extends CComponent
|
10
|
|
|
{
|
11
|
|
|
public $templatesAlias = 'frontend.views.xml';
|
12
|
|
|
|
13
|
|
|
public $charset = 'windows-1251';
|
14
|
|
|
|
15
|
|
|
public $template;
|
16
|
|
|
|
17
|
|
|
public $filePath;
|
18
|
|
|
|
19
|
|
|
public $dataProviderClass;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @var CDbCriteria
|
23
|
|
|
*/
|
24
|
|
|
public $criteria;
|
25
|
|
|
|
26
|
|
|
public $cacheDurationInSeconds = 86400;
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @var SimpleXMLElement
|
30
|
|
|
*/
|
31
|
|
|
protected $xmlDocument;
|
32
|
|
|
|
33
|
|
|
protected $dataProvider;
|
34
|
|
|
|
35
|
|
|
abstract public function buildXml();
|
36
|
|
|
|
37
|
|
|
public function init()
|
38
|
|
|
{
|
39
|
|
|
$this->filePath = $this->getXmlPath();
|
40
|
|
|
|
41
|
|
|
if( !$this->loadXml() )
|
42
|
|
|
{
|
43
|
|
|
$this->xmlDocument = new SimpleXMLElement($this->getTemplatePath($this->template), false, true);
|
44
|
|
|
|
45
|
|
View Code Duplication |
if( isset($this->dataProviderClass) )
|
|
|
|
|
46
|
|
|
{
|
47
|
|
|
$this->criteria = isset($this->criteria) ? $this->criteria : new CDbCriteria();
|
48
|
|
|
$this->dataProvider = new $this->dataProviderClass($this->criteria);
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
$this->buildXml();
|
52
|
|
|
$this->saveXml();
|
53
|
|
|
}
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
public function render()
|
57
|
|
|
{
|
58
|
|
|
header('Content-Type: text/xml; charset='.$this->charset);
|
59
|
|
|
echo $this->xmlDocument->asXML();
|
60
|
|
|
Yii::app()->end();
|
|
|
|
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* @return bool
|
65
|
|
|
*/
|
66
|
|
|
private function loadXml()
|
67
|
|
|
{
|
68
|
|
|
if( Yii::app()->request->getQuery('force') === 'force' || !file_exists($this->filePath) || ( time() - filemtime($this->filePath) > $this->cacheDurationInSeconds ) )
|
69
|
|
|
{
|
70
|
|
|
set_time_limit(0);
|
71
|
|
|
ignore_user_abort(true);
|
72
|
|
|
return false;
|
73
|
|
|
}
|
74
|
|
|
else
|
75
|
|
|
{
|
76
|
|
|
$this->xmlDocument = simplexml_load_file($this->filePath);
|
77
|
|
|
return true;
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
return false;
|
|
|
|
|
81
|
|
|
}
|
82
|
|
|
|
83
|
|
View Code Duplication |
private function saveXml()
|
|
|
|
|
84
|
|
|
{
|
85
|
|
|
$dir = pathinfo($this->filePath, PATHINFO_DIRNAME);
|
86
|
|
|
if( !file_exists($dir) )
|
87
|
|
|
{
|
88
|
|
|
mkdir($dir);
|
89
|
|
|
chmod($dir, 0775);
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
$this->xmlDocument->saveXML($this->filePath);
|
93
|
|
|
chmod($this->filePath, 0775);
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
/**
|
97
|
|
|
* @param string $template
|
98
|
|
|
*
|
99
|
|
|
* @return string $path
|
100
|
|
|
* @throws InvalidArgumentException
|
101
|
|
|
*/
|
102
|
|
View Code Duplication |
private function getTemplatePath($template)
|
|
|
|
|
103
|
|
|
{
|
104
|
|
|
if( $template === null )
|
105
|
|
|
{
|
106
|
|
|
$template = str_replace("Xml", "", lcfirst(get_called_class()));
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
$path = Yii::getPathOfAlias($this->templatesAlias.'.'.$template).'.php';
|
110
|
|
|
|
111
|
|
|
if( !file_exists($path) )
|
112
|
|
|
{
|
113
|
|
|
throw new InvalidArgumentException('Отсутствует файл шаблона '.$path);
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
return $path;
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* @return string $filePath
|
121
|
|
|
*/
|
122
|
|
View Code Duplication |
private function getXmlPath()
|
|
|
|
|
123
|
|
|
{
|
124
|
|
|
if( !isset($this->filePath) )
|
125
|
|
|
{
|
126
|
|
|
$class = str_replace('DataProvider', '', $this->dataProviderClass);
|
127
|
|
|
$this->filePath = 'f/xml/'.lcfirst($class).'.xml';
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
return $this->filePath;
|
131
|
|
|
}
|
132
|
|
|
} |
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.