ParserController.php
2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Created by PhpStorm.
* User: Cibermag
* Date: 30.09.2015
* Time: 14:38
*/
use yii\console\Controller;
use yii\helpers\Console;
use common\components\PriceWriter;
use backend\models\ImportersFiles;
class ParserController extends Controller{
public function actionParseCSV ()
{
\common\components\CustomVarDamp::dumpAndDie(45);
if( $arr_id_files = Yii::$app->cache->get( 'files_to_parse' ) ) {
$arr_id_files = json_decode( $arr_id_files );
foreach ( $arr_id_files as $file_name ) {
$file_path = Yii::getAlias('@auto_upload') . '/' . $file_name . '.csv';
$config = ['record_id' => $file_name,
'importer_id' => ImportersFiles::findOne(['id' => $file_name])->id,
'parser_config' => ['keys' => ['DESCR', 'ARTICLE', 'BRAND', 'PRICE', 'BOX']]
];
if( $this->parseFileConsole( $file_path, $config ) ){
unlink( $file_path );
if (isset( $arr_id_files[$file_path] ) ) {
unset($arr_id_files[$file_path]);
}
} else {
// Yii::$app->log->
// не дошли до конца по этому остаки вернем в кеш
Yii::$app->cache->set( 'files_to_parse',json_encode( $arr_id_files ) );
}
}
if ( !count( $arr_id_files ) ) {
Yii::$app->cache->delete( 'files_to_parse' );
}
}
}
public function actionParseXML ()
{
}
protected function parseFileConsole( $file_path, $configuration ){
$parser_config = [];
if ( isset( $configuration['parser_config'] ) ) {
$parser_config = $configuration['parser_config'];
}
$data = Yii::$app->multiparser->parse( $file_path, $parser_config );
$writer = new PriceWriter();
$writer->configuration = $configuration;
$writer->data = $data;
$writer->mode = 1; //console-режим
if ( $writer->writeDataToDB() ){
Console::output('It is working');
return true;
}
return false;
}
}