/**
  * import a single file into a collection
  *
  * @param SplFileInfo     $file   file
  * @param InputInterface  $input  User input on console
  * @param OutputInterface $output Output of the command
  *
  * @return void
  */
 private function importResource(\SplFileInfo $file, InputInterface $input, OutputInterface $output)
 {
     $doc = $this->frontMatter->parse($file->getContents());
     $origDoc = $this->serializer->unserialize($doc->getContent());
     if (is_null($origDoc)) {
         $output->writeln("<error>Could not deserialize file <{$file}></error>");
     } else {
         $collectionName = $doc->getData()['collection'];
         $this->getClient($input)->selectCollection($this->databaseName, $collectionName)->save($origDoc);
         $output->writeln("<info>Imported <{$file}> to <{$collectionName}></info>");
     }
 }
Example #2
0
 /**
  * @param Finder          $finder      finder primmed with files to import
  * @param OutputInterface $output      output interfac
  * @param string          $host        host to import into
  * @param string          $rewriteHost string to replace with value from $rewriteTo during loading
  * @param string          $rewriteTo   string to replace value from $rewriteHost with during loading
  * @param boolean         $sync        send requests syncronously
  *
  * @return void
  *
  * @throws MissingTargetException
  */
 protected function importPaths(Finder $finder, OutputInterface $output, $host, $rewriteHost, $rewriteTo, $sync = false)
 {
     $promises = [];
     foreach ($finder as $file) {
         $doc = $this->frontMatter->parse($file->getContents());
         $output->writeln("<info>Loading data from {$file}</info>");
         if (!array_key_exists('target', $doc->getData())) {
             throw new MissingTargetException('Missing target in \'' . $file . '\'');
         }
         $targetUrl = sprintf('%s%s', $host, $doc->getData()['target']);
         $promises[] = $this->importResource($targetUrl, (string) $file, $output, $doc, $host, $rewriteHost, $rewriteTo, $sync);
     }
     try {
         Promise\unwrap($promises);
     } catch (\GuzzleHttp\Exception\ClientException $e) {
         // silently ignored since we already output an error when the promise fails
     }
 }
Example #3
0
 protected function parseAttributes()
 {
     // We set an empty array first to
     // avoid a loop when "parseAttributes"
     // is called in "getContent"
     $this->attributes = [];
     $frontMatter = new FrontMatter();
     $content = $this->getContent();
     if (substr($content, 0, 3) == "") {
         $content = substr($content, 3);
     }
     $document = $frontMatter->parse($content);
     $this->attributes = array_replace_recursive($this->attributes, $document->getData());
     $this->setContent($document->getContent());
 }
Example #4
0
 /**
  * @dataProvider getJson
  */
 public function testDumpToJson($string, $data, $content)
 {
     $frontMatter = new FrontMatter(new JsonProcessor());
     $document = new Document($content, $data);
     $this->assertEquals($string, $frontMatter->dump($document));
 }