From ad59ffeb35bbb10eaaba2786128ca23acb6f9f13 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 8 Jul 2016 21:35:07 +0200 Subject: [PATCH] Added PSR4 autoload function for the project. --- src/autoload.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/index.php | 12 ++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/autoload.php diff --git a/src/autoload.php b/src/autoload.php new file mode 100644 index 00000000..ed9b710b --- /dev/null +++ b/src/autoload.php @@ -0,0 +1,44 @@ + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.2.0 + * ---------------------------------------------------------------------------- */ + +function register($namespace, $path) { + spl_autoload_register(function ($class) { + // project-specific namespace prefix + $prefix = 'EA\\' . $namespace . '\\'; + + // base directory for the namespace prefix + $base_dir = __DIR__ . '/' . $path; + + // does the class use the namespace prefix? + $len = strlen($prefix); + if (strncmp($prefix, $class, $len) !== 0) { + // no, move to the next registered autoloader + return; + } + + // get the relative class name + $relative_class = substr($class, $len); + + // replace the namespace prefix with the base directory, replace namespace + // separators with directory separators in the relative class name, append + // with .php + $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; + + // if the file exists, require it + if (file_exists($file)) { + require $file; + } + }); +} + +register('Engine', __DIR__ . '/engine'); diff --git a/src/index.php b/src/index.php index fc186491..7046b933 100644 --- a/src/index.php +++ b/src/index.php @@ -49,6 +49,18 @@ require_once __DIR__ . '/config.php'; + /* + *--------------------------------------------------------------- + * EASY!APPOINTMENTS AUTOLOAD FILE + *--------------------------------------------------------------- + * + * Include Easy!Appointments autoload file so that namespace + * classes are loaded automatically. + * + */ + + require_once __DIR__ . '/autoload.php'; + /* *---------------------------------------------------------------