Permalink
Browse files

Add `inspect_header`to allow inspection of headers beeing sent.

 - New function _lim_header that takes care of sending the header
 - Replace all direct calls of header() with _lim_header()
 - Added Example to abstract.php
 - Added Example Usage to the Documentation.
  • Loading branch information...
1 parent a3b34ee commit 039e69956689c9851ca854271ce09eadc3ec7c02 @CBeerta CBeerta committed May 29, 2011
Showing with 60 additions and 12 deletions.
  1. +19 −0 README.mkd
  2. +27 −12 lib/limonade.php
  3. +14 −0 lib/limonade/abstract.php
View
@@ -525,6 +525,25 @@ If you define a `before_exit`, it is called at the begining of the stop/exit pro
# by default it is true
}
+### Inspecting Headers that are being sent ###
+
+You can define a `inspect_header` function that will be called before Limonade emits a header() call. This way you can add additional Headers:
+
+ dispatch('/style.css', 'css');
+ function css()
+ {
+ # Generate css file and output
+ return css('style.css.php');
+ }
+
+ function inspect_header($header)
+ {
+ if (strpos($header, 'text/css') !== false)
+ {
+ # intercept text/css content-type and add caching to the headers
+ header("Cache-Control: max-age=600, public");
+ }
+ }
## Configuration ##
View
@@ -186,6 +186,7 @@ function render_limonade_file()
# - function before_exit(){}
# - function before_render($content_or_func, $layout, $locals, $view_path){}
# - function autorender($route){}
+# - function inspect_header($header){}
#
# See abstract.php for more details.
@@ -367,7 +368,7 @@ function run($env = null)
}
# 2.2 Set X-Limonade header
- if($signature = option('signature')) header("X-Limonade: $signature");
+ if($signature = option('signature')) _lim_header("X-Limonade: $signature");
# 3. Loading libs
require_once_dir(option('lib_dir'));
@@ -1462,7 +1463,7 @@ function partial($content_or_func, $locals = array())
*/
function html($content_or_func, $layout = '', $locals = array())
{
- if(!headers_sent()) header('Content-Type: text/html; charset='.strtolower(option('encoding')));
+ _lim_header('Content-Type: text/html; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
@@ -1490,7 +1491,7 @@ function layout($function_or_file = null)
*/
function xml($data)
{
- if(!headers_sent()) header('Content-Type: text/xml; charset='.strtolower(option('encoding')));
+ _lim_header('Content-Type: text/xml; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
@@ -1505,7 +1506,7 @@ function xml($data)
*/
function css($content_or_func, $layout = '', $locals = array())
{
- if(!headers_sent()) header('Content-Type: text/css; charset='.strtolower(option('encoding')));
+ _lim_header('Content-Type: text/css; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
@@ -1520,7 +1521,7 @@ function css($content_or_func, $layout = '', $locals = array())
*/
function js($content_or_func, $layout = '', $locals = array())
{
- if(!headers_sent()) header('Content-Type: application/javascript; charset='.strtolower(option('encoding')));
+ _lim_header('Content-Type: application/javascript; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
@@ -1535,7 +1536,7 @@ function js($content_or_func, $layout = '', $locals = array())
*/
function txt($content_or_func, $layout = '', $locals = array())
{
- if(!headers_sent()) header('Content-Type: text/plain; charset='.strtolower(option('encoding')));
+ _lim_header('Content-Type: text/plain; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
@@ -1549,7 +1550,7 @@ function txt($content_or_func, $layout = '', $locals = array())
*/
function json($data, $json_option = 0)
{
- if(!headers_sent()) header('Content-Type: application/json; charset='.strtolower(option('encoding')));
+ _lim_header('Content-Type: application/json; charset='.strtolower(option('encoding')));
return version_compare(PHP_VERSION, '5.3.0', '>=') ? json_encode($data, $json_option) : json_encode($data);
}
@@ -1579,12 +1580,26 @@ function render_file($filename, $return = false)
$content_type = mime_type(file_extension($filename));
$header = 'Content-type: '.$content_type;
if(file_is_text($filename)) $header .= '; charset='.strtolower(option('encoding'));
- if(!headers_sent()) header($header);
+ _lim_header($header);
return file_read($filename, $return);
}
else halt(NOT_FOUND, "unknown filename $filename");
}
+/**
+ * Call inspect_header() if it exists, then send headers
+ *
+ * @param string $header
+ * @return void
+ */
+function _lim_header($header = null, $replace = true, $code = false)
+{
+ if(!headers_sent())
+ {
+ call_if_exists('inspect_header', $header);
+ header($header, $replace, $code);
+ }
+}
@@ -2011,7 +2026,7 @@ function status($code = 500)
if(!headers_sent())
{
$str = http_response_status_code($code);
- header($str);
+ _lim_header($str);
}
}
@@ -2057,10 +2072,10 @@ function redirect_to($params)
}
$n_params[] = $param;
}
- $uri = call_user_func_array('url_for', $n_params);
- $uri = htmlspecialchars_decode($uri, ENT_NOQUOTES);
+ $uri = call_user_func_array('url_for', $n_params);
+ $uri = htmlspecialchars_decode($uri, ENT_NOQUOTES);
stop_and_exit(false);
- header('Location: '.$uri, true, $status);
+ _lim_header('Location: '.$uri, true, $status);
exit;
}
}
View
@@ -177,3 +177,17 @@ function autorender($route)
# process output depending on $route
return $output;
}
+
+/**
+ * Called if a header is about to be sent
+ *
+ * @abstract this function might be defined by user
+ * @param string the headers that limonade will send
+ * @return void
+ */
+function inspect_header($header)
+{
+
+}
+
+

0 comments on commit 039e699

Please sign in to comment.