PluginProbe
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress / 2.0
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress v2.0
2.0.14 trunk 1.0 1.2 1.2.1 1.2.2 1.2.3 1.2.4 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
counter-box / classes / Autoloader.php

Autoloader.php in Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress 2.0, 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 CounterBox;
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 }