All pastes #901450 Raw Edit

Miscellany

public php v1 · immutable
·published 2008-02-12 17:46 UTC
rendered paste body
<?	class Spelserver_Directory implements Iterator {		private $_dir;		private $_workingDir = '';		private $_dirName = '';		private $_tmpFile = null;		private $_ignoreFiles = array();		public function __construct($dir = '', $ignore = array(), $workingDir = '') {			if (is_string($dir) && !empty($dir))				$this->_openDir($dir, $workingDir);			if (is_array($ignore))				$this->setIgnore($ignore);		}		public function __destruct() {			$this->_closeDir();		}		public function __toString() {			return $this->_dirName;		}		public function setIgnore($ignore) {			if (!is_array($ignore))				throw new Spelserver_Directory_Exception('Ignored files have to be an array');			$this->_ignoreFiles = $ignore;		}		public function getIgnore() {			return $this->_ignoreFiles;		}		public function getCwd() {			return $this->_workingDir;		}		private function _openDir($dir, $workingDir) {			if (!empty($workingDir))				$path = "$workingDir/$dir";			else				$path = $dir;			$this->_workingDir = $path;			if (($this->_dir = @opendir($path)) === false) {				require_once 'Spelserver/Directory/Exception.php';				throw new Spelserver_Directory_Exception('Couldn\'t read directory "' . $path . '"');			}			$this->_dirName = $dir;		}		private function _closeDir() {			if (is_resource($this->_dir))				closedir($this->_dir);		}		public function rewind() {			if (is_resource($this->_dir))				rewinddir($this->_dir);		}		public function current() {			return (is_null($this->_tmpFile) ? $this->next() : $this->_tmpFile);		}		public function key() {			return 0;		}		public function next() {			$this->_tmpFile = readdir($this->_dir);			if (in_array($this->_tmpFile, $this->_ignoreFiles))				$this->_tmpFile = $this->next();			if (	$this->_tmpFile !== false &&						is_dir($this->_workingDir . '/' . $this->_tmpFile) &&						($this->_tmpFile != '.' && $this->_tmpFile != '..')) {				return new Spelserver_Directory($this->_tmpFile, $this->_ignoreFiles, $this->_workingDir);			} else {				return $this->_tmpFile;			}		}		public function valid() {			if (!is_resource($this->_dir))				return false;			elseif ($this->_tmpFile === false)				return false;			return true;		}	}?>