Browse Source

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
phpnut 20 years ago
parent
commit
fc0854e397

+ 1 - 1
VERSION.txt

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

+ 5 - 21
cake/bootstrap.php

@@ -107,6 +107,8 @@ else
 }
 
 $TIME_START = getMicrotime();
+
+require CAKE.'dispatcher.php';
 if(defined('CACHE_CHECK') && CACHE_CHECK === true)
 {
     if (empty($uri))
@@ -118,29 +120,11 @@ if(defined('CACHE_CHECK') && CACHE_CHECK === true)
 
     if (file_exists($filename))
     {
-        ob_start();
-        include($filename);
-        if (DEBUG)
-        {
-            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));
-            }
-        }
+        uses(DS.'controller'.DS.'component', DS.'view'.DS.'view');
+        $view = new View();
+        $view->renderCache($filename, $TIME_START);
     }
 }
-
-require CAKE.'dispatcher.php';
 require LIBS.'model'.DS.'connection_manager.php';
 
 config('database');

+ 1 - 0
cake/dispatcher.php

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

+ 6 - 0
cake/libs/controller/components/request_handler.php

@@ -388,6 +388,12 @@ class RequestHandlerComponent extends Object
         }
         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)))
             {
                 return false;

+ 29 - 5
cake/libs/controller/controller.php

@@ -241,6 +241,10 @@ class Controller extends Object
                 }
             }
         }
+        if (!empty($this->components))
+        {
+            $component =& new Component($this);
+        }
         parent::__construct();
     }
 
@@ -251,11 +255,6 @@ class Controller extends Object
  */
     function constructClasses()
     {
-        if (!empty($this->components))
-        {
-            $component =& new Component($this);
-        }
-
         if(empty($this->params['pass']))
         {
             $id = false;
@@ -841,6 +840,31 @@ class Controller extends Object
     }
 
 /**
+ * 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.
  *
  */

+ 41 - 34
cake/libs/model/model_php4.php

@@ -1399,45 +1399,52 @@ class Model extends Object
  * @param array $data
  * @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))
-         {
-             return true;
-         }
-         else
-         {
-             return $this->validationErrors;
-         }
-      }
+        if (!isset($this->validate))
+        {
+            return true;
+        }
 
-      if ($data == null)
-      {
-          if (isset($this->data))
-          {
-              $data = $this->data;
-          }
-          else
-          {
-              $data = array();
-          }
-      }
+        if (is_array($this->validationErrors))
+        {
+            return $this->validationErrors;
+        }
 
-      $errors = array();
-      foreach ($data as $table => $field)
-      {
-         foreach ($this->validate as $field_name => $validator)
-         {
-            if (isset($data[$table][$field_name]) && !preg_match($validator, $data[$table][$field_name]))
+        if (empty($data) && isset($this->data))
+        {
+            $data = $this->data;
+        }
+
+        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 $errors;
-      }
+        }
+        return $this->validationErrors;
+    }
+
+/**
+ * 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;
     }
 
 /**

+ 41 - 34
cake/libs/model/model_php5.php

@@ -1395,45 +1395,52 @@ class Model extends Object
  * @param array $data
  * @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))
-         {
-             return true;
-         }
-         else
-         {
-             return $this->validationErrors;
-         }
-      }
+        if (!isset($this->validate))
+        {
+            return true;
+        }
 
-      if ($data == null)
-      {
-          if (isset($this->data))
-          {
-              $data = $this->data;
-          }
-          else
-          {
-              $data = array();
-          }
-      }
+        if (is_array($this->validationErrors))
+        {
+            return $this->validationErrors;
+        }
 
-      $errors = array();
-      foreach ($data as $table => $field)
-      {
-         foreach ($this->validate as $field_name => $validator)
-         {
-            if (isset($data[$table][$field_name]) && !preg_match($validator, $data[$table][$field_name]))
+        if (empty($data) && isset($this->data))
+        {
+            $data = $this->data;
+        }
+
+        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 $errors;
-      }
+        }
+        return $this->validationErrors;
+    }
+
+/**
+ * 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;
     }
 
 /**

+ 18 - 2
cake/libs/view/helpers/cache.php

@@ -43,6 +43,7 @@ class CacheHelper extends Helper
 {
     var $replace = array();
     var $match = array();
+    var $view;
 
     function cache($file, $out, $cache = false)
     {
@@ -106,7 +107,15 @@ class CacheHelper extends Helper
 
     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('/<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);
         $cache = str_replace('/', '_', $result.'.php');
         $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);
     }
 }

+ 31 - 1
cake/libs/view/view.php

@@ -214,7 +214,7 @@ class View extends Object
  *
  * @return View
  */
-    function __construct (&$controller)
+    function __construct (&$controller = null)
     {
         if ($controller != null)
         {
@@ -664,8 +664,13 @@ class View extends Object
             if (is_a($this->loaded['cache'], 'CacheHelper'))
             {
                 $cache =& $this->loaded['cache'];
+                if($cached === true)
+                {
+                    $cache->view =& $this;
+                }
                 $cache->base =  $this->base;
                 $cache->here =  $this->here;
+                $cache->helpers = $this->loaded;
                 $cache->controllerName = $this->params['controller'];
                 $cache->cacheAction = $this->controller->cacheAction;
                 $cache->cache($___viewFn, $out, $cached);
@@ -783,5 +788,30 @@ class View extends Object
                                     '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();
+            }
+        }
+    }
 }
 ?>