PluginProbe
WP Offload SES Lite / 1.3
WP Offload SES Lite v1.3
1.8.2 trunk 0.1.2 0.2.1 0.7.2.1 0.8.2 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
wp-ses / classes / Autoloader.php

Autoloader.php in WP Offload SES Lite 1.3, at classes/Autoloader.php

132 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Autoloader for WP Offload SES.
4 *
5 * @author Delicious Brains
6 * @package WP Offload SES
7 */
8
9 namespace DeliciousBrains\WP_Offload_SES;
10
11 /**
12 * Class Autoloader
13 *
14 * @since 1.0.0
15 */
16 class Autoloader {
17
18 /**
19 * Path to the main plugin file.
20 *
21 * @var string
22 */
23 protected $abspath;
24
25 /**
26 * Prefix to use in namespaces.
27 *
28 * @var string
29 */
30 protected $prefix;
31
32 /**
33 * Vendor to use in namespaces.
34 *
35 * @var string
36 */
37 protected $vendor = 'DeliciousBrains';
38
39 /**
40 * Autoloader constructor.
41 *
42 * @param string $prefix Prefix to use in namespaces.
43 * @param string $abspath Path to the main plugin file.
44 *
45 * @throws \Exception
46 */
47 public function __construct( $prefix, $abspath ) {
48 $this->prefix = $prefix;
49 $this->abspath = $abspath;
50
51 $this->register_autoloader();
52 }
53
54 /**
55 * Registers the autoloader.
56 *
57 * @return bool
58 * @throws \Exception
59 */
60 public function register_autoloader() {
61 return spl_autoload_register( array( $this, 'autoloader' ) );
62 }
63
64 /**
65 * Load the classes.
66 *
67 * @param string $class_name The class to load.
68 */
69 public function autoloader( $class_name ) {
70 if ( ! $this->class_belongs_to_plugin( $class_name ) ) {
71 return;
72 }
73
74 $class_path = $this->get_class_path( $class_name );
75 $path = $this->get_classes_directory() . $class_path;
76
77 if ( file_exists( $path ) ) {
78 require $path;
79 } else {
80 $path = $this->get_classes_directory( true ) . $class_path;
81
82 if ( file_exists( $path ) ) {
83 require $path;
84 }
85 }
86 }
87
88 /**
89 * Class belong to plugin.
90 *
91 * @param string $class_name The class name.
92 *
93 * @return bool
94 */
95 protected function class_belongs_to_plugin( $class_name ) {
96 if ( 0 !== strpos( $class_name, $this->vendor . '\\' . $this->prefix . '\\' ) ) {
97 return false;
98 }
99
100 return true;
101 }
102
103 /**
104 * Get class path.
105 *
106 * @param string $class_name The class name.
107 *
108 * @return string
109 */
110 protected function get_class_path( $class_name ) {
111 $parts = explode( '\\', $class_name );
112 $parts = array_slice( $parts, 2 );
113
114 $filename = implode( DIRECTORY_SEPARATOR, $parts ) . '.php';
115
116 return str_replace( '_', '-', $filename );
117 }
118
119 /**
120 * Get classes directory.
121 *
122 * @param bool $vendor If the path should include the vendor directory.
123 *
124 * @return string
125 */
126 protected function get_classes_directory( $vendor = false ) {
127 $dir = $vendor ? 'vendor' : 'classes';
128 return $this->abspath . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
129 }
130
131 }
132