/** * Reads csv records * * @param string $fileName * @param int $position * @param int $step * @return array * @throws \Exception */ public function readRecords($fileName, $position, $step) { $tempFileName = ''; if (file_exists($fileName)) { $tempFileName = $this->uploadPathProvider->getRealPath(md5(microtime() . '.csv')); file_put_contents($tempFileName, file_get_contents($fileName)); $fileName = $tempFileName; } $file = new \SplFileObject($fileName); $file->setCsvControl(";", '"'); $file->setFlags(\SplFileObject::READ_CSV); $columnNames = $this->getColumnNames($file); //moves the file pointer to a certain line // +1 to ignore the first line of the file $file->seek($position + 1); $readRows = []; $data = []; for ($i = 1; $i <= $step; $i++) { $row = $file->current(); if ($this->isInvalidRecord($row)) { break; } foreach ($columnNames as $key => $name) { $data[$name] = isset($row[$key]) ? $row[$key] : ''; } $readRows[] = $data; // Move the pointer to the next line $file->next(); } unlink($tempFileName); return $this->toUtf8($readRows); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->prepareImportInputValidation($input); $this->registerErrorHandler($output); $this->start($output, $this->profileEntity, $this->filePath, $this->format); $profilesMapper = array('articles', 'articlesImages'); $uploadPathProvider = new UploadPathProvider(Shopware()->DocPath()); //loops the unprocessed data $pathInfo = pathinfo($this->filePath); foreach ($profilesMapper as $profileName) { $tmpFile = $uploadPathProvider->getRealPath($pathInfo['filename'] . '-' . $profileName . '-tmp.csv'); if (file_exists($tmpFile)) { $outputFile = str_replace('-tmp', '-swag', $tmpFile); rename($tmpFile, $outputFile); /** @var Profile $profile */ $profile = $this->getPlugin()->getProfileFactory()->loadHiddenProfile($profileName); /** @var ProfileEntity $profileEntity */ $profileEntity = $profile->getEntity(); $this->start($output, $profileEntity, $outputFile, 'csv'); } } }