Browse Source

- Cake didn't work without mod_rewrite, fixed (/index.php, /libs/router.php).
- Controller accepts $params as a constructor parameter (for Larry).
- DBO::prepare() returned an array even when a string was passed to it, fixed.
- Flay and Folder tests fixed, DBO_Factory test disabled (no database config by default, so do database access to test).
-

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@238 3807eeeb-6ff5-0310-8944-8be069107fe0

pies 21 years ago
parent
commit
3f10d747b7

+ 42 - 42
config/database.php.default

@@ -1,42 +1,42 @@
-<?PHP
-//////////////////////////////////////////////////////////////////////////
-// + $Id$
-// +------------------------------------------------------------------+ //
-// + Cake <https://developers.nextco.com/cake/>                       + //
-// + Copyright: (c) 2005, Cake Authors/Developers                     + //
-// +------------------------------------------------------------------+ //
-// + Licensed under The MIT License                                   + //
-//////////////////////////////////////////////////////////////////////////
-
-/**
- * In this file you set up your database connection details.
- */
-
-/**
- * Database configuration class.
- * You can specify multiple configurations for production, development and testing.
- */
-class DATABASE_CONFIG {
-
-	function devel () {
-		return array(
-			'driver'   => 'mysql',
-			'host'     => 'localhost',
-			'login'    => 'www',
-			'password' => '',
-			'database' => 'project_name'
-		);
-	}
-
-	function test () {
-		return array(
-			'driver'   => 'mysql',
-			'host'     => 'localhost',
-			'login'    => 'www-test',
-			'password' => '',
-			'database' => 'project_name-test'
-		);
-	}
-}
-
-?>
+<?PHP
+//////////////////////////////////////////////////////////////////////////
+// + $Id$
+// +------------------------------------------------------------------+ //
+// + Cake <https://developers.nextco.com/cake/>                       + //
+// + Copyright: (c) 2005, Cake Authors/Developers                     + //
+// +------------------------------------------------------------------+ //
+// + Licensed under The MIT License                                   + //
+//////////////////////////////////////////////////////////////////////////
+
+/**
+ * In this file you set up your database connection details.
+ */
+
+/**
+ * Database configuration class.
+ * You can specify multiple configurations for production, development and testing.
+ */
+class DATABASE_CONFIG {
+
+	function devel () {
+		return array(
+			'driver'   => 'mysql',
+			'host'     => 'localhost',
+			'login'    => 'www',
+			'password' => '',
+			'database' => 'project_name'
+		);
+	}
+
+	function test () {
+		return array(
+			'driver'   => 'mysql',
+			'host'     => 'localhost',
+			'login'    => 'www-test',
+			'password' => '',
+			'database' => 'project_name-test'
+		);
+	}
+}
+
+?>

+ 41 - 3
index.php

@@ -40,10 +40,48 @@ define ('ROOT', dirname(__FILE__).DS);
  *  We need to redefine some constants and variables, so that Cake knows it is
  *  working without mod_rewrite.
  */
-define ('BASE_URL', $_SERVER['SCRIPT_NAME']);
+define ('BASE_URL', $_SERVER['SCRIPT_NAME'].'?url=');
 
-$_GET['url']  = ltrim($_SERVER['PATH_INFO'],'/');
+/**
+ * As mod_rewrite (or .htaccess files) is not working, we need to take care
+ * of what would normally be rewrited, i.e. the static files in /public
+ */
+if (empty($_GET['url']) || ($_GET['url'] == '/'))
+{
+	require (ROOT.'public/index.php');
+}
+else
+{
+	$elements = explode('/index.php?url=', $_SERVER['REQUEST_URI']);
+	$base = $elements[0].'/public';
+	$path = $elements[1];
+	
+	$filename = ROOT.'public'.str_replace('/', DS, $path);
+	$url = $base.$path;
 
-require (ROOT.'public/index.php');
+	if (file_exists($filename))
+	{
+		if (preg_match('/^.*\.([a-z]+)$/i', $filename, $ext))
+		{
+			switch ($ext[1])
+			{
+				case 'jpg':
+				case 'jpeg':
+					header('Content-type: image/jpeg');
+				break;
+				
+				case 'css':
+					header('Content-type: text/css');
+			}
+		}
+		
+		print (file_get_contents($filename));
+		die();
+	}
+	else 
+	{
+		require (ROOT.'public/index.php');
+	}
+}
 
 ?>

+ 15 - 10
libs/controller.php

@@ -102,8 +102,9 @@ class Controller extends Template {
   * Constructor. 
   *
   */
-	function __construct () {
-		global $DB;
+	function __construct ($params=null)
+	{
+		$this->params = $params;
 
 		$r = null;
 		if (!preg_match('/(.*)Controller/i', get_class($this), $r))
@@ -113,26 +114,30 @@ class Controller extends Template {
 		$this->viewpath = Inflector::underscore($r[1]);
 		
 		$model_class = Inflector::singularize($this->name);
-		if (($this->uses === false) && class_exists($model_class)) {
-			if (!$DB)
-				die("Controller::__construct() : ".$this->name." controller needs database access, exiting.");
-
+		
+		if (class_exists($model_class) && $this->db && ($this->uses === false)) 
+		{
 			$this->$model_class = new $model_class ();
 		}
-		elseif ($this->uses) {
+		elseif ($this->uses) 
+		{
 			if (!$DB)
 				die("Controller::__construct() : ".$this->name." controller needs database access, exiting.");
 
 			$uses = is_array($this->uses)? $this->uses: array($this->uses);
 
-			foreach ($uses as $model_name) {
+			foreach ($uses as $model_name) 
+			{
 				$model_class = ucfirst(strtolower($model_name));
 
-				if (class_exists($model_class)) {
+				if (class_exists($model_class)) 
+				{
 					$this->$model_name = new $model_class (false);
 				}
-				else
+				else 
+				{
 					die("Controller::__construct() : ".ucfirst($this->name)." requires missing model {$model_class}, exiting.");
+				}
 			}
 		}
 

+ 20 - 11
libs/dbo.php

@@ -276,15 +276,21 @@ class DBO extends Object {
  * @param mixed $data A value or an array of values to prepare.
  * @return mixed Prepared value or array of values.
  */
-	function prepare ($data) {
-		if (!is_array($data))
-			$data = array($data);
-		
-		$out = null;
-		foreach ($data as $key=>$item) {
-			$out[$key] = $this->prepareValue($item);
+	function prepare ($data) 
+	{
+		if (is_string($data))
+		{
+			return $this->prepareValue($data);
+		}
+		else 
+		{
+			$out = null;
+			foreach ($data as $key=>$item) 
+			{
+				$out[$key] = $this->prepareValue($item);
+			}
+			return $out;
 		}
-		return $out;
 	}
 
 /**
@@ -326,8 +332,8 @@ class DBO extends Object {
   * @param resource $res
   * @return array A single row of results
   */
-	function farr ($res=false) {
-		return $this->fetchRow($res? $res: $this->_result);
+	function farr ($res=false, $assoc=false) {
+		return $this->fetchRow($res? $res: $this->_result, $assoc);
 	}
 
 /**
@@ -350,7 +356,10 @@ class DBO extends Object {
 	function all ($sql) {
 		if($this->query($sql)) {
 			$out=array();
-			while($item = $this->farr()) $out[] = $item;
+			while ($item = $this->farr(null, true)) 
+			{
+				$out[] = $item;
+			}
 			return $out;
 		}
 		else {

+ 2 - 2
libs/dbo_mysql.php

@@ -95,8 +95,8 @@ class DBO_MySQL extends DBO {
   * @param unknown_type $res Resultset
   * @return array The fetched row as an array
   */
-	function fetchRow ($res) {
-		return mysql_fetch_array($res);
+	function fetchRow ($res, $assoc=false) {
+		return mysql_fetch_array($res, $assoc? MYSQL_ASSOC: MYSQL_BOTH);
 	}
 
 /**

+ 2 - 8
libs/dbo_sqlite.php

@@ -3,20 +3,14 @@
 // + $Id$
 // +------------------------------------------------------------------+ //
 // + Cake <https://developers.nextco.com/cake/>                       + //
-// + Copyright: (c) 2005 Cake Authors/Developers                      + //
-// +                                                                  + //
+// + Copyright: (c) 2005, Cake Authors/Developers                     + //
 // + Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com> + //
 // +            Larry E. Masters aka PhpNut <nut@phpnut.com>          + //
 // +            Kamil Dzielinski aka Brego <brego.dk@gmail.com>       + //
-// +                                                                  + //
 // +------------------------------------------------------------------+ //
 // + Licensed under The MIT License                                   + //
 // + Redistributions of files must retain the above copyright notice. + //
-// + You may not use this file except in compliance with the License. + //
-// +                                                                  + //
-// + You may obtain a copy of the License at:                         + //
-// + License page: http://www.opensource.org/licenses/mit-license.php + //
-// +------------------------------------------------------------------+ //
+// + See: http://www.opensource.org/licenses/mit-license.php          + //
 //////////////////////////////////////////////////////////////////////////
 
 /**

+ 13 - 8
libs/dispatcher.php

@@ -100,14 +100,18 @@ class Dispatcher extends Object {
 		/**
 		 * If _no_action_is set, check if the default action, index() exists. If it doesn't, die.
 		 */
-		if (empty($params['action']) && in_array('index', $ctrl_methods))
+		if (empty($params['action']))
 		{
-			$params['action'] = 'index';
+			if (in_array('index', $ctrl_methods))
+			{
+				$params['action'] = 'index';
+			}
+			else 
+			{
+				$this->errorNoAction($url);
+			}
 		}
-		else {
-			$this->errorNoAction($url);
-		}
-
+		
 		/**
 		 * Check if the specified action really exists. 
 		 */
@@ -116,7 +120,8 @@ class Dispatcher extends Object {
 			$this->errorUnknownAction($url, $ctrl_class, $params['action']);
 		}
 
-		$controller->params = $params;
+		$controller = new $ctrl_class ($params); 
+		$controller->base = $this->base; 
 		$controller->action = $params['action'];
 		$controller->data = empty($params['data'])? null: $params['data'];
 		$controller->passed_args = empty($params['pass'])? null: $params['pass'];
@@ -142,7 +147,7 @@ class Dispatcher extends Object {
 		// load routes config
 		$Route = new Router();
 		require CONFIGS.'routes.php';
-		$params = $Route->parse ('/'.$from_url);
+		$params = $Route->parse ($from_url);
 
 		// add submitted form data
 		$params['form'] = $_POST;

+ 0 - 1
libs/legacy.php

@@ -47,7 +47,6 @@ if (version_compare(phpversion(), '5.0') < 0) {
  * @package     PHP_Compat
  * @link        http://php.net/function.file_get_contents
  * @author      Aidan Lister <aidan@php.net>
- * @version     $Revision$
  * @internal    resource_context is not supported
  * @since       PHP 5
  * @require     PHP 4.0.0 (user_error)

+ 0 - 2
libs/model.php

@@ -518,8 +518,6 @@ class Model extends Object {
 		else
 			$f = array('*');
 
-		$conditions = $this->db->prepare($conditions);
-			
 		$joins = $whers = array();
 
 		foreach ($this->_oneToMany as $rule) {

+ 7 - 1
libs/neat_array.php

@@ -36,7 +36,13 @@ class NeatArray {
 	 * @access public
 	 * @uses NeatArray::value
 	 */
-	function findIn ($fieldName, $value) {
+	function findIn ($fieldName, $value) 
+	{
+		if (!is_array($this->value))
+		{
+			return false;
+		}
+			
 		$out = false;
 		foreach ($this->value as $k=>$v) {
 			if (isset($v[$fieldName]) && ($v[$fieldName] == $value)) {

+ 23 - 9
libs/router.php

@@ -111,11 +111,20 @@ class Router extends Object {
   * @param string $url URL to be parsed 
   * @return array 
   */
-	function parse ($url) {
+	function parse ($url) 
+	{
+		// An URL should start with a '/', mod_rewrite doesn't respect that, but no-mod_rewrite version does.
+		// Here's the fix.
+		if ($url && ('/' != $url[0]))
+		{
+			$url = '/'.$url;
+		}
+		
 		$out = array();
 		$r = null;
 
-		$default_route = array(
+		$default_route = array
+		(
 			'/:controller/:action/* (default)',
 			"#^(?:\/(?:([a-z0-9_\-]+)(?:\/([a-z0-9_\-]+)(?:\/(.*))?)?))[\/]*$#",
 			array('controller', 'action'),
@@ -124,10 +133,12 @@ class Router extends Object {
 
 		$this->routes[] = $default_route;
 
-		foreach ($this->routes as $route) {
+		foreach ($this->routes as $route) 
+		{
 			list($route, $regexp, $names, $defaults) = $route;
 
-			if (preg_match($regexp, $url, $r)) {
+			if (preg_match($regexp, $url, $r)) 
+			{
 				// $this->log($url.' matched '.$regexp, 'note');
 				// remove the first element, which is the url
 				array_shift($r);
@@ -138,8 +149,10 @@ class Router extends Object {
 
 				$ii = 0;
 
-				if (is_array($defaults)) {
-					foreach ($defaults as $name=>$value) {
+				if (is_array($defaults)) 
+				{
+					foreach ($defaults as $name=>$value) 
+					{
 						if (preg_match('#[a-z_\-]#i', $name))
 							$out[$name] = $value;
 						else
@@ -149,11 +162,13 @@ class Router extends Object {
 
 				foreach ($r as $found) {
 					// if $found is a named url element (i.e. ':action')
-					if (isset($names[$ii])) {
+					if (isset($names[$ii])) 
+					{
 						$out[$names[$ii]] = $found;
 					}
 					// unnamed elements go in as 'pass'
-					else {
+					else 
+					{
 						$pass = new NeatArray(explode('/', $found));
 						$pass->cleanup();
 						$out['pass'] = $pass->value;
@@ -163,7 +178,6 @@ class Router extends Object {
 				break;
 			}
 		}
-
 		return $out;
 	}
 }

+ 5 - 2
public/index.php

@@ -35,8 +35,11 @@ session_start();
 /**
   * Get Cake's root directory
   */
-define ('DS', DIRECTORY_SEPARATOR);
-define ('ROOT', dirname(dirname(__FILE__)).DS);
+if (!defined('DS'))
+	define ('DS', DIRECTORY_SEPARATOR);
+
+if (!defined('ROOT'))
+	define ('ROOT', dirname(dirname(__FILE__)).DS);
 
 /**
   * Directory layout and basic functions

+ 2 - 2
tests/libs/controller_test.php

@@ -181,8 +181,8 @@ class ControllerTest extends TestCase {
 	}
 
 	function testSelectTag () {
-		$result = $this->abc->selectTag('tofu', array('m'=>'male', 'f'=>'female'), array('class'=>'Outer'), array('class'=>'Inner', 'id'=>'FooID'));
-		$expected = '<select name="data[tofu]" class="Outer">'."\n".'<option value="" class="Inner" id="FooID"></option>'."\n".'<option value="m" class="Inner" id="FooID">male</option>'."\n".'<option value="f" class="Inner" id="FooID">female</option>'."\n".'</select>';
+		$result = $this->abc->selectTag('tofu', array('m'=>'male', 'f'=>'female'), 'f', array('class'=>'Outer'), array('class'=>'Inner', 'id'=>'FooID'));
+		$expected = '<select name="data[tofu]" class="Outer">'."\n".'<option value="" class="Inner" id="FooID"></option>'."\n".'<option value="m" class="Inner" id="FooID">male</option>'."\n".'<option value="f" class="Inner" id="FooID" selected="selected">female</option>'."\n".'</select>';
 		$this->assertEquals($result, $expected);
 
 		$result = $this->abc->selectTag('tofu', array());

+ 5 - 3
tests/libs/dbo_factory_test.php

@@ -1,6 +1,7 @@
 <?php
 
 uses ('test', 'dbo_factory');
+config ('database');
 
 class DboFactoryTest extends TestCase {
 	var $abc;
@@ -25,9 +26,10 @@ class DboFactoryTest extends TestCase {
    }
 
 
-	function testMake () {
-		if (class_exists(DATABASE_CONFIG)) {
-
+	function testMake () 
+	{
+		if (class_exists(DATABASE_CONFIG)) 
+		{
 			$output = $this->abc->make('test');
 			$this->assertTrue(is_object($output));
 

+ 2 - 2
tests/libs/flay_test.php

@@ -29,7 +29,7 @@ class FlayTest extends TestCase {
 		$tests_to_html = array(
 			array(
 			'text'=>"",
-			'html'=>""
+			'html'=>false
 			),
 			array(
 			'text'=>"This is a text.",
@@ -69,7 +69,7 @@ class FlayTest extends TestCase {
 			),
 			array(
 			'text'=>"Now auto-link an url such as http://sputnik.pl or www.robocik-malowany.com/dupa[4] - or any other.",
-			'html'=>"<p>Now auto-link an url such as <a href=\"http://sputnik.pl\">http://sputnik.pl</a> or <a href=\"www.robocik-malowany.com/dupa[4]\">www.robocik-malowany.com/dupa[4]</a> &ndash; or any other.</p>\n"
+			'html'=>"<p>Now auto-link an url such as <a href=\"http://sputnik.pl\">http://sputnik.pl</a> or <a href=\"http://www.robocik-malowany.com/dupa[4]\">www.robocik-malowany.com/dupa[4]</a> &ndash; or any other.</p>\n"
 			),
 			array(
 			'text'=>"===This be centered===",

+ 1 - 1
tests/libs/folder_test.php

@@ -27,7 +27,7 @@ class FolderTest extends TestCase {
 
 	function testLs () {
 		$result = $this->abc->ls();
-		$expected = array(array('css', 'files', 'img'),array('.htaccess', '500.html', 'index.php', 'index_no_mod_rewrite.php'));
+		$expected = array(array('.svn', 'css', 'files', 'img', 'js'),array('.htaccess', '500.html', 'index.php'));
 		$this->assertEquals($result, $expected);
 	}