From 036717a10cde2c2a2ba5a5127f88ff57429c3867 Mon Sep 17 00:00:00 2001 From: Mihail Date: Mon, 7 Sep 2015 15:00:12 +0300 Subject: [PATCH] add error exceptions, rewrite parser to universal composer pac --- backend/components/parsers/CsvParser.php | 163 ------------------------------------------------------------------------------------------------------------------------------------------------------------------- backend/components/parsers/CustomCsvParser.php | 2 +- backend/components/parsers/ParserConfigurator.php | 22 ---------------------- backend/components/parsers/ParserHandler.php | 47 ----------------------------------------------- backend/components/parsers/ParserInterface.php | 18 ------------------ backend/models/UploadFileParsingForm.php | 5 +++-- common/components/CustomVarDamp.php | 27 +++++++++++++++++++++++++++ common/components/debug/CustomVarDamp.php | 27 --------------------------- composer.json | 3 ++- vendor/composer/autoload_psr4.php | 1 + vendor/yiisoft/extensions.php | 9 +++++++++ vendor/yiisoft/multiparser/CsvParser.php | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vendor/yiisoft/multiparser/ParserConfigurator.php | 26 ++++++++++++++++++++++++++ vendor/yiisoft/multiparser/ParserHandler.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vendor/yiisoft/multiparser/ParserInterface.php | 19 +++++++++++++++++++ 15 files changed, 340 insertions(+), 281 deletions(-) delete mode 100644 backend/components/parsers/CsvParser.php delete mode 100644 backend/components/parsers/ParserConfigurator.php delete mode 100644 backend/components/parsers/ParserHandler.php delete mode 100644 backend/components/parsers/ParserInterface.php create mode 100644 common/components/CustomVarDamp.php delete mode 100644 common/components/debug/CustomVarDamp.php create mode 100644 vendor/yiisoft/multiparser/CsvParser.php create mode 100644 vendor/yiisoft/multiparser/ParserConfigurator.php create mode 100644 vendor/yiisoft/multiparser/ParserHandler.php create mode 100644 vendor/yiisoft/multiparser/ParserInterface.php diff --git a/backend/components/parsers/CsvParser.php b/backend/components/parsers/CsvParser.php deleted file mode 100644 index 66b9b73..0000000 --- a/backend/components/parsers/CsvParser.php +++ /dev/null @@ -1,163 +0,0 @@ -file->setCsvControl($this->delimiter); - $this->file->setFlags(\SplFileObject::READ_CSV); - $this->file->setFlags(\SplFileObject::SKIP_EMPTY); - if ($this->auto_detect_first_line) { - $this->shiftToFirstValuableLine(); - } - } - - /** - * определяет первую значимую строку, - * считывается файл пока в нем не встретится строка с непустыми колонками - * в количестве указанном в атрибуте min_column_quantity - * в результате выполнения курсор ресурса будет находится на последней незначимой строке - */ - protected function shiftToFirstValuableLine() - { - - $finish = false; - - while (!$finish) { - $j = 0; - $row = $this->readRow(); - if ($row === false) { - continue; - } - - for ($i = 1; $i <= count($row); $i++) { - - if ($row[$i - 1] <> '') { - $j++; - } - - if ($j >= $this->min_column_quantity) { - break 2; - } - } - } - } - - /** - * @return array - итоговый двумерный массив с результатом парсинга - * метод считывает с открытого файла данные построчно - */ - public function read() - { - $return = []; - - $current_line = 0; - $this->keys = NULL; - - while (($row = $this->readRow()) !== FALSE) { - $current_line++; - - if ($this->hasHeaderRow) { - if ($this->keys === NULL) { - $this->keys = array_values($row); - } else { - - if (count($this->keys) !== count($row)) { -// - Yii::warning("Invalid columns detected on line #$current_line ."); - return $return; - } - - $return[] = array_combine($this->keys, $row); - } - } - else - { - $return[] = $row; - } - // если у нас установлен лимит, при его достижении прекращаем парсинг - if (($this->last_line) && ($current_line > $this->last_line)) { - break; - } - - } - - $this->closeHandler(); - return $return; - } - - - protected function closeHandler() - { - $this->file = NULL; - } - - /** - * @return array - одномерный массив результата парсинга строки - */ - protected function readRow() - { - - $row = $this->file->fgetcsv(); - if (is_array($row) && $this->first_column) { - - $row = array_slice($row, $this->first_column); - - } - if (is_null($row)) - $row = false; - - return $row; - - } - - -} \ No newline at end of file diff --git a/backend/components/parsers/CustomCsvParser.php b/backend/components/parsers/CustomCsvParser.php index 18df388..0c6c4d0 100644 --- a/backend/components/parsers/CustomCsvParser.php +++ b/backend/components/parsers/CustomCsvParser.php @@ -9,7 +9,7 @@ namespace backend\components\parsers; -class CustomCsvParser extends CsvParser { +class CustomCsvParser extends \yii\multiparser\CsvParser { protected function readRow() { diff --git a/backend/components/parsers/ParserConfigurator.php b/backend/components/parsers/ParserConfigurator.php deleted file mode 100644 index 7908a8c..0000000 --- a/backend/components/parsers/ParserConfigurator.php +++ /dev/null @@ -1,22 +0,0 @@ - 'backend\components\parsers\CustomCsvParser', - // 'file' => $this->fileObject, - 'auto_detect_first_line' => true, - ]; - } - -} \ No newline at end of file diff --git a/backend/components/parsers/ParserHandler.php b/backend/components/parsers/ParserHandler.php deleted file mode 100644 index cd59c48..0000000 --- a/backend/components/parsers/ParserHandler.php +++ /dev/null @@ -1,47 +0,0 @@ -filePath = $filePath; - $this->mode = $mode; - - try { - $this->fileObject = new \SplFileObject( $this->filePath , 'r' );; - } catch (\ErrorException $e) { - Yii::warning("Ошибка открытия файла {$this->filePath}"); - } - - //preg_match( '/\.[^\.]+$/i',$filePath, $resultArray ); - $this->extension = $this->fileObject->getExtension(); - } - - public function run(){ - - $parser = Yii::createObject( ParserConfigurator::getConfiguration() ); - $parser->setup(); - return $parser->read(); - } -} - diff --git a/backend/components/parsers/ParserInterface.php b/backend/components/parsers/ParserInterface.php deleted file mode 100644 index 46db651..0000000 --- a/backend/components/parsers/ParserInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -run(); if( !is_array($data) ){ diff --git a/common/components/CustomVarDamp.php b/common/components/CustomVarDamp.php new file mode 100644 index 0000000..adb1aff --- /dev/null +++ b/common/components/CustomVarDamp.php @@ -0,0 +1,27 @@ +"; + echo static::dumpAsString($var, $depth, $highlight); + echo ""; + die; + } + public static function dump($var, $depth = 10, $highlight = false) + { + echo "
";
+        echo static::dumpAsString($var, $depth, $highlight);
+        echo "
"; + + } +} \ No newline at end of file diff --git a/common/components/debug/CustomVarDamp.php b/common/components/debug/CustomVarDamp.php deleted file mode 100644 index 7bdcb78..0000000 --- a/common/components/debug/CustomVarDamp.php +++ /dev/null @@ -1,27 +0,0 @@ -"; - echo static::dumpAsString($var, $depth, $highlight); - echo ""; - die; - } - public static function dump($var, $depth = 10, $highlight = false) - { - echo "
";
-        echo static::dumpAsString($var, $depth, $highlight);
-        echo "
"; - - } -} \ No newline at end of file diff --git a/composer.json b/composer.json index 22993b0..abe9d61 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "php": ">=5.4.0", "yiisoft/yii2": ">=2.0.6", "yiisoft/yii2-bootstrap": "*", - "yiisoft/yii2-swiftmailer": "*" + "yiisoft/yii2-swiftmailer": "*", + "yiisoft/multiparser": "*" }, "require-dev": { "yiisoft/yii2-codeception": "*", diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php index 5cef906..3bef212 100644 --- a/vendor/composer/autoload_psr4.php +++ b/vendor/composer/autoload_psr4.php @@ -7,6 +7,7 @@ $baseDir = dirname($vendorDir); return array( 'yii\\swiftmailer\\' => array($vendorDir . '/yiisoft/yii2-swiftmailer'), + 'yii\\multiparser\\' => array($vendorDir . '/yiisoft/multiparser'), 'yii\\gii\\' => array($vendorDir . '/yiisoft/yii2-gii'), 'yii\\faker\\' => array($vendorDir . '/yiisoft/yii2-faker'), 'yii\\debug\\' => array($vendorDir . '/yiisoft/yii2-debug'), diff --git a/vendor/yiisoft/extensions.php b/vendor/yiisoft/extensions.php index aa78107..611a823 100644 --- a/vendor/yiisoft/extensions.php +++ b/vendor/yiisoft/extensions.php @@ -12,6 +12,15 @@ return array ( '@yii/swiftmailer' => $vendorDir . '/yiisoft/yii2-swiftmailer', ), ), + 'yiisoft/yii2-multiparser' => + array ( + 'name' => 'yiisoft/multiparser', + 'version' => '1.0', + 'alias' => + array ( + '@yii/multiparser' => $vendorDir . '/yiisoft/multiparser', + ), + ), 'yiisoft/yii2-codeception' => array ( 'name' => 'yiisoft/yii2-codeception', diff --git a/vendor/yiisoft/multiparser/CsvParser.php b/vendor/yiisoft/multiparser/CsvParser.php new file mode 100644 index 0000000..cf0972d --- /dev/null +++ b/vendor/yiisoft/multiparser/CsvParser.php @@ -0,0 +1,157 @@ +file->setCsvControl($this->delimiter); + $this->file->setFlags(\SplFileObject::READ_CSV); + $this->file->setFlags(\SplFileObject::SKIP_EMPTY); + if ($this->auto_detect_first_line) { + $this->shiftToFirstValuableLine(); + } + } + + /** + * определяет первую значимую строку, + * считывается файл пока в нем не встретится строка с непустыми колонками + * в количестве указанном в атрибуте min_column_quantity + * в результате выполнения курсор ресурса будет находится на последней незначимой строке + */ + protected function shiftToFirstValuableLine() + { + + $finish = false; + + while (!$finish) { + $j = 0; + $row = $this->readRow(); + if ($row === false) { + continue; + } + + for ($i = 1; $i <= count($row); $i++) { + + if ($row[$i - 1] <> '') { + $j++; + } + + if ($j >= $this->min_column_quantity) { + break 2; + } + } + } + } + + /** + * @return array - итоговый двумерный массив с результатом парсинга + * метод считывает с открытого файла данные построчно + */ + public function read() + { + $return = []; + + $current_line = 0; + $this->keys = NULL; + + while (($row = $this->readRow()) !== FALSE) { + $current_line++; + + if ($this->hasHeaderRow) { + if ($this->keys === NULL) { + $this->keys = array_values($row); + } else { + + if (count($this->keys) !== count($row)) { +// + throw new \ErrorException( "Invalid columns detected on line # {$current_line}", 0, 1, $this->file->getBasename(), $current_line); + } + + $return[] = array_combine($this->keys, $row); + } + } + else + { + $return[] = $row; + } + // если у нас установлен лимит, при его достижении прекращаем парсинг + if (($this->last_line) && ($current_line > $this->last_line)) { + break; + } + + } + + $this->closeHandler(); + return $return; + } + + + protected function closeHandler() + { + $this->file = NULL; + } + + /** + * @return array - одномерный массив результата парсинга строки + */ + protected function readRow() + { + + $row = $this->file->fgetcsv(); + if (is_array($row) && $this->first_column) { + + $row = array_slice($row, $this->first_column); + + } + if (is_null($row)) + $row = false; + + return $row; + + } + + +} \ No newline at end of file diff --git a/vendor/yiisoft/multiparser/ParserConfigurator.php b/vendor/yiisoft/multiparser/ParserConfigurator.php new file mode 100644 index 0000000..fef137b --- /dev/null +++ b/vendor/yiisoft/multiparser/ParserConfigurator.php @@ -0,0 +1,26 @@ + + ['web' => + ['class' => 'backend\components\parsers\CustomCsvParser', + 'auto_detect_first_line' => true,]]]; + + + public static function getConfiguration($extension, $mode) + { + if (!isset( self::$configuration[$extension] )){ + throw new \ErrorException( "Parser do not maintain file with extension {$extension}"); + } + if (!isset( self::$configuration[$extension][$mode] )){ + throw new \ErrorException( "Parser configurator do not have settings for {$mode} mode"); + } + + return self::$configuration[$extension][$mode]; + } + +} \ No newline at end of file diff --git a/vendor/yiisoft/multiparser/ParserHandler.php b/vendor/yiisoft/multiparser/ParserHandler.php new file mode 100644 index 0000000..7fad905 --- /dev/null +++ b/vendor/yiisoft/multiparser/ParserHandler.php @@ -0,0 +1,95 @@ +filePath = $filePath; + if (isset($options['mode'])) { + + $this->mode = $options['mode']; + unset($options['mode']); + + } else { + + $this->mode = self::DEFAULT_MODE; + + } + + $this->options = $options; + + try { + $this->fileObject = new \SplFileObject($this->filePath, 'r'); + } catch (\ErrorException $e) { + // Yii::warning("Ошибка открытия файла {$this->filePath}"); + echo "Ошибка открытия файла {$this->filePath}"; + return []; + } + + $options['file'] = $this->fileObject; + $this->extension = $this->fileObject->getExtension(); + + try { + $this->configuration = ParserConfigurator::getConfiguration($this->extension, $this->mode); + $this->configuration = array_merge($this->configuration, $options); + + } catch (\ErrorException $e) { + echo $e->getMessage(); + return []; + } + + } + + public function run() + { + + $result = []; + // @todo - rewrite to universal manner + // \common\components\CustomVarDamp::dumpAndDie($this); + if (count($this->configuration)) { + $parser = Yii::createObject($this->configuration); + + try { + + $parser->setup(); + $result = $parser->read(); + + } catch (\ErrorException $e) { + + echo $e->getMessage(); + + } + + } + + return $result; + } +} + diff --git a/vendor/yiisoft/multiparser/ParserInterface.php b/vendor/yiisoft/multiparser/ParserInterface.php new file mode 100644 index 0000000..1cdeb0f --- /dev/null +++ b/vendor/yiisoft/multiparser/ParserInterface.php @@ -0,0 +1,19 @@ +