PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / autoload.php

autoload.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/autoload.php

104 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 class Autoload {
10
11 /**
12 * Instance
13 *
14 * @access private
15 * @var ?Autoload Class Instance.
16 */
17 private static ?Autoload $instance = null;
18
19 /**
20 * Autoload directories for different namespaces.
21 *
22 * @var array
23 */
24 private array $autoload_directories = [
25 'StoreEngine' => STOREENGINE_ROOT_DIR_PATH . 'includes/',
26 'StoreEngine\Payment\Gateways' => STOREENGINE_INCLUDES_DIR_PATH . 'payment-gateways/',
27 ];
28
29 /**
30 * Initiator
31 */
32 public static function get_instance(): Autoload {
33 if ( ! isset( self::$instance ) ) {
34 self::$instance = new self();
35 }
36
37 return self::$instance;
38 }
39
40 /**
41 * Register autoload directories for namespaces.
42 *
43 * @param string $namespace Namespace to autoload.
44 * @param string $directory Directory path for the namespace.
45 */
46 public function add_namespace_directory( string $namespace, string $directory ) {
47 $this->autoload_directories[ $namespace ] = trailingslashit( $directory );
48 }
49
50 /**
51 * Autoload classes.
52 *
53 * @param string $class Class name.
54 */
55 public function autoload( string $class ) {
56 foreach ( $this->autoload_directories as $namespace => $directory ) {
57 if ( 0 === strpos( $class, $namespace ) ) {
58 $class_to_load = $class;
59 $filename = strtolower(
60 preg_replace(
61 [ '/^' . preg_quote( $namespace, '\\\/' ) . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ],
62 [ '', '$1-$2', '-', DIRECTORY_SEPARATOR ],
63 $class_to_load
64 )
65 );
66
67 $file = $directory . $filename . '.php';
68
69 if ( strpos( $file, 'includes/addons/' ) !== false ) {
70 $file = str_replace( 'includes/addons/', 'addons/', $file );
71 }
72
73 // If the file is readable, include it.
74 if ( is_readable( $file ) ) {
75 require_once $file;
76 }
77 }
78 }
79 }
80
81 /**
82 * Constructor
83 */
84 protected function __construct() {
85 spl_autoload_register( [ $this, 'autoload' ] );
86 }
87
88 /**
89 * Cloning is forbidden.
90 */
91 public function __clone() {
92 _doing_it_wrong( __FUNCTION__, esc_html__( 'Cloning is forbidden.', 'storeengine' ), '1.3.3' );
93 }
94
95 /**
96 * Unserializing instances of this class is forbidden.
97 */
98 public function __wakeup() {
99 _doing_it_wrong( __FUNCTION__, esc_html__( 'Unserializing instances of this class is forbidden.', 'storeengine' ), '1.3.3' );
100 }
101 }
102
103 Autoload::get_instance();
104