Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Last active May 28, 2019 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgestephanis/69477eaad73e50fa7f0f77bb64f3898b to your computer and use it in GitHub Desktop.
Save georgestephanis/69477eaad73e50fa7f0f77bb64f3898b to your computer and use it in GitHub Desktop.
<?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