Last active
December 21, 2015 06:18
-
-
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.
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 | |
$fileLoader = new FileLoader( 'assets/php' ); | |
foreach ( $fileLoader as $file ) | |
$fileLoader->loadFile( $file ); |
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 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