loader.php
110 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * VikAppointments loader class. |
| 16 | * |
| 17 | * @since 1.6 |
| 18 | * @since 1.7 Renamed from UILoader |
| 19 | */ |
| 20 | abstract class VAPLoader |
| 21 | { |
| 22 | /** |
| 23 | * The list containing all the resources loaded. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private static $includes = array(); |
| 28 | |
| 29 | /** |
| 30 | * The list containing all the filename aliases. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | private static $aliases = array(); |
| 35 | |
| 36 | /** |
| 37 | * Base path to load resources. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public static $base = VAPBASE; |
| 42 | |
| 43 | /** |
| 44 | * Loads the specified file. |
| 45 | * |
| 46 | * @param string $key The class name to look for (dot notation). |
| 47 | * @param string $base Search this directory for the class. |
| 48 | * |
| 49 | * @return boolean True on success, otherwise false. |
| 50 | */ |
| 51 | public static function import($key, $base = null) |
| 52 | { |
| 53 | // if the resource is not loaded, try to do it |
| 54 | if (!isset(static::$includes[$key])) |
| 55 | { |
| 56 | $success = false; |
| 57 | |
| 58 | // if no base provided, use the default one |
| 59 | if (empty($base)) |
| 60 | { |
| 61 | $base = static::$base; |
| 62 | } |
| 63 | |
| 64 | // remove trailing slash (if any) |
| 65 | $base = rtrim($base, DIRECTORY_SEPARATOR); |
| 66 | |
| 67 | $parts = explode('.', $key); |
| 68 | $class = array_pop($parts); |
| 69 | |
| 70 | // if the file has been registered with an alias, replace it with the original one |
| 71 | if (isset(static::$aliases[$class])) |
| 72 | { |
| 73 | $class = static::$aliases[$class]; |
| 74 | } |
| 75 | |
| 76 | // re-insert class to build the relative path |
| 77 | $parts[] = $class; |
| 78 | |
| 79 | // build the path |
| 80 | $path = implode(DIRECTORY_SEPARATOR, $parts); |
| 81 | |
| 82 | // if the file exists, load it |
| 83 | if (is_file($base . DIRECTORY_SEPARATOR . $path . '.php')) |
| 84 | { |
| 85 | $success = (bool) include_once $base . DIRECTORY_SEPARATOR . $path . '.php'; |
| 86 | } |
| 87 | |
| 88 | // cache the loading status |
| 89 | static::$includes[$key] = $success; |
| 90 | } |
| 91 | |
| 92 | return static::$includes[$key]; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Register an alias of a given class filename. |
| 97 | * This is useful for those files that contain a dot in their name. |
| 98 | * |
| 99 | * @param string $name The filename to register. |
| 100 | * @param string $alias The alias to use. |
| 101 | */ |
| 102 | public static function registerAlias($name, $alias) |
| 103 | { |
| 104 | if (!isset(static::$aliases[$alias])) |
| 105 | { |
| 106 | static::$aliases[$alias] = $name; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 |