/** * Method auto detect adapter based on mime type * * @param object $import * @return void */ private function autoDetectAdapter(Import $import) { // make sure we dont already have an adapter if ($this->adapter) { return; } // get path to data file $dataPath = $import->getDataPath(); // get the mime type of file $file = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($file, $dataPath); if ($mime == 'text/plain') { $mime = pathinfo($dataPath, PATHINFO_EXTENSION); } // anonymous function to see if we can use any $respondsTo = function ($class) use($mime) { return $class::accepts($mime); }; // set the adapter if we found one $responded = array_filter($this->adapters, $respondsTo); if ($adapter = array_shift($responded)) { $this->setAdapter(new $adapter()); } // do we still not have adapter if (!$this->adapter) { throw new \Exception(\Lang::txt('Content Import: No adapter found to count import data.')); } }
/** * Process Import data * * @param object $import Import record * @param array $callbacks Array of Callbacks * @param bool $dryRun Dry Run mode? * @return object */ public function process(Import $import, array $callbacks, $dryRun) { // create new xml reader $iterator = new Reader($import->getDataPath(), $this->key); // get the import params $options = new Parameter($import->get('params')); // get the mode $mode = $import->get('mode', 'UPDATE'); // loop through each item foreach ($iterator as $index => $record) { // make sure we have a record if ($record === null) { continue; } // do we have a post parse callback ? $record = $this->map($record, $callbacks['postparse'], $dryRun); // convert to objects $entry = $import->getRecord($record, $options->toArray(), $mode); // do we have a post map callback ? $entry = $this->map($entry, $callbacks['postmap'], $dryRun); // run check & store $entry->check()->store($dryRun); // do we have a post convert callback ? $entry = $this->map($entry, $callbacks['postconvert'], $dryRun); // add to data array array_push($this->data, $entry); // mark record processed $import->currentRun()->processed(1); } return $this->data; }
/** * Get a list of headers * * @param object $import * @return array */ public function headers(Import $import) { // create iterator $iterator = new Reader($import->getDataPath(), $this->key); $iterator->rewind(); $headers = array(); $row = $iterator->current(); foreach ($row as $key => $val) { $headers[] = $key; } // return count return $headers; }
/** * Get a list of headers * * @param object $import * @return array */ public function headers(Import $import) { // create iterator $iterator = new Reader($import->getDataPath(), $this->key); return $iterator->headers(); }