Пример #1
0
 /**
  * Writes data to a JSON file.
  *
  * If a $threshold is given, files will be enumerated with ordinal numbers,
  * for example if $path = "/path/to/file.json" is given, the data will be
  * written in files:
  *
  * - /path/to/file.00001.json
  * - /path/to/file.00002.json
  * - /path/to/file.00003.json
  * - etc.
  *
  * By default, the writer will output a JSON array if given $data has
  * integer keys, and a JSON object for string keys. This can be forced
  * by setting one of FLAG_AS_ARRAY or FLAG_AS_OBJECT in $flags.
  *
  * @param  string   $path      Path to the file to write.
  * @param  Iterator $data      An iterator or generator yielding the data.
  * @param  numeric  $threshold Max. file size in bytes (null for no limit).
  * @param  integer  $flags     Additional options, see FLAG_* constants.
  */
 public function write($path, Iterator $data, $threshold = null, $flags = 0)
 {
     $asObject = $this->isAsObject($data, $flags);
     $pretty = $flags & self::FLAG_PRETTY_PRINT;
     $jsonFlags = $pretty ? JSON_PRETTY_PRINT : 0;
     // When a threshold is given, enumerate files
     $ord = isset($threshold) ? 1 : null;
     $file = $this->open($path, $asObject, $pretty, $ord);
     while ($data->valid()) {
         if ($pretty) {
             $file->fwrite("\t");
         }
         if ($asObject) {
             $file->fwrite('"' . $data->key() . '": ');
         }
         $file->fwrite(Json::encode($data->current(), $jsonFlags));
         // Move to next item to see if next exists
         $data->next();
         $hasNext = $data->valid();
         // Check whether a rollover is necessary
         $shouldRollover = $this->shouldRollover($file, $threshold);
         // Write a comma separator if it's not the last item for this file
         if (!$shouldRollover && $hasNext) {
             $file->fwrite(",");
         }
         if ($pretty) {
             $file->fwrite("\n");
         }
         if ($shouldRollover && $hasNext) {
             $ord += 1;
             $file = $this->rollover($file, $asObject, $path, $pretty, $ord);
         }
     }
     $this->close($file, $asObject, $pretty);
 }
Пример #2
0
 /**
  * Returns core status info.
  *
  * @param string $core Name of the core to return the status for, or null
  *                     to return status for all cores.
  */
 public function status($core = null)
 {
     $query = ["action" => "STATUS", "wt" => "json"];
     if (isset($core)) {
         $query['core'] = $core;
     }
     $path = "admin/cores?" . http_build_query($query);
     $response = $this->get($path);
     $contents = $response->getBody()->getContents();
     return Json::decode($contents, true);
 }
Пример #3
0
 private function importFile(Client $client, $core, SplFileInfo $source, $output)
 {
     $output->writeln("Importing data from: <info>{$source}</info>");
     $fp = fopen($source, 'r');
     $path = "{$core}/update?" . http_build_query(['commit' => 'true', 'wt' => 'json']);
     $headers = ['Content-Type' => 'application/json'];
     $response = $client->post($path, $fp, $headers);
     $contents = $response->getBody()->getContents();
     $reply = Json::decode($contents);
     if ($reply->responseHeader->status != 0) {
         throw new \Exception("Solr returned an error.");
     }
     $time = $reply->responseHeader->QTime;
     $output->writeln("Time taken: <comment>{$time} ms</comment>\n");
 }
Пример #4
0
 /**
  * @expectedException Exception
  * @expectedExceptionMessage Failed writing JSON data to file
  */
 public function testDumpError()
 {
     @Json::dump("", __DIR__ . '/../var/does_not_exist/foo.json');
 }
Пример #5
0
 public function testOptimize()
 {
     $coreName = "xyz";
     $expected = "expected response";
     $headers = ['Content-Type' => 'application/json'];
     $response = m::mock(Response::class);
     $response->shouldReceive('getBody->getContents')->andReturn(Json::encode($expected));
     $client = m::mock(Client::class);
     $client->shouldReceive('post')->with("{$coreName}/update?optimize=true&wt=json", null, $headers)->once()->andReturn($response);
     $core = new Core($client, $coreName);
     $actual = $core->optimize();
     $this->assertSame($expected, $actual);
 }
Пример #6
0
 /**
  * Pings the server to check it's there.
  *
  * @return array Decoded response from the Solr server.
  */
 public function ping()
 {
     $path = implode('/', [$this->name, $this->pingHandler]);
     $path = "{$path}?wt=json";
     $response = $this->client->get($path);
     $contents = $response->getBody()->getContents();
     return Json::decode($contents, true);
 }