feat(bootstrap): Introduce Libs\Loader for directory loading
This commit is contained in:
17
index.php
17
index.php
@@ -1,8 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Libs\Loader;
|
||||||
|
use Libs\Router;
|
||||||
|
|
||||||
require_once('config.php');
|
require_once('config.php');
|
||||||
|
|
||||||
// Incluir clases
|
// Autocarga de clases
|
||||||
spl_autoload_register(function ($className) {
|
spl_autoload_register(function ($className) {
|
||||||
$fp = str_replace('\\', '/', $className);
|
$fp = str_replace('\\', '/', $className);
|
||||||
$name = basename($fp);
|
$name = basename($fp);
|
||||||
@@ -13,11 +16,11 @@ spl_autoload_register(function ($className) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Incluir routers
|
// Autocarga de routers
|
||||||
$routers = glob(ROOT_CORE . '/Routers/*.php');
|
Loader::load(ROOT_CORE . '/Routers/');
|
||||||
|
|
||||||
foreach ($routers as $file) {
|
// Otros autoloaders
|
||||||
require_once($file);
|
Loader::load(ROOT_CORE . '/Autoload/');
|
||||||
}
|
|
||||||
|
|
||||||
\Libs\Router::apply();
|
// Correr los routers
|
||||||
|
Router::apply();
|
||||||
|
|||||||
31
src/Libs/Loader.php
Normal file
31
src/Libs/Loader.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Libs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loader - DuckBrain
|
||||||
|
*
|
||||||
|
* Simple library to bulk load multiple php files inside a folder.
|
||||||
|
*
|
||||||
|
* @author KJ
|
||||||
|
* @website https://kj2.me
|
||||||
|
* @license MIT
|
||||||
|
*/
|
||||||
|
class Loader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Loads all PHP files from a specified directory.
|
||||||
|
* If the directory does not exist or is not a directory, no files will be loaded.
|
||||||
|
*
|
||||||
|
* @param string $directoryPath The path to the directory containing the PHP files to load.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function load(string $directoryPath): void
|
||||||
|
{
|
||||||
|
if (is_dir($directoryPath)) {
|
||||||
|
foreach (glob(rtrim($directoryPath, '/') . '/*.php') as $file) {
|
||||||
|
require_once($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user