Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:sofadesign/limonade
Browse files Browse the repository at this point in the history
Conflicts:
	TODO
  • Loading branch information
Fabrice Luraine committed Jul 9, 2010
2 parents 422e920 + 5b276ab commit d619bc2
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
.DS_STORE
.DS_STORE
.tmproj
tests/config/config.php
28 changes: 28 additions & 0 deletions README.mkd
Expand Up @@ -295,6 +295,8 @@ Output is temporized so that it can easily handle large files.

## Before and after request ##

### Before ###

You can define a `before` function that will be executed before each request. This is very useful to define a default layout or passing common variables to the templates

function before($route)
Expand All @@ -303,6 +305,7 @@ You can define a `before` function that will be executed before each request. Th
set('site_title', 'My Website');
}


The current matching route is also passed to the before function, so you can test it. It's an array as returned by the internal `route_find` function, with those values:

* `method` (HTTP method)
Expand All @@ -312,6 +315,8 @@ The current matching route is also passed to the before function, so you can tes
* `options` (route options)
* `params` (current params)

### After ###

An `after` output filter is also available. It's executed after each request and can apply a transformation to the output (except for `render_file` outputs which are sent directly to the output buffer).

function after($output, $route){
Expand All @@ -325,6 +330,29 @@ An `after` output filter is also available. It's executed after each request and

The current executed route is also available for `after` function.

### Autorender ###

You can define your own `autorender` function to make automatic rendering depending on current matching route. It will be executed if your controller returns a null output.

dispatch('/', 'hello');
function hello()
{
# process some stuff...
set('name', 'Bob');
# but don't return anything
# ( like if you were ending this function with return null; )
}

function autorender($route)
{
$view = $route['function'] . ".html.php";
return html($view);
}

In this example, when url `/` is called, `hello()` is executed and then `autorender()` renders the matching `hello.html.php` view.


## Configuration ##

You can define a `configure` that will be executed when application is launched (at the begining of the `run` execution ).
Expand Down
1 change: 1 addition & 0 deletions TODO
@@ -1,6 +1,7 @@
# TODO #

- add a flash_keep() function like in rails (http://guides.rubyonrails.org/action_controller_overview.html#the-flash)
- Add instructions about running tests
- in route() functions, replace "function" name with "callback" name (example: in route_build() returned array). Enhanced use of callback pseudo-type instead of functions names.
- adding abstracts (for handling enhanced callback controllers)
- route_callback_[controller_]exists()
Expand Down
5 changes: 3 additions & 2 deletions lib/limonade.php
Expand Up @@ -329,7 +329,6 @@ function run($env = null)
option('session', LIM_SESSION_NAME); // true, false or the name of your session
option('encoding', 'utf-8');
option('gzip', false);
option('autorender', false);
option('x-sendfile', 0); // 0: disabled,
// X-SENDFILE: for Apache and Lighttpd v. >= 1.5,
// X-LIGHTTPD-SEND-FILE: for Apache and Lighttpd v. < 1.5
Expand Down Expand Up @@ -377,6 +376,8 @@ function route_missing($request_method, $request_uri)
}
}

call_if_exists('initialize');

# 6. Check request
if($rm = request_method())
{
Expand Down Expand Up @@ -407,7 +408,7 @@ function autoload_controller($callback)

# 6.4 Call matching controller function and output result
$output = call_user_func_array($route['function'], array_values($route['params']));
if(is_null($output) && option('autorender')) $output = call_if_exists('autorender', $route);
if(is_null($output)) $output = call_if_exists('autorender', $route);
echo after(error_notices_render() . $output, $route);
}
else halt(SERVER_ERROR, "Routing error: undefined function '{$route['function']}'", $route);
Expand Down
2 changes: 1 addition & 1 deletion lib/limonade/abstract.php
Expand Up @@ -149,7 +149,7 @@ function before_render($content_or_func, $layout, $locals, $view_path)


/**
* Called only if option('autorender') is enabled and rendering $output is_null,
* Called only if rendering $output is_null,
* like in a controller with no return statement.
*
* @abstract this function might be defined by user
Expand Down
93 changes: 92 additions & 1 deletion tests/all.php
Expand Up @@ -6,7 +6,98 @@

if(!defined('TESTS_DOC_ROOT'))
{
$doc_root = "http://localhost/limonade-php.net/code/tests/apps/";
# 1. CONFIG file is required
$config_file = dirname(__FILE__).'/config/config.php';
if(file_exists($config_file))
{
include $config_file;
$doc_root = $config['limonade_base_url']."tests/apps/";
}
else
{
echo <<<OUTPUT
ERROR: MISSING CONFIG FILE FOR TESTS
====================================
In order to run test, you must have a valid tests/config/config.php file.
Please copy tests/config/config.php.dist into tests/config/config.php and
set required values.
The \$config['limonade_base_url'] is required to run functional tests.
NOTE: the Limonade source code must be somewhere in your HTTP server public
folder in order to call testing limonade apps.
---
OUTPUT;
exit;
}

# 2. HTTP+CURL requirements
if(function_exists('curl_version'))
{
$url = $doc_root.'index.php';
$response = test_request($url, 'GET');
if($response)
{
$v = phpversion();
$curl_v = curl_version();
$cv = $curl_v['version'];
if($response == $v)
{

echo <<<OUTPUT
==== RUNNING LIMONADE TESTS [PHP $v — cURL $cv] =====
OUTPUT;
} else {
echo <<<OUTPUT
ERROR: Wrong response to HTTP request test
==========================================
Requesting $url
must return '$v' but returns this response:
$response
---
Your \$config['limonade_base_url'] might be wrong or maybe it's your HTTP
server configuration and/or php installation.
Please fix it in order to run tests.
---
OUTPUT;
exit;
}
}
else
{
exit;
}

}
else
{
echo <<<OUTPUT
ERROR: cURL Library is required
===============================
Please install PHP cURL library in order to run Limonade tests.
---
OUTPUT;
}


define('TESTS_DOC_ROOT', $doc_root);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/apps/02-outputs.php
Expand Up @@ -26,4 +26,14 @@ function jpeg_file()
return render_file(dirname(dirname(__FILE__)).'/data/deer.jpg');
}

dispatch('/autorender', 'empty_controller');
function empty_controller()
{

}

function autorender($route){
return "AUTORENDERED OUTPUT for ".$route['function'];
}

run();
1 change: 1 addition & 0 deletions tests/apps/index.php
@@ -0,0 +1 @@
<?php echo phpversion(); ?>
2 changes: 1 addition & 1 deletion tests/apps/views/hello_world.html.php
@@ -1,2 +1,2 @@
<p>Hello World</p>
<?if(isset($lorem)):?><p><?=$lorem?></p><?endif;?>
<?php if(isset($lorem)):?><p><?php echo $lorem; ?></p><?php endif; ?>
2 changes: 1 addition & 1 deletion tests/apps/views/hello_world_filtered.html.php
@@ -1,2 +1,2 @@
<p>Hello World Filtered</p>
<?if(isset($lorem)):?><p><?=$lorem?></p><?endif;?>
<?php if(isset($lorem)):?><p><?php echo $lorem ?></p><?php endif; ?>
2 changes: 1 addition & 1 deletion tests/apps/views/layouts/default.html.php
Expand Up @@ -3,6 +3,6 @@
<title>Page title</title>
</head>
<body>
<?=$content?>
<?php echo $content?>
</body>
</html>
4 changes: 4 additions & 0 deletions tests/config/config.php.dist
@@ -0,0 +1,4 @@
<?php
$config = array();
$config['limonade_base_url'] = 'http://localhost/limonade/';
?>
15 changes: 10 additions & 5 deletions tests/output.php
Expand Up @@ -9,7 +9,6 @@ function before_each_test_in_output()
{
env(null);
option('encoding', 'utf-8');

}

function test_output_layout()
Expand Down Expand Up @@ -127,6 +126,12 @@ function before_render($content_or_func, $layout, $locals, $view_path)

}

function test_output_autorender()
{
$response = test_request(TESTS_DOC_ROOT.'02-outputs.php/autorender', 'GET');
assert_equal($response, 'AUTORENDERED OUTPUT for empty_controller');
}

end_test_case();


Expand All @@ -138,12 +143,12 @@ function _test_output_html_my_layout($vars){ extract($vars);?>
<title>Page title</title>
</head>
<body>
<?=$content?>
<?php echo $content ?>
</body>
</html>
<?}
<?php }

function _test_output_html_hello_world($vars){ extract($vars);?>
<p>Hello World</p>
<?if(isset($lorem)):?><p><?=$lorem?></p><?endif;?>
<?}
<?php if(isset($lorem)): ?><p><?php echo $lorem?></p><?php endif;?>
<?php }

0 comments on commit d619bc2

Please sign in to comment.