1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/corpsepk/yii2-yandex-market-yml |
4
|
|
|
* @copyright Copyright (c) 2016 Corpsepk |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace corpsepk\yml\controllers; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\web\Controller; |
12
|
|
|
use corpsepk\yml\YandexMarketYml; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Corpsepk |
16
|
|
|
* @package corpsepk\yml |
17
|
|
|
*/ |
18
|
|
|
class DefaultController extends Controller |
19
|
|
|
{ |
20
|
|
|
public function actionIndex() |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var YandexMarketYml $module |
24
|
|
|
*/ |
25
|
|
|
$module = $this->module; |
26
|
|
|
|
27
|
|
|
$ymlData = $module->cacheProvider->get($module->cacheKey); |
28
|
|
|
|
29
|
|
|
if (!$ymlData) { |
30
|
|
|
$shop = $module->buildShop(); |
31
|
|
|
$shop->validate(); |
32
|
|
|
|
33
|
|
|
if ($shop->hasErrors()) { |
34
|
|
|
$module->logErrors($shop); |
35
|
|
|
|
36
|
|
|
if (YII_ENV_DEV) { |
37
|
|
|
// Render errors in `dev` environment |
38
|
|
|
return $this->render('errors', ['shop' => $shop]); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$ymlData = $module->buildYml($shop); |
43
|
|
|
|
44
|
|
|
// Build cache if no errors |
45
|
|
|
if (!$shop->hasErrors()) { |
46
|
|
|
$module->cacheProvider->set($module->cacheKey, $ymlData, $module->cacheExpire); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; |
51
|
|
|
$headers = Yii::$app->response->headers; |
52
|
|
|
$headers->add('Content-Type', 'application/xml'); |
53
|
|
|
|
54
|
|
|
if ($module->enableGzip) { |
55
|
|
|
$ymlData = gzencode($ymlData); |
56
|
|
|
$headers->add('Content-Encoding', 'gzip'); |
57
|
|
|
$headers->add('Content-Length', strlen($ymlData)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $ymlData; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|