-
Notifications
You must be signed in to change notification settings - Fork 0
/
bricks.php
81 lines (68 loc) · 2.04 KB
/
bricks.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
<?php
class Bricks {
const VERSION = '0.0.2';
const PRIORITY = 9999;
public static $singleton = null;
public static function instance() {
if ( self::$singleton === null ) {
self::$singleton = new self();
}
return self::$singleton;
}
private function __construct() {
if ( ! defined('BRICKS_LOADED') ) {
define( 'BRICKS_LOADED', self::PRIORITY );
}
if (function_exists('add_action')) {
$this->initialize();
add_action('init', [$this, 'init'], self::PRIORITY);
}
}
private function initialize () {
$this->load_libs();
$this->register_autoloader();
}
public function init(){
$this->load_models();
$this->init_cmb2();
}
private function load_libs() {
require_once 'src/Helpers.php';
require_once 'src/BaseItem.php';
require_once 'src/CustomUser.php';
require_once 'src/CustomPost.php' ;
require_once 'src/CustomSingle.php';
require_once 'src/Hookable.php';
}
private function init_cmb2() {
require_once implode(DIRECTORY_SEPARATOR, [__DIR__, '../../cmb2/cmb2/init.php']);
}
private function register_autoloader(){
spl_autoload_register(function(string $class) {
if (defined('BRICKS_NAMESPACE')) {
$class_path = explode('\\', $class);
$class_name = $class_path[sizeof($class_path)-1];
$namespace = $class_path[0];
if(BRICKS_NAMESPACE == $namespace){
$path = implode(DIRECTORY_SEPARATOR, [BRICKS_BASE_DIR, 'models', "$class_name.php"]);
if (!file_exists($path)) return;
require_once $path;
}
}
});
}
private function load_models() {
if ( defined('BRICKS_NAMESPACE') ) {
foreach(glob(implode(DIRECTORY_SEPARATOR, [BRICKS_BASE_DIR, 'models', '*.php'])) as $file){
$path = explode(DIRECTORY_SEPARATOR, $file);
$class_name = explode('.', $path[sizeof($path)-1])[0] ;
$class = BRICKS_NAMESPACE.'\\'.$class_name;
if(method_exists($class, 'init')){
$class::init();
}
}
}
}
}
Bricks::instance();
?>