Admin
8 years ago
Column
8 years ago
Form
8 years ago
Helper
8 years ago
ListScreen
8 years ago
Meta
8 years ago
Notice
8 years ago
Plugin
8 years ago
Relation
8 years ago
Settings
8 years ago
ThirdParty
8 years ago
API.php
8 years ago
Addon.php
8 years ago
Admin.php
8 years ago
Autoloader.php
8 years ago
Collection.php
8 years ago
Column.php
8 years ago
Groups.php
8 years ago
Helper.php
8 years ago
ListScreen.php
8 years ago
ListScreenPost.php
8 years ago
ListScreenWP.php
8 years ago
Plugin.php
8 years ago
PluginInformation.php
8 years ago
Preferences.php
8 years ago
Relation.php
8 years ago
TableScreen.php
8 years ago
View.php
8 years ago
ViewInterface.php
8 years ago
Autoloader.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Autoloader { |
| 8 | |
| 9 | /** |
| 10 | * @var AC_Autoloader; |
| 11 | */ |
| 12 | protected static $instance; |
| 13 | |
| 14 | /** |
| 15 | * Register prefixes and their path |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $prefixes = array(); |
| 20 | |
| 21 | private function __construct() { |
| 22 | spl_autoload_register( array( $this, 'autoload' ) ); |
| 23 | } |
| 24 | |
| 25 | public static function instance() { |
| 26 | if ( null === self::$instance ) { |
| 27 | self::$instance = new self(); |
| 28 | } |
| 29 | |
| 30 | return self::$instance; |
| 31 | } |
| 32 | |
| 33 | public static function string_to_classname( $string ) { |
| 34 | return implode( array_map( 'ucfirst', explode( '_', str_replace( '-', '_', $string ) ) ) ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Register a prefix that should autoload |
| 39 | * |
| 40 | * @param $prefix string Unique prefix to this set of classes |
| 41 | * @param $path string Path to directory where classes are stored |
| 42 | */ |
| 43 | public function register_prefix( $prefix, $path ) { |
| 44 | $prefix = rtrim( $prefix, '_' ) . '_'; |
| 45 | $path = trailingslashit( $path ); |
| 46 | |
| 47 | $this->prefixes[ $prefix ] = $path; |
| 48 | |
| 49 | // make sure that more specific prefixes are checked first |
| 50 | krsort( $this->prefixes ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param $class |
| 55 | */ |
| 56 | public function autoload( $class ) { |
| 57 | foreach ( $this->prefixes as $prefix => $prefix_path ) { |
| 58 | if ( 0 !== strpos( $class, $prefix ) ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | $class_path = str_replace( array( $prefix, '_' ), array( '', '/' ), $class ); |
| 63 | $file = $prefix_path . $class_path . '.php'; |
| 64 | |
| 65 | if ( is_readable( $file ) ) { |
| 66 | require_once $file; |
| 67 | |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | // Git does not detect case-difference in a filename and older versions used same filename but with a different case |
| 72 | $basename = basename( $file ); |
| 73 | $file_lc = str_replace( $basename, strtolower( $basename ), $file ); |
| 74 | |
| 75 | if ( is_readable( $file_lc ) ) { |
| 76 | require_once $file_lc; |
| 77 | |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param string $prefix |
| 85 | * |
| 86 | * @return false|string |
| 87 | */ |
| 88 | private function get_path_by_prefix( $prefix ) { |
| 89 | return isset( $this->prefixes[ $prefix ] ) ? $this->prefixes[ $prefix ] : false; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get list of all class names from a directory |
| 94 | * |
| 95 | * @param string $dir |
| 96 | * @param string $prefix |
| 97 | * |
| 98 | * @return array Class names |
| 99 | */ |
| 100 | public function get_class_names_from_dir( $dir, $prefix ) { |
| 101 | $path = trailingslashit( $dir ); |
| 102 | $classes_dir = $this->get_path_by_prefix( $prefix ); |
| 103 | |
| 104 | // skip if directory is not auto loaded |
| 105 | if ( false === strpos( $path, $classes_dir ) ) { |
| 106 | return array(); |
| 107 | } |
| 108 | |
| 109 | $class_names = array(); |
| 110 | |
| 111 | $prefix = $prefix . str_replace( array( $classes_dir, '/' ), array( '', '_' ), untrailingslashit( $path ) ) . '_'; |
| 112 | |
| 113 | if ( is_dir( $dir ) ) { |
| 114 | $iterator = new DirectoryIterator( $dir ); |
| 115 | |
| 116 | foreach ( $iterator as $leaf ) { |
| 117 | // skip non php files |
| 118 | if ( $leaf->isDot() || $leaf->isDir() || 'php' !== pathinfo( $leaf->getFilename(), PATHINFO_EXTENSION ) ) { |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | $class_name = $prefix . str_replace( '.php', '', $leaf->getFilename() ); |
| 123 | |
| 124 | $r = new ReflectionClass( $class_name ); |
| 125 | |
| 126 | if ( $r->isInstantiable() ) { |
| 127 | $class_names[] = $class_name; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return $class_names; |
| 133 | } |
| 134 | |
| 135 | } |
| 136 |