Updating doc blocks on Folder class and implementing a few small refactors to make code simpler.

This commit is contained in:
Mark Story 2010-01-25 09:42:17 -05:00
parent 23a62b488b
commit b33fdba6d9
2 changed files with 52 additions and 40 deletions

View file

@ -28,8 +28,7 @@ if (!class_exists('Object')) {
/** /**
* Folder structure browser, lists folders and files. * Folder structure browser, lists folders and files.
* * Provides an Object interface for Common directory related tasks.
* Long description for class
* *
* @package cake * @package cake
* @subpackage cake.cake.libs * @subpackage cake.cake.libs
@ -45,7 +44,8 @@ class Folder extends Object {
var $path = null; var $path = null;
/** /**
* Sortedness. * Sortedness. Whether or not list results
* should be sorted by name.
* *
* @var boolean * @var boolean
* @access public * @access public
@ -53,15 +53,15 @@ class Folder extends Object {
var $sort = false; var $sort = false;
/** /**
* mode to be used on create. * Mode to be used on create. Does nothing on windows platforms.
* *
* @var boolean * @var integer
* @access public * @access public
*/ */
var $mode = 0755; var $mode = 0755;
/** /**
* holds messages from last method. * Holds messages from last method.
* *
* @var array * @var array
* @access private * @access private
@ -69,7 +69,7 @@ class Folder extends Object {
var $__messages = array(); var $__messages = array();
/** /**
* holds errors from last method. * Holds errors from last method.
* *
* @var array * @var array
* @access private * @access private
@ -77,7 +77,7 @@ class Folder extends Object {
var $__errors = false; var $__errors = false;
/** /**
* holds array of complete directory paths. * Holds array of complete directory paths.
* *
* @var array * @var array
* @access private * @access private
@ -85,7 +85,7 @@ class Folder extends Object {
var $__directories; var $__directories;
/** /**
* holds array of complete file paths. * Holds array of complete file paths.
* *
* @var array * @var array
* @access private * @access private
@ -148,7 +148,8 @@ class Folder extends Object {
* Returns an array of the contents of the current directory. * Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files. * The returned array holds two arrays: One of directories and one of files.
* *
* @param boolean $sort * @param boolean $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param mixed $exceptions Either an array or boolean true will not grab dot files * @param mixed $exceptions Either an array or boolean true will not grab dot files
* @param boolean $fullPath True returns the full path * @param boolean $fullPath True returns the full path
* @return mixed Contents of current directory as an array, an empty array on failure * @return mixed Contents of current directory as an array, an empty array on failure
@ -195,6 +196,7 @@ class Folder extends Object {
* Returns an array of all matching files in current directory. * Returns an array of all matching files in current directory.
* *
* @param string $pattern Preg_match pattern (Defaults to: .*) * @param string $pattern Preg_match pattern (Defaults to: .*)
* @param boolean $sort Whether results should be sorted.
* @return array Files that match given pattern * @return array Files that match given pattern
* @access public * @access public
*/ */
@ -207,6 +209,7 @@ class Folder extends Object {
* Returns an array of all matching files in and below current directory. * Returns an array of all matching files in and below current directory.
* *
* @param string $pattern Preg_match pattern (Defaults to: .*) * @param string $pattern Preg_match pattern (Defaults to: .*)
* @param boolean $sort Whether results should be sorted.
* @return array Files matching $pattern * @return array Files matching $pattern
* @access public * @access public
*/ */
@ -224,6 +227,7 @@ class Folder extends Object {
* Private helper function for findRecursive. * Private helper function for findRecursive.
* *
* @param string $pattern Pattern to match against * @param string $pattern Pattern to match against
* @param boolean $sort Whether results should be sorted.
* @return array Files matching pattern * @return array Files matching pattern
* @access private * @access private
*/ */
@ -254,23 +258,19 @@ class Folder extends Object {
* @static * @static
*/ */
function isWindowsPath($path) { function isWindowsPath($path) {
if (preg_match('/^[A-Z]:\\\\/i', $path)) { return (bool) preg_match('/^[A-Z]:\\\\/i', $path);
return true;
}
return false;
} }
/** /**
* Returns true if given $path is an absolute path. * Returns true if given $path is an absolute path.
* *
* @param string $path Path to check * @param string $path Path to check
* @return bool * @return bool true if path is absolute.
* @access public * @access public
* @static * @static
*/ */
function isAbsolute($path) { function isAbsolute($path) {
$match = preg_match('/^\\//', $path) || preg_match('/^[A-Z]:\\\\/i', $path); return (bool) (preg_match('/^\\//', $path) || preg_match('/^[A-Z]:\\\\/i', $path));
return $match;
} }
/** /**
@ -294,10 +294,7 @@ class Folder extends Object {
* @static * @static
*/ */
function correctSlashFor($path) { function correctSlashFor($path) {
if (Folder::isWindowsPath($path)) { return (Folder::isWindowsPath($path)) ? '\\' : '/';
return '\\';
}
return '/';
} }
/** /**
@ -331,6 +328,7 @@ class Folder extends Object {
/** /**
* Returns true if the File is in a given CakePath. * Returns true if the File is in a given CakePath.
* *
* @param string $path The path to check.
* @return bool * @return bool
* @access public * @access public
*/ */
@ -344,6 +342,8 @@ class Folder extends Object {
/** /**
* Returns true if the File is in given path. * Returns true if the File is in given path.
* *
* @param string $path The path to check that the current pwd() resides with in.
* @param boolean $reverse
* @return bool * @return bool
* @access public * @access public
*/ */
@ -356,11 +356,7 @@ class Folder extends Object {
} else { } else {
$return = preg_match('/^(.*)' . preg_quote($current, '/') . '(.*)/', $dir); $return = preg_match('/^(.*)' . preg_quote($current, '/') . '(.*)/', $dir);
} }
if ($return == 1) { return (bool) $return;
return true;
} else {
return false;
}
} }
/** /**
@ -368,7 +364,7 @@ class Folder extends Object {
* *
* @param string $path The path to chmod * @param string $path The path to chmod
* @param integer $mode octal value 0755 * @param integer $mode octal value 0755
* @param boolean $recursive chmod recursively * @param boolean $recursive chmod recursively, set to false to only change the current directory.
* @param array $exceptions array of files, directories to skip * @param array $exceptions array of files, directories to skip
* @return boolean Returns TRUE on success, FALSE on failure * @return boolean Returns TRUE on success, FALSE on failure
* @access public * @access public
@ -419,7 +415,7 @@ class Folder extends Object {
* Returns an array of nested directories and files in each directory * Returns an array of nested directories and files in each directory
* *
* @param string $path the directory path to build the tree from * @param string $path the directory path to build the tree from
* @param boolean $hidden return hidden files and directories * @param mixed $exceptions Array of files to exclude, defaults to excluding hidden files.
* @param string $type either file or dir. null returns both files and directories * @param string $type either file or dir. null returns both files and directories
* @return mixed array of nested directories and files in each directory * @return mixed array of nested directories and files in each directory
* @access public * @access public
@ -431,8 +427,8 @@ class Folder extends Object {
$this->__directories = array($path); $this->__directories = array($path);
$directories = array(); $directories = array();
if ($exceptions === false) { if ($hidden === false) {
$exceptions = true; $hidden = true;
} }
while (count($this->__directories)) { while (count($this->__directories)) {
$dir = array_pop($this->__directories); $dir = array_pop($this->__directories);
@ -454,20 +450,21 @@ class Folder extends Object {
/** /**
* Private method to list directories and files in each directory * Private method to list directories and files in each directory
* *
* @param string $path * @param string $path The Path to read.
* @param = boolean $hidden * @param mixed $hidden Array of files to exclude from the read that will be performed.
* @access private * @access private
*/ */
function __tree($path, $exceptions) { function __tree($path, $hidden) {
if ($this->cd($path)) { if ($this->cd($path)) {
list($dirs, $files) = $this->read(false, $exceptions, true); list($dirs, $files) = $this->read(false, $hidden, true);
$this->__directories = array_merge($this->__directories, $dirs); $this->__directories = array_merge($this->__directories, $dirs);
$this->__files = array_merge($this->__files, $files); $this->__files = array_merge($this->__files, $files);
} }
} }
/** /**
* Create a directory structure recursively. * Create a directory structure recursively. Can be used to create
* deep path structures like `/foo/bar/baz/shoe/horn`
* *
* @param string $pathname The directory structure to create * @param string $pathname The directory structure to create
* @param integer $mode octal value 0755 * @param integer $mode octal value 0755
@ -507,7 +504,7 @@ class Folder extends Object {
} }
/** /**
* Returns the size in bytes of this Folder. * Returns the size in bytes of this Folder and its contents.
* *
* @param string $directory Path to directory * @param string $directory Path to directory
* @return int size in bytes of current folder * @return int size in bytes of current folder
@ -544,7 +541,7 @@ class Folder extends Object {
} }
/** /**
* Recursively Remove directories if system allow. * Recursively Remove directories if the system allows.
* *
* @param string $path Path of directory to delete * @param string $path Path of directory to delete
* @return boolean Success * @return boolean Success
@ -596,8 +593,15 @@ class Folder extends Object {
/** /**
* Recursive directory copy. * Recursive directory copy.
* *
* @param array $options (to, from, chmod, skip) * ### Options
* @return bool *
* - `to` The directory to copy to.
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
* - `chmod` The mode to copy the files/directories with.
* - `skip` Files/directories to skip.
*
* @param mixed $options Either an array of options (see above) or a string of the destination directory.
* @return bool Success
* @access public * @access public
*/ */
function copy($options = array()) { function copy($options = array()) {
@ -675,6 +679,13 @@ class Folder extends Object {
/** /**
* Recursive directory move. * Recursive directory move.
* *
* ### Options
*
* - `to` The directory to copy to.
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
* - `chmod` The mode to copy the files/directories with.
* - `skip` Files/directories to skip.
*
* @param array $options (to, from, chmod, skip) * @param array $options (to, from, chmod, skip)
* @return boolean Success * @return boolean Success
* @access public * @access public

View file

@ -21,7 +21,8 @@ if (!class_exists('File')) {
require LIBS . 'file.php'; require LIBS . 'file.php';
} }
/** /**
* File Storage stream for Logging * File Storage stream for Logging. Writes logs to different files
* based on the type of log it is.
* *
* @package cake * @package cake
* @subpackage cake.cake.libs.log * @subpackage cake.cake.libs.log