Example #1
0
 /**
  * Encode the given data in the format
  *
  * @param array   $data                resulting data that needs to
  *                                     be encoded in the given format
  * @param boolean $humanReadable       set to TRUE when restler
  *                                     is not running in production mode.
  *                                     Formatter has to make the encoded
  *                                     output more human readable
  *
  * @throws \Exception
  * @return string encoded string
  */
 public function encode($data, $humanReadable = false)
 {
     if (!is_readable(static::$viewPath)) {
         throw new \Exception('The views directory `' . self::$viewPath . '` should exist with read permission.');
     }
     static::$data['basePath'] = dirname($_SERVER['SCRIPT_NAME']);
     static::$data['baseUrl'] = $this->restler->getBaseUrl();
     static::$data['currentPath'] = $this->restler->url;
     try {
         $exception = $this->restler->exception;
         $success = is_null($exception);
         $error = $success ? null : $exception->getMessage();
         $data = array('response' => static::$convertResponseToArray ? Object::toArray($data) : $data, 'stages' => $this->restler->getEvents(), 'success' => $success, 'error' => $error);
         $info = $data['api'] = $this->restler->apiMethodInfo;
         $metadata = Util::nestedValue($this->restler, 'apiMethodInfo', 'metadata');
         $view = $success ? 'view' : 'errorView';
         $value = false;
         if (static::$parseViewMetadata && isset($metadata[$view])) {
             if (is_array($metadata[$view])) {
                 self::$view = $metadata[$view]['description'];
                 $value = Util::nestedValue($metadata[$view], 'properties', 'value');
             } else {
                 self::$view = $metadata[$view];
             }
         } elseif (!self::$view) {
             $file = static::$viewPath . '/' . $this->restler->url . '.' . static::getViewExtension();
             self::$view = static::$useSmartViews && is_readable($file) ? $this->restler->url : static::$errorView;
         }
         if (isset($metadata['param']) && (!$value || 0 === strpos($value, 'request'))) {
             $params = $metadata['param'];
             foreach ($params as $index => &$param) {
                 $index = intval($index);
                 if (is_numeric($index)) {
                     $param['value'] = $this->restler->apiMethodInfo->parameters[$index];
                 }
             }
             $data['request']['parameters'] = $params;
         }
         if ($value) {
             $data = Util::nestedValue($data, explode('.', $value));
         }
         $data += static::$data;
         if (false === ($i = strrpos(self::$view, '.'))) {
             $template = self::$template;
         } else {
             self::$template = $template = substr(self::$view, $i + 1);
             self::$view = substr(self::$view, 0, $i);
         }
         if (!static::$cacheDirectory) {
             static::$cacheDirectory = Defaults::$cacheDirectory . DIRECTORY_SEPARATOR . $template;
             if (!file_exists(static::$cacheDirectory)) {
                 if (!mkdir(static::$cacheDirectory, 0770, true)) {
                     throw new RestException(500, 'Unable to create cache directory `' . static::$cacheDirectory . '`');
                 }
             }
         }
         if (method_exists($class = get_called_class(), $template)) {
             if ($template == 'blade') {
                 $this->checkDependency('Illuminate\\View\\View');
             } elseif ($template == 'twig') {
                 $this->checkDependency('Twig_Environment');
             } elseif ($template == 'mustache' || $template == 'handlebar') {
                 $this->checkDependency('Mustache_Engine');
             }
             return call_user_func("{$class}::{$template}", $data, $humanReadable);
         }
         throw new RestException(500, "Unsupported template system `{$template}`");
     } catch (Exception $e) {
         static::$parseViewMetadata = false;
         $this->reset();
         throw $e;
     }
 }