Hawk - PHP documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • Hawk
    • View
      • Plugins

Classes

  • Hawk\App
  • Hawk\ButtonInput
  • Hawk\Cache
  • Hawk\CheckboxInput
  • Hawk\ColorInput
  • Hawk\Conf
  • Hawk\Controller
  • Hawk\Crypto
  • Hawk\DatabaseSessionHandler
  • Hawk\DatetimeInput
  • Hawk\DB
  • Hawk\DBExample
  • Hawk\DeleteInput
  • Hawk\Dialogbox
  • Hawk\EmailInput
  • Hawk\ErrorHandler
  • Hawk\Event
  • Hawk\FileInput
  • Hawk\FileSystem
  • Hawk\FloatInput
  • Hawk\Form
  • Hawk\FormFieldset
  • Hawk\FormInput
  • Hawk\GenericModel
  • Hawk\GifImage
  • Hawk\HawkApi
  • Hawk\HawkUpdater
  • Hawk\HiddenInput
  • Hawk\HtmlInput
  • Hawk\HTTPRequest
  • Hawk\Icon
  • Hawk\Image
  • Hawk\IntegerInput
  • Hawk\ItemList
  • Hawk\ItemListField
  • Hawk\JpegImage
  • Hawk\Lang
  • Hawk\Language
  • Hawk\LeftSidebarTab
  • Hawk\Less
  • Hawk\Logger
  • Hawk\Mail
  • Hawk\MenuItem
  • Hawk\Model
  • Hawk\NoSidebarTab
  • Hawk\NumberInput
  • Hawk\ObjectInput
  • Hawk\Option
  • Hawk\Panel
  • Hawk\PasswordInput
  • Hawk\Permission
  • Hawk\Plugin
  • Hawk\PluginInstaller
  • Hawk\PngImage
  • Hawk\ProfileQuestion
  • Hawk\ProfileQuestionValue
  • Hawk\RadioInput
  • Hawk\Request
  • Hawk\Response
  • Hawk\RightSidebarTab
  • Hawk\Role
  • Hawk\RolePermission
  • Hawk\Route
  • Hawk\Router
  • Hawk\SelectInput
  • Hawk\Session
  • Hawk\Singleton
  • Hawk\SubmitInput
  • Hawk\Tabs
  • Hawk\TextareaInput
  • Hawk\TextInput
  • Hawk\Theme
  • Hawk\TimeInput
  • Hawk\Upload
  • Hawk\User
  • Hawk\View
  • Hawk\View\Plugins\Accordion
  • Hawk\View\Plugins\Button
  • Hawk\View\Plugins\Form
  • Hawk\View\Plugins\Icon
  • Hawk\View\Plugins\Import
  • Hawk\View\Plugins\Panel
  • Hawk\View\Plugins\Tabs
  • Hawk\View\Plugins\Text
  • Hawk\View\Plugins\Uri
  • Hawk\View\Plugins\Widget
  • Hawk\ViewPlugin
  • Hawk\Widget
  • Hawk\WysiwygInput

Traits

  • Hawk\Utils

Exceptions

  • Hawk\AppStopException
  • Hawk\DBExampleException
  • Hawk\DBException
  • Hawk\FileSystemException
  • Hawk\HawkApiException
  • Hawk\ImageException
  • Hawk\MailException
  • Hawk\UploadException
  • Hawk\ViewException
  1 <?php
  2 
  3 /**
  4  * FileSystem.php
  5  *
  6  * @author  Elyrra SAS
  7  * @license http://rem.mit-license.org/ MIT
  8  */
  9 
 10 namespace Hawk;
 11 
 12 /**
 13  * This class has utilities to manage the file system, for missing functions in PHP
 14  *
 15  * @package Utils
 16  */
 17 final class FileSystem extends Singleton{
 18     /**
 19      * Directory separator
 20      */
 21     const DS = DIRECTORY_SEPARATOR;
 22 
 23     /**
 24      * Constant to find only files in Filsystem::find
 25      */
 26     const FIND_FILE_ONLY = 'file';
 27 
 28     /**
 29      * Constant to find only directories in Filsystem::find
 30      */
 31     const FIND_DIR_ONLY = 'dir';
 32 
 33     /**
 34      * Constant to find both files and directories in Filsystem::find
 35      */
 36     const FIND_ANY_TYPE = 'any';
 37 
 38     /**
 39      * The filesystem instance
 40      *
 41      * @var FileSysteme
 42      */
 43     protected static $instance;
 44 
 45 
 46     /**
 47      * Get all files in a directory, including those beginning by '.'
 48      *
 49      * @param string $dir The directory to find files into
 50      *
 51      * @return array The list of files matching the pattern
 52      */
 53     public function getAllFilesInDir($dir){
 54         $files = array_merge(glob($dir . self::DS . '*'), glob($dir . self::DS . '.*'));
 55         return array_filter(
 56             $files, function ($file) {
 57                 return basename($file) != '.' && basename($file) != '..';
 58             }
 59         );
 60     }
 61 
 62     /**
 63      * Equivalent to cp -r
 64      *
 65      * @param string $source The source file or directory to copy
 66      * @param string $dest   The destination file or directory
 67      */
 68     public function copy($source, $dest){
 69         if(basename($source) == '*') {
 70             foreach($this->getAllFilesInDir(dirname($source)) as $element){
 71                 $this->copy($element, $dest);
 72             }
 73         }
 74         else{
 75             if(! file_exists($source)) {
 76                 throw new FileSystemException('Cannot copy ' . $source . ' : No such file or directory');
 77             }
 78 
 79             if(is_file($source)) {
 80                 // Copy a file
 81                 if(is_dir($dest)) {
 82                     $dest = $dest . self::DS . basename($source);
 83                 }
 84                 copy($source, $dest);
 85             }
 86             else{
 87                 // Copy a directory
 88                 $base = basename($source);
 89                 if(!is_dir($dest . self::DS . $base)) {
 90                     mkdir($dest . self::DS . $base, fileperms($source), true);
 91                 }
 92 
 93                 // Copy all files and folder under this directory
 94                 foreach(glob($source . self::DS . '*') as $element){
 95                     $this->copy($element, $dest . self::DS . $base);
 96                 }
 97             }
 98         }
 99     }
100 
101 
102     /**
103      * Find files by a pattern
104      *
105      * @param string $source  The directory to search in
106      * @param string $pattern The pattern to find the files
107      * @param string $type    The type of source to find : 'file', 'dir', 'any'
108      *
109      * @return array The list of files or directories found
110      */
111     public function find($source, $pattern, $type = self::FIND_ANY_TYPE){
112         if(!is_dir($source)) {
113             throw new FileSystemException(
114                 'The method ' . __METHOD__ . ' requires the first argument to be an existing directory : ' .
115                 $source . ' is not a directory'
116             );
117         }
118         switch($type){
119             case self::FIND_FILE_ONLY :
120                 $result = array_filter(glob($source . self::DS . $pattern), 'is_file');
121                 break;
122 
123             case self::FIND_DIR_ONLY :
124                 $result = glob($source . self::DS . $pattern, GLOB_ONLYDIR);
125                 break;
126 
127             default :
128                 $result = glob($source . self::DS . $pattern);
129                 break;
130         }
131 
132         $subdirs = glob($source . self::DS . '*', GLOB_ONLYDIR);
133         foreach($subdirs as $dir){
134             $result = array_merge($result, self::find($dir, $pattern, $type));
135         }
136 
137         return $result;
138     }
139 
140 
141     /**
142      * Remove a directory or a file
143      *
144      * @param string $source The file or directory name to remove
145      *
146      * @return boolean, TRUE if the source was removed, else FALSE
147      */
148     public function remove($source){
149         if(basename($source) == '*') {
150             foreach(self::getAllFilesInDir(dirname($source)) as $element){
151                 $this->remove($element);
152             }
153         }
154         else{
155             if(! file_exists($source)) {
156                 throw new FileSystemException('Cannot remove ' . $source . ' : No such file or directory');
157             }
158 
159             if(is_file($source)) {
160                 // remove a file
161                 return unlink($source);
162             }
163             else{
164                 // remove a directory
165                 foreach(self::getAllFilesInDir($source) as $element){
166                     $this->remove($element);
167                 }
168                 return rmdir($source);
169             }
170         }
171     }
172 }
173 
174 
175 /**
176  * This class describes exceptions throwed by FileSystem class
177  *
178  * @package Exceptions
179  */
180 class FileSystemException extends \Exception{
181 }
Hawk - PHP documentation API documentation generated by ApiGen