Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- FINAL CLASS ILLI_Container_Api_Directory EXTENDS ILLI_Container
- {
- /**
- * dir-cache
- *
- * save parsed directories
- *
- * @var array
- */
- private $dircache = array();
- private $sizecache = array();
- const DIRECTORIES_ONLY = '.';
- /**
- * create directories from array
- *
- * @param array array with dir-paths
- * @param bool $exception enable throw-exception
- * @throws ILLI_Container_Api_Directory_Exception when directory does't exists
- * @throws ILLI_Container_Api_Directory_Exception when directory can not create
- */
- public function create(array $stack, $exception = FALSE)
- {
- foreach($stack as $key => $value)
- if(!is_dir($value))
- {
- if($exception)
- {
- throw new ILLI_Container_Api_Directory_Exception
- (
- sprintf
- (
- ILLI_Exception_Interface::ERR_DIR_NOT_FOUND,
- $value
- )
- );
- return FALSE;
- }
- if(!mkdir($value, 755, TRUE))
- {
- throw new ILLI_Container_Api_Directory_Exception
- (
- sprintf
- (
- ILLI_Exception_Interface::ERR_DIR_CAN_NOT_CREATE,
- $value
- )
- );
- return FALSE;
- }
- }
- return $this;
- }
- public function read($path)
- {
- return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
- }
- public function unlinkFilesOf($dirpath, $filetype = NULL)
- {
- $files = array();
- $queue = $this->contentOf($dirpath, $filetype);
- if(is_array($dirpath))
- {
- if(is_array($queue))
- foreach($queue as $p => $dir)
- if(is_array($dir))
- foreach($dir as $f => $file)
- $files[] = $file->getRealPath();
- }
- else
- {
- if(is_array($queue))
- foreach($queue as $f => $file)
- $files[] = $file->getRealPath();
- }
- foreach($files as $trash)
- @unlink($trash);
- }
- public function sizeOf($dirpath, $filetype = NULL, $secure = FALSE)
- {
- $size = 0;
- $queue = $this->contentOf($dirpath, $filetype, $secure);
- if(is_array($dirpath))
- {
- if(is_array($queue))
- foreach($queue as $p => $dir)
- if(is_array($dir))
- foreach($dir as $f => $file)
- $size += $file->getSize();
- }
- else
- {
- if(is_array($queue))
- foreach($queue as $f => $file)
- $size += $file->getSize();
- }
- return $size;
- }
- public function countFiles($dirpath, $filetype = NULL, $secure = FALSE)
- {
- $count = 0;
- $queue = $this->contentOf($dirpath, $filetype, $secure);
- if(is_array($dirpath))
- {
- if(is_array($queue))
- foreach($queue as $p => $dir)
- if(is_array($dir))
- foreach($dir as $f => $file)
- $count++;
- }
- else
- {
- if(is_array($queue))
- foreach($queue as $f => $file)
- $count++;
- }
- return $count;
- }
- /**
- * directory-content as array with SplFileInfo Objects
- *
- * @param $dirpath string path of dir
- * @param $filetype string filter files by extension
- * @param $secure bool will parse mime-type
- * @throws ILLI_Container_Api_Directory_Exception when file/mimetype is not readable
- * @return array SplFileInfo-objects-array filtered by filtype
- */
- public function contentOf($dirpath, $filetype = NULL, $secure = FALSE)
- {
- $result = array();
- if(is_string($dirpath))
- {
- if(!is_dir($dirpath))
- return FALSE;
- if(array_key_exists($dirpath, $this->dircache))
- return $this->dircache[$dirpath];
- foreach(new RecursiveDirectoryIterator($dirpath) as $file_obj)
- {
- if(NULL === $filetype)
- {
- $result[$file_obj->getFilename()] = $file_obj;
- }
- else
- {
- if(is_string($filetype))
- {
- if($filetype == self::DIRECTORIES_ONLY)
- {
- if(is_dir($file_obj->getPathname()))
- $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME))] = $file_obj;
- }
- else
- if(strtolower($filetype) == strtolower(pathinfo($file_obj->getFilename(), PATHINFO_EXTENSION)))
- $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME))] = $file_obj;
- }
- else
- if(is_array($filetype))
- {
- foreach($filetype as $i => $type)
- if(strtolower($type) == strtolower(pathinfo($file_obj->getFilename(), PATHINFO_EXTENSION)))
- $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME))] = $file_obj;
- }
- }
- if($secure)
- {
- if(!function_exists('stream_get_meta_data'))
- throw new ILLI_Container_Api_Directory_Exception('Unable to use function ["stream_get_meta_data"]: not implemented.');
- if(is_dir($dirpath.DIRECTORY_SEPARATOR.$file_obj->getFilename()))
- continue;
- $parts = stream_get_meta_data($hook = fopen($file = $dirpath.DIRECTORY_SEPARATOR.$file_obj->getFilename(), "r"));
- if($hook && $parts)
- {
- fclose($hook);
- $result[strtolower(pathinfo($file_obj->getFilename(), PATHINFO_FILENAME)).'_META'] = $parts;
- }
- else
- {
- throw new ILLI_Container_Api_Directory_Exception
- (
- sprintf
- (
- ILLI_Exception_Interface::ERR_FILE_METADATA_ERROR,
- $file
- )
- );
- }
- }
- }
- $this->dircache[$dirpath] = $result;
- }
- else
- if(is_array($dirpath))
- {
- foreach($dirpath as $path)
- {
- $isset = $this->contentOf($path, $filetype, $secure);
- if(FALSE === $isset)
- continue;
- $result[$path] = $isset;
- }
- }
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement