예제 #1
0
 /**
  * @return array
  *
  * @throws \Braincrafted\Json\JsonDecodeException
  */
 protected function getJson()
 {
     if ($this->data === null) {
         $this->data = Json::decode($this->json, true);
     }
     return $this->data;
 }
예제 #2
0
 /**
  * Loads data from a JSON file and returns data as a Data object.
  *
  * @param string $filename Filename of data file.
  *
  * @return Data Data object.
  */
 public function load($filename)
 {
     if (false === file_exists($filename)) {
         throw new DataLoadException(sprintf('Could not load "%s" because file does not exist.', $filename));
     }
     return new Data(Json::decode(file_get_contents($filename), true));
 }
예제 #3
0
 /**
  * @return array
  *
  * @throws \Braincrafted\Json\JsonDecodeException
  */
 protected function getJson()
 {
     if ($this->data === null) {
         $this->data = Json::decode(file_get_contents($this->fileName), true);
     }
     return $this->data;
 }
예제 #4
0
 /**
  * Produces a new message.
  *
  * @param string $type    The type of message
  * @param mixed  $message The message
  *
  * @return void
  *
  * @codeCoverageIgnore
  */
 public function produce($type, $message)
 {
     $fp = @stream_socket_client(sprintf('tcp://%s:%d', $this->hostname, $this->port), $errno, $errstr, 30);
     if (!$fp) {
         throw new \RuntimeException(sprintf('%s (%s)', $errstr, $errno));
     }
     fwrite($fp, Json::encode(array('type' => $type, 'message' => $message)));
     fclose($fp);
 }
예제 #5
0
 /**
  * {@inheritDoc}
  */
 public function consume($data)
 {
     $data = Json::decode(stripslashes($data), true);
     if (!isset($data['type'])) {
         throw new \InvalidArgumentException('Message can\'t be consumed because the type information is missing.');
     }
     if (!isset($data['message'])) {
         throw new \InvalidArgumentException('Message can\'t be consumed because the message is missing.');
     }
     if (isset($this->consumers[$data['type']])) {
         call_user_func($this->consumers[$data['type']], $data['message']);
     }
 }
예제 #6
0
 /**
  * @param mixed $item
  *
  * @return mixed
  */
 public function convert($item)
 {
     return Json::encode($item, $this->options);
 }
예제 #7
0
 /**
  * Finish the writer.
  */
 public function finish()
 {
     file_put_contents($this->filename, Json::encode($this->data));
 }
 /**
  * {@inheritDoc}
  *
  * @throws FileNotFoundException if the file does not exist.
  * @throws \RuntimeException if the file could not be opened.
  */
 public function generate()
 {
     if (false === file_exists($this->filename) || false === is_file($this->filename)) {
         throw new FileNotFoundException(sprintf('The file "%s" does not exist.', $this->filename));
     }
     $json = Json::decode(file_get_contents($this->filename), true);
     for ($i = 0; $i < count($json); $i++) {
         $json[$i] = $this->filterByKey($json[$i], function ($key, $value) {
             return null === $this->parameters || true === in_array($key, $this->parameters);
         });
     }
     return array_filter($json, function ($value) {
         return count($value) > 0;
     });
 }
예제 #9
0
파일: Watchman.php 프로젝트: cocur/watchman
 /**
  * @param Process $process
  *
  * @return array JSON-decoded output of the watchman result.
  *
  * @throws \RuntimeException when the watcher could not be created.
  */
 protected function runProcess(Process $process)
 {
     $process->run();
     if (!$process->isSuccessful()) {
         throw new \RuntimeException($process->getErrorOutput());
     }
     $output = Json::decode($process->getOutput(), true);
     if (!empty($output['error'])) {
         throw new \RuntimeException($output['error']);
     }
     return $output;
 }
예제 #10
0
 /**
  * @param mixed $item
  *
  * @return mixed
  */
 public function convert($item)
 {
     return Json::decode($item, $this->assoc, $this->depth, $this->options);
 }
예제 #11
0
 /**
  * Finish the writer.
  */
 public function finish()
 {
     $this->json = Json::encode($this->data);
 }