[lux commit] r120 - trunk/Lux

0 views
Skip to first unread message

codesite...@google.com

unread,
Mar 4, 2008, 2:05:29 PM3/4/08
to lux...@googlegroups.com
Author: rodrigo.moraes
Date: Tue Mar 4 11:04:32 2008
New Revision: 120

Modified:
trunk/Lux/Filesystem.php

Log:
* Refactored copyR(), and renamed it to copyDir(). Added option to
ignore files.
* Added config keys for default chmod values. Still neeed to work on
the copy methods, because they're ugly.

Modified: trunk/Lux/Filesystem.php
==============================================================================
--- trunk/Lux/Filesystem.php (original)
+++ trunk/Lux/Filesystem.php Tue Mar 4 11:04:32 2008
@@ -1,14 +1,12 @@
<?php
/**
*
- * Handy filesystem manipulation utilities.
+ * Filesystem manipulation utilities.
*
* @category Lux
*
* @package Lux_Filesystem
*
- * @author Clay Loveless <cl...@killersoft.com>
- *
* @author Rodrigo Moraes <rodrigo...@gmail.com>
*
* @license http://opensource.org/licenses/bsd-license.php BSD
@@ -16,33 +14,18 @@
* @version $Id$
*
*/
-
-/**
- *
- * Handy filesystem manipulation utilities.
- *
- * @category Lux
- *
- * @package Lux_Filesystem
- *
- */
-class Lux_Filesystem extends Solar_Base {
-
+class Lux_Filesystem extends Solar_Base
+{
/**
*
- * User-provided configuration.
- *
- * Keys are ...
- *
- * `suppress_warnings`
- * : (bool) Whether or not to suppress warnings during filesystem
- * operations.
+ * User-provided configuration values.
*
* @var array
*
*/
protected $_Lux_Filesystem = array(
- 'suppress_warnings' => true,
+ 'chmod_dir' => 0755,
+ 'chmod_file' => 0644,
);

/**
@@ -53,38 +36,40 @@
*
* @param string $to To directory path.
*
- * @param RecursiveDirectoryIterator $iter.
+ * @param string $ignore Regular expression to ignore files or directories.
*
*/
- public function copyR($from, $to, $iter = null)
+ public function copyDir($from, $to, $ignore = null)
{
+ if ($ignore && preg_match($ignore, $from)) {
+ // Ignore the file or dir.
+ return;
+ }
+
if (is_file($from)) {
- return $this->copyFile($from, $to);
- } elseif (!is_dir($to)) {
- $this->createDir($to);
+ // Single file copy.
+ $this->copyFile($from, $to);
+ return;
}

- if(is_null($iter)) {
- $iter = new RecursiveDirectoryIterator($from);
+ if (! file_exists($to)) {
+ // Create destination dir.
+ $this->createDir($to);
}

- for ($iter->rewind(); $iter->valid(); $iter->next()) {
- $file = basename($iter->current()->getPathname());
+ $iter = new DirectoryIterator($from);

- if ($iter->isDot()) {
- // Skip dot-files.
+ foreach($iter as $info) {
+ // Ignore dots and links.
+ if ($info->isDot() || (!$info->isFile()
&& !$info->isDir())) {
continue;
- } else {
- $new_from = $from . DIRECTORY_SEPARATOR . $file;
- $new_to = $to . DIRECTORY_SEPARATOR . $file;
- $new_iter = ($iter->isDir() && $iter->hasChildren())
- ? $iter->getChildren()
- : null;
-
- if ($to !== $new_from) {
- $this->copyR($new_from, $new_to, $new_iter);
- }
}
+
+ // Increment the destination with the file/dir name.
+ $new_to = $to . DIRECTORY_SEPARATOR . basename($info->getPathname());
+
+ // Copy recursivelly.
+ $this->copyDir($info->getPathname(), $new_to, $ignore);
}
}

@@ -99,18 +84,28 @@
* @return bool True on success.
*
*/
- public function createDir($path, $chmod = null) {
+ public function createDir($path, $chmod = null)
+ {
+ if (! $chmod) {
+ $chmod = $this->_config['chmod_dir'];
+ }
+
+ $base_path = dirname($path);
+
+ if (! is_writable($base_path)) {
+ throw $this->_exception('ERR_DIRECTORY_NOT_WRITABLE', array(
+ 'path' => $base_path,
+ ));
+ }
+
$res = false;
- if (!is_dir($path)) {
+ if (! file_exists($path)) {
$umask = umask(0000);
if (mkdir($path)) {
$res = true;
}
umask($umask);
-
- if($chmod) {
- chmod($path, $chmod);
- }
+ chmod($path, $chmod);
}
return $res;
}
@@ -126,14 +121,20 @@
* @return bool True on success.
*
*/
- public function copyFile($from, $to) {
+ public function copyFile($from, $to, $chmod = null)
+ {
+ if (! $chmod) {
+ $chmod = $this->_config['chmod_file'];
+ }
+
$res = false;
if (is_file($from)) {
- $oldumask = umask(0000);
+ $umask = umask(0000);
if (copy($from, $to)) {
$res = true;
}
- umask($oldumask);
+ umask($umask);
+ chmod($to, $chmod);
}
return $res;
}

Reply all
Reply to author
Forward
0 new messages