-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAutoload.php
More file actions
67 lines (58 loc) · 1.65 KB
/
Copy pathAutoload.php
File metadata and controls
67 lines (58 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace JustCoded\WP\Framework;
/**
* SPL autoload registration for theme to prevent using file includes
*/
class Autoload {
/**
* Namespace prefix in PSR-4 format (Vendor/Module)
*
* @var string
*/
protected $app_namespace;
/**
* Directory name path to search classes
*
* @var string
*/
protected $app_path;
/**
* Class constructor
* register SPL autoload callback functions for App and framework
*
* @param string $app_namespace App namespace.
* @param string $app_path App directory path.
*/
public function __construct( $app_namespace, $app_path ) {
$this->app_namespace = $app_namespace;
$this->app_path = $app_path;
spl_autoload_register( array( $this, 'spl_autoload' ) );
}
/**
* Search for the class by app namespace
*
* @param string $class_name not loaded class name.
*/
public function spl_autoload( $class_name ) {
$this->load_class( $class_name, $this->app_namespace, $this->app_path );
}
/**
* Search for the class by $namespace and include it from $path if found.
*
* @param string $class_name not loaded class name.
* @param string $namespace namespace in PSR-4 format.
* @param string $dir_path path to search files.
*/
protected function load_class( $class_name, $namespace, $dir_path ) {
// check if this class is related to the plugin namespace. exit if not.
if ( strpos( $class_name, $namespace ) !== 0 ) {
return;
}
$class_path = substr( $class_name, strlen( $namespace ) );
$class_path = str_replace( '\\', '/', $class_path );
$path = $dir_path . '/' . trim( $class_path, '/' ) . '.php';
if ( is_file( $path ) ) {
require_once $path;
}
}
}