PluginProbe
Bubble Menu – Floating Button Menu with Sticky Navigation / trunk
Bubble Menu – Floating Button Menu with Sticky Navigation vtrunk
trunk 1.3 2.0 2.2 2.2.1 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.1 4.1.1
bubble-menu / classes / Autoloader.php

Autoloader.php in Bubble Menu – Floating Button Menu with Sticky Navigation trunk, at classes/Autoloader.php

70 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Autoloader class
5 *
6 * The Autoloader class is responsible for loading classes automatically based on their namespace.
7 *
8 * @package BubbleMenu
9 * @subpackage Autoloader
10 * @author Dmytro Lobov <hey@wow-company.com>, Wow-Company
11 * @copyright 2024 Dmytro Lobov
12 * @license GPL-2.0+
13 */
14
15 namespace BubbleMenu;
16
17 // Exit if accessed directly.
18 defined( 'ABSPATH' ) || exit;
19
20 class Autoloader {
21 /**
22 * @var mixed
23 */
24 private $namespace;
25 private $directory;
26
27 public function __construct( $namespace ) {
28 $this->namespace = $namespace;
29 $this->directory = __DIR__;
30 spl_autoload_register( [ $this, 'autoload' ] );
31 }
32
33 public function autoload( $class ): void {
34
35 if ( strpos( $class, $this->namespace ) === 0 ) {
36 $file = $this->get_file_path( $class );
37
38 if ( $file && file_exists( $file ) ) {
39 require_once( $file );
40
41 return;
42 }
43 }
44 }
45
46 /**
47 * Get the file path for a class.
48 *
49 * @param string $class The fully qualified name of the class.
50 *
51 * @return string|null The file path, or null if the file could not be found.
52 */
53 public function get_file_path( string $class ): ?string {
54
55 $relativeClass = substr( $class, strlen( $this->namespace ) );
56
57 $file = str_replace( '\\', DIRECTORY_SEPARATOR, $relativeClass ) . '.php';
58
59 $full_path = $this->directory . DIRECTORY_SEPARATOR . $file;
60
61 if ( file_exists( $full_path ) ) {
62 return $full_path;
63 }
64
65
66 return null;
67 }
68
69
70 }