99use Mathielen \ImportEngine \Import \Run \ImportRunner ;
1010use Mathielen \ImportEngine \ValueObject \ImportRequest ;
1111use Mathielen \ImportEngine \ValueObject \ImportRun ;
12+ use Mathielen \ImportEngineBundle \Utils ;
1213use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
1314use Symfony \Component \Console \Helper \ProgressBar ;
1415use Symfony \Component \Console \Helper \Table ;
@@ -68,7 +69,7 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
6869 {
6970 $ output ->writeln ("Commencing " .($ isDryrun ?'<comment>dry-run</comment> ' :'' )."import using importer " .(empty ($ importerId )?'<comment>unknown</comment> ' :"<info> $ importerId</info> " )." with source provider <info> $ sourceProviderId</info> and source id <info> $ sourceId</info> " );
7071
71- $ sourceId = $ this -> parseSourceId ($ sourceId );
72+ $ sourceId = Utils:: parseSourceId ($ sourceId );
7273 $ progress = new ProgressBar ($ output );
7374
7475 //set limit
@@ -81,7 +82,7 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
8182 /** @var ImportBuilder $importBuilder */
8283 $ importBuilder = $ this ->getContainer ()->get ('mathielen_importengine.import.builder ' );
8384
84- $ importRequest = new ImportRequest ($ sourceId , $ sourceProviderId , $ importerId , self ::whoAmI ().'@CLI ' );
85+ $ importRequest = new ImportRequest ($ sourceId , $ sourceProviderId , $ importerId , Utils ::whoAmI ().'@CLI ' );
8586
8687 $ import = $ importBuilder ->build ($ importRequest );
8788
@@ -90,7 +91,7 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
9091 $ importRun ->setContext ($ context );
9192
9293 //status callback
93- $ this ->getContainer ()->get ('event_dispatcher ' )->addListener (' data-import.read ' , function (ImportItemEvent $ event ) use ($ output , &$ progress ) {
94+ $ this ->getContainer ()->get ('event_dispatcher ' )->addListener (ImportItemEvent:: AFTER_READ , function (ImportItemEvent $ event ) use ($ output , &$ progress ) {
9495 /** @var ImportRun $importRun */
9596 $ importRun = $ event ->getContext ();
9697 $ stats = $ importRun ->getStatistics ();
@@ -132,6 +133,9 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
132133
133134 protected function writeValidationViolations (array $ violations , Table $ table )
134135 {
136+ if (empty ($ violations )) {
137+ return ;
138+ }
135139 $ violations = $ violations ['source ' ] + $ violations ['target ' ];
136140
137141 $ table
@@ -152,7 +156,7 @@ protected function writeValidationViolations(array $violations, Table $table)
152156
153157 $ i = 0 ;
154158 foreach ($ tree as $ violation =>$ lines ) {
155- $ table ->addRow ([$ violation , join (', ' , self ::numbersToRangeText ($ lines ))]);
159+ $ table ->addRow ([$ violation , join (', ' , Utils ::numbersToRangeText ($ lines ))]);
156160 ++$ i ;
157161
158162 if ($ i === self ::MAX_VIOLATION_ERRORS ) {
@@ -182,97 +186,4 @@ protected function writeStatistics(array $statistics, Table $table)
182186 $ table ->render ();
183187 }
184188
185- private function parseSourceId ($ sourceId )
186- {
187- if (is_file ($ sourceId )) {
188- return $ sourceId ;
189- } elseif (preg_match ('/^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+/ ' , $ sourceId )) {
190- $ parsedSourceId = parse_url ($ sourceId );
191- if (isset ($ parsedSourceId ['query ' ])) {
192- parse_str ($ parsedSourceId ['query ' ], $ parsedSourceId ['query ' ]);
193- }
194- $ pathTokens = explode ('. ' , $ parsedSourceId ['path ' ]);
195- $ method = array_pop ($ pathTokens );
196- $ service = join ('. ' , $ pathTokens );
197-
198- return array (
199- 'service ' => $ service ,
200- 'method ' => $ method ,
201- 'arguments ' => isset ($ parsedSourceId ['query ' ])?$ parsedSourceId ['query ' ]:null
202- );
203- }
204-
205- return $ sourceId ;
206- }
207-
208- /**
209- * @return bool
210- */
211- public static function isWindows ()
212- {
213- return (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' );
214- }
215-
216- /**
217- * @return bool
218- */
219- public static function isCli ()
220- {
221- return php_sapi_name () == "cli " ;
222- }
223-
224- /**
225- * @return string
226- */
227- public static function whoAmI ()
228- {
229- if (self ::isWindows ()) {
230- $ user = getenv ("username " );
231- } else {
232- $ processUser = posix_getpwuid (posix_geteuid ());
233- $ user = $ processUser ['name ' ];
234- }
235-
236- return $ user ;
237- }
238-
239- public static function numbersToRangeText (array $ numbers )
240- {
241- if (empty ($ numbers )) {
242- return [];
243- }
244-
245- $ ranges = [];
246- sort ($ numbers );
247-
248- $ currentRange = [];
249- foreach ($ numbers as $ number ) {
250- if (empty ($ currentRange ) || current ($ currentRange ) === $ number -1 ) {
251- $ currentRange [] = $ number ;
252- end ($ currentRange );
253- } else {
254- $ lastItem = current ($ currentRange );
255-
256- if (count ($ currentRange ) === 1 ) {
257- $ ranges [] = $ lastItem ;
258- } else {
259- $ firstItem = reset ($ currentRange );
260- $ ranges [] = $ firstItem . '- ' . $ lastItem ;
261- }
262-
263- $ currentRange = [];
264- }
265- }
266-
267- $ lastItem = current ($ currentRange );
268- if (count ($ currentRange ) === 1 ) {
269- $ ranges [] = $ lastItem ;
270- } else {
271- $ firstItem = reset ($ currentRange );
272- $ ranges [] = $ firstItem . '- ' . $ lastItem ;
273- }
274-
275- return $ ranges ;
276- }
277-
278189}
0 commit comments