Last active
May 15, 2018 12:49
-
-
Save TorbenKoehn/5877192 to your computer and use it in GitHub Desktop.
Path manipulation class for PHP frameworks (not fully tested)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Dwarf; | |
class Path extends Object implements \IteratorAggregate, \Countable { | |
protected $path; | |
public function __construct( $path ) { | |
$this->path = $path; | |
} | |
public function exists() { | |
return $this->isDir() | |
? true | |
: stream_resolve_include_path( $this->path ); | |
} | |
public function isWritable() { | |
return is_writable( $this->path ); | |
} | |
public function isReadable() { | |
return is_readable( $this->path ); | |
} | |
public function isExecutable() { | |
return is_executable( $this->path ); | |
} | |
public function isFile() { | |
return is_file( $this->path ); | |
} | |
public function isDirectory() { | |
return is_dir( $this->path ); | |
} | |
public function isLink() { | |
return is_link( $this->path ); | |
} | |
public function getExtension() { | |
return pathinfo( $this->path, PATHINFO_EXTENSION ); | |
} | |
public function getFilename() { | |
return pathinfo( $this->path, PATHINFO_FILENAME ); | |
} | |
public function getDirectory( $asObject = true ) { | |
return $asObject | |
? new static( pathinfo( $this->path, PATHINFO_DIRNAME ) ) | |
: pathinfo( $this->path, PATHINFO_DIRNAME ); | |
} | |
public function getBasename() { | |
return pathinfo( $this->path, PATHINFO_BASENAME ); | |
} | |
public function getAccessGroup() { | |
return filegroup( $this->path ); | |
} | |
public function getAccessOwner() { | |
return fileowner( $this->path ); | |
} | |
public function getSize() { | |
return filesize( $this->path ); | |
} | |
public function getModificationTime() { | |
return new DateTime( filemtime( $this->path ) ); | |
} | |
public function getCreationTime() { | |
return new DateTime( filectime( $this->path ) ); | |
} | |
public function getAccessTime() { | |
return new DateTime( fileatime( $this->path ) ); | |
} | |
public function getRealPath( $asObject = true ) { | |
return $asObject | |
? new static( realpath( $this->path ) ) | |
: realpath( $this->path ); | |
} | |
public function __toString() { | |
return $this->path; | |
} | |
public function getFiles( $asObject = true ) { | |
if( !$this->isDirectory() ) | |
throw new Exception( "$this->path is not a directory and thus " | |
. "doesn't have files. Please check with Path->isDir() first" ); | |
$dir = opendir( $this->path ); | |
while( $file = readdir( $dir ) ) { | |
if( $file !== '.' && $file !== '..' ) | |
yield Path::combine( $this->path, $file, $asObject ); | |
} | |
closedir( $dir ); | |
} | |
public function getIterator() { | |
return $this->getFiles(); | |
} | |
public function count() { | |
return iterator_count( $this->getIterator() ); | |
} | |
public static function combine( $folder, $path, $asObject = true ) { | |
//both can be Path instances, so lets make them a string first | |
$folder = (string)$folder; | |
$path = (string)$path; | |
//do the slashes sit right? | |
if( $folder[ strlen( $folder ) - 1 ] !== DIRECTORY_SEPARATOR ) | |
$folder .= DIRECTORY_SEPARATOR; | |
if( $path[ 0 ] === DIRECTORY_SEPARATOR ) | |
$path = substr( $path, 1 ); | |
return $asObject ? new static( $folder.$path ) : $folder.$path; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include '../library/Dwarf/Object.php'; | |
include '../library/Dwarf/Path.php'; | |
$path = '../library/Dwarf'; | |
$dir = new Dwarf\Path( $path ); | |
echo "Is dir:"; | |
var_dump( $dir->isDirectory() ); | |
echo "Is file:"; | |
var_dump( $dir->isFile() ); | |
echo "Files:"; | |
var_dump( $dir->getFiles() ); | |
echo "Generator iteration:<br>"; | |
foreach( $dir->getFiles() as $file ) | |
echo "File: $file<br>"; | |
echo '<br><br>'; | |
echo "Aggregated Iterator iteration:<br>"; | |
foreach( $dir as $file ) { | |
if( $file->isDirectory() ) | |
echo "Directory: $file (".count( $file ).")<br>"; | |
else | |
echo "File: $file<br>"; | |
} | |
echo "<br><br>File count:"; | |
var_dump( count( $dir ) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is dir: | |
boolean true | |
Is file: | |
boolean false | |
Files: | |
object(Generator)[2] | |
Generator iteration: | |
File: ../library/Dwarf/App.php | |
File: ../library/Dwarf/Config | |
File: ../library/Dwarf/Config.php | |
File: ../library/Dwarf/Configurable.php | |
File: ../library/Dwarf/Environment.php | |
File: ../library/Dwarf/EnvironmentException.php | |
File: ../library/Dwarf/Environments | |
File: ../library/Dwarf/Exception.php | |
File: ../library/Dwarf/Loader.php | |
File: ../library/Dwarf/Object.php | |
File: ../library/Dwarf/Path.php | |
Aggregated Iterator iteration: | |
File: ../library/Dwarf/App.php | |
Directory: ../library/Dwarf/Config (1) | |
File: ../library/Dwarf/Config.php | |
File: ../library/Dwarf/Configurable.php | |
File: ../library/Dwarf/Environment.php | |
File: ../library/Dwarf/EnvironmentException.php | |
Directory: ../library/Dwarf/Environments (1) | |
File: ../library/Dwarf/Exception.php | |
File: ../library/Dwarf/Loader.php | |
File: ../library/Dwarf/Object.php | |
File: ../library/Dwarf/Path.php | |
File count: | |
int 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment