From 221da14e7555f531c3ed670becdf127cb8273514 Mon Sep 17 00:00:00 2001 From: Mihail Date: Mon, 9 Nov 2015 10:56:35 +0200 Subject: [PATCH] change SplFileObject om fopen --- lib/CsvParser.php | 8 ++++---- lib/Parser.php | 8 +++++++- lib/ParserHandler.php | 29 +++++++++++++++-------------- lib/TableParser.php | 2 +- lib/XlsxParser.php | 3 ++- lib/XmlParser.php | 10 +++++----- lib/YiiMultiparser.php | 4 ++-- lib/YiiParserHandler.php | 10 +++++----- 8 files changed, 41 insertions(+), 33 deletions(-) diff --git a/lib/CsvParser.php b/lib/CsvParser.php index 05b57c2..1d3a079 100644 --- a/lib/CsvParser.php +++ b/lib/CsvParser.php @@ -22,9 +22,9 @@ class CsvParser extends TableParser public function setup() { - $this->file->setCsvControl($this->delimiter); - $this->file->setFlags(\SplFileObject::READ_CSV); - $this->file->setFlags(\SplFileObject::SKIP_EMPTY); +// $this->file->setCsvControl($this->delimiter); +// $this->file->setFlags(\SplFileObject::READ_CSV); +// $this->file->setFlags(\SplFileObject::SKIP_EMPTY); parent::setup(); @@ -42,7 +42,7 @@ class CsvParser extends TableParser protected function readRow( ) { - $this->row = $this->file->fgetcsv(); + $this->row = fgetcsv( $this->file, 0, $this->delimiter ); } protected function isEmptyRow(){ diff --git a/lib/Parser.php b/lib/Parser.php index 4315c10..2aca70f 100644 --- a/lib/Parser.php +++ b/lib/Parser.php @@ -12,13 +12,17 @@ namespace yii\multiparser; //@todo - xml - убрать из названий функций xml и array - это и так понятно +use common\components\CustomVarDamp; + abstract class Parser { public $converter_conf = []; protected $converter = NULL; - /** @var экземляр SplFileObject читаемого файла */ + /** @var file-resource читаемого файла */ public $file; + /** @var string путь читаемого файла */ + public $file_path; /** * @var array - результирующий массив с отпарсенными значениями @@ -90,10 +94,12 @@ abstract class Parser protected function cleanUp( ) { + unset( $this->file ); unset( $this->converter ); unset( $this->converter_conf ); + } diff --git a/lib/ParserHandler.php b/lib/ParserHandler.php index 6658962..cf7d6f5 100644 --- a/lib/ParserHandler.php +++ b/lib/ParserHandler.php @@ -3,24 +3,25 @@ namespace yii\multiparser; +use common\components\CustomVarDamp; + class ParserHandler { //@todo - добавить комменты на анг язе (ошибки выкидывать тоже на англ яз.) //@todo - сделать универсальную обработку ошибок //@todo - возможно отказаться от YiiParserHandler const DEFAULT_MODE = 'web'; - /** @var string */ - protected $filePath; + /** @var string */ protected $configuration = []; /** @var string */ protected $custom_configuration = []; - /** @var instance of SplFileObject */ - protected $fileObject; + /** @var file handle */ + protected $file; - /** @var string - extension of file $filePath */ + /** @var string - extension of file $file_path */ protected $extension; /** @var string - */ @@ -32,9 +33,9 @@ class ParserHandler /** * @param string first line in file for parsing */ - public function setup($filePath, $options = []) + public function setup($file_path, $options = []) { - $this->filePath = $filePath; + //$this->file_path = $file_path; if (isset($options['mode'])) { $this->mode = $options['mode']; @@ -47,12 +48,10 @@ class ParserHandler } $this->options = $options; - - $this->fileObject = new \SplFileObject($this->filePath, 'r'); - - $options['file'] = $this->fileObject; - $this->extension = $this->fileObject->getExtension(); - + $this->file = fopen($file_path, 'r'); + $options['file'] = $this->file; + $options['file_path'] = $file_path; + $this->extension = pathinfo( $file_path, PATHINFO_EXTENSION ); $this->custom_configuration = $this->getCustomConfiguration($this->extension, $this->mode); $this->custom_configuration = array_merge_recursive($this->custom_configuration, $options); @@ -65,7 +64,9 @@ class ParserHandler $parser->setup(); $result = $parser->read(); - unset($this->fileObject); + unset($parser); + fclose( $this->file ); + return $result; } diff --git a/lib/TableParser.php b/lib/TableParser.php index 748da48..36f15e2 100644 --- a/lib/TableParser.php +++ b/lib/TableParser.php @@ -160,7 +160,7 @@ abstract class TableParser extends Parser { protected function filterRow(){ // если есть заголовок - все значения нужны, не фильтруем - if ( $this->has_header_row || $this->row === NULL ) { + if ( $this->has_header_row || !is_array( $this->row ) ) { return; } $this->row = array_filter( $this->row, function($val){ diff --git a/lib/XlsxParser.php b/lib/XlsxParser.php index ed28043..b7ed297 100644 --- a/lib/XlsxParser.php +++ b/lib/XlsxParser.php @@ -86,12 +86,13 @@ class XlsxParser extends TableParser { } $zip = new \ZipArchive; - if ( $zip->open( $this->file->getPathname() ) === TRUE ) { + if ( $zip->open( $this->file_path ) === TRUE ) { $zip->extractTo( $this->path_for_extract_files . '/' ); $zip->close(); } else { throw new \Exception( 'Ошибка чтения xlsx файла' ); } + unset($zip); } protected function readSheets () diff --git a/lib/XmlParser.php b/lib/XmlParser.php index d021dc1..a4a665b 100644 --- a/lib/XmlParser.php +++ b/lib/XmlParser.php @@ -15,8 +15,8 @@ class XmlParser extends Parser{ public function read() { - $file = $this->file; - $result = $this->xmlToArray( $file->getPathname() ); + //$file = $this->file; + $result = $this->xmlToArray( ); if ( isset($this->node) ) { @@ -36,13 +36,13 @@ class XmlParser extends Parser{ * @throws Exception * @throws \Exception */ - protected function xmlToArray( $file_path ) { + protected function xmlToArray( ) { try { - $xml = new \SimpleXMLElement( $file_path, 0, true ); + $xml = new \SimpleXMLElement( $this->file_path, 0, true ); //\common\components\CustomVarDamp::dumpAndDie($xml->children()->children()); $result = $this->recursiveXMLToArray( $xml ); - } catch(Exception $ex) { + } catch(\Exception $ex) { throw $ex; } diff --git a/lib/YiiMultiparser.php b/lib/YiiMultiparser.php index 886e7fe..b449c98 100644 --- a/lib/YiiMultiparser.php +++ b/lib/YiiMultiparser.php @@ -17,7 +17,7 @@ class YiiMultiparser extends Component{ public $configuration; public $parserHandler; -public $filePath; +//public $file_path; public function init() { @@ -30,7 +30,7 @@ public $filePath; public function parse( $filePath, $options = [] ){ - $this->filePath = $filePath; + // $this->file_path = $file_path; $this->parserHandler->setup( $filePath, $options ); return $this->parserHandler->run(); diff --git a/lib/YiiParserHandler.php b/lib/YiiParserHandler.php index 5c8253b..2fbaefc 100644 --- a/lib/YiiParserHandler.php +++ b/lib/YiiParserHandler.php @@ -18,9 +18,9 @@ class YiiParserHandler extends ParserHandler{ * @param array $options * проверяет читабельность переданного файла, а также наличие настроек парсера в конфигурационном файле для данного типа файла */ -// public function setup($filePath, $options = []) +// public function setup($file_path, $options = []) // { -// $this->filePath = $filePath; +// $this->file_path = $file_path; // if (isset($options['mode'])) { // // $this->mode = $options['mode']; @@ -35,10 +35,10 @@ class YiiParserHandler extends ParserHandler{ // $this->options = $options; // // try { -// $this->fileObject = new \SplFileObject($this->filePath, 'r'); +// $this->fileObject = new \SplFileObject($this->file_path, 'r'); // } catch (\ErrorException $e) { -// // Yii::warning("Ошибка открытия файла {$this->filePath}"); -// echo "Ошибка открытия файла {$this->filePath}"; +// // Yii::warning("Ошибка открытия файла {$this->file_path}"); +// echo "Ошибка открытия файла {$this->file_path}"; // return []; // } // -- libgit2 0.21.4