1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
<?php namespace amekusa\phio; main::required;
use amekusa\plz\path;
/**
* File abstraction
* @author amekusa <post@amekusa.com>
*/
abstract class File {
protected
$path,
$io,
$perms,
$isExclusive;
/**
* Creates a proper object of {@link File} subclass from a path
*
* If the path indicates:
*
* + a directory, creates a {@link Directory} object.
* + a file, creates a {@link RegFile} object.
*
* @example Passing an existing directory path, creates a {@link Directory} object
* ```php
* use amekusa\phio\Directory;
*
* $dir = File::create(__DIR__);
*
* echo 'Is $dir a directory? - ';
* echo $dir instanceof Directory ? 'Yes.' : 'No.';
* ```
* ```php
* Is $dir a directory? - Yes.
* ```
* @example Passing an existing regular file path or non-existent path, creates a {@link RegFile} object
* ```php
* use amekusa\phio\RegFile;
*
* $file = File::create(__FILE__);
*
* echo 'Is $file a regular file? - ';
* echo $file instanceof RegFile ? 'Yes.' : 'No.';
* ```
* ```php
* Is $file a regular file? - Yes.
* ```
* @param string $Path
* @return File
*/
public static function create($Path) {
$r = is_dir($Path) ? new Directory($Path) : new RegFile($Path);
return $r;
}
/**
* Returns a {@link File} instance associated with a specific file path
*
* The operation is the same as {@link File}`::create($Path)`
* except for the returned object is cached in {@link FilePool}.
*
* @example Cache Demonstration
* ```php
* $X1 = File::create(__FILE__); // Not cached
* $Y1 = File::create(__FILE__); // Not cached
*
* $X2 = File::instance(__FILE__); // Not cached
* $Y2 = File::instance(__FILE__); // Cached
*
* $X3 = File::instance(__FILE__, true); // Not cached
* $Y3 = File::instance(__FILE__, true); // Not cached
* $Z3 = File::instance(__FILE__); // Cached
*
* echo 'Are $X1 and $Y1 identical? - ';
* echo $X1 === $Y1 ? 'Yes.' : 'No.';
* echo "\n";
*
* echo 'Are $X2 and $Y2 identical? - ';
* echo $X2 === $Y2 ? 'Yes.' : 'No.';
* echo "\n";
*
* echo 'Are $X3 and $Y3 identical? - ';
* echo $X3 === $Y3 ? 'Yes.' : 'No.';
* echo "\n";
*
* echo 'Are $Y3 and $Z3 identical? - ';
* echo $Y3 === $Z3 ? 'Yes.' : 'No.';
* ```
* ```php
* Are $X1 and $Y1 identical? - No.
* Are $X2 and $Y2 identical? - Yes.
* Are $X3 and $Y3 identical? - No.
* Are $Y3 and $Z3 identical? - Yes.
* ```
* @param string $Path The path of a file
* @param boolean $ForceNew If `true`, always returns a newly created instance of {@link File}
* @return File
*/
public static function instance($Path, $ForceNew = false) {
return FilePool::instance()->get($Path, $ForceNew);
}
/**
* Creates a {@link File} object from a path
* @param string $Path File path
*/
public function __construct($Path) {
$this->path = $Path;
$this->normalizePath();
}
public function __toString() {
return $this->path;
}
public function isExclusive() {
return $this->isExclusive;
}
public function isOpened() {
return isset($this->io);
}
/**
* @return boolean
*/
public function exists() {
return file_exists($this->path);
}
/**
* @return string
*/
public function getPath() {
return $this->path;
}
/**
* @return string
*/
public function getName() {
return basename($this->path);
}
/**
* @return Perms
*/
public function getPerms() {
if (!$this->perms) {
clearstatcache();
$mode = @fileperms($this->path);
$this->perms = $mode ? new Perms(decoct($mode & 0777)) : new Perms();
}
return $this->perms;
}
/**
* @param string $Format
* @return integer|string
*/
public function modifiedAt($Format = '') {
$time = filemtime($this->path);
return $Format ? date($Format, $time) : $time;
}
/**
* @param boolean $IsExclusive
* @return File
*/
public function beExclusive($IsExclusive = true) {
$this->isExclusive = $IsExclusive;
return $this;
}
protected function normalizePath() {
$this->path = rtrim($this->path, '/');
}
public function open() {
if ($this->isOpened()) {
// TODO Warn or throw exception
return false;
}
$this->_open();
if ($this->isExclusive()) flock($this->io, LOCK_EX);
return true;
}
protected abstract function _open();
public function close() {
if (!$this->isOpened()) {
// TODO Warn or throw exception
return false;
}
if ($this->isExclusive()) flock($this->io, LOCK_UN);
$this->_close();
$this->io = null;
return true;
}
protected abstract function _close();
/**
* Moves this file to another directory
* @param string|Directory $Destination The directory that this file moves to
* @throws IOException on failure
*/
public function moveTo($Destination) {
$newPath = path::join_with(DIRECTORY_SEPARATOR, $Destination, $this->getName());
if (!rename($this->path, $newPath))
throw IOException::create("Failed to rename the file: {$this}")->setIOFile($this);
$this->path = $newPath;
}
public function remove() {
return unlink($this->path);
}
}