Blame view

console/controllers/ParserController.php 2.36 KB
f0dbd829   Mihail   add PriceWriter a...
1
  <?php
db543d22   Mihail   temp commit - wor...
2
3
  namespace console\controllers;
  
f0dbd829   Mihail   add PriceWriter a...
4
  use yii\console\Controller;
1fe29bbe   Mihail   fixed parser and ...
5
6
7
  use yii\helpers\Console;
  use common\components\PriceWriter;
  use backend\models\ImportersFiles;
f0dbd829   Mihail   add PriceWriter a...
8
9
10
11
  
  class ParserController extends Controller{
      public function actionParseCSV ()
      {
1fe29bbe   Mihail   fixed parser and ...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
          \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' );
              }
          }
f0dbd829   Mihail   add PriceWriter a...
36
37
38
39
40
      }
      public function actionParseXML ()
      {
  
      }
1fe29bbe   Mihail   fixed parser and ...
41
42
43
44
45
46
47
      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 );
f0dbd829   Mihail   add PriceWriter a...
48
  
1fe29bbe   Mihail   fixed parser and ...
49
50
51
52
53
54
55
56
          $writer = new PriceWriter();
          $writer->configuration = $configuration;
          $writer->data = $data;
          $writer->mode = 1; //console-режим
          if ( $writer->writeDataToDB() ){
              Console::output('It is working');
              return true;
          }
f0dbd829   Mihail   add PriceWriter a...
57
  
1fe29bbe   Mihail   fixed parser and ...
58
          return false;
f0dbd829   Mihail   add PriceWriter a...
59
      }
db543d22   Mihail   temp commit - wor...
60
61
62
63
64
65
66
67
68
69
  
      public function actionTest ()
      {
         // Console::output('It is working ');
          \Yii::warning('1');
          \Yii::info('2');
  //        \Yii::info('3');
  //        \Yii::warning('4');
  
      }
f0dbd829   Mihail   add PriceWriter a...
70
  }