Commit 221da14e7555f531c3ed670becdf127cb8273514

Authored by Mihail
1 parent cd8b9f70

change SplFileObject om fopen

@@ -22,9 +22,9 @@ class CsvParser extends TableParser @@ -22,9 +22,9 @@ class CsvParser extends TableParser
22 public function setup() 22 public function setup()
23 { 23 {
24 24
25 - $this->file->setCsvControl($this->delimiter);  
26 - $this->file->setFlags(\SplFileObject::READ_CSV);  
27 - $this->file->setFlags(\SplFileObject::SKIP_EMPTY); 25 +// $this->file->setCsvControl($this->delimiter);
  26 +// $this->file->setFlags(\SplFileObject::READ_CSV);
  27 +// $this->file->setFlags(\SplFileObject::SKIP_EMPTY);
28 28
29 parent::setup(); 29 parent::setup();
30 30
@@ -42,7 +42,7 @@ class CsvParser extends TableParser @@ -42,7 +42,7 @@ class CsvParser extends TableParser
42 42
43 protected function readRow( ) 43 protected function readRow( )
44 { 44 {
45 - $this->row = $this->file->fgetcsv(); 45 + $this->row = fgetcsv( $this->file, 0, $this->delimiter );
46 } 46 }
47 47
48 protected function isEmptyRow(){ 48 protected function isEmptyRow(){
@@ -12,13 +12,17 @@ namespace yii\multiparser; @@ -12,13 +12,17 @@ namespace yii\multiparser;
12 //@todo - xml - убрать из названий функций xml и array - это и так понятно 12 //@todo - xml - убрать из названий функций xml и array - это и так понятно
13 13
14 14
  15 +use common\components\CustomVarDamp;
  16 +
15 abstract class Parser 17 abstract class Parser
16 { 18 {
17 public $converter_conf = []; 19 public $converter_conf = [];
18 protected $converter = NULL; 20 protected $converter = NULL;
19 21
20 - /** @var экземляр SplFileObject читаемого файла */ 22 + /** @var file-resource читаемого файла */
21 public $file; 23 public $file;
  24 + /** @var string путь читаемого файла */
  25 + public $file_path;
22 26
23 /** 27 /**
24 * @var array - результирующий массив с отпарсенными значениями 28 * @var array - результирующий массив с отпарсенными значениями
@@ -90,10 +94,12 @@ abstract class Parser @@ -90,10 +94,12 @@ abstract class Parser
90 94
91 protected function cleanUp( ) 95 protected function cleanUp( )
92 { 96 {
  97 +
93 unset( $this->file ); 98 unset( $this->file );
94 unset( $this->converter ); 99 unset( $this->converter );
95 unset( $this->converter_conf ); 100 unset( $this->converter_conf );
96 101
  102 +
97 } 103 }
98 104
99 105
lib/ParserHandler.php
@@ -3,24 +3,25 @@ @@ -3,24 +3,25 @@
3 namespace yii\multiparser; 3 namespace yii\multiparser;
4 4
5 5
  6 +use common\components\CustomVarDamp;
  7 +
6 class ParserHandler 8 class ParserHandler
7 { 9 {
8 //@todo - добавить комменты на анг язе (ошибки выкидывать тоже на англ яз.) 10 //@todo - добавить комменты на анг язе (ошибки выкидывать тоже на англ яз.)
9 //@todo - сделать универсальную обработку ошибок 11 //@todo - сделать универсальную обработку ошибок
10 //@todo - возможно отказаться от YiiParserHandler 12 //@todo - возможно отказаться от YiiParserHandler
11 const DEFAULT_MODE = 'web'; 13 const DEFAULT_MODE = 'web';
12 - /** @var string */  
13 - protected $filePath; 14 +
14 15
15 /** @var string */ 16 /** @var string */
16 protected $configuration = []; 17 protected $configuration = [];
17 /** @var string */ 18 /** @var string */
18 protected $custom_configuration = []; 19 protected $custom_configuration = [];
19 20
20 - /** @var instance of SplFileObject */  
21 - protected $fileObject; 21 + /** @var file handle */
  22 + protected $file;
22 23
23 - /** @var string - extension of file $filePath */ 24 + /** @var string - extension of file $file_path */
24 protected $extension; 25 protected $extension;
25 26
26 /** @var string - */ 27 /** @var string - */
@@ -32,9 +33,9 @@ class ParserHandler @@ -32,9 +33,9 @@ class ParserHandler
32 /** 33 /**
33 * @param string first line in file for parsing 34 * @param string first line in file for parsing
34 */ 35 */
35 - public function setup($filePath, $options = []) 36 + public function setup($file_path, $options = [])
36 { 37 {
37 - $this->filePath = $filePath; 38 + //$this->file_path = $file_path;
38 if (isset($options['mode'])) { 39 if (isset($options['mode'])) {
39 40
40 $this->mode = $options['mode']; 41 $this->mode = $options['mode'];
@@ -47,12 +48,10 @@ class ParserHandler @@ -47,12 +48,10 @@ class ParserHandler
47 } 48 }
48 49
49 $this->options = $options; 50 $this->options = $options;
50 -  
51 - $this->fileObject = new \SplFileObject($this->filePath, 'r');  
52 -  
53 - $options['file'] = $this->fileObject;  
54 - $this->extension = $this->fileObject->getExtension();  
55 - 51 + $this->file = fopen($file_path, 'r');
  52 + $options['file'] = $this->file;
  53 + $options['file_path'] = $file_path;
  54 + $this->extension = pathinfo( $file_path, PATHINFO_EXTENSION );
56 $this->custom_configuration = $this->getCustomConfiguration($this->extension, $this->mode); 55 $this->custom_configuration = $this->getCustomConfiguration($this->extension, $this->mode);
57 $this->custom_configuration = array_merge_recursive($this->custom_configuration, $options); 56 $this->custom_configuration = array_merge_recursive($this->custom_configuration, $options);
58 57
@@ -65,7 +64,9 @@ class ParserHandler @@ -65,7 +64,9 @@ class ParserHandler
65 $parser->setup(); 64 $parser->setup();
66 $result = $parser->read(); 65 $result = $parser->read();
67 66
68 - unset($this->fileObject); 67 + unset($parser);
  68 + fclose( $this->file );
  69 +
69 return $result; 70 return $result;
70 } 71 }
71 72
lib/TableParser.php
@@ -160,7 +160,7 @@ abstract class TableParser extends Parser { @@ -160,7 +160,7 @@ abstract class TableParser extends Parser {
160 160
161 protected function filterRow(){ 161 protected function filterRow(){
162 // если есть заголовок - все значения нужны, не фильтруем 162 // если есть заголовок - все значения нужны, не фильтруем
163 - if ( $this->has_header_row || $this->row === NULL ) { 163 + if ( $this->has_header_row || !is_array( $this->row ) ) {
164 return; 164 return;
165 } 165 }
166 $this->row = array_filter( $this->row, function($val){ 166 $this->row = array_filter( $this->row, function($val){
lib/XlsxParser.php
@@ -86,12 +86,13 @@ class XlsxParser extends TableParser { @@ -86,12 +86,13 @@ class XlsxParser extends TableParser {
86 } 86 }
87 87
88 $zip = new \ZipArchive; 88 $zip = new \ZipArchive;
89 - if ( $zip->open( $this->file->getPathname() ) === TRUE ) { 89 + if ( $zip->open( $this->file_path ) === TRUE ) {
90 $zip->extractTo( $this->path_for_extract_files . '/' ); 90 $zip->extractTo( $this->path_for_extract_files . '/' );
91 $zip->close(); 91 $zip->close();
92 } else { 92 } else {
93 throw new \Exception( 'Ошибка чтения xlsx файла' ); 93 throw new \Exception( 'Ошибка чтения xlsx файла' );
94 } 94 }
  95 + unset($zip);
95 } 96 }
96 97
97 protected function readSheets () 98 protected function readSheets ()
@@ -15,8 +15,8 @@ class XmlParser extends Parser{ @@ -15,8 +15,8 @@ class XmlParser extends Parser{
15 15
16 public function read() 16 public function read()
17 { 17 {
18 - $file = $this->file;  
19 - $result = $this->xmlToArray( $file->getPathname() ); 18 + //$file = $this->file;
  19 + $result = $this->xmlToArray( );
20 20
21 if ( isset($this->node) ) { 21 if ( isset($this->node) ) {
22 22
@@ -36,13 +36,13 @@ class XmlParser extends Parser{ @@ -36,13 +36,13 @@ class XmlParser extends Parser{
36 * @throws Exception 36 * @throws Exception
37 * @throws \Exception 37 * @throws \Exception
38 */ 38 */
39 - protected function xmlToArray( $file_path ) { 39 + protected function xmlToArray( ) {
40 40
41 try { 41 try {
42 - $xml = new \SimpleXMLElement( $file_path, 0, true ); 42 + $xml = new \SimpleXMLElement( $this->file_path, 0, true );
43 //\common\components\CustomVarDamp::dumpAndDie($xml->children()->children()); 43 //\common\components\CustomVarDamp::dumpAndDie($xml->children()->children());
44 $result = $this->recursiveXMLToArray( $xml ); 44 $result = $this->recursiveXMLToArray( $xml );
45 - } catch(Exception $ex) { 45 + } catch(\Exception $ex) {
46 46
47 throw $ex; 47 throw $ex;
48 } 48 }
lib/YiiMultiparser.php
@@ -17,7 +17,7 @@ class YiiMultiparser extends Component{ @@ -17,7 +17,7 @@ class YiiMultiparser extends Component{
17 17
18 public $configuration; 18 public $configuration;
19 public $parserHandler; 19 public $parserHandler;
20 -public $filePath; 20 +//public $file_path;
21 21
22 public function init() 22 public function init()
23 { 23 {
@@ -30,7 +30,7 @@ public $filePath; @@ -30,7 +30,7 @@ public $filePath;
30 30
31 public function parse( $filePath, $options = [] ){ 31 public function parse( $filePath, $options = [] ){
32 32
33 - $this->filePath = $filePath; 33 + // $this->file_path = $file_path;
34 $this->parserHandler->setup( $filePath, $options ); 34 $this->parserHandler->setup( $filePath, $options );
35 35
36 return $this->parserHandler->run(); 36 return $this->parserHandler->run();
lib/YiiParserHandler.php
@@ -18,9 +18,9 @@ class YiiParserHandler extends ParserHandler{ @@ -18,9 +18,9 @@ class YiiParserHandler extends ParserHandler{
18 * @param array $options 18 * @param array $options
19 * проверяет читабельность переданного файла, а также наличие настроек парсера в конфигурационном файле для данного типа файла 19 * проверяет читабельность переданного файла, а также наличие настроек парсера в конфигурационном файле для данного типа файла
20 */ 20 */
21 -// public function setup($filePath, $options = []) 21 +// public function setup($file_path, $options = [])
22 // { 22 // {
23 -// $this->filePath = $filePath; 23 +// $this->file_path = $file_path;
24 // if (isset($options['mode'])) { 24 // if (isset($options['mode'])) {
25 // 25 //
26 // $this->mode = $options['mode']; 26 // $this->mode = $options['mode'];
@@ -35,10 +35,10 @@ class YiiParserHandler extends ParserHandler{ @@ -35,10 +35,10 @@ class YiiParserHandler extends ParserHandler{
35 // $this->options = $options; 35 // $this->options = $options;
36 // 36 //
37 // try { 37 // try {
38 -// $this->fileObject = new \SplFileObject($this->filePath, 'r'); 38 +// $this->fileObject = new \SplFileObject($this->file_path, 'r');
39 // } catch (\ErrorException $e) { 39 // } catch (\ErrorException $e) {
40 -// // Yii::warning("Ошибка открытия файла {$this->filePath}");  
41 -// echo "Ошибка открытия файла {$this->filePath}"; 40 +// // Yii::warning("Ошибка открытия файла {$this->file_path}");
  41 +// echo "Ошибка открытия файла {$this->file_path}";
42 // return []; 42 // return [];
43 // } 43 // }
44 // 44 //