-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliyun-oss-media-autoloader.php
94 lines (77 loc) · 1.75 KB
/
aliyun-oss-media-autoloader.php
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
// Check if already defined
if ( ! class_exists( 'Aliyun_OSS_Media_Autoloader' ) ) {
class Aliyun_OSS_Media_Autoloader {
/**
* @var string
*/
protected $abspath;
/**
* @var string
*/
protected $prefix;
/**
* @var string
*/
protected $vendor = 'GuztiaConsulting';
/**
* Autoloader constructor.
*
* @param string $prefix
* @param string $abspath
*/
public function __construct( $prefix, $abspath ) {
$this->prefix = $prefix;
$this->abspath = $abspath;
spl_autoload_register( array( $this, 'autoloader' ) );
}
/**
* Autoloader.
*
* @param string $class_name
*/
public function autoloader( $class_name ) {
if ( ! $this->class_belongs_to_plugin( $class_name ) ) {
return;
}
$path = $this->get_classes_directory() . $this->get_class_path( $class_name );
if ( file_exists( $path ) ) {
require_once $path;
}
}
/**
* Class belong to plugin.
*
* @param string $class_name
*
* @return bool
*/
protected function class_belongs_to_plugin( $class_name ) {
if ( 0 !== strpos( $class_name, $this->vendor . '\\' . $this->prefix . '\\' ) ) {
return false;
}
return true;
}
/**
* Get class path.
*
* @param string $class_name
*
* @return string
*/
protected function get_class_path( $class_name ) {
$parts = explode( '\\', strtolower( $class_name ) );
$parts = array_slice( $parts, 2 );
$filename = implode( DIRECTORY_SEPARATOR, $parts ) . '.php';
return str_replace( '_', '-', strtolower( $filename ) );
}
/**
* Get classes directory.
*
* @return string
*/
protected function get_classes_directory() {
return $this->abspath . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR;
}
}
}