1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
|
|
abstract class BaseXml extends CComponent
|
9
|
|
|
{
|
10
|
|
|
public $templatesAlias = 'frontend.views.xml';
|
11
|
|
|
|
12
|
|
|
public $charset = 'windows-1251';
|
13
|
|
|
|
14
|
|
|
public $template;
|
15
|
|
|
|
16
|
|
|
public $filePath;
|
17
|
|
|
|
18
|
|
|
public $dataProviderClass;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* @var CDbCriteria
|
22
|
|
|
*/
|
23
|
|
|
public $criteria;
|
24
|
|
|
|
25
|
|
|
public $itemsBufferSize = 1000;
|
26
|
|
|
|
27
|
|
|
/**
|
28
|
|
|
* @var XMLWriter
|
29
|
|
|
*/
|
30
|
|
|
protected $xmlWriter;
|
31
|
|
|
|
32
|
|
|
protected $dataProvider;
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @var bool $outCashedXml отдавать кэшированый файл
|
36
|
|
|
*/
|
37
|
|
|
protected $outCashedXml = false;
|
38
|
|
|
|
39
|
|
|
private $itemsBufferCounter = 0;
|
40
|
|
|
|
41
|
|
|
abstract public function buildXml();
|
42
|
|
|
|
43
|
|
|
public function init()
|
44
|
|
|
{
|
45
|
|
|
set_time_limit(0);
|
46
|
|
|
|
47
|
|
|
$this->filePath = $this->getXmlPath();
|
48
|
|
|
|
49
|
|
|
if( !$this->isInvalidateCache() )
|
50
|
|
|
{
|
51
|
|
|
$this->outCashedXml = true;
|
52
|
|
|
|
53
|
|
|
return;
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
ignore_user_abort(true);
|
57
|
|
|
$this->xmlWriter = new XMLWriter();
|
58
|
|
|
$this->xmlWriter->openURI($this->filePath);
|
59
|
|
|
|
60
|
|
View Code Duplication |
if( isset($this->dataProviderClass) )
|
|
|
|
|
61
|
|
|
{
|
62
|
|
|
$this->criteria = isset($this->criteria) ? $this->criteria : new CDbCriteria();
|
63
|
|
|
$this->dataProvider = new $this->dataProviderClass($this->criteria);
|
64
|
|
|
}
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
public function render()
|
68
|
|
|
{
|
69
|
|
|
header('Content-Type: text/xml; charset='.$this->charset);
|
70
|
|
|
|
71
|
|
|
readfile($this->filePath);
|
72
|
|
|
|
73
|
|
|
Yii::app()->end();
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
View Code Duplication |
protected function saveXml()
|
|
|
|
|
77
|
|
|
{
|
78
|
|
|
$dir = pathinfo($this->filePath, PATHINFO_DIRNAME);
|
79
|
|
|
if( !file_exists($dir) )
|
80
|
|
|
{
|
81
|
|
|
mkdir($dir);
|
82
|
|
|
chmod($dir, 0775);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
$this->xmlWriter->flush();
|
86
|
|
|
@chmod($this->filePath, 0775);
|
|
|
|
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
/**
|
90
|
|
|
* @param string $template
|
91
|
|
|
*
|
92
|
|
|
* @return string $path
|
93
|
|
|
* @throws InvalidArgumentException
|
94
|
|
|
*/
|
95
|
|
View Code Duplication |
protected function getTemplatePath($template)
|
|
|
|
|
96
|
|
|
{
|
97
|
|
|
if( $template === null )
|
98
|
|
|
{
|
99
|
|
|
$template = str_replace("Xml", "", lcfirst(get_called_class()));
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
$path = Yii::getPathOfAlias($this->templatesAlias.'.'.$template).'.php';
|
103
|
|
|
|
104
|
|
|
if( !file_exists($path) )
|
105
|
|
|
{
|
106
|
|
|
throw new InvalidArgumentException('Отсутствует файл шаблона '.$path);
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
return $path;
|
110
|
|
|
}
|
111
|
|
|
|
112
|
|
|
/**
|
113
|
|
|
* @return string $filePath
|
114
|
|
|
*/
|
115
|
|
View Code Duplication |
protected function getXmlPath()
|
|
|
|
|
116
|
|
|
{
|
117
|
|
|
if( !isset($this->filePath) )
|
118
|
|
|
{
|
119
|
|
|
$class = str_replace('DataProvider', '', $this->dataProviderClass);
|
120
|
|
|
$this->filePath = 'f/xml/'.lcfirst($class).'.xml';
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
return $this->filePath;
|
124
|
|
|
}
|
125
|
|
|
|
126
|
|
|
/**
|
127
|
|
|
* Следить за буфером, при необходимост и обнулить
|
128
|
|
|
*/
|
129
|
|
|
protected function followBuffer()
|
130
|
|
|
{
|
131
|
|
|
if( $this->increaseBufferCounter() == $this->itemsBufferSize )
|
132
|
|
|
$this->flushItemsBuffer();
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
private function flushItemsBuffer()
|
136
|
|
|
{
|
137
|
|
|
$this->xmlWriter->flush();
|
138
|
|
|
$this->itemsBufferSize = 0;
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
private function increaseBufferCounter()
|
142
|
|
|
{
|
143
|
|
|
return $this->itemsBufferCounter++;
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
private function isInvalidateCache()
|
147
|
|
|
{
|
148
|
|
|
if( Yii::app()->request->getQuery('force') === 'force' )
|
149
|
|
|
return true;
|
150
|
|
|
|
151
|
|
|
if( file_exists($this->filePath) )
|
152
|
|
|
{
|
153
|
|
|
if( !empty($this->cacheDurationInSeconds) && (time() - filemtime($this->filePath) > $this->cacheDurationInSeconds) )
|
154
|
|
|
return true;
|
155
|
|
|
|
156
|
|
|
return false;
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
return true;
|
160
|
|
|
}
|
161
|
|
|
} |
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.