From 0c8b9dfc85a639b655e7d5d3c7dd419978f25419 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 ++- 9 files changed, 33 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 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": "*", -- libgit2 0.21.4