Last active
May 28, 2019 18:32
-
-
Save georgestephanis/69477eaad73e50fa7f0f77bb64f3898b to your computer and use it in GitHub Desktop.
This file contains 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 | |
// Should this use RecursiveDirectoryIterator instead? | |
namespace Stephanis\Scan; | |
trait Props { | |
public $modified; | |
public $owner; | |
public $group; | |
public $perms; | |
function populate( $path ) { | |
$this->modified = date( 'c', filemtime( $path ) ); | |
$this->perms = substr( sprintf( '%o', fileperms( $path ) ), -4 ); | |
$this->owner = posix_getpwuid( fileowner( $path ) )['name']; | |
$this->group = posix_getgrgid( filegroup( $path ) )['name']; | |
} | |
} | |
class File { | |
use Props; | |
public $md5; | |
public $size; | |
public function __construct( $path ) { | |
if ( ! file_exists( $path ) ) { | |
return null; | |
} | |
$this->md5 = md5_file( $path ); | |
$this->size = filesize( $path ); | |
$this->populate( $path ); | |
return $this; | |
} | |
} | |
class Dir { | |
use Props; | |
public $children = []; | |
public function __construct( $path ) { | |
if ( ! file_exists( $path ) ) { | |
return null; | |
} | |
$this->populate( $path ); | |
$files = glob( rtrim( $path, DIRECTORY_SEPARATOR ) . '/*' ); | |
foreach ( $files as $file ) { | |
$this->children[ basename( $file ) ] = is_dir( $file ) ? new Dir( $file ) : new File( $file ); | |
} | |
return $this; | |
} | |
} | |
// Get data about all files and directories contained within the specified directory: | |
echo json_encode( new Dir( getcwd() ), JSON_PRETTY_PRINT ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment