Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding a license

Adding method require_once_dir
and make some changes in limonade.php
  • Loading branch information...
commit e9b8267e1ef2cd082215605953aaf97bc33897de 1 parent cc07048
Fabrice Luraine authored
22 LICENSE
View
@@ -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  examples/example01/index.php
View
@@ -1,6 +1,6 @@
<?php
-require_once dirname(dirname(__FILE__)).'/lib/limonade.php';
+require_once dirname(dirname(dirname(__FILE__))).'/lib/limonade.php';
function configure()
{
45 lib/limonade.php
View
@@ -237,14 +237,18 @@ function run($env = null)
if(is_null($env)) $env = env();
# Configure
- option('public_dir', 'public/');
- option('views_dir', 'views/');
- option('controllers_dir', 'controllers/');
- option('libs_dir', 'lib/');
+ $root_dir = dirname(app_file());
+ option('root_dir', $root_dir);
+ option('limonade_dir', dirname(__FILE__).'/');
+ 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('encoding', 'utf-8');
# loading libs
+ require_once_dir(option('libs_dir'));
call_if_exists('configure');
@@ -286,7 +290,7 @@ function error($name, $msg="", $debug_args = null)
{
function not_found($msg="")
{
- option('views_dir', dirname(__FILE__).'/limonade/views/');
+ option('views_dir', option('limonade_dir').'limonade/views/');
$msg = h($msg);
return html("<h1>Page not found:</h1><p>{$msg}</p>", "default_layout.php");
}
@@ -453,10 +457,10 @@ function url_for($params = null)
#TODO enhanced url_for (url rewriting or not...)
$env = env();
- $request_uri = $env['SERVER']['REQUEST_URI'];
+ $request_uri = rtrim($env['SERVER']['REQUEST_URI'], '?');
$base_path = $env['SERVER']['SCRIPT_NAME'];
- if(strpos($request_uri, '?') !== FALSE) $base_path .= "?";
+ $base_path = ereg_replace('index\.php$', '?', $base_path);
$paths = array();
$params = func_get_args();
@@ -503,6 +507,21 @@ function define_unless_exists($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
*
* @return void
@@ -756,8 +775,12 @@ function request_methods()
*/
function request_uri($env = null)
{
- #TODO test request_uri
- if(is_null($env)) $env = env();
+ static $uri = null;
+ if(is_null($env))
+ {
+ if(!is_null($uri)) return $uri;
+ $env = env();
+ }
if(array_key_exists('url', $env['GET']))
{
@@ -790,10 +813,10 @@ function request_uri($env = null)
}
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'];
- 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);
}
elseif($env['SERVER']['argc'] > 1 && trim($env['SERVER']['argv'][1], '/') != '')
6 lib/limonade/assertions.php
View
@@ -29,6 +29,12 @@ function assert_empty($value, $message = '<1> should be empty')
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>')
{
tests_execute_before_assert();
3  tests/data/lib0/a.php
View
@@ -0,0 +1,3 @@
+<?php
+define('TEST_LIB_A', true);
+?>
3  tests/data/lib0/b.php
View
@@ -0,0 +1,3 @@
+<?php
+define('TEST_LIB_B', true);
+?>
3  tests/data/lib0/c.php
View
@@ -0,0 +1,3 @@
+<?php
+define('TEST_LIB_C', true);
+?>
38 tests/main.php
View
@@ -35,22 +35,22 @@ function test_main_params()
assert_equal(params('first'), 6);
assert_true(is_array(params()));
assert_equal(params('first', 12), 12);
- assert_equal(count(params()), 1);
+ assert_length_of(params(), 1);
params('my_array', 1, 2, 3, 4);
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_equal(count(params()), 2);
+ assert_length_of(params(), 2);
params(array('zero','one'));
- assert_equal(count(params()), 4);
+ assert_length_of(params(), 4);
assert_equal(params(0), 'zero');
assert_equal(params(1), '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('first'), 'my one');
@@ -89,7 +89,7 @@ function test_main_call_if_exists()
{
assert_empty(call_if_exists("unknown_function"));
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()
@@ -102,6 +102,32 @@ function test_main_define_unless_exists()
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();
?>
2  tests/request.php
View
@@ -11,7 +11,7 @@ function tests_before_each_test_in_request()
function test_request_methods()
{
$m = request_methods();
- assert_equal(count($m), 4);
+ assert_length_of($m, 4);
}
function test_request_method_is_allowed()
22 tests/router.php
View
@@ -49,7 +49,7 @@ function test_router_build_route()
assert_equal ($r["names"][0], 0);
preg_match($r["pattern"], "/test/foo////", $matches);
- assert_equal(count($matches), 2);
+ assert_length_of($matches, 2);
assert_equal($matches[1], "foo");
$r = route_build("GET","/test/*/two", 'get_index');
@@ -65,7 +65,7 @@ function test_router_build_route()
assert_equal ($r["names"][0], 0);
preg_match($r["pattern"], "/test/foo/two/", $matches);
- assert_equal(count($matches), 2);
+ assert_length_of($matches, 2);
assert_equal($matches[1], "foo");
/* testing single asterisk routes with params names */
@@ -85,7 +85,7 @@ function test_router_build_route()
assert_equal ($r["names"][0], 0);
preg_match($r["pattern"], "/test/foo", $matches);
- assert_equal(count($matches), 2);
+ assert_length_of($matches, 2);
assert_equal($matches[1], "foo");
$r = route_build("GET","/test/**/two/", 'get_index');
@@ -100,7 +100,7 @@ function test_router_build_route()
assert_no_match($r["pattern"], "/test/truc/one/two/three");
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");
/* testing named parameters routes */
@@ -144,19 +144,19 @@ function test_router_route()
assert_empty(route());
$r = route("get", "/index", "my_func");
- assert_equal(count($r), 1);
- assert_equal(count($r[0]), 5);
+ assert_length_of($r, 1);
+ assert_length_of($r[0], 5);
assert_equal($r[0]["method"], "GET");
assert_equal($r[0]["pattern"], "#^/index(?:/*?)?$#i");
assert_empty($r[0]["names"]);
assert_equal($r[0]["function"], "my_func");
$r = route("put", "/blog/:id", "my_update_func");
- assert_equal(count($r), 2);
- assert_equal(count($r[1]), 5);
+ assert_length_of($r, 2);
+ assert_length_of($r[1], 5);
assert_equal($r[1]["method"], "PUT");
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]["function"], "my_update_func");
}
@@ -170,7 +170,7 @@ function test_router_find_route()
route( "put", "/update/:id", "my_update_func" );
$routes = route( "delete", "/delete/:id", "my_delete_func" );
- assert_equal(count($routes), 6);
+ assert_length_of($routes, 6);
$r = route_find("GET", "/unkown");
assert_false($r);
@@ -191,7 +191,7 @@ function test_router_find_route()
route( "get", "/index/*", "my_index_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");
assert_equal($r["function"], "my_index_func");
Please sign in to comment.
Something went wrong with that request. Please try again.