С версии: 1.0
Автор: Bernhard Schussek (bschussek@gmail.com)
 private function encode($jsonData)
 {
     $encoder = new JsonEncoder();
     $encoder->setPrettyPrinting(true);
     $encoder->setEscapeSlash(false);
     $encoder->setTerminateWithLineFeed(true);
     return $encoder->encode($jsonData);
 }
Пример #2
0
 /**
  * @param GuzzleClient $client
  * @param RequestInterface $request
  * @return ResponseInterface
  */
 public function handle(GuzzleClient $client, RequestInterface $request)
 {
     try {
         $response = $client->send(new HttpRequest($request->getMethod(), $request->getUri(), $request->getHeaders(), $this->jsonEncoder->encode($request->getBody())));
     } catch (\Exception $e) {
         $this->logger->debug(sprintf('Exception thrown during Guzzle request'), ['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'trace' => $e->getTraceAsString()]);
         $response = new Response(500);
         if ($e instanceof ClientException) {
             $response = new Response(401);
         }
     }
     return $request->getResponseFactory()->create($response);
 }
Пример #3
0
 private function encode($jsonData)
 {
     $encoder = new JsonEncoder();
     $encoder->setPrettyPrinting(true);
     $encoder->setEscapeSlash(false);
     $encoder->setTerminateWithLineFeed(true);
     $decoder = new JsonDecoder();
     // We can't use realpath(), which doesn't work inside PHARs.
     // However, we want to display nice paths if the file is not found.
     $schema = $decoder->decodeFile(Path::canonicalize(__DIR__ . '/../../res/schema/package-schema-1.0.json'));
     $configSchema = $schema->properties->config;
     return $encoder->encode($jsonData, $configSchema);
 }
Пример #4
0
 private function encode(stdClass $jsonData, $path)
 {
     try {
         return $this->jsonEncoder->encode($jsonData);
     } catch (EncodingFailedException $e) {
         throw new InvalidConfigException(sprintf('The configuration in %s could not be encoded: %s', $path, $e->getMessage()), 0, $e);
     }
 }
Пример #5
0
 /**
  * Test that the file name is present in the output.
  *
  * @expectedException \Webmozart\Json\EncodingFailedException
  * @expectedExceptionMessage JsonEncoderTest
  * @expectedExceptionCode 5
  */
 public function testEncodeFileFailsIfNonUtf8()
 {
     if (version_compare(PHP_VERSION, '5.5.0', '<')) {
         $this->markTestSkipped('PHP >= 5.5.0 only');
         return;
     }
     $this->encoder->encodeFile(file_get_contents($this->fixturesDir . '/win-1258.json'), $this->tempFile);
 }
 /**
  * @return string
  */
 public function __toString()
 {
     if (empty($this->data['repositories'])) {
         unset($this->data['repositories']);
     }
     if (empty($this->data['require'])) {
         unset($this->data['require']);
     }
     return $this->jsonEncoder->encode($this->data);
 }
Пример #7
0
 private function encodeFile($jsonData, $path)
 {
     if (!is_string($path) || !Path::isAbsolute($path)) {
         throw new IOException(sprintf('Cannot write "%s": Expected an absolute path.', $path));
     }
     if (is_dir($path)) {
         throw new IOException(sprintf('Cannot write %s: Is a directory.', $path));
     }
     $encoder = new JsonEncoder();
     $encoder->setPrettyPrinting(true);
     $encoder->setEscapeSlash(false);
     $encoder->setTerminateWithLineFeed(true);
     $decoder = new JsonDecoder();
     // We can't use realpath(), which doesn't work inside PHARs.
     // However, we want to display nice paths if the file is not found.
     $schema = $decoder->decodeFile(Path::canonicalize(__DIR__ . '/../../res/schema/package-schema-1.0.json'));
     $configSchema = $schema->properties->config;
     if (!is_dir($dir = Path::getDirectory($path))) {
         $filesystem = new Filesystem();
         $filesystem->mkdir($dir);
     }
     $encoder->encodeFile($jsonData, $path, $configSchema);
 }
Пример #8
0
 /**
  * Writes the JSON file.
  */
 protected function flush()
 {
     // The root node always exists
     if (!isset($this->json['/'])) {
         $this->json['/'] = null;
     }
     // Always save in reverse order
     krsort($this->json);
     // Comply to schema
     $json = (object) $this->json;
     if (isset($json->{'_order'})) {
         $order = $json->{'_order'};
         foreach ($order as $path => $entries) {
             foreach ($entries as $key => $entry) {
                 $order[$path][$key] = (object) $entry;
             }
         }
         $json->{'_order'} = (object) $order;
     }
     $this->encoder->encodeFile($json, $this->path, $this->schemaPath);
 }
Пример #9
0
 /**
  * Returns the JSON encoder.
  *
  * @return JsonEncoder The JSON encoder.
  */
 public function getJsonEncoder()
 {
     if (!$this->jsonEncoder) {
         $this->jsonEncoder = new JsonEncoder();
         $this->jsonEncoder->setPrettyPrinting(true);
         $this->jsonEncoder->setEscapeSlash(false);
         $this->jsonEncoder->setTerminateWithLineFeed(true);
     }
     return $this->jsonEncoder;
 }
Пример #10
0
 /**
  * Writes the JSON file.
  */
 private function flush()
 {
     $this->encoder->encodeFile($this->json, $this->path);
 }
Пример #11
0
 private function encode(stdClass $jsonData, $path = null)
 {
     $encoder = new JsonEncoder();
     $encoder->setPrettyPrinting(true);
     $encoder->setEscapeSlash(false);
     $encoder->setTerminateWithLineFeed(true);
     $this->validate($jsonData, $path);
     return $encoder->encode($jsonData);
 }
Пример #12
0
 private function save($data)
 {
     try {
         $this->encoder->encodeFile($data, $this->path);
     } catch (EncodingFailedException $e) {
         if (JSON_ERROR_UTF8 === $e->getCode()) {
             throw UnsupportedValueException::forType('binary', $this);
         }
         throw new WriteException($e->getMessage(), 0, $e);
     } catch (IOException $e) {
         throw new WriteException($e->getMessage(), 0, $e);
     }
 }