updating file class deprecated getters, fixes for tickets #1838 and #2641, adding tests, changing Folder default directory to TMP

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5203 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-05-27 00:30:43 +00:00
parent 2f6d2d313a
commit ae652431e9
3 changed files with 185 additions and 45 deletions

View file

@ -65,10 +65,11 @@ class File extends Object{
*/ */
function __construct($path, $create = false, $mode = 0755) { function __construct($path, $create = false, $mode = 0755) {
parent::__construct(); parent::__construct();
$this->Folder = new Folder(dirname($path), $create, $mode); $this->Folder =& new Folder(dirname($path), $create, $mode);
$this->name = basename($path); $this->name = basename($path);
if (!$this->exists()) { if (!$this->exists()) {
if ($create === true) { if ($create === true) {
$this->safe();
if (!$this->create()) { if (!$this->create()) {
return false; return false;
} }
@ -84,7 +85,7 @@ class File extends Object{
* @access public * @access public
*/ */
function read() { function read() {
$contents = file_get_contents($this->getFullPath()); $contents = file_get_contents($this->pwd());
return $contents; return $contents;
} }
/** /**
@ -106,13 +107,13 @@ class File extends Object{
* @access public * @access public
*/ */
function write($data, $mode = 'w') { function write($data, $mode = 'w') {
$file = $this->getFullPath(); $file = $this->pwd();
if (!($handle = fopen($file, $mode))) { if (!($handle = fopen($file, $mode))) {
trigger_error(sprintf(__("[File] Could not open %s with mode %s!", true), $file, $mode), E_USER_WARNING); trigger_error(sprintf(__("[File] Could not open %s with mode %s!", true), $file, $mode), E_USER_WARNING);
return false; return false;
} }
if (!fwrite($handle, $data)) { if (false === fwrite($handle, $data)) {
return false; return false;
} }
@ -121,6 +122,22 @@ class File extends Object{
} }
return true; return true;
} }
/**
* makes filename safe for saving
*
* @param string $name the name of the file to make safe if different from $this->name
* @return string $ext the extension of the file
* @access public
*/
function safe($name = null, $ext = null) {
if(!$name) {
$name = $this->name;
}
if(!$ext) {
$ext = $this->ext();
}
return preg_replace( "/[^\w\.-]+/", "_", basename($name, $ext));
}
/** /**
* Get md5 Checksum of file with previous check of Filesize * Get md5 Checksum of file with previous check of Filesize
* *
@ -128,10 +145,10 @@ class File extends Object{
* @return string md5 Checksum {@link http://php.net/md5_file See md5_file()} * @return string md5 Checksum {@link http://php.net/md5_file See md5_file()}
* @access public * @access public
*/ */
function getMd5($force = false) { function md5($force = false) {
$md5 = ''; $md5 = '';
if ($force == true || $this->getSize(false) < MAX_MD5SIZE) { if ($force == true || $this->size(false) < MAX_MD5SIZE) {
$md5 = md5_file($this->getFullPath()); $md5 = md5_file($this->pwd());
} }
return $md5; return $md5;
} }
@ -142,8 +159,8 @@ class File extends Object{
* @return string|int filesize as int or as a human-readable string * @return string|int filesize as int or as a human-readable string
* @access public * @access public
*/ */
function getSize() { function size() {
$size = filesize($this->getFullPath()); $size = filesize($this->pwd());
return $size; return $size;
} }
/** /**
@ -152,9 +169,9 @@ class File extends Object{
* @return string The File extension * @return string The File extension
* @access public * @access public
*/ */
function getExt() { function ext() {
$ext = ''; $ext = '';
$parts = explode('.', $this->getName()); $parts = explode('.', $this->name);
if (count($parts) > 1) { if (count($parts) > 1) {
$ext = array_pop($parts); $ext = array_pop($parts);
@ -163,22 +180,13 @@ class File extends Object{
} }
return $ext; return $ext;
} }
/**
* Returns the filename.
*
* @return string The Filename
* @access public
*/
function getName() {
return $this->name;
}
/** /**
* Returns the File's owner. * Returns the File's owner.
* *
* @return int the Fileowner * @return int the Fileowner
*/ */
function getOwner() { function owner() {
$fileowner = fileowner($this->getFullPath()); $fileowner = fileowner($this->pwd());
return $fileowner; return $fileowner;
} }
/** /**
@ -187,8 +195,8 @@ class File extends Object{
* @return int the Filegroup * @return int the Filegroup
* @access public * @access public
*/ */
function getGroup() { function group() {
$filegroup = filegroup($this->getFullPath()); $filegroup = filegroup($this->pwd());
return $filegroup; return $filegroup;
} }
/** /**
@ -201,14 +209,14 @@ class File extends Object{
$dir = $this->Folder->pwd(); $dir = $this->Folder->pwd();
if (file_exists($dir) && is_dir($dir) && is_writable($dir) && !$this->exists()) { if (file_exists($dir) && is_dir($dir) && is_writable($dir) && !$this->exists()) {
if (!touch($this->getFullPath())) { if (!touch($this->pwd())) {
print (sprintf(__('[File] Could not create %s', true), $this->getName())); print (sprintf(__('[File] Could not create %s', true), $this->name));
return false; return false;
} else { } else {
return true; return true;
} }
} else { } else {
print (sprintf(__('[File] Could not create %s', true), $this->getName())); print (sprintf(__('[File] Could not create %s', true), $this->name));
return false; return false;
} }
} }
@ -219,7 +227,7 @@ class File extends Object{
* @access public * @access public
*/ */
function exists() { function exists() {
$exists = file_exists($this->getFullPath()); $exists = (file_exists($this->pwd()) && is_file($this->pwd()));
return $exists; return $exists;
} }
/** /**
@ -229,7 +237,7 @@ class File extends Object{
* @access public * @access public
*/ */
function delete() { function delete() {
$unlink = unlink($this->getFullPath()); $unlink = unlink($this->pwd());
return $unlink; return $unlink;
} }
/** /**
@ -239,7 +247,7 @@ class File extends Object{
* @access public * @access public
*/ */
function writable() { function writable() {
$writable = is_writable($this->getFullPath()); $writable = is_writable($this->pwd());
return $writable; return $writable;
} }
/** /**
@ -249,7 +257,7 @@ class File extends Object{
* @access public * @access public
*/ */
function executable() { function executable() {
$executable = is_executable($this->getFullPath()); $executable = is_executable($this->pwd());
return $executable; return $executable;
} }
/** /**
@ -259,7 +267,7 @@ class File extends Object{
* @access public * @access public
*/ */
function readable() { function readable() {
$readable = is_readable($this->getFullPath()); $readable = is_readable($this->pwd());
return $readable; return $readable;
} }
/** /**
@ -269,7 +277,7 @@ class File extends Object{
* @access public * @access public
*/ */
function lastAccess() { function lastAccess() {
$fileatime = fileatime($this->getFullPath()); $fileatime = fileatime($this->pwd());
return $fileatime; return $fileatime;
} }
/** /**
@ -279,7 +287,7 @@ class File extends Object{
* @access public * @access public
*/ */
function lastChange() { function lastChange() {
$filemtime = filemtime($this->getFullPath()); $filemtime = filemtime($this->pwd());
return $filemtime; return $filemtime;
} }
/** /**
@ -288,7 +296,7 @@ class File extends Object{
* @return Folder Current folder * @return Folder Current folder
* @access public * @access public
*/ */
function getFolder() { function &Folder() {
return $this->Folder; return $this->Folder;
} }
/** /**
@ -297,18 +305,83 @@ class File extends Object{
* @return string Permissions for the file * @return string Permissions for the file
* @access public * @access public
*/ */
function getChmod() { function perms() {
$substr = substr(sprintf('%o', fileperms($this->getFullPath())), -4); $substr = substr(sprintf('%o', fileperms($this->pwd())), -4);
return $substr; return $substr;
} }
/** /**
* Returns the full path of the File. * Returns the full path of the File.
* *
* @return string Full path to file * @return string Full path to file
* @access public * @access public
*/
function pwd() {
return $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
}
/* Deprecated methods */
/**
* @deprecated
* @see File::pwd
*/ */
function getFullPath() { function getFullPath() {
return $this->Folder->slashTerm($this->Folder->pwd()) . $this->getName(); return $this->pwd();
}
/**
* @deprecated
* @see File::name
*/
function getName() {
return $this->name;
}
/**
* @deprecated
* @see File::ext()
*/
function getExt() {
return $this->ext();
}
/**
* @deprecated
* @see File::group()
*/
function getMd5() {
return $this->md5();
}
/**
* @deprecated
* @see File::size()
*/
function getSize() {
return $this->size();
}
/**
* @deprecated
* @see File::owner()
*/
function getOwner() {
return $this->owner();
}
/**
* @deprecated
* @see File::group()
*/
function getGroup() {
return $this->group();
}
/**
* @deprecated
* @see File::perms()
*/
function getChmod() {
return $this->perms();
}
/**
* @deprecated
* @see File::Folder()
*/
function getFolder() {
return $this->Folder();
} }
} }
?> ?>

View file

@ -85,7 +85,7 @@ class Folder extends Object{
function __construct($path = false, $create = false, $mode = false) { function __construct($path = false, $create = false, $mode = false) {
parent::__construct(); parent::__construct();
if (empty($path)) { if (empty($path)) {
$path = getcwd(); $path = TMP;
} }
if($mode) { if($mode) {

View file

@ -36,5 +36,72 @@
*/ */
class FileTest extends UnitTestCase { class FileTest extends UnitTestCase {
var $File;
function testBasic() {
$file = dirname(__FILE__) . DS . basename(__FILE__);
$this->File =& new File($file);
$result = $this->File->pwd();
$expecting = $file;
$this->assertEqual($result, $expecting);
$result = $this->File->name;
$expecting = basename(__FILE__);
$this->assertEqual($result, $expecting);
$result = $this->File->ext();
$expecting = 'php';
$this->assertEqual($result, $expecting);
$result = $this->File->md5();
$expecting = md5_file($file);
$this->assertEqual($result, $expecting);
$result = $this->File->size();
$expecting = filesize($file);
$this->assertEqual($result, $expecting);
$result = $this->File->owner();
$expecting = fileowner($file);
$this->assertEqual($result, $expecting);
$result = $this->File->group();
$expecting = filegroup($file);
$this->assertEqual($result, $expecting);
$result = $this->File->perms();
$expecting = '0644';
$this->assertEqual($result, $expecting);
$result = $this->File->Folder();
$this->assertIsA($result, 'Folder');
}
function testOperations() {
$new = TMP . 'test_file_new.php';
$this->File =& new File($new, true);
$data = 'hello';
$result = $this->File->write($data);
$this->assertTrue($result);
$result = $this->File->append($data);
$this->assertTrue($result);
$result = $this->File->read();
$expecting = 'hellohello';
$this->assertEqual($result, $expecting);
$result = $this->File->write('');
$this->assertTrue($result);
$result = $this->File->delete($new);
$this->assertTrue($result);
}
} }
?> ?>