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.commands
|
8
|
|
|
*/
|
9
|
|
|
Yii::import('backend.components.*');
|
10
|
|
|
Yii::import('backend.components.db.*');
|
11
|
|
|
Yii::import('backend.components.interfaces.*');
|
12
|
|
|
Yii::import('backend.modules.product.models.*');
|
13
|
|
|
Yii::import('backend.models.behaviors.*');
|
14
|
|
|
Yii::import('frontend.share.behaviors.*');
|
15
|
|
|
Yii::import('frontend.share.helpers.*');
|
16
|
|
|
Yii::import('frontend.share.formatters.*');
|
17
|
|
|
Yii::import('frontend.share.validators.*');
|
18
|
|
|
Yii::import('frontend.extensions.upload.components.*');
|
19
|
|
|
Yii::import('frontend.share.validators.*');
|
20
|
|
|
|
21
|
|
|
Yii::import('backend.modules.product.components.FacetIndexer');
|
22
|
|
|
Yii::import('frontend.commands.components.*');
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* Class IndexerCommand
|
26
|
|
|
*
|
27
|
|
|
* Комана для индексирования паарметров фасеточного поиска
|
28
|
|
|
*/
|
29
|
|
|
class IndexerCommand extends LoggingCommand
|
30
|
|
|
{
|
31
|
|
|
const MAX_CHUNK_SIZE = 10000;
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* @var CDbCommandBuilder
|
35
|
|
|
*/
|
36
|
|
|
private $builder;
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* @var int
|
40
|
|
|
*/
|
41
|
|
|
private $insertedRecords = 0;
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* @var FacetIndexer $facetIndexer
|
45
|
|
|
*/
|
46
|
|
|
private $facetIndexer;
|
47
|
|
|
|
48
|
|
|
public function init()
|
49
|
|
|
{
|
50
|
|
|
parent::init();
|
51
|
|
|
|
52
|
|
|
$this->builder = new CDbCommandBuilder(Yii::app()->db->getSchema());
|
53
|
|
|
$this->facetIndexer = new FacetIndexer(self::MAX_CHUNK_SIZE);
|
54
|
|
|
$this->facetIndexer->attachEventHandler('onSaveRecords', array($this, 'onSaveRecords'));
|
55
|
|
|
|
56
|
|
|
$this->logger->startTimer(get_class($this));
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
public function actionRefresh()
|
60
|
|
|
{
|
61
|
|
|
$this->logger->log('Начало индексеции фильтров');
|
62
|
|
|
|
63
|
|
|
$this->facetIndexer->reindexAll();
|
64
|
|
|
|
65
|
|
|
$this->showSummary();
|
66
|
|
|
|
67
|
|
|
$this->updateProduction();
|
68
|
|
|
|
69
|
|
|
$this->logger->log('Индексеция фильтров завершена', true, true);
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
public function actionDelete()
|
73
|
|
|
{
|
74
|
|
|
$this->facetIndexer->clearIndex();
|
75
|
|
|
$this->updateProduction();
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
public function onSaveRecords(CEvent $event)
|
79
|
|
|
{
|
80
|
|
|
$count = $event->params['count'];
|
81
|
|
|
|
82
|
|
|
$this->logger->log('Обработано '.Yii::app()->format->formatNumber($count).' записей', true, true);
|
83
|
|
|
$this->insertedRecords += $count;
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
private function updateProduction()
|
87
|
|
|
{
|
88
|
|
|
$path = Yii::getPathOfAlias('frontend.config.production').'.php';
|
89
|
|
|
if( file_exists($path) )
|
90
|
|
|
touch($path);
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
private function showSummary()
|
94
|
|
|
{
|
95
|
|
|
if( $this->insertedRecords )
|
96
|
|
|
{
|
97
|
|
|
$message = 'Обработано записей: '.Yii::app()->format->formatNumber($this->insertedRecords).PHP_EOL;
|
98
|
|
|
$message .= $this->logger->finishTimer(get_class($this), 'Время выполнения: ');
|
99
|
|
|
$this->logger->log($message, true, true);
|
100
|
|
|
}
|
101
|
|
|
else
|
102
|
|
|
{
|
103
|
|
|
throw new CException('Обработано 0 записей', 500);
|
104
|
|
|
}
|
105
|
|
|
}
|
106
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.