From 977f897a856a8803a8a4e8343fbae01d18b4f58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Mon, 26 Apr 2010 02:32:32 -0430 Subject: [PATCH 1/4] First steps into removing internal usage of File and Folder class in favor of SPL equivalents --- cake/libs/cache/file.php | 70 +++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/cake/libs/cache/file.php b/cake/libs/cache/file.php index 0476f9872..9e54dfdd1 100644 --- a/cake/libs/cache/file.php +++ b/cake/libs/cache/file.php @@ -78,19 +78,11 @@ class FileEngine extends CacheEngine { ), $settings )); - if (!isset($this->_File)) { - $this->_File =& new File($this->settings['path'] . DS . 'cake'); - } if (DIRECTORY_SEPARATOR === '\\') { $this->settings['isWindows'] = true; } - - $path = $this->_File->Folder->cd($this->settings['path']); - if ($path) { - $this->settings['path'] = $path; - } - return $this->__active(); + return $this->_active(); } /** @@ -115,7 +107,7 @@ class FileEngine extends CacheEngine { return false; } - if ($this->_setKey($key) === false) { + if ($this->_setKey($key, true) === false) { return false; } @@ -134,12 +126,11 @@ class FileEngine extends CacheEngine { } if ($this->settings['lock']) { - $this->_File->lock = true; + //$this->_File->lock = true; } $expires = time() + $duration; $contents = $expires . $lineBreak . $data . $lineBreak; - $success = $this->_File->write($contents); - $this->_File->close(); + $success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents); return $success; } @@ -150,20 +141,27 @@ class FileEngine extends CacheEngine { * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it */ public function read($key) { - if ($this->_setKey($key) === false || !$this->_init || !$this->_File->exists()) { + if (!$this->_init || $this->_setKey($key) === false) { return false; } if ($this->settings['lock']) { - $this->_File->lock = true; + //$this->_File->lock = true; } + $this->_File->rewind(); $time = time(); - $cachetime = intval($this->_File->read(11)); + $cachetime = intval($this->_File->current()); if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) { - $this->_File->close(); return false; } - $data = $this->_File->read(true); + + $data = ''; + $this->_File->next(); + while ($this->_File->valid()) { + $data .= $this->_File->current(); + $this->_File->next(); + } + $data = trim($data); if ($data !== '' && !empty($this->settings['serialize'])) { if ($this->settings['isWindows']) { @@ -171,7 +169,6 @@ class FileEngine extends CacheEngine { } $data = unserialize((string)$data); } - $this->_File->close(); return $data; } @@ -185,7 +182,7 @@ class FileEngine extends CacheEngine { if ($this->_setKey($key) === false || !$this->_init) { return false; } - return $this->_File->delete(); + return unlink($this->_File->getRealPath()); } /** @@ -208,20 +205,19 @@ class FileEngine extends CacheEngine { continue; } if ($check) { - $mtime = $this->_File->lastChange(); + $mtime = $this->_File->getMTime(); - if ($mtime === false || $mtime > $threshold) { + if ($mtime > $threshold) { continue; } - $expires = $this->_File->read(11); - $this->_File->close(); + $expires = (int)$this->_File->current(); if ($expires > $now) { continue; } } - $this->_File->delete(); + unlink($this->_File->getRealPath()); } $dir->close(); return true; @@ -252,27 +248,29 @@ class FileEngine extends CacheEngine { * * @param string $key The key * @return mixed Absolute cache file for the given key or false if erroneous - * @access private + * @access protected */ - function _setKey($key) { - $this->_File->Folder->cd($this->settings['path']); - if ($key !== $this->_File->name) { - $this->_File->name = $key; - $this->_File->path = null; - } - if (!$this->_File->Folder->inPath($this->_File->pwd(), true)) { + protected function _setKey($key, $createKey = false) { + $path = new SplFileInfo($this->settings['path'] . DS . $key); + + if (!$createKey && !$path->isFile()) { return false; } + + if (empty($this->_File) || $this->_File->getBaseName() !== $key) { + $this->_File = $path->openFile('a+'); + } } /** * Determine is cache directory is writable * * @return boolean - * @access private + * @access protected */ - function __active() { - if ($this->_init && !is_writable($this->settings['path'])) { + protected function _active() { + $dir = new SplFileInfo($this->settings['path']); + if ($this->_init && !($dir->isDir() && $dir->isWritable())) { $this->_init = false; trigger_error(sprintf(__('%s is not writable'), $this->settings['path']), E_USER_WARNING); } From d3b257fffb1cb082630a89146a92d2a6a8d13d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Mon, 26 Apr 2010 21:42:16 -0430 Subject: [PATCH 2/4] Implementing cache lockig in FileEngine using SplFileObject --- cake/libs/cache/file.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/cake/libs/cache/file.php b/cake/libs/cache/file.php index 9e54dfdd1..9b004c1ed 100644 --- a/cake/libs/cache/file.php +++ b/cake/libs/cache/file.php @@ -19,9 +19,6 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -if (!class_exists('File')) { - require LIBS . 'file.php'; -} /** * File Storage engine for cache * @@ -32,9 +29,9 @@ if (!class_exists('File')) { class FileEngine extends CacheEngine { /** - * Instance of File class + * Instance of SplFileObject class * - * @var File + * @var _File * @access protected */ protected $_File = null; @@ -126,11 +123,17 @@ class FileEngine extends CacheEngine { } if ($this->settings['lock']) { - //$this->_File->lock = true; + $this->_File->flock(LOCK_EX); } + $expires = time() + $duration; $contents = $expires . $lineBreak . $data . $lineBreak; $success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents); + + if ($this->settings['lock']) { + $this->_File->flock(LOCK_EX); + } + return $success; } @@ -144,9 +147,11 @@ class FileEngine extends CacheEngine { if (!$this->_init || $this->_setKey($key) === false) { return false; } + if ($this->settings['lock']) { - //$this->_File->lock = true; + $this->_File->flock(LOCK_SH); } + $this->_File->rewind(); $time = time(); $cachetime = intval($this->_File->current()); @@ -161,6 +166,11 @@ class FileEngine extends CacheEngine { $data .= $this->_File->current(); $this->_File->next(); } + + if ($this->settings['lock']) { + $this->_File->flock(LOCK_SH); + } + $data = trim($data); if ($data !== '' && !empty($this->settings['serialize'])) { @@ -244,10 +254,11 @@ class FileEngine extends CacheEngine { } /** - * Get absolute file for a given key + * Sets the current cache key this class is managing * * @param string $key The key - * @return mixed Absolute cache file for the given key or false if erroneous + * @param boolean $createKey Whether the key should be created if it doesn't exists, or not + * @return boolean true if the cache key could be set, false otherwise * @access protected */ protected function _setKey($key, $createKey = false) { @@ -260,6 +271,8 @@ class FileEngine extends CacheEngine { if (empty($this->_File) || $this->_File->getBaseName() !== $key) { $this->_File = $path->openFile('a+'); } + + return true; } /** From c1eaa4b24c366eef89e4b15f2d3e9f1b63847b3d Mon Sep 17 00:00:00 2001 From: predominant Date: Mon, 3 May 2010 13:28:09 +1000 Subject: [PATCH 3/4] Fixed access and removed @access tags. --- cake/libs/controller/controller.php | 90 +++++++---------------------- 1 file changed, 20 insertions(+), 70 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 77f992473..040f2a139 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -23,6 +23,7 @@ */ App::import('Controller', 'Component', false); App::import('View', 'View', false); + /** * Controller * @@ -40,7 +41,6 @@ class Controller extends Object { * The name of this controller. Controller names are plural, named after the model they manipulate. * * @var string - * @access public * @link http://book.cakephp.org/view/959/Controller-Attributes */ public $name = null; @@ -49,7 +49,6 @@ class Controller extends Object { * Stores the current URL, relative to the webroot of the application. * * @var string - * @access public */ public $here = null; @@ -57,7 +56,6 @@ class Controller extends Object { * The webroot of the application. * * @var string - * @access public */ public $webroot = null; @@ -65,7 +63,6 @@ class Controller extends Object { * The name of the currently requested controller action. * * @var string - * @access public */ public $action = null; @@ -78,7 +75,6 @@ class Controller extends Object { * use no models and prevent the merging of $uses with AppController * * @var mixed A single name as a string or a list of names as an array. - * @access protected * @link http://book.cakephp.org/view/961/components-helpers-and-uses */ public $uses = false; @@ -90,7 +86,6 @@ class Controller extends Object { * Example: `public $helpers = array('Html', 'Javascript', 'Time', 'Ajax');` * * @var mixed A single name as a string or a list of names as an array. - * @access protected * @link http://book.cakephp.org/view/961/components-helpers-and-uses */ public $helpers = array('Session', 'Html', 'Form'); @@ -100,7 +95,6 @@ class Controller extends Object { * about the request, etc. * * @var array - * @access public * @link http://book.cakephp.org/view/963/The-Parameters-Attribute-params */ public $params = array(); @@ -110,7 +104,6 @@ class Controller extends Object { * using the `$this->data['ModelName']['fieldName']` pattern. * * @var array - * @access public */ public $data = array(); @@ -130,7 +123,6 @@ class Controller extends Object { * }}} * * @var array - * @access public * @link http://book.cakephp.org/view/1231/Pagination */ public $paginate = array('limit' => 20, 'page' => 1); @@ -139,7 +131,6 @@ class Controller extends Object { * The name of the views subfolder containing views for this controller. * * @var string - * @access public */ public $viewPath = null; @@ -147,7 +138,6 @@ class Controller extends Object { * The name of the layouts subfolder containing layouts for this controller. * * @var string - * @access public */ public $layoutPath = null; @@ -155,7 +145,6 @@ class Controller extends Object { * Contains variables to be handed to the view. * * @var array - * @access public */ public $viewVars = array(); @@ -163,7 +152,6 @@ class Controller extends Object { * An array containing the class names of the models this controller uses. * * @var array Array of model objects. - * @access public */ public $modelNames = array(); @@ -171,7 +159,6 @@ class Controller extends Object { * Base URL path. * * @var string - * @access public */ public $base = null; @@ -181,7 +168,6 @@ class Controller extends Object { * extension. * * @var string - * @access public * @link http://book.cakephp.org/view/962/Page-related-Attributes-layout-and-pageTitle */ public $layout = 'default'; @@ -191,7 +177,6 @@ class Controller extends Object { * after action logic. * * @var boolean - * @access public */ public $autoRender = true; @@ -199,7 +184,6 @@ class Controller extends Object { * Set to true to automatically render the layout around views. * * @var boolean - * @access public */ public $autoLayout = true; @@ -207,7 +191,6 @@ class Controller extends Object { * Instance of Component used to handle callbacks. * * @var string - * @access public */ public $Component = null; @@ -218,7 +201,6 @@ class Controller extends Object { * Example: `public $components = array('Session', 'RequestHandler', 'Acl');` * * @var array - * @access public * @link http://book.cakephp.org/view/961/components-helpers-and-uses */ public $components = array('Session'); @@ -227,7 +209,6 @@ class Controller extends Object { * The name of the View class this controller sends output to. * * @var string - * @access public */ public $view = 'View'; @@ -235,7 +216,6 @@ class Controller extends Object { * File extension for view templates. Defaults to Cake's conventional ".ctp". * * @var string - * @access public */ public $ext = '.ctp'; @@ -245,7 +225,6 @@ class Controller extends Object { * You can use this var in child controllers' afterFilter() callbacks to alter output. * * @var string - * @access public */ public $output = null; @@ -253,7 +232,6 @@ class Controller extends Object { * Automatically set to the name of a plugin. * * @var string - * @access public */ public $plugin = null; @@ -275,7 +253,6 @@ class Controller extends Object { * marks all the actions in the controller for view caching. * * @var mixed - * @access public * @link http://book.cakephp.org/view/1380/Caching-in-the-Controller */ public $cacheAction = false; @@ -286,7 +263,6 @@ class Controller extends Object { * This can increase performance in many cases. * * @var boolean - * @access public */ public $persistModel = false; @@ -294,7 +270,6 @@ class Controller extends Object { * Holds all params passed and named. * * @var mixed - * @access public */ public $passedArgs = array(); @@ -302,7 +277,6 @@ class Controller extends Object { * Triggers Scaffolding * * @var mixed - * @access public * @link http://book.cakephp.org/view/1103/Scaffolding */ public $scaffold = false; @@ -311,8 +285,6 @@ class Controller extends Object { * Holds current methods of the controller * * @var array - * @access public - * @link */ public $methods = array(); @@ -323,7 +295,6 @@ class Controller extends Object { * Example: For a controller named 'Comments', the modelClass would be 'Comment' * * @var string - * @access public */ public $modelClass = null; @@ -333,7 +304,6 @@ class Controller extends Object { * Example: For a controller named 'ArticleComments', the modelKey would be 'article_comment' * * @var string - * @access public */ public $modelKey = null; @@ -341,7 +311,6 @@ class Controller extends Object { * Holds any validation errors produced by the last call of the validateErrors() method/ * * @var array Validation errors, or false if none - * @access public */ public $validationErrors = null; @@ -351,7 +320,6 @@ class Controller extends Object { * tasked with their lazy-loading. * * @var array Associative array of HTTP codes and their associated messages. - * @access private */ private $__httpCodes = null; @@ -359,7 +327,7 @@ class Controller extends Object { * Constructor. * */ - function __construct() { + public function __construct() { if ($this->name === null) { $r = null; if (!preg_match('/(.*)Controller/i', get_class($this), $r)) { @@ -477,11 +445,10 @@ class Controller extends Object { * Loads Components and prepares them for initialization. * * @return mixed true if models found and instance created, or cakeError if models not found. - * @access public * @see Controller::loadModel() * @link http://book.cakephp.org/view/977/Controller-Methods#constructClasses-986 */ - function constructClasses() { + public function constructClasses() { $this->__mergeVars(); $this->Component->init($this); @@ -557,7 +524,7 @@ class Controller extends Object { * @return mixed Associative array of the HTTP codes as keys, and the message * strings as values, or null of the given $code does not exist. */ - function httpCodes($code = null) { + public function httpCodes($code = null) { if (empty($this->__httpCodes)) { $this->__httpCodes = array( 100 => 'Continue', 101 => 'Switching Protocols', @@ -605,7 +572,6 @@ class Controller extends Object { * @param string $modelClass Name of model class to load * @param mixed $id Initial ID the instanced model class should have * @return mixed true when single model found and instance created, error returned if model not found. - * @access public */ public function loadModel($modelClass = null, $id = null) { if ($modelClass === null) { @@ -667,10 +633,9 @@ class Controller extends Object { * @param integer $status Optional HTTP status code (eg: 404) * @param boolean $exit If true, exit() will be called after the redirect * @return mixed void if $exit = false. Terminates script if $exit = true - * @access public * @link http://book.cakephp.org/view/982/redirect */ - function redirect($url, $status = null, $exit = true) { + public function redirect($url, $status = null, $exit = true) { $this->autoRender = false; if (is_array($status)) { @@ -749,10 +714,9 @@ class Controller extends Object { * @param mixed $two Value in case $one is a string (which then works as the key). * Unused if $one is an associative array, otherwise serves as the values to $one's keys. * @return void - * @access public * @link http://book.cakephp.org/view/979/set */ - function set($one, $two = null) { + public function set($one, $two = null) { $data = array(); if (is_array($one)) { @@ -794,10 +758,9 @@ class Controller extends Object { * Only called when AuthComponent::$authorize is set to 'controller'. * * @return bool true if authorized, false otherwise - * @access public * @link http://book.cakephp.org/view/1275/authorize */ - function isAuthorized() { + public function isAuthorized() { trigger_error(sprintf( __('%s::isAuthorized() is not defined.'), $this->name ), E_USER_WARNING); @@ -850,10 +813,9 @@ class Controller extends Object { * @param string $layout Layout to use * @param string $file File to use for rendering * @return string Full output string of view contents - * @access public * @link http://book.cakephp.org/view/980/render */ - function render($action = null, $layout = null, $file = null) { + public function render($action = null, $layout = null, $file = null) { $this->beforeRender(); $viewClass = $this->view; @@ -912,10 +874,9 @@ class Controller extends Object { * @param string $default Default URL to use if HTTP_REFERER cannot be read from headers * @param boolean $local If true, restrict referring URLs to local server * @return string Referring URL - * @access public * @link http://book.cakephp.org/view/987/referer */ - function referer($default = null, $local = false) { + public function referer($default = null, $local = false) { $ref = env('HTTP_REFERER'); if (!empty($ref) && defined('FULL_BASE_URL')) { $base = FULL_BASE_URL . $this->webroot; @@ -941,10 +902,9 @@ class Controller extends Object { * Forces the user's browser not to cache the results of the current request. * * @return void - * @access public * @link http://book.cakephp.org/view/988/disableCache */ - function disableCache() { + public function disableCache() { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); @@ -962,10 +922,9 @@ class Controller extends Object { * @param integer $pause Time to show the message * @param string $layout Layout you want to use, defaults to 'flash' * @return void Renders flash layout - * @access public * @link http://book.cakephp.org/view/983/flash */ - function flash($message, $url, $pause = 1, $layout = 'flash') { + public function flash($message, $url, $pause = 1, $layout = 'flash') { $this->autoRender = false; $this->set('url', Router::url($url)); $this->set('message', $message); @@ -984,10 +943,9 @@ class Controller extends Object { * @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be * included in the returned conditions * @return array An array of model conditions - * @access public * @link http://book.cakephp.org/view/989/postConditions */ - function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) { + public function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) { if (!is_array($data) || empty($data)) { if (!empty($this->data)) { $data = $this->data; @@ -1041,10 +999,9 @@ class Controller extends Object { * @param mixed $scope Conditions to use while paginating * @param array $whitelist List of allowed options for paging * @return array Model query results - * @access public * @link http://book.cakephp.org/view/1232/Controller-Setup */ - function paginate($object = null, $scope = array(), $whitelist = array()) { + public function paginate($object = null, $scope = array(), $whitelist = array()) { if (is_array($object)) { $whitelist = $scope; $scope = $object; @@ -1237,28 +1194,25 @@ class Controller extends Object { /** * Called before the controller action. * - * @access public * @link http://book.cakephp.org/view/984/Callbacks */ - function beforeFilter() { + public function beforeFilter() { } /** * Called after the controller action is run, but before the view is rendered. * - * @access public * @link http://book.cakephp.org/view/984/Callbacks */ - function beforeRender() { + public function beforeRender() { } /** * Called after the controller action is run and rendered. * - * @access public * @link http://book.cakephp.org/view/984/Callbacks */ - function afterFilter() { + public function afterFilter() { } /** @@ -1266,10 +1220,9 @@ class Controller extends Object { * * @param string $method name of method called example index, edit, etc. * @return boolean Success - * @access protected * @link http://book.cakephp.org/view/984/Callbacks */ - function _beforeScaffold($method) { + public function _beforeScaffold($method) { return true; } @@ -1278,10 +1231,9 @@ class Controller extends Object { * * @param string $method name of method called either edit or update. * @return boolean Success - * @access protected * @link http://book.cakephp.org/view/984/Callbacks */ - function _afterScaffoldSave($method) { + public function _afterScaffoldSave($method) { return true; } @@ -1290,10 +1242,9 @@ class Controller extends Object { * * @param string $method name of method called either edit or update. * @return boolean Success - * @access protected * @link http://book.cakephp.org/view/984/Callbacks */ - function _afterScaffoldSaveError($method) { + public function _afterScaffoldSaveError($method) { return true; } @@ -1304,10 +1255,9 @@ class Controller extends Object { * * @param string $method name of method called example index, edit, etc. * @return boolean Success - * @access protected * @link http://book.cakephp.org/view/984/Callbacks */ - function _scaffoldError($method) { + public function _scaffoldError($method) { return false; } } From 87db96638384dcf548aa589de731aa54c856deab Mon Sep 17 00:00:00 2001 From: predominant Date: Mon, 3 May 2010 13:36:32 +1000 Subject: [PATCH 4/4] Remove PHP5 conditional statements. --- cake/libs/controller/controller.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 040f2a139..480b58004 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -594,15 +594,9 @@ class Controller extends Object { if (($cached === false)) { $this->modelNames[] = $modelClass; - if (!PHP5) { - $this->{$modelClass} =& ClassRegistry::init(array( - 'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id - )); - } else { - $this->{$modelClass} = ClassRegistry::init(array( - 'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id - )); - } + $this->{$modelClass} = ClassRegistry::init(array( + 'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id + )); if (!$this->{$modelClass}) { return $this->cakeError('missingModel', array(array(