PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/file.php

http://github.com/ericbarnes/Status-Board
PHP | 298 lines | 114 code | 36 blank | 148 comment | 12 complexity | 9faa5f6cf853aa0cc6a0839216e52cd5 MD5 | raw file
Possible License(s): MIT
  1. <?php namespace Laravel; use Closure, FilesystemIterator as fIterator;
  2. class File {
  3. /**
  4. * Determine if a file exists.
  5. *
  6. * @param string $path
  7. * @return bool
  8. */
  9. public static function exists($path)
  10. {
  11. return file_exists($path);
  12. }
  13. /**
  14. * Get the contents of a file.
  15. *
  16. * <code>
  17. * // Get the contents of a file
  18. * $contents = File::get(path('app').'routes'.EXT);
  19. *
  20. * // Get the contents of a file or return a default value if it doesn't exist
  21. * $contents = File::get(path('app').'routes'.EXT, 'Default Value');
  22. * </code>
  23. *
  24. * @param string $path
  25. * @param mixed $default
  26. * @return string
  27. */
  28. public static function get($path, $default = null)
  29. {
  30. return (file_exists($path)) ? file_get_contents($path) : value($default);
  31. }
  32. /**
  33. * Write to a file.
  34. *
  35. * @param string $path
  36. * @param string $data
  37. * @return int
  38. */
  39. public static function put($path, $data)
  40. {
  41. return file_put_contents($path, $data, LOCK_EX);
  42. }
  43. /**
  44. * Append to a file.
  45. *
  46. * @param string $path
  47. * @param string $data
  48. * @return int
  49. */
  50. public static function append($path, $data)
  51. {
  52. return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
  53. }
  54. /**
  55. * Delete a file.
  56. *
  57. * @param string $path
  58. * @return void
  59. */
  60. public static function delete($path)
  61. {
  62. if (static::exists($path)) @unlink($path);
  63. }
  64. /**
  65. * Extract the file extension from a file path.
  66. *
  67. * @param string $path
  68. * @return string
  69. */
  70. public static function extension($path)
  71. {
  72. return pathinfo($path, PATHINFO_EXTENSION);
  73. }
  74. /**
  75. * Get the file type of a given file.
  76. *
  77. * @param string $path
  78. * @return string
  79. */
  80. public static function type($path)
  81. {
  82. return filetype($path);
  83. }
  84. /**
  85. * Get the file size of a given file.
  86. *
  87. * @param string $path
  88. * @return int
  89. */
  90. public static function size($path)
  91. {
  92. return filesize($path);
  93. }
  94. /**
  95. * Get the file's last modification time.
  96. *
  97. * @param string $path
  98. * @return int
  99. */
  100. public static function modified($path)
  101. {
  102. return filemtime($path);
  103. }
  104. /**
  105. * Get a file MIME type by extension.
  106. *
  107. * <code>
  108. * // Determine the MIME type for the .tar extension
  109. * $mime = File::mime('tar');
  110. *
  111. * // Return a default value if the MIME can't be determined
  112. * $mime = File::mime('ext', 'application/octet-stream');
  113. * </code>
  114. *
  115. * @param string $extension
  116. * @param string $default
  117. * @return string
  118. */
  119. public static function mime($extension, $default = 'application/octet-stream')
  120. {
  121. $mimes = Config::get('mimes');
  122. if ( ! array_key_exists($extension, $mimes)) return $default;
  123. return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  124. }
  125. /**
  126. * Determine if a file is a given type.
  127. *
  128. * The Fileinfo PHP extension is used to determine the file's MIME type.
  129. *
  130. * <code>
  131. * // Determine if a file is a JPG image
  132. * $jpg = File::is('jpg', 'path/to/file.jpg');
  133. *
  134. * // Determine if a file is one of a given list of types
  135. * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/file');
  136. * </code>
  137. *
  138. * @param array|string $extensions
  139. * @param string $path
  140. * @return bool
  141. */
  142. public static function is($extensions, $path)
  143. {
  144. $mimes = Config::get('mimes');
  145. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  146. // The MIME configuration file contains an array of file extensions and
  147. // their associated MIME types. We will spin through each extension the
  148. // developer wants to check and look for the MIME type.
  149. foreach ((array) $extensions as $extension)
  150. {
  151. if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
  152. {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. /**
  159. * Move a directory from one location to another.
  160. *
  161. * @param string $source
  162. * @param string $destination
  163. * @param int $options
  164. * @return void
  165. */
  166. public static function mvdir($source, $destination, $options = fIterator::SKIP_DOTS)
  167. {
  168. static::cpdir($source, $destination, true, $options);
  169. }
  170. /**
  171. * Recursively copy directory contents to another directory.
  172. *
  173. * @param string $source
  174. * @param string $destination
  175. * @param bool $delete
  176. * @param int $options
  177. * @return void
  178. */
  179. public static function cpdir($source, $destination, $delete = false, $options = fIterator::SKIP_DOTS)
  180. {
  181. if ( ! is_dir($source)) return;
  182. // First we need to create the destination directory if it doesn't
  183. // already exists. This directory hosts all of the assets we copy
  184. // from the installed bundle's source directory.
  185. if ( ! is_dir($destination))
  186. {
  187. mkdir($destination, 0777, true);
  188. }
  189. $items = new fIterator($source, $options);
  190. foreach ($items as $item)
  191. {
  192. $location = $destination.DS.$item->getBasename();
  193. // If the file system item is a directory, we will recurse the
  194. // function, passing in the item directory. To get the proper
  195. // destination path, we'll add the basename of the source to
  196. // to the destination directory.
  197. if ($item->isDir())
  198. {
  199. $path = $item->getRealPath();
  200. static::cpdir($path, $location, $delete, $options);
  201. if ($delete) @rmdir($item->getRealPath());
  202. }
  203. // If the file system item is an actual file, we can copy the
  204. // file from the bundle asset directory to the public asset
  205. // directory. The "copy" method will overwrite any existing
  206. // files with the same name.
  207. else
  208. {
  209. copy($item->getRealPath(), $location);
  210. if ($delete) @unlink($item->getRealPath());
  211. }
  212. }
  213. if ($delete) rmdir($source);
  214. }
  215. /**
  216. * Recursively delete a directory.
  217. *
  218. * @param string $directory
  219. * @return void
  220. */
  221. public static function rmdir($directory)
  222. {
  223. if ( ! is_dir($directory)) return;
  224. $items = new fIterator($directory);
  225. foreach ($items as $item)
  226. {
  227. // If the item is a directory, we can just recurse into the
  228. // function and delete that sub-directory, otherwise we'll
  229. // just deleete the file and keep going!
  230. if ($item->isDir())
  231. {
  232. static::rmdir($item->getRealPath());
  233. }
  234. else
  235. {
  236. @unlink($item->getRealPath());
  237. }
  238. }
  239. @rmdir($directory);
  240. }
  241. /**
  242. * Get the most recently modified file in a directory.
  243. *
  244. * @param string $directory
  245. * @param int $options
  246. * @return SplFileInfo
  247. */
  248. public static function latest($directory, $options = fIterator::SKIP_DOTS)
  249. {
  250. $time = 0;
  251. $items = new fIterator($directory, $options);
  252. // To get the latest created file, we'll simply spin through the
  253. // directory, setting the latest file if we encounter a file
  254. // with a UNIX timestamp greater than the latest one we
  255. // have encountered thus far in the loop.
  256. foreach ($items as $item)
  257. {
  258. if ($item->getMTime() > $time) $latest = $item;
  259. }
  260. return $latest;
  261. }
  262. }