Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Last active December 21, 2015 06:18
Show Gist options
  • Save franz-josef-kaiser/6262742 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/6262742 to your computer and use it in GitHub Desktop.
A simple class based file (semi-auto-)loader that needs a path, relative to the plugins root directory as input argument.
<?php
$fileLoader = new FileLoader( 'assets/php' );
foreach ( $fileLoader as $file )
$fileLoader->loadFile( $file );
<?php
namespace SomeNamespace;
defined( 'ABSPATH' ) OR exit;
class FileLoader implements \IteratorAggregate
{
private $path = '';
private $files = array();
public function __construct( $path )
{
$this->setPath( $path );
$this->setFiles();
}
public function setPath( $path )
{
empty( $this->path )
AND $this->path = trailingslashit( plugin_dir_path( __FILE__ ).$path );
}
public function setFiles()
{
return $this->files = glob( "{$this->getPath()}*.php" );
}
public function getPath()
{
return $this->path;
}
public function getIterator()
{
$iterator = new \ArrayIterator( $this->files );
return $iterator;
}
public function loadFile( $file )
{
include_once $file;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment