PluginProbe
Button Generator – Easily Create Custom Buttons with Icons and Analytics / 3.1
Button Generator – Easily Create Custom Buttons with Icons and Analytics v3.1
trunk 1.1.1 2.0 2.1 2.2 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.1.3 3.2 3.2.1 3.2.2 3.2.3 All 28 releases
button-generation / classes / Autoloader.php

Autoloader.php in Button Generator – Easily Create Custom Buttons with Icons and Analytics 3.1, 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 WowPlugin
9 * @subpackage Autoloader
10 * @author Dmytro Lobov <dev@wow-company.com>, Wow-Company
11 * @copyright 2024 Dmytro Lobov
12 * @license GPL-2.0+
13 */
14
15 namespace ButtonGenerator;
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 }