Last active
July 5, 2017 22:04
-
-
Save naosim/4492b8054564f13998fa51be03ec0340 to your computer and use it in GitHub Desktop.
EasyInclude
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 | |
class EasyInclude { | |
private $root; | |
private $excludePhp; | |
public function __construct($root, $excludePhp = null) { | |
$this->root = $root; | |
$this->excludePhp = $excludePhp; | |
} | |
private function loadFromWeb($url) { | |
$file = $this->root . '/vendor/' . explode('//', $url)[1]; | |
$dir = substr($file, 0, strrpos($file, '/')); | |
if(!file_exists($file)) { | |
if(!file_exists($dir)) { | |
mkdir($dir, 0777, true); | |
} | |
$text = trim(file_get_contents($url)); | |
if(strpos($text, '<?php') === false) { | |
throw new RuntimeException("Not PHP FILE: " . $url); | |
} | |
file_put_contents($file, $text); | |
} | |
include_once $file; | |
} | |
public static function eachPhpFile($path, $callback) { | |
foreach(glob($path . '/*') as $file){ | |
if(strpos($file, '.php')) { | |
// echo "$file<br>"; | |
$callback($file); | |
} else { | |
self::eachPhpFile($file, $callback); | |
} | |
} | |
} | |
private function loadFromDir($path) { | |
self::eachPhpFile($this->root . $path, function($file) { | |
if($this->excludePhp === null || strpos($file, $this->excludePhp) === false) { | |
include_once $file; | |
} | |
}); | |
} | |
public function load(...$ary) { | |
foreach($ary as $v) { | |
if(strpos($v, 'http') === 0) { | |
$this->loadFromWeb($v); | |
} else { | |
$this->loadFromDir($v); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment