66use Symfony \Component \Console \Input \InputArgument ;
77use Symfony \Component \Console \Input \InputOption ;
88use Illuminate \Support \Facades \Log ;
9+ use Symfony \Component \Console \Helper \ProgressIndicator ;
910
1011ini_set ('max_execution_time ' , env ('IMPORT_TIME_LIMIT ' , 600 )); //600 seconds = 10 minutes
1112ini_set ('memory_limit ' , env ('IMPORT_MEMORY_LIMIT ' , '500M ' ));
@@ -29,6 +30,11 @@ class ObjectImportCommand extends Command
2930 */
3031 protected $ description = 'Import Items from CSV ' ;
3132
33+ /**
34+ * The progress indicator instance.
35+ */
36+ protected ProgressIndicator $ progressIndicator ;
37+
3238 /**
3339 * Create a new command instance.
3440 *
@@ -39,15 +45,15 @@ public function __construct()
3945 parent ::__construct ();
4046 }
4147
42- private $ bar ;
43-
4448 /**
4549 * Execute the console command.
4650 *
4751 * @return mixed
4852 */
4953 public function handle ()
5054 {
55+ $ this ->progressIndicator = new ProgressIndicator ($ this ->output );
56+
5157 $ filename = $ this ->argument ('filename ' );
5258 $ class = title_case ($ this ->option ('item-type ' ));
5359 $ classString = "App \\Importer \\{$ class }Importer " ;
@@ -61,46 +67,25 @@ public function handle()
6167 // This $logFile/useFiles() bit is currently broken, so commenting it out for now
6268 // $logFile = $this->option('logfile');
6369 // Log::useFiles($logFile);
64- $ this ->comment ('======= Importing Items from ' .$ filename .' ========= ' );
65- $ importer ->import ();
70+ $ this ->progressIndicator ->start ('======= Importing Items from ' .$ filename .' ========= ' );
6671
67- $ this -> bar = null ;
72+ $ importer -> import () ;
6873
69- if (! empty ($ this ->errors )) {
70- $ this ->comment ('The following Errors were encountered. ' );
71- foreach ($ this ->errors as $ asset => $ error ) {
72- $ this ->comment ('Error: Item: ' .$ asset .' failed validation: ' .json_encode ($ error ));
73- }
74- } else {
75- $ this ->comment ('All Items imported successfully! ' );
76- }
77- $ this ->comment ('' );
74+ $ this ->progressIndicator ->finish ('Import finished. ' );
7875 }
7976
80- public function errorCallback ($ item , $ field , $ errorString )
77+ public function errorCallback ($ item , $ field , $ error )
8178 {
82- $ this ->errors [$ item ->name ][$ field ] = $ errorString ;
79+ $ this ->output ->write ("\x0D\x1B[2K " );
80+
81+ $ this ->warn ('Error: Item: ' .$ item ->name .' failed validation: ' .json_encode ($ error ));
8382 }
8483
85- public function progress ($ count )
84+ public function progress ($ importedItemsCount )
8685 {
87- if (! $ this ->bar ) {
88- $ this ->bar = $ this ->output ->createProgressBar ($ count );
89- }
90- static $ index = 0 ;
91- $ index ++;
92- if ($ index < $ count ) {
93- $ this ->bar ->advance ();
94- } else {
95- $ this ->bar ->finish ();
96- }
86+ $ this ->progressIndicator ->advance ();
9787 }
9888
99- // Tracks the current item for error messages
100- private $ updating ;
101- // An array of errors encountered while parsing
102- private $ errors ;
103-
10489 /**
10590 * Log a message to file, configurable by the --log-file parameter.
10691 * If a warning message is passed, we'll spit it to the console as well.
0 commit comments