forked from ryansechrest/github-updater-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoloader.php
39 lines (30 loc) · 1.02 KB
/
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
<?php
namespace RYSE\GitHubUpdaterDemo;
/**
* Require plugin classes on demand.
*
* @author Ryan Sechrest
* @package RYSE\GitHubUpdaterDemo
*/
spl_autoload_register(function ($class) {
// Remove organization and plugin name from namespace:
// <organizationName>\<pluginName>\Foo\Bar => Foo\Bar
$class = substr($class, strlen(__NAMESPACE__ . '\\'));
// Convert namespace to path:
// Foo\Bar => Foo/Bar
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
// Append PHP file extension:
// Foo/Bar => Foo/Bar.php
$class = $class . '.php';
// Prepend plugin directory:
// Foo/Bar.php => plugin/Foo/Bar.php
$class = 'plugin/' . $class;
// Prepend current directory:
// plugin/Foo/Bar.php => .../wp-content/plugins/<pluginName>/plugin/Foo/Bar.php
$class = dirname(__FILE__) . '/' . $class;
// If file does not exist, exit
if (!file_exists($class)) return;
// Require class:
// .../wp-content/plugins/<pluginName>/plugin/Foo/Bar.php
require_once $class;
});