Commit 036717a10cde2c2a2ba5a5127f88ff57429c3867
1 parent
37656b1f
add error exceptions, rewrite parser to universal composer pac
Showing
12 changed files
with
142 additions
and
83 deletions
Show diff stats
backend/components/parsers/CustomCsvParser.php
backend/components/parsers/ParserConfigurator.php deleted
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Created by PhpStorm. | |
| 4 | - * User: Cibermag | |
| 5 | - * Date: 04.09.2015 | |
| 6 | - * Time: 18:17 | |
| 7 | - */ | |
| 8 | - | |
| 9 | -namespace backend\components\parsers; | |
| 10 | - | |
| 11 | - | |
| 12 | -class ParserConfigurator { | |
| 13 | - public static function getConfiguration () | |
| 14 | - { | |
| 15 | - return [ | |
| 16 | - 'class' => 'backend\components\parsers\CustomCsvParser', | |
| 17 | - // 'file' => $this->fileObject, | |
| 18 | - 'auto_detect_first_line' => true, | |
| 19 | - ]; | |
| 20 | - } | |
| 21 | - | |
| 22 | -} | |
| 23 | 0 | \ No newline at end of file |
backend/components/parsers/ParserHandler.php deleted
| 1 | -<?php | |
| 2 | -namespace backend\components\parsers; | |
| 3 | -use common\components\debug\CustomVarDamp; | |
| 4 | - | |
| 5 | -use Yii; | |
| 6 | - | |
| 7 | - | |
| 8 | -class ParserHandler { | |
| 9 | - | |
| 10 | -/** @var string */ | |
| 11 | - private $filePath; | |
| 12 | - | |
| 13 | - /** @var instance of SplFileObject */ | |
| 14 | - private $fileObject; | |
| 15 | - | |
| 16 | - /** @var string - extension of file $filePath */ | |
| 17 | - private $extension; | |
| 18 | - | |
| 19 | - /** @var string - extension of file $filePath */ | |
| 20 | - private $mode; | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * @param string first line in file for parsing | |
| 24 | - */ | |
| 25 | - public function __construct( $filePath, $mode ) | |
| 26 | - { | |
| 27 | - $this->filePath = $filePath; | |
| 28 | - $this->mode = $mode; | |
| 29 | - | |
| 30 | - try { | |
| 31 | - $this->fileObject = new \SplFileObject( $this->filePath , 'r' );; | |
| 32 | - } catch (\ErrorException $e) { | |
| 33 | - Yii::warning("Ошибка открытия файла {$this->filePath}"); | |
| 34 | - } | |
| 35 | - | |
| 36 | - //preg_match( '/\.[^\.]+$/i',$filePath, $resultArray ); | |
| 37 | - $this->extension = $this->fileObject->getExtension(); | |
| 38 | - } | |
| 39 | - | |
| 40 | - public function run(){ | |
| 41 | - | |
| 42 | - $parser = Yii::createObject( ParserConfigurator::getConfiguration() ); | |
| 43 | - $parser->setup(); | |
| 44 | - return $parser->read(); | |
| 45 | - } | |
| 46 | -} | |
| 47 | - |
backend/models/UploadFileParsingForm.php
| ... | ... | @@ -3,7 +3,7 @@ namespace backend\models; |
| 3 | 3 | |
| 4 | 4 | use yii\base\Model; |
| 5 | 5 | use yii\web\UploadedFile; |
| 6 | -use backend\components\parsers\ParserHandler; | |
| 6 | +use yii\multiparser\ParserHandler; | |
| 7 | 7 | use Yii; |
| 8 | 8 | use common\components\debug\CustomVarDamp; |
| 9 | 9 | |
| ... | ... | @@ -51,7 +51,8 @@ class UploadFileParsingForm extends Model |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | public function readFile($filePath){ |
| 54 | - $parser = new ParserHandler( $filePath, $this ); | |
| 54 | + | |
| 55 | + $parser = new ParserHandler( $filePath ); | |
| 55 | 56 | $data = $parser->run(); |
| 56 | 57 | |
| 57 | 58 | if( !is_array($data) ){ | ... | ... |
common/components/debug/CustomVarDamp.php renamed to common/components/CustomVarDamp.php
composer.json
| ... | ... | @@ -17,7 +17,8 @@ |
| 17 | 17 | "php": ">=5.4.0", |
| 18 | 18 | "yiisoft/yii2": ">=2.0.6", |
| 19 | 19 | "yiisoft/yii2-bootstrap": "*", |
| 20 | - "yiisoft/yii2-swiftmailer": "*" | |
| 20 | + "yiisoft/yii2-swiftmailer": "*", | |
| 21 | + "yiisoft/multiparser": "*" | |
| 21 | 22 | }, |
| 22 | 23 | "require-dev": { |
| 23 | 24 | "yiisoft/yii2-codeception": "*", | ... | ... |
vendor/composer/autoload_psr4.php
| ... | ... | @@ -7,6 +7,7 @@ $baseDir = dirname($vendorDir); |
| 7 | 7 | |
| 8 | 8 | return array( |
| 9 | 9 | 'yii\\swiftmailer\\' => array($vendorDir . '/yiisoft/yii2-swiftmailer'), |
| 10 | + 'yii\\multiparser\\' => array($vendorDir . '/yiisoft/multiparser'), | |
| 10 | 11 | 'yii\\gii\\' => array($vendorDir . '/yiisoft/yii2-gii'), |
| 11 | 12 | 'yii\\faker\\' => array($vendorDir . '/yiisoft/yii2-faker'), |
| 12 | 13 | 'yii\\debug\\' => array($vendorDir . '/yiisoft/yii2-debug'), | ... | ... |
vendor/yiisoft/extensions.php
| ... | ... | @@ -12,6 +12,15 @@ return array ( |
| 12 | 12 | '@yii/swiftmailer' => $vendorDir . '/yiisoft/yii2-swiftmailer', |
| 13 | 13 | ), |
| 14 | 14 | ), |
| 15 | + 'yiisoft/yii2-multiparser' => | |
| 16 | + array ( | |
| 17 | + 'name' => 'yiisoft/multiparser', | |
| 18 | + 'version' => '1.0', | |
| 19 | + 'alias' => | |
| 20 | + array ( | |
| 21 | + '@yii/multiparser' => $vendorDir . '/yiisoft/multiparser', | |
| 22 | + ), | |
| 23 | + ), | |
| 15 | 24 | 'yiisoft/yii2-codeception' => |
| 16 | 25 | array ( |
| 17 | 26 | 'name' => 'yiisoft/yii2-codeception', | ... | ... |
backend/components/parsers/CsvParser.php renamed to vendor/yiisoft/multiparser/CsvParser.php
| ... | ... | @@ -2,14 +2,9 @@ |
| 2 | 2 | /** |
| 3 | 3 | |
| 4 | 4 | */ |
| 5 | -//@ todo add exceptions | |
| 6 | -namespace backend\components\parsers; | |
| 5 | +namespace yii\multiparser; | |
| 7 | 6 | |
| 8 | 7 | |
| 9 | -use Yii; | |
| 10 | -use yii\base\ErrorException; | |
| 11 | -use common\components\debug\CustomVarDamp; | |
| 12 | - | |
| 13 | 8 | class CsvParser implements ParserInterface |
| 14 | 9 | { |
| 15 | 10 | |
| ... | ... | @@ -112,8 +107,7 @@ class CsvParser implements ParserInterface |
| 112 | 107 | |
| 113 | 108 | if (count($this->keys) !== count($row)) { |
| 114 | 109 | // |
| 115 | - Yii::warning("Invalid columns detected on line #$current_line ."); | |
| 116 | - return $return; | |
| 110 | + throw new \ErrorException( "Invalid columns detected on line # {$current_line}", 0, 1, $this->file->getBasename(), $current_line); | |
| 117 | 111 | } |
| 118 | 112 | |
| 119 | 113 | $return[] = array_combine($this->keys, $row); | ... | ... |
| 1 | +<?php | |
| 2 | +namespace yii\multiparser; | |
| 3 | + | |
| 4 | +class ParserConfigurator | |
| 5 | +{ | |
| 6 | + | |
| 7 | + private static $configuration = [ | |
| 8 | + 'csv' => | |
| 9 | + ['web' => | |
| 10 | + ['class' => 'backend\components\parsers\CustomCsvParser', | |
| 11 | + 'auto_detect_first_line' => true,]]]; | |
| 12 | + | |
| 13 | + | |
| 14 | + public static function getConfiguration($extension, $mode) | |
| 15 | + { | |
| 16 | + if (!isset( self::$configuration[$extension] )){ | |
| 17 | + throw new \ErrorException( "Parser do not maintain file with extension {$extension}"); | |
| 18 | + } | |
| 19 | + if (!isset( self::$configuration[$extension][$mode] )){ | |
| 20 | + throw new \ErrorException( "Parser configurator do not have settings for {$mode} mode"); | |
| 21 | + } | |
| 22 | + | |
| 23 | + return self::$configuration[$extension][$mode]; | |
| 24 | + } | |
| 25 | + | |
| 26 | +} | |
| 0 | 27 | \ No newline at end of file | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace yii\multiparser; | |
| 4 | + | |
| 5 | +use Yii; | |
| 6 | + | |
| 7 | +class ParserHandler | |
| 8 | +{ | |
| 9 | + | |
| 10 | + const DEFAULT_MODE = 'web'; | |
| 11 | + /** @var string */ | |
| 12 | + protected $filePath; | |
| 13 | + | |
| 14 | + /** @var string */ | |
| 15 | + protected $configuration; | |
| 16 | + | |
| 17 | + /** @var instance of SplFileObject */ | |
| 18 | + protected $fileObject; | |
| 19 | + | |
| 20 | + /** @var string - extension of file $filePath */ | |
| 21 | + protected $extension; | |
| 22 | + | |
| 23 | + /** @var string - extension of file $filePath */ | |
| 24 | + protected $mode; | |
| 25 | + | |
| 26 | + /** @var string - extension of file $filePath */ | |
| 27 | + protected $options; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @param string first line in file for parsing | |
| 31 | + */ | |
| 32 | + public function __construct($filePath, $options = []) | |
| 33 | + { | |
| 34 | + $this->filePath = $filePath; | |
| 35 | + if (isset($options['mode'])) { | |
| 36 | + | |
| 37 | + $this->mode = $options['mode']; | |
| 38 | + unset($options['mode']); | |
| 39 | + | |
| 40 | + } else { | |
| 41 | + | |
| 42 | + $this->mode = self::DEFAULT_MODE; | |
| 43 | + | |
| 44 | + } | |
| 45 | + | |
| 46 | + $this->options = $options; | |
| 47 | + | |
| 48 | + try { | |
| 49 | + $this->fileObject = new \SplFileObject($this->filePath, 'r'); | |
| 50 | + } catch (\ErrorException $e) { | |
| 51 | + // Yii::warning("Ошибка открытия файла {$this->filePath}"); | |
| 52 | + echo "Ошибка открытия файла {$this->filePath}"; | |
| 53 | + return []; | |
| 54 | + } | |
| 55 | + | |
| 56 | + $options['file'] = $this->fileObject; | |
| 57 | + $this->extension = $this->fileObject->getExtension(); | |
| 58 | + | |
| 59 | + try { | |
| 60 | + $this->configuration = ParserConfigurator::getConfiguration($this->extension, $this->mode); | |
| 61 | + $this->configuration = array_merge($this->configuration, $options); | |
| 62 | + | |
| 63 | + } catch (\ErrorException $e) { | |
| 64 | + echo $e->getMessage(); | |
| 65 | + return []; | |
| 66 | + } | |
| 67 | + | |
| 68 | + } | |
| 69 | + | |
| 70 | + public function run() | |
| 71 | + { | |
| 72 | + | |
| 73 | + $result = []; | |
| 74 | + // @todo - rewrite to universal manner | |
| 75 | + // \common\components\CustomVarDamp::dumpAndDie($this); | |
| 76 | + if (count($this->configuration)) { | |
| 77 | + $parser = Yii::createObject($this->configuration); | |
| 78 | + | |
| 79 | + try { | |
| 80 | + | |
| 81 | + $parser->setup(); | |
| 82 | + $result = $parser->read(); | |
| 83 | + | |
| 84 | + } catch (\ErrorException $e) { | |
| 85 | + | |
| 86 | + echo $e->getMessage(); | |
| 87 | + | |
| 88 | + } | |
| 89 | + | |
| 90 | + } | |
| 91 | + | |
| 92 | + return $result; | |
| 93 | + } | |
| 94 | +} | |
| 95 | + | ... | ... |
backend/components/parsers/ParserInterface.php renamed to vendor/yiisoft/multiparser/ParserInterface.php