123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952 |
- <?php
- include_once 'phing/system/io/FileSystem.php';
- include_once 'phing/system/lang/NullPointerException.php';
- class PhingFile {
-
- public static $separator;
-
- public static $pathSeparator;
-
-
- private $path = null;
-
- private $prefixLength = 0;
-
- function __construct($arg1 = null, $arg2 = null) {
-
- if (self::$separator === null || self::$pathSeparator === null) {
- $fs = FileSystem::getFileSystem();
- self::$separator = $fs->getSeparator();
- self::$pathSeparator = $fs->getPathSeparator();
- }
-
- if ($arg1 instanceof PhingFile && is_string($arg2)) {
- $this->_constructFileParentStringChild($arg1, $arg2);
- } elseif (is_string($arg1) && ($arg2 === null)) {
- $this->_constructPathname($arg1);
- } elseif(is_string($arg1) && is_string($arg2)) {
- $this->_constructStringParentStringChild($arg1, $arg2);
- } else {
- if ($arg1 === null) {
- throw new NullPointerException("Argument1 to function must not be null");
- }
- $this->path = (string) $arg1;
- $this->prefixLength = (int) $arg2;
- }
- }
-
- function getPrefixLength() {
- return (int) $this->prefixLength;
- }
-
-
- function _constructPathname($pathname) {
-
- $fs = FileSystem::getFileSystem();
- if ($pathname === null) {
- throw new NullPointerException("Argument to function must not be null");
- }
- $this->path = (string) $fs->normalize($pathname);
- $this->prefixLength = (int) $fs->prefixLength($this->path);
- }
- function _constructStringParentStringChild($parent, $child = null) {
-
- $fs = FileSystem::getFileSystem();
- if ($child === null) {
- throw new NullPointerException("Argument to function must not be null");
- }
- if ($parent !== null) {
- if ($parent === "") {
- $this->path = $fs->resolve($fs->getDefaultParent(), $fs->normalize($child));
- } else {
- $this->path = $fs->resolve($fs->normalize($parent), $fs->normalize($child));
- }
- } else {
- $this->path = (string) $fs->normalize($child);
- }
- $this->prefixLength = (int) $fs->prefixLength($this->path);
- }
- function _constructFileParentStringChild($parent, $child = null) {
-
- $fs = FileSystem::getFileSystem();
- if ($child === null) {
- throw new NullPointerException("Argument to function must not be null");
- }
- if ($parent !== null) {
- if ($parent->path === "") {
- $this->path = $fs->resolve($fs->getDefaultParent(), $fs->normalize($child));
- } else {
- $this->path = $fs->resolve($parent->path, $fs->normalize($child));
- }
- } else {
- $this->path = $fs->normalize($child);
- }
- $this->prefixLength = $fs->prefixLength($this->path);
- }
-
-
- function getName() {
-
- $index = ((($res = strrpos($this->path, self::$separator)) === false) ? -1 : $res);
- if ($index < $this->prefixLength) {
- return substr($this->path, $this->prefixLength);
- }
- return substr($this->path, $index + 1);
- }
-
- function getParent() {
-
- $index = ((($res = strrpos($this->path, self::$separator)) === false) ? -1 : $res);
- if ($index < $this->prefixLength) {
- if (($this->prefixLength > 0) && (strlen($this->path) > $this->prefixLength)) {
- return substr($this->path, 0, $this->prefixLength);
- }
- return null;
- }
- return substr($this->path, 0, $index);
- }
-
- function getParentFile() {
- $p = $this->getParent();
- if ($p === null) {
- return null;
- }
- return new PhingFile((string) $p, (int) $this->prefixLength);
- }
-
- function getPath() {
- return (string) $this->path;
- }
-
- function getPathWithoutBase($basedir)
- {
- if (!StringHelper::endsWith(self::$separator, $basedir)) {
- $basedir .= self::$separator;
- }
- $path = $this->getPath();
- if (!substr($path, 0, strlen($basedir)) == $basedir) {
-
- return $path;
- }
- return substr($path, strlen($basedir));
- }
-
- function isAbsolute() {
- return ($this->prefixLength !== 0);
- }
-
- function getAbsolutePath() {
- $fs = FileSystem::getFileSystem();
- return $fs->resolveFile($this);
- }
-
- function getAbsoluteFile() {
- return new PhingFile((string) $this->getAbsolutePath());
- }
-
- function getCanonicalPath() {
- $fs = FileSystem::getFileSystem();
- return $fs->canonicalize($this->path);
- }
-
- function getCanonicalFile() {
- return new PhingFile($this->getCanonicalPath());
- }
-
- function toURL() {
-
- }
-
- function toURI() {
-
- }
- function _slashify($path, $isDirectory) {
- $p = (string) $path;
- if (self::$separator !== '/') {
- $p = str_replace(self::$separator, '/', $p);
- }
- if (!StringHelper::startsWith('/', $p)) {
- $p = '/'.$p;
- }
- if (!StringHelper::endsWith('/', $p) && $isDirectory) {
- $p = $p.'/';
- }
- return $p;
- }
-
-
- function canRead() {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this)) {
- return (boolean) @is_readable($this->getAbsolutePath());
- }
- return false;
- }
-
- function canWrite() {
- $fs = FileSystem::getFileSystem();
- return $fs->checkAccess($this, true);
- }
-
- function exists() {
- clearstatcache();
-
- if (is_link($this->path)) {
- return true;
- } else if ($this->isFile()) {
- return @file_exists($this->path) || is_link($this->path);
- } else {
- return @is_dir($this->path);
- }
- }
-
- function isDirectory() {
- clearstatcache();
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this) !== true) {
- throw new IOException("No read access to ".$this->path);
- }
- return @is_dir($this->path) && !@is_link($this->path);
- }
-
- function isFile() {
- clearstatcache();
-
- return @is_file($this->path);
- }
-
- function isHidden() {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this) !== true) {
- throw new IOException("No read access to ".$this->path);
- }
- return (($fs->getBooleanAttributes($this) & $fs->BA_HIDDEN) !== 0);
- }
-
-
- public function isLink()
- {
- clearstatcache();
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this) !== true) {
- throw new IOException("No read access to ".$this->path);
- }
- return @is_link($this->path);
- }
-
- public function getLinkTarget()
- {
- return @readlink($this->path);
- }
-
- function lastModified() {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this) !== true) {
- throw new IOException("No read access to " . $this->path);
- }
- return $fs->getLastModifiedTime($this);
- }
-
- function length() {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this) !== true) {
- throw new IOException("No read access to ".$this->path."\n");
- }
- return $fs->getLength($this);
- }
-
- function contents() {
- if (!$this->canRead() || !$this->isFile()) {
- throw new IOException("Cannot read file contents!");
- }
- return file_get_contents($this->getAbsolutePath());
- }
-
-
-
- function createNewFile($parents=true, $mode=0777) {
- $file = FileSystem::getFileSystem()->createNewFile($this->path);
- return $file;
- }
-
- function delete($recursive = false) {
- $fs = FileSystem::getFileSystem();
- if ($fs->canDelete($this) !== true) {
- throw new IOException("Cannot delete " . $this->path . "\n");
- }
- return $fs->delete($this, $recursive);
- }
-
- function deleteOnExit() {
- $fs = FileSystem::getFileSystem();
- $fs->deleteOnExit($this);
- }
-
- function listDir($filter = null) {
- $fs = FileSystem::getFileSystem();
- return $fs->lister($this, $filter);
- }
- function listFiles($filter = null) {
- $ss = $this->listDir($filter);
- if ($ss === null) {
- return null;
- }
- $n = count($ss);
- $fs = array();
- for ($i = 0; $i < $n; $i++) {
- $fs[$i] = new PhingFile((string)$this->path, (string)$ss[$i]);
- }
- return $fs;
- }
-
- function mkdirs($mode = 0755) {
- if ($this->exists()) {
- return false;
- }
- try {
- if ($this->mkdir($mode)) {
- return true;
- }
- } catch (IOException $ioe) {
-
- }
- $parentFile = $this->getParentFile();
- return (($parentFile !== null) && ($parentFile->mkdirs($mode) && $this->mkdir($mode)));
- }
-
- function mkdir($mode = 0755) {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess(new PhingFile($this->path), true) !== true) {
- throw new IOException("No write access to " . $this->getPath());
- }
- return $fs->createDirectory($this, $mode);
- }
-
- function renameTo(PhingFile $destFile) {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this) !== true) {
- throw new IOException("No write access to ".$this->getPath());
- }
- return $fs->rename($this, $destFile);
- }
-
- function copyTo(PhingFile $destFile) {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this) !== true) {
- throw new IOException("No read access to ".$this->getPath()."\n");
- }
- if ($fs->checkAccess($destFile, true) !== true) {
- throw new IOException("File::copyTo() No write access to ".$destFile->getPath());
- }
- return $fs->copy($this, $destFile);
- }
-
- function setLastModified($time) {
- $time = (int) $time;
- if ($time < 0) {
- throw new Exception("IllegalArgumentException, Negative $time\n");
- }
- $fs = FileSystem::getFileSystem();
- return $fs->setLastModifiedTime($this, $time);
- }
-
- function setReadOnly() {
- $fs = FileSystem::getFileSystem();
- if ($fs->checkAccess($this, true) !== true) {
-
- throw new IOException("No write access to " . $this->getPath());
- }
- return $fs->setReadOnly($this);
- }
-
- public function setUser($user) {
- $fs = FileSystem::getFileSystem();
- return $fs->chown($this->getPath(), $user);
- }
-
-
- function getUser() {
- return @fileowner($this->getPath());
- }
-
-
- public function setGroup($group) {
- $fs = FileSystem::getFileSystem();
- return $fs->chgrp($this->getPath(), $group);
- }
-
-
- function getGroup() {
- return @filegroup($this->getPath());
- }
-
- function setMode($mode) {
- $fs = FileSystem::getFileSystem();
- return $fs->chmod($this->getPath(), $mode);
- }
-
- function getMode() {
- return @fileperms($this->getPath());
- }
-
-
- function listRoots() {
- $fs = FileSystem::getFileSystem();
- return (array) $fs->listRoots();
- }
-
-
- function getTempDir() {
- return Phing::getProperty('php.tmpdir');
- }
-
- function createTempFile($prefix, $suffix, PhingFile $directory) {
-
-
- $result = null;
- do {
- $result = new PhingFile($directory, $prefix . substr(md5(time()), 0, 8) . $suffix);
- } while (file_exists($result->getPath()));
- $fs = FileSystem::getFileSystem();
- $fs->createNewFile($result->getPath());
- $fs->lock($result);
- return $result;
- }
-
- function removeTempFile() {
- $fs = FileSystem::getFileSystem();
-
- $fs->unlock($this);
- $this->delete();
- }
-
-
- function compareTo(PhingFile $file) {
- $fs = FileSystem::getFileSystem();
- return $fs->compare($this, $file);
- }
-
- function equals($obj) {
- if (($obj !== null) && ($obj instanceof PhingFile)) {
- return ($this->compareTo($obj) === 0);
- }
- return false;
- }
-
- function toString() {
- return $this->getPath();
- }
-
-
- function __toString() {
- return $this->getPath();
- }
- }
|