Merging fixes and enhancements into trunk.

Revision: [2287]
Merging changes from model_php4.php

Revision: [2286]
Added serialized object data to the cached file.
Instances of the view helpers are available in the views now.
You also have access to the Controller::<component>, example $this->controller->Session;

Revision: [2285]
Adding Controller::postConditions() to convert a POST'ed data array to a Model conditions array

Revision: [2284]
Adding Model::invalidate() and refactoring Model::invalidFields().  Adding a fix for RequestHandler::accepts()

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2288 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2006-03-14 02:26:08 +00:00
parent f9197dc7a5
commit fc0854e397
9 changed files with 173 additions and 98 deletions

View file

@ -6,4 +6,4 @@
// +---------------------------------------------------------------------------------------------------+ // // +---------------------------------------------------------------------------------------------------+ //
/////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
1.0.0.2283 1.0.0.2288

View file

@ -107,6 +107,8 @@ else
} }
$TIME_START = getMicrotime(); $TIME_START = getMicrotime();
require CAKE.'dispatcher.php';
if(defined('CACHE_CHECK') && CACHE_CHECK === true) if(defined('CACHE_CHECK') && CACHE_CHECK === true)
{ {
if (empty($uri)) if (empty($uri))
@ -118,29 +120,11 @@ if(defined('CACHE_CHECK') && CACHE_CHECK === true)
if (file_exists($filename)) if (file_exists($filename))
{ {
ob_start(); uses(DS.'controller'.DS.'component', DS.'view'.DS.'view');
include($filename); $view = new View();
if (DEBUG) $view->renderCache($filename, $TIME_START);
{
echo "<!-- Cached Render Time: ". round(getMicrotime() - $TIME_START, 4) ."s -->";
}
$out = ob_get_clean();
if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match))
{
if(time() >= $match['1'])
{
@unlink($filename);
unset($out);
}
else
{
die(e($out));
}
}
} }
} }
require CAKE.'dispatcher.php';
require LIBS.'model'.DS.'connection_manager.php'; require LIBS.'model'.DS.'connection_manager.php';
config('database'); config('database');

View file

@ -213,6 +213,7 @@ class Dispatcher extends Object
{ {
array_push($controller->components, $controller->webservices); array_push($controller->components, $controller->webservices);
array_push($controller->helpers, $controller->webservices); array_push($controller->helpers, $controller->webservices);
$component =& new Component($controller);
} }
if((in_array('scaffold', array_keys($classVars))) && ($missingAction === true)) if((in_array('scaffold', array_keys($classVars))) && ($missingAction === true))

View file

@ -388,6 +388,12 @@ class RequestHandlerComponent extends Object
} }
else if (is_string($type)) else if (is_string($type))
{ {
// If client only accepts */*, then assume default HTML browser
if ($type == 'html' && $this->__acceptTypes === array('*/*'))
{
return true;
}
if (!in_array($type, array_keys($this->__requestContent))) if (!in_array($type, array_keys($this->__requestContent)))
{ {
return false; return false;

View file

@ -241,6 +241,10 @@ class Controller extends Object
} }
} }
} }
if (!empty($this->components))
{
$component =& new Component($this);
}
parent::__construct(); parent::__construct();
} }
@ -251,11 +255,6 @@ class Controller extends Object
*/ */
function constructClasses() function constructClasses()
{ {
if (!empty($this->components))
{
$component =& new Component($this);
}
if(empty($this->params['pass'])) if(empty($this->params['pass']))
{ {
$id = false; $id = false;
@ -840,6 +839,31 @@ class Controller extends Object
return $fieldNames; return $fieldNames;
} }
/**
* Converts POST'ed model data to a model conditions array, suitable for a find
* or findAll Model query
*
* @param array $data POST'ed data organized by model and field
* @return array An array of model conditions
*/
function postConditions ($data)
{
if (!is_array($data) || empty($data))
{
return null;
}
$conditions = array();
foreach ($data as $model => $fields)
{
foreach ($fields as $field => $value)
{
$conditions[$model.'.'.$field] = $value;
}
}
return $conditions;
}
/** /**
* Cleans up the date fields of current Model. * Cleans up the date fields of current Model.
* *

View file

@ -1399,45 +1399,52 @@ class Model extends Object
* @param array $data * @param array $data
* @return array Array of invalid fields * @return array Array of invalid fields
*/ */
function invalidFields ($data=null) function invalidFields ($data = array())
{ {
if (!isset($this->validate) || is_array($this->validationErrors)) if (!isset($this->validate))
{ {
if (!isset($this->validate)) return true;
{ }
return true;
}
else
{
return $this->validationErrors;
}
}
if ($data == null) if (is_array($this->validationErrors))
{ {
if (isset($this->data)) return $this->validationErrors;
{ }
$data = $this->data;
}
else
{
$data = array();
}
}
$errors = array(); if (empty($data) && isset($this->data))
foreach ($data as $table => $field) {
{ $data = $this->data;
foreach ($this->validate as $field_name => $validator) }
{
if (isset($data[$table][$field_name]) && !preg_match($validator, $data[$table][$field_name])) if (isset($data[$this->name]))
{
$data = $data[$this->name];
}
$this->validationErrors = array();
foreach ($this->validate as $field_name => $validator)
{
if (isset($data[$field_name]) && !preg_match($validator, $data[$field_name]))
{ {
$errors[$field_name] = 1; $this->validationErrors[$field_name] = 1;
} }
} }
$this->validationErrors = $errors; return $this->validationErrors;
return $errors; }
}
/**
* Sets a field as invalid
*
* @param string $field The name of the field to invalidate
* @return void
*/
function invalidate ($field)
{
if (!is_array($this->validationErrors))
{
$this->validationErrors = array();
}
$this->validationErrors[$field] = 1;
} }
/** /**

View file

@ -1395,45 +1395,52 @@ class Model extends Object
* @param array $data * @param array $data
* @return array Array of invalid fields * @return array Array of invalid fields
*/ */
function invalidFields ($data=null) function invalidFields ($data = array())
{ {
if (!isset($this->validate) || is_array($this->validationErrors)) if (!isset($this->validate))
{ {
if (!isset($this->validate)) return true;
{ }
return true;
}
else
{
return $this->validationErrors;
}
}
if ($data == null) if (is_array($this->validationErrors))
{ {
if (isset($this->data)) return $this->validationErrors;
{ }
$data = $this->data;
}
else
{
$data = array();
}
}
$errors = array(); if (empty($data) && isset($this->data))
foreach ($data as $table => $field) {
{ $data = $this->data;
foreach ($this->validate as $field_name => $validator) }
{
if (isset($data[$table][$field_name]) && !preg_match($validator, $data[$table][$field_name])) if (isset($data[$this->name]))
{
$data = $data[$this->name];
}
$this->validationErrors = array();
foreach ($this->validate as $field_name => $validator)
{
if (isset($data[$field_name]) && !preg_match($validator, $data[$field_name]))
{ {
$errors[$field_name] = 1; $this->validationErrors[$field_name] = 1;
} }
} }
$this->validationErrors = $errors; return $this->validationErrors;
return $errors; }
}
/**
* Sets a field as invalid
*
* @param string $field The name of the field to invalidate
* @return void
*/
function invalidate ($field)
{
if (!is_array($this->validationErrors))
{
$this->validationErrors = array();
}
$this->validationErrors[$field] = 1;
} }
/** /**

View file

@ -43,6 +43,7 @@ class CacheHelper extends Helper
{ {
var $replace = array(); var $replace = array();
var $match = array(); var $match = array();
var $view;
function cache($file, $out, $cache = false) function cache($file, $out, $cache = false)
{ {
@ -106,7 +107,15 @@ class CacheHelper extends Helper
function __parseFile($file, $cache) function __parseFile($file, $cache)
{ {
$file = file_get_contents($file); if(is_file($file))
{
$file = file_get_contents($file);
}
else if($file = fileExistsInPath($file))
{
$file = file_get_contents($file);
}
preg_match_all('/(?P<found><cake:nocache>(?:.*|(?:[\\S\\s]*[^\\S]))<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER); preg_match_all('/(?P<found><cake:nocache>(?:.*|(?:[\\S\\s]*[^\\S]))<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER);
preg_match_all('/<cake:nocache>(?P<replace>(?:.*|(?:[\\S\\s]*[^\\S])))<\/cake:nocache>/i', $file, $result, PREG_PATTERN_ORDER); preg_match_all('/<cake:nocache>(?P<replace>(?:.*|(?:[\\S\\s]*[^\\S])))<\/cake:nocache>/i', $file, $result, PREG_PATTERN_ORDER);
@ -151,7 +160,14 @@ class CacheHelper extends Helper
$result = preg_replace('/\/\//', '/', $this->here); $result = preg_replace('/\/\//', '/', $this->here);
$cache = str_replace('/', '_', $result.'.php'); $cache = str_replace('/', '_', $result.'.php');
$cache = str_replace('favicon.ico', '', $cache); $cache = str_replace('favicon.ico', '', $cache);
$file = '<!--cachetime:'.$cacheTime.'-->'.$file; $file = '<!--cachetime:'.$cacheTime.'-->'.
'<?php loadController(\''.$this->view->name.'\'); ?>'.
'<?php $this->controller = new '.$this->view->name.'Controller(); ?>'.
'<?php $this->helpers = unserialize(\''. serialize($this->view->helpers).'\'); ?>'.
'<?php $this->webroot = \''. $this->view->webroot.'\'; ?>'.
'<?php $this->data = unserialize(\''. serialize($this->view->data).'\'); ?>'.
'<?php $loaded = array(); ?>'.
'<?php $this->_loadHelpers($loaded, $this->helpers); ?>'.$file;
return cache('views'.DS.$cache, $file, $timestamp); return cache('views'.DS.$cache, $file, $timestamp);
} }
} }

View file

@ -214,7 +214,7 @@ class View extends Object
* *
* @return View * @return View
*/ */
function __construct (&$controller) function __construct (&$controller = null)
{ {
if ($controller != null) if ($controller != null)
{ {
@ -664,8 +664,13 @@ class View extends Object
if (is_a($this->loaded['cache'], 'CacheHelper')) if (is_a($this->loaded['cache'], 'CacheHelper'))
{ {
$cache =& $this->loaded['cache']; $cache =& $this->loaded['cache'];
if($cached === true)
{
$cache->view =& $this;
}
$cache->base = $this->base; $cache->base = $this->base;
$cache->here = $this->here; $cache->here = $this->here;
$cache->helpers = $this->loaded;
$cache->controllerName = $this->params['controller']; $cache->controllerName = $this->params['controller'];
$cache->cacheAction = $this->controller->cacheAction; $cache->cacheAction = $this->controller->cacheAction;
$cache->cache($___viewFn, $out, $cached); $cache->cache($___viewFn, $out, $cached);
@ -783,5 +788,30 @@ class View extends Object
'file' => $viewFileName))); 'file' => $viewFileName)));
} }
} }
function renderCache($filename, $time)
{
ob_start();
include($filename);
if (DEBUG)
{
echo "<!-- Cached Render Time: ". round(getMicrotime() - $time, 4) ."s -->";
}
$out = ob_get_clean();
if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match))
{
if(time() >= $match['1'])
{
@unlink($filename);
unset($out);
return;
}
else
{
e($out);
die();
}
}
}
} }
?> ?>