Admin
5 years ago
Ajax
5 years ago
Asset
5 years ago
Autoloader
5 years ago
Capabilities
5 years ago
Check
5 years ago
Column
5 years ago
ColumnRepository
5 years ago
Controller
5 years ago
Deprecated
5 years ago
Exception
5 years ago
Form
5 years ago
Helper
5 years ago
Integration
5 years ago
ListScreen
5 years ago
ListScreenRepository
5 years ago
ListTable
5 years ago
Message
5 years ago
Meta
5 years ago
Plugin
5 years ago
Preferences
5 years ago
Promo
5 years ago
Relation
5 years ago
Request
5 years ago
Response
5 years ago
Sanitize
5 years ago
Screen
5 years ago
Settings
5 years ago
Storage
5 years ago
Table
5 years ago
ThirdParty
5 years ago
Transient
5 years ago
Type
5 years ago
Addon.php
5 years ago
AdminColumns.php
5 years ago
AdminFactoryInterface.php
5 years ago
ApplyFilter.php
5 years ago
ArrayIterator.php
5 years ago
Autoloader.php
5 years ago
Builder.php
5 years ago
Capabilities.php
5 years ago
Collection.php
5 years ago
Column.php
5 years ago
ColumnGroups.php
5 years ago
ColumnRepository.php
5 years ago
Config.php
5 years ago
DefaultColumnsRepository.php
5 years ago
Dependencies.php
5 years ago
EncodedListScreenData.php
5 years ago
EncodedListScreenDataFactory.php
5 years ago
Expirable.php
5 years ago
Groups.php
5 years ago
Helper.php
5 years ago
Installer.php
5 years ago
Integration.php
5 years ago
Integrations.php
5 years ago
ListScreen.php
5 years ago
ListScreenCollection.php
5 years ago
ListScreenFactory.php
5 years ago
ListScreenGroups.php
5 years ago
ListScreenPost.php
5 years ago
ListScreenRepository.php
5 years ago
ListScreenRepositoryWritable.php
5 years ago
ListScreenTypes.php
5 years ago
ListScreenWP.php
5 years ago
ListScreens.php
5 years ago
ListTable.php
5 years ago
ListTableFactory.php
5 years ago
Message.php
5 years ago
MetaType.php
5 years ago
Middleware.php
5 years ago
NoticeChecks.php
5 years ago
OpCacheInvalidateTrait.php
5 years ago
PermissionChecker.php
5 years ago
Plugin.php
5 years ago
PluginActionLinks.php
5 years ago
PluginInformation.php
5 years ago
Preferences.php
5 years ago
Promo.php
5 years ago
PromoCollection.php
5 years ago
Registrable.php
5 years ago
Relation.php
5 years ago
Renderable.php
5 years ago
Request.php
5 years ago
Sanitize.php
5 years ago
Screen.php
5 years ago
ScreenController.php
5 years ago
Transient.php
5 years ago
TypedArrayIterator.php
5 years ago
View.php
5 years ago
WpListTableFactory.php
5 years ago
Autoloader.php
193 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC; |
| 4 | |
| 5 | use DirectoryIterator; |
| 6 | use FilesystemIterator; |
| 7 | |
| 8 | class Autoloader { |
| 9 | |
| 10 | /** |
| 11 | * @var self; |
| 12 | */ |
| 13 | protected static $instance; |
| 14 | |
| 15 | /** |
| 16 | * Register prefixes and their path |
| 17 | * @var string[] |
| 18 | */ |
| 19 | protected $prefixes; |
| 20 | |
| 21 | /** |
| 22 | * @var string[] |
| 23 | */ |
| 24 | protected $class_map = []; |
| 25 | |
| 26 | protected function __construct() { |
| 27 | $this->prefixes = []; |
| 28 | |
| 29 | spl_autoload_register( [ $this, 'autoload' ] ); |
| 30 | } |
| 31 | |
| 32 | public static function instance() { |
| 33 | if ( null === self::$instance ) { |
| 34 | self::$instance = new self(); |
| 35 | } |
| 36 | |
| 37 | return self::$instance; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Register a prefix that should autoload |
| 42 | * |
| 43 | * @param $prefix string Unique prefix to this set of classes |
| 44 | * @param $dir string Path to directory where classes are stored |
| 45 | * |
| 46 | * @return $this |
| 47 | */ |
| 48 | public function register_prefix( $prefix, $dir ) { |
| 49 | $this->prefixes[ $prefix ] = trailingslashit( $dir ); |
| 50 | |
| 51 | // make sure that more specific prefixes are checked first |
| 52 | krsort( $this->prefixes ); |
| 53 | |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param array $class_map |
| 59 | * |
| 60 | * @return $this |
| 61 | */ |
| 62 | public function register_class_map( array $class_map ) { |
| 63 | $this->class_map = array_merge( $this->class_map, $class_map ); |
| 64 | |
| 65 | // keep the classes organized for faster lookup |
| 66 | ksort( $this->class_map ); |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param $namespace |
| 73 | * |
| 74 | * @return false|string |
| 75 | */ |
| 76 | protected function get_prefix( $namespace ) { |
| 77 | foreach ( array_keys( $this->prefixes ) as $prefix ) { |
| 78 | if ( 0 === strpos( $namespace, $prefix ) ) { |
| 79 | return $prefix; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param $prefix |
| 88 | * |
| 89 | * @return false|string |
| 90 | */ |
| 91 | protected function get_path( $prefix ) { |
| 92 | if ( ! isset( $this->prefixes[ $prefix ] ) ) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | return $this->prefixes[ $prefix ]; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the path from a given namespace that has a registered prefix |
| 101 | * |
| 102 | * @param string $namespace |
| 103 | * |
| 104 | * @return false|string |
| 105 | */ |
| 106 | protected function get_path_from_namespace( $namespace ) { |
| 107 | $namespace = rtrim( $namespace, '\\' ); |
| 108 | $prefix = $this->get_prefix( $namespace ); |
| 109 | |
| 110 | if ( ! $prefix ) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | $path = $this->get_path( $prefix ) . substr( $namespace, strlen( $prefix ) ); |
| 115 | $path = str_replace( '\\', '/', $path ); |
| 116 | |
| 117 | return $path; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param string $class |
| 122 | * |
| 123 | * @return bool |
| 124 | */ |
| 125 | public function autoload( $class ) { |
| 126 | $file = array_key_exists( $class, $this->class_map ) |
| 127 | ? $this->class_map[ $class ] |
| 128 | : realpath( $this->get_path_from_namespace( $class ) . '.php' ); |
| 129 | |
| 130 | if ( ! $file ) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | require_once $file; |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get list of all auto-loadable class names from a directory |
| 141 | * |
| 142 | * @param $namespace |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | public function get_class_names_from_dir( $namespace ) { |
| 147 | $classes = []; |
| 148 | $namespace = rtrim( $namespace, '\\' ) . '\\'; |
| 149 | |
| 150 | foreach ( $this->class_map as $class => $path ) { |
| 151 | // Check if it the same, but only 1 level deep |
| 152 | if ( strpos( $class, $namespace ) !== 0 || false !== strpos( '\\', str_replace( $namespace, '', $class ) ) ) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | $classes[] = $class; |
| 157 | } |
| 158 | |
| 159 | if ( empty( $classes ) ) { |
| 160 | $classes = $this->get_class_names_from_filesystem( $namespace ); |
| 161 | } |
| 162 | |
| 163 | return $classes; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @param string $namespace |
| 168 | * |
| 169 | * @return array |
| 170 | */ |
| 171 | protected function get_class_names_from_filesystem( $namespace ) { |
| 172 | $classes = []; |
| 173 | $namespace_path = realpath( $this->get_path_from_namespace( $namespace ) ); |
| 174 | |
| 175 | if ( $namespace_path ) { |
| 176 | $iterator = new FilesystemIterator( $namespace_path, FilesystemIterator::SKIP_DOTS ); |
| 177 | |
| 178 | /* @var DirectoryIterator $leaf */ |
| 179 | foreach ( $iterator as $leaf ) { |
| 180 | // Exclude system files |
| 181 | if ( 0 === strpos( $leaf->getBasename(), '.' ) ) { |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | if ( 'php' === $leaf->getExtension() ) { |
| 186 | $classes[] = $namespace . pathinfo( $leaf->getBasename(), PATHINFO_FILENAME ); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return $classes; |
| 192 | } |
| 193 | } |