Exemple #1
0
 /**
  * Decompiles a route, replacing valid tokens with parameter values
  *
  * @static
  * @access private
  * @param string $route The route to decompile
  * @param array $params An associative array of param names => values
  * @param array $remainder The unused params
  * @return string The decompiled route
  */
 private static function decompile($route, $params, &$remainder = NULL)
 {
     $remainder = $params;
     if (preg_match_all(self::REGEX_TOKEN, $route, $matches)) {
         foreach ($matches[0] as $token) {
             $split_pos = strrpos($token, ':');
             if ($split_pos !== FALSE) {
                 $param = trim(substr($token, $split_pos + 1, -1));
                 $transform = trim(substr($token, 1, $split_pos - 1));
             } else {
                 $param = trim($token, '[ ]');
                 $transform = NULL;
             }
             if (!isset($params[$param])) {
                 throw new Flourish\ProgrammerException('Missing parameter %s in supplied parameters', $param);
             }
             $value = $params[$param];
             switch ($transform) {
                 case NULL:
                     break;
                 case 'uc':
                     $value = self::wordSeparatorToUndercore($value);
                     $value = App\Text::create($value)->camelize(TRUE);
                     break;
                 case 'lc':
                     $value = self::wordSeparatorToUndercore($value);
                     $value = App\Text::create($value)->camelize();
                     break;
                 case 'us':
                     $value = App\Text::create($value)->underscorize();
                     break;
                 case 'ws':
                     $value = App\Text::create($value)->underscorize();
                     $value = str_replace('_', self::$wordSeparator, $value);
                     break;
                 default:
                     throw new Flourish\ProgrammerException('Invalid decompilation transformation type %s', $transform);
             }
             $route = str_replace($token, $value, $route);
             unset($remainder[$param]);
         }
     }
     return $route;
 }
 public function addText(Request $request, $id)
 {
     $result = $request->all();
     $result['placeId'] = $id;
     Text::create($result);
     return redirect('places/' . $id);
 }
Exemple #3
0
 /**
  * Sends the response to the screen
  *
  * @access public
  * @param boolean $headers_only Whether or not we're only sending headers
  * @return integer The status of the request
  */
 public function send($headers_only = FALSE)
 {
     $protocol = explode($_SERVER['SERVER_PROTOCOL'], '/');
     $version = end($protocol);
     $aliases = array('1.0' => array(405 => 400, 406 => 400), '1.1' => array());
     //
     // If for some reason we have been provided a NULL view we should try to
     // see if we have a default body for the response type.  If we don't, let's
     // provide a legitimate no content response.
     //
     if ($this->view === NULL) {
         if (isset(self::$states[$this->status]['body'])) {
             $this->view = self::$states[$this->status]['body'];
             $this->view = App\Text::create($this->view)->compose();
         } else {
             $this->code = 204;
             $this->status = HTTP\NO_CONTENT;
         }
     }
     //
     // We want to let any renderers work their magic before doing anything else.  A
     // good renderer will do whatever it can to resolve the response to a string.
     // Otherwise whatever the response is will be casted as a (string) and may not do
     // what one expects.
     //
     // NOTE: This logic is kept separate from the if statement above, in the event
     // the default body for a response needs additional processing.
     //
     if ($this->view !== NULL) {
         if (isset(self::$renderFilters[$this->type])) {
             foreach (self::$renderFilters[$this->type] as $filter) {
                 if ($filter::filter($this)) {
                     break;
                 }
             }
         }
         if (is_object($this->view)) {
             $view_class = get_class($this->view);
             $class_key = strtolower($view_class);
             if (isset(self::$renderMethods[$class_key])) {
                 $method = self::$renderMethods[$class_key];
                 if (!is_callable([$this->view, $method])) {
                     throw new Flourish\ProgrammerException('Cannot render view with registered non-callable method %s()', $method);
                 }
             } elseif (is_callable([$this->view, '__toString'])) {
                 $method = '__toString';
             } else {
                 throw new Flourish\ProgrammerException('Cannot render object of class %s, no rendering method available', $view_class);
             }
             $this->view = $this->view->{$method}();
         }
     }
     $this->view = (string) $this->view;
     $this->code = isset($aliases[$version][$this->code]) ? $aliases[$version][$this->code] : $this->code;
     //
     // Now that our view is rendered to a string and we have our code looked up
     // we want to deal with caching and etag generation.
     //
     $etag = $this->request->getHeader('If-None-Match');
     $cache_control = $this->request->getHeader('Cache-Control');
     $cache_file = $this->cache($etag);
     if (!$cache_file) {
         $this->view = NULL;
         $this->status = 'Not Modified';
         $this->code = 304;
     } else {
         $this->setHeader('Etag', $etag);
         if (strpos($cache_control, 'no-store') !== FALSE) {
             $cache_file->delete();
         }
     }
     //
     // Output all of our headers.
     //
     // Apparently fastCGI explicitly does not like the standard header format, so
     // so we send different leading headers based on that.  The content type downward,
     // however, is exactly the same.
     //
     $headers = [!App\Core::checkSAPI('cgi-fcgi') ? sprintf('%s %d %s', $_SERVER['SERVER_PROTOCOL'], $this->code, $this->status) : sprintf('Status: %d %s', $this->code, $this->status)];
     if ($this->code != 204) {
         if (in_array($this->type, self::$textTypes)) {
             $headers[] = sprintf('Content-Type: %s; charset=utf-8', $this->type);
         } else {
             $headers[] = sprintf('Content-Type: %s', $this->type);
         }
     }
     if ($this->expireCache) {
         $headers[] = sprintf('Cache-Control: max-age=0, s-maxage=0, must-revalidate');
     } elseif ($this->aging) {
         $cc_parts = ['must-revalidate', 'max-age=' . $this->aging];
         if ($this->sharedAging) {
             $cc_parts[] = 's-maxage=' . $this->sharedAging;
         }
         if ($this->cacheVisibility) {
             $cc_parts[] = $this->cacheVisibility;
         }
         if ($this->noCache) {
             $cc_parts[] = 'no-cache';
         }
         $headers[] = sprintf('Cache-Control: %s', implode(', ', $cc_parts));
     }
     foreach ($this->headers as $header => $value) {
         if ($value !== NULL) {
             $headers[] = $header . ': ' . $value;
         }
     }
     if ($headers_only && App\Core::checkSAPI('cli')) {
         foreach ($headers as $header) {
             print $header . LB;
         }
     } else {
         foreach ($headers as $header) {
             header($header);
         }
     }
     if (!$headers_only) {
         print $this->view;
     }
     return $this->code;
 }