I wrote an PHP Autoloader to include it "easyer" to my projects.
Here the folder structure:
www
├───index.php
├───init.php
└───Libarys
└───bootstrap
├───index.php
├───bootstrap_loader.php
└───bootstrap_core_files (Files and Directories (Abbreviated so it doesn't get too much))
www/index.php
:
It's just like the main method in Java, which starts on execution. This integrates the init.php
file.
<?php
include_once("./init.php");
$init = new Initialisation();
$init->loadBS();
?>
www/init.php
:
It's just like the init method in Java, which initialized integrate following things:
Directory Locations (initialized)
BootStrapLoader (integrate (www/Libarys/bootstrap/index.php)
<?php
class Initialisation {
private $direct;
function getFileLocation() {
$value = "";
$value = $_SERVER["REQUEST_URI"];
return($value);
}
function getCurrentDirectory() {
$dir = "";
$getFileLocation = $this->getFileLocation();
$countetSlashes = substr_count($getFileLocation,"/")-1;
if ($getFileLocation !== "") {
for ($i = 0; $i < $countetSlashes; $i++) {
$dir = $dir."../";
}
}
$dir = $dir."./Libarys/bootstrap";
return $dir;
}
function setDirect($dir) {
$this->direct = $dir;
}
function getDirect() {
return $this->direct;
}
function loadBS() {
$this->setDirect($this->getCurrentDirectory());
include_once($this->getDirect()."/index.php");
$loadBS = new LoadBootStrap();
$loadBS->loadBootStrap();
}
}
?>
www/Libarys/bootstrap/index.php
:
This index.php file just starts the true loader (www/Libarys/bootstrap/bootstrap_loader.php
).
<?php
class LoadBootStrap {
function loadBootStrap() {
$init = new Initialisation();
$init->setDirect($init->getCurrentDirectory());
include_once($init->getDirect()."/bootstrap_loader.php");
$bootstrap = new BootStrap();
$bootstrap->loadFileGeter("JS", $init->getDirect());
$bootstrap->loadFileGeter("CSS", $init->getDirect());
}
}
?>
www/Libarys/bootstrap/bootstrap_loader.php
:
The bootstrap_loader.php
searches and includes the CSS and JS files from the BootStrap Framework to the www/index.php
file. I tested those scripts in more than 4 sub directories with sub directories and they worked well.
<?php
class BootStrap {
function loadFileGeter($param, $direct) {
switch ($param) {
case "CSS":
$this->getFileList($direct."/".strtolower($param), $param);
break;
case "JS":
$this->getFileList($direct."/".strtolower($param), $param);
break;
}
}
function getFileList($dir, $param) {
$dir = $dir;
$files = scandir($dir, 1);
sort($files);
foreach ($files as &$value) {
if (str_ends_with($value,".".strtolower($param))) {
switch ($param) {
case "CSS":
$string = '<link rel="stylesheet" type="text/css" href="'.$dir.'/'.$value.'" />'."\n";
ECHO($string);
break;
case "JS":
$string = '<script async="" src="'.$dir.'/'.$value.'"></script>'."\n";
ECHO($string);
break;
}
}
}
}
}
?>