Skip to content

Commit

Permalink
Adding a license
Browse files Browse the repository at this point in the history
Adding method require_once_dir
and make some changes in limonade.php
  • Loading branch information
Fabrice Luraine committed Apr 8, 2009
1 parent cc07048 commit e9b8267
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 30 deletions.
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2009 Fabrice Luraine

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion examples/example01/index.php
@@ -1,6 +1,6 @@
<?php <?php


require_once dirname(dirname(__FILE__)).'/lib/limonade.php'; require_once dirname(dirname(dirname(__FILE__))).'/lib/limonade.php';


function configure() function configure()
{ {
Expand Down
45 changes: 34 additions & 11 deletions lib/limonade.php
Expand Up @@ -237,14 +237,18 @@ function run($env = null)
if(is_null($env)) $env = env(); if(is_null($env)) $env = env();


# Configure # Configure
option('public_dir', 'public/'); $root_dir = dirname(app_file());
option('views_dir', 'views/'); option('root_dir', $root_dir);
option('controllers_dir', 'controllers/'); option('limonade_dir', dirname(__FILE__).'/');
option('libs_dir', 'lib/'); option('public_dir', $root_dir.'/public/');
option('views_dir', $root_dir.'/views/');
option('controllers_dir', $root_dir.'/controllers/');
option('libs_dir', $root_dir.'/lib/');
option('env', ENV_PRODUCTION); option('env', ENV_PRODUCTION);
option('encoding', 'utf-8'); option('encoding', 'utf-8');


# loading libs # loading libs
require_once_dir(option('libs_dir'));


call_if_exists('configure'); call_if_exists('configure');


Expand Down Expand Up @@ -286,7 +290,7 @@ function error($name, $msg="", $debug_args = null)
{ {
function not_found($msg="") function not_found($msg="")
{ {
option('views_dir', dirname(__FILE__).'/limonade/views/'); option('views_dir', option('limonade_dir').'limonade/views/');
$msg = h($msg); $msg = h($msg);
return html("<h1>Page not found:</h1><p>{$msg}</p>", "default_layout.php"); return html("<h1>Page not found:</h1><p>{$msg}</p>", "default_layout.php");
} }
Expand Down Expand Up @@ -453,10 +457,10 @@ function url_for($params = null)
#TODO enhanced url_for (url rewriting or not...) #TODO enhanced url_for (url rewriting or not...)


$env = env(); $env = env();
$request_uri = $env['SERVER']['REQUEST_URI']; $request_uri = rtrim($env['SERVER']['REQUEST_URI'], '?');
$base_path = $env['SERVER']['SCRIPT_NAME']; $base_path = $env['SERVER']['SCRIPT_NAME'];


if(strpos($request_uri, '?') !== FALSE) $base_path .= "?"; $base_path = ereg_replace('index\.php$', '?', $base_path);


$paths = array(); $paths = array();
$params = func_get_args(); $params = func_get_args();
Expand Down Expand Up @@ -502,6 +506,21 @@ function define_unless_exists($name, $value)
if(!defined($anme)) define($name, $value); if(!defined($anme)) define($name, $value);
} }


/**
* Load php files with require_once in a given dir
*
* @param string $path Path in which are the file to load
* @param string $pattern a regexp pattern that filter files to load
* @return array paths of loaded files
*/
function require_once_dir($path, $pattern = "*.php")
{
if($path[strlen($path) - 1] != "/") $path .= "/";
$filenames = glob($path.$pattern);
foreach($filenames as $filename) require_once $filename;
return $filenames;
}

/** /**
* Returns application root file path * Returns application root file path
* *
Expand Down Expand Up @@ -756,8 +775,12 @@ function request_methods()
*/ */
function request_uri($env = null) function request_uri($env = null)
{ {
#TODO test request_uri static $uri = null;
if(is_null($env)) $env = env(); if(is_null($env))
{
if(!is_null($uri)) return $uri;
$env = env();
}


if(array_key_exists('url', $env['GET'])) if(array_key_exists('url', $env['GET']))
{ {
Expand Down Expand Up @@ -790,10 +813,10 @@ function request_uri($env = null)
} }
elseif(array_key_exists('REQUEST_URI', $env['SERVER']) && !empty($env['SERVER']['REQUEST_URI'])) elseif(array_key_exists('REQUEST_URI', $env['SERVER']) && !empty($env['SERVER']['REQUEST_URI']))
{ {
$request_uri = $env['SERVER']['REQUEST_URI']; $request_uri = rtrim($env['SERVER']['REQUEST_URI'], '?');
$base_path = $env['SERVER']['SCRIPT_NAME']; $base_path = $env['SERVER']['SCRIPT_NAME'];


if(strpos($request_uri, '?') !== FALSE) $base_path .= "?"; if($request_uri."index.php" == $base_path) $request_uri .= "index.php";
$uri = str_replace($base_path, '', $request_uri); $uri = str_replace($base_path, '', $request_uri);
} }
elseif($env['SERVER']['argc'] > 1 && trim($env['SERVER']['argv'][1], '/') != '') elseif($env['SERVER']['argc'] > 1 && trim($env['SERVER']['argv'][1], '/') != '')
Expand Down
6 changes: 6 additions & 0 deletions lib/limonade/assertions.php
Expand Up @@ -29,6 +29,12 @@ function assert_empty($value, $message = '<1> should be empty')
return assert('empty($value); //'.$message); return assert('empty($value); //'.$message);
} }


function assert_not_empty($value, $message = '<1> should not be empty')
{
tests_execute_before_assert();
return assert('!empty($value); //'.$message);
}

function assert_equal($expected, $value, $message = '<1> should be equal to <2>') function assert_equal($expected, $value, $message = '<1> should be equal to <2>')
{ {
tests_execute_before_assert(); tests_execute_before_assert();
Expand Down
3 changes: 3 additions & 0 deletions tests/data/lib0/a.php
@@ -0,0 +1,3 @@
<?php
define('TEST_LIB_A', true);
?>
3 changes: 3 additions & 0 deletions tests/data/lib0/b.php
@@ -0,0 +1,3 @@
<?php
define('TEST_LIB_B', true);
?>
3 changes: 3 additions & 0 deletions tests/data/lib0/c.php
@@ -0,0 +1,3 @@
<?php
define('TEST_LIB_C', true);
?>
38 changes: 32 additions & 6 deletions tests/main.php
Expand Up @@ -35,22 +35,22 @@ function test_main_params()
assert_equal(params('first'), 6); assert_equal(params('first'), 6);
assert_true(is_array(params())); assert_true(is_array(params()));
assert_equal(params('first', 12), 12); assert_equal(params('first', 12), 12);
assert_equal(count(params()), 1); assert_length_of(params(), 1);


params('my_array', 1, 2, 3, 4); params('my_array', 1, 2, 3, 4);
assert_true(is_array(params('my_array'))); assert_true(is_array(params('my_array')));
assert_equal(count(params('my_array')), 4); assert_length_of(params('my_array'), 4);


assert_true(is_array(params())); assert_true(is_array(params()));
assert_equal(count(params()), 2); assert_length_of(params(), 2);


params(array('zero','one')); params(array('zero','one'));
assert_equal(count(params()), 4); assert_length_of(params(), 4);
assert_equal(params(0), 'zero'); assert_equal(params(0), 'zero');
assert_equal(params(1), 'one'); assert_equal(params(1), 'one');


params(array(2 => 'two', 'first' => 'my one')); params(array(2 => 'two', 'first' => 'my one'));
assert_equal(count(params()), 5); assert_length_of(params(), 5);
assert_equal(params(2), 'two'); assert_equal(params(2), 'two');
assert_equal(params('first'), 'my one'); assert_equal(params('first'), 'my one');


Expand Down Expand Up @@ -89,7 +89,7 @@ function test_main_call_if_exists()
{ {
assert_empty(call_if_exists("unknown_function")); assert_empty(call_if_exists("unknown_function"));
assert_equal(call_if_exists("count", array(1,2,3)), 3); assert_equal(call_if_exists("count", array(1,2,3)), 3);
assert_equal(count(call_if_exists("array_merge", array(1,2,3), array(4,5,6))), 6); assert_length_of(call_if_exists("array_merge", array(1,2,3), array(4,5,6)), 6);
} }


function test_main_define_unless_exists() function test_main_define_unless_exists()
Expand All @@ -102,6 +102,32 @@ function test_main_define_unless_exists()
assert_equal(MY_SPECIAL_CONST, "special value"); assert_equal(MY_SPECIAL_CONST, "special value");
} }


function test_main_require_once_dir()
{
$root = dirname(dirname(__FILE__));

assert_empty(require_once_dir($root));
$files = require_once_dir($root, "*.mkd");
assert_length_of($files, 1);
assert_match('/README\.mkd$/', $files[0]);

$lib = $root.'/lib';
$limonade = $lib.'/limonade';

$files = require_once_dir($limonade);
assert_not_empty($files);

$tests_lib = $root.'/tests/data/lib0';
$libs = array('a', 'b', 'c');
foreach($libs as $lib) assert_false(defined('TEST_LIB_'.strtoupper($lib)));

$files = require_once_dir($tests_lib);
assert_not_empty($files);
assert_length_of($files, 3);

foreach($libs as $lib) assert_true(defined('TEST_LIB_'.strtoupper($lib)));
}



endtests(); endtests();
?> ?>
2 changes: 1 addition & 1 deletion tests/request.php
Expand Up @@ -11,7 +11,7 @@ function tests_before_each_test_in_request()
function test_request_methods() function test_request_methods()
{ {
$m = request_methods(); $m = request_methods();
assert_equal(count($m), 4); assert_length_of($m, 4);
} }


function test_request_method_is_allowed() function test_request_method_is_allowed()
Expand Down
22 changes: 11 additions & 11 deletions tests/router.php
Expand Up @@ -49,7 +49,7 @@ function test_router_build_route()
assert_equal ($r["names"][0], 0); assert_equal ($r["names"][0], 0);


preg_match($r["pattern"], "/test/foo////", $matches); preg_match($r["pattern"], "/test/foo////", $matches);
assert_equal(count($matches), 2); assert_length_of($matches, 2);
assert_equal($matches[1], "foo"); assert_equal($matches[1], "foo");


$r = route_build("GET","/test/*/two", 'get_index'); $r = route_build("GET","/test/*/two", 'get_index');
Expand All @@ -65,7 +65,7 @@ function test_router_build_route()
assert_equal ($r["names"][0], 0); assert_equal ($r["names"][0], 0);


preg_match($r["pattern"], "/test/foo/two/", $matches); preg_match($r["pattern"], "/test/foo/two/", $matches);
assert_equal(count($matches), 2); assert_length_of($matches, 2);
assert_equal($matches[1], "foo"); assert_equal($matches[1], "foo");


/* testing single asterisk routes with params names */ /* testing single asterisk routes with params names */
Expand All @@ -85,7 +85,7 @@ function test_router_build_route()
assert_equal ($r["names"][0], 0); assert_equal ($r["names"][0], 0);


preg_match($r["pattern"], "/test/foo", $matches); preg_match($r["pattern"], "/test/foo", $matches);
assert_equal(count($matches), 2); assert_length_of($matches, 2);
assert_equal($matches[1], "foo"); assert_equal($matches[1], "foo");


$r = route_build("GET","/test/**/two/", 'get_index'); $r = route_build("GET","/test/**/two/", 'get_index');
Expand All @@ -100,7 +100,7 @@ function test_router_build_route()
assert_no_match($r["pattern"], "/test/truc/one/two/three"); assert_no_match($r["pattern"], "/test/truc/one/two/three");


preg_match($r["pattern"], "/test/foo/bar/two", $matches); preg_match($r["pattern"], "/test/foo/bar/two", $matches);
assert_equal(count($matches), 2); assert_length_of($matches, 2);
assert_equal($matches[1], "foo/bar"); assert_equal($matches[1], "foo/bar");


/* testing named parameters routes */ /* testing named parameters routes */
Expand Down Expand Up @@ -144,19 +144,19 @@ function test_router_route()
assert_empty(route()); assert_empty(route());


$r = route("get", "/index", "my_func"); $r = route("get", "/index", "my_func");
assert_equal(count($r), 1); assert_length_of($r, 1);
assert_equal(count($r[0]), 5); assert_length_of($r[0], 5);
assert_equal($r[0]["method"], "GET"); assert_equal($r[0]["method"], "GET");
assert_equal($r[0]["pattern"], "#^/index(?:/*?)?$#i"); assert_equal($r[0]["pattern"], "#^/index(?:/*?)?$#i");
assert_empty($r[0]["names"]); assert_empty($r[0]["names"]);
assert_equal($r[0]["function"], "my_func"); assert_equal($r[0]["function"], "my_func");


$r = route("put", "/blog/:id", "my_update_func"); $r = route("put", "/blog/:id", "my_update_func");
assert_equal(count($r), 2); assert_length_of($r, 2);
assert_equal(count($r[1]), 5); assert_length_of($r[1], 5);
assert_equal($r[1]["method"], "PUT"); assert_equal($r[1]["method"], "PUT");
assert_match($r[1]["pattern"], "/blog/102"); assert_match($r[1]["pattern"], "/blog/102");
assert_equal(count($r[1]["names"]), 1); assert_length_of($r[1]["names"], 1);
assert_equal($r[1]["names"][0], "id"); assert_equal($r[1]["names"][0], "id");
assert_equal($r[1]["function"], "my_update_func"); assert_equal($r[1]["function"], "my_update_func");
} }
Expand All @@ -170,7 +170,7 @@ function test_router_find_route()
route( "put", "/update/:id", "my_update_func" ); route( "put", "/update/:id", "my_update_func" );
$routes = route( "delete", "/delete/:id", "my_delete_func" ); $routes = route( "delete", "/delete/:id", "my_delete_func" );


assert_equal(count($routes), 6); assert_length_of($routes, 6);


$r = route_find("GET", "/unkown"); $r = route_find("GET", "/unkown");
assert_false($r); assert_false($r);
Expand All @@ -191,7 +191,7 @@ function test_router_find_route()
route( "get", "/index/*", "my_index_func2" ); route( "get", "/index/*", "my_index_func2" );
$routes = route( "delete", "/delete/:id/:confirm", "my_delete_func2" ); $routes = route( "delete", "/delete/:id/:confirm", "my_delete_func2" );


assert_equal(count($routes), 8); assert_length_of($routes, 8);
$r = route_find("GET", "/index"); $r = route_find("GET", "/index");
assert_equal($r["function"], "my_index_func"); assert_equal($r["function"], "my_index_func");


Expand Down

0 comments on commit e9b8267

Please sign in to comment.