class-wprun-autoloader.php
156 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPRun_Autoloader_1x0x0 |
| 4 | * |
| 5 | * @package WPRun |
| 6 | * @category WordPress Library |
| 7 | * @version 1.0.0 |
| 8 | |
| 9 | * @link https://www.webfactoryltd.com/ |
| 10 | */ |
| 11 | class WPRun_Autoloader_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | private $settings = array( |
| 18 | 'file_name_prefix' => 'class-', |
| 19 | 'replace_dashes_with' => '-', |
| 20 | ); |
| 21 | |
| 22 | /** |
| 23 | * @var array |
| 24 | */ |
| 25 | private $paths = array(); |
| 26 | |
| 27 | /** |
| 28 | * Constructor |
| 29 | * @param array $settings Optional |
| 30 | */ |
| 31 | final public function __construct( array $settings = array() ) |
| 32 | { |
| 33 | $this->settings = wp_parse_args( $settings, $this->settings ); |
| 34 | |
| 35 | spl_autoload_register( array( $this, 'load_class' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Add path to folder containing classes |
| 40 | * @param string $path |
| 41 | * @param boolean $include_subfolders Optional |
| 42 | * @return void |
| 43 | */ |
| 44 | final public function add_path( $path, $include_subfolders = false ) |
| 45 | { |
| 46 | $absolute_path = self::get_absolute_path( $path ); |
| 47 | |
| 48 | if ( ! is_dir( $absolute_path ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( in_array( $absolute_path, $this->paths ) ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $this->paths[] = $absolute_path; |
| 57 | |
| 58 | // include subfolders |
| 59 | if ( true === $include_subfolders ) { |
| 60 | $entries = scandir( $absolute_path ); |
| 61 | |
| 62 | foreach ( $entries as $entry ) { |
| 63 | if ( '.' === $entry || '..' === $entry ) { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | $item = $absolute_path . DIRECTORY_SEPARATOR . $entry; |
| 68 | |
| 69 | if ( ! is_dir( $item ) ) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | $this->add_path( $item, true ); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get all paths |
| 80 | * @return array |
| 81 | */ |
| 82 | final public function get_paths() |
| 83 | { |
| 84 | return $this->paths; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Loads a class file |
| 89 | * @param string $class_name |
| 90 | * @return void |
| 91 | */ |
| 92 | public function load_class( $class_name ) |
| 93 | { |
| 94 | // remove version postfix |
| 95 | $pure_class_name = preg_replace( '/_\d+x\d+x\d+/', '', $class_name ); |
| 96 | |
| 97 | $file_name = ''; |
| 98 | $file_name .= $this->settings[ 'file_name_prefix' ]; |
| 99 | $file_name .= str_replace( '_', $this->settings[ 'replace_dashes_with' ], $pure_class_name ); |
| 100 | $file_name .= '.php'; |
| 101 | |
| 102 | $lower_file_name = strtolower( $file_name ); |
| 103 | |
| 104 | foreach ( $this->paths as $path ) { |
| 105 | $file_path = $path . DIRECTORY_SEPARATOR . $lower_file_name; |
| 106 | |
| 107 | if ( file_exists( $file_path ) ) { |
| 108 | require_once $file_path; |
| 109 | |
| 110 | // return if class is available else it was probably |
| 111 | // the wrong version (postfix) and should continue looking |
| 112 | if ( class_exists( $class_name ) ) { |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Convert to clean absolute path |
| 121 | * @param string $path |
| 122 | * @return string |
| 123 | */ |
| 124 | final static protected function get_absolute_path( $path ) |
| 125 | { |
| 126 | // convert to OS directory separator |
| 127 | $clean_path = str_replace( array( '/', '\\' ), DIRECTORY_SEPARATOR, $path ); |
| 128 | |
| 129 | $parts = array_filter( explode( DIRECTORY_SEPARATOR, $clean_path ), 'strlen' ); |
| 130 | |
| 131 | $absolutes = array(); |
| 132 | |
| 133 | foreach ( $parts as $part ) { |
| 134 | if ( '.' === $part ) { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | if ( '..' === $part ) { |
| 139 | array_pop( $absolutes ); |
| 140 | } else { |
| 141 | $absolutes[] = $part; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | $absolute_path = implode( DIRECTORY_SEPARATOR, $absolutes ); |
| 146 | |
| 147 | // check if given path started with directory separator |
| 148 | if ( DIRECTORY_SEPARATOR === $clean_path[ 0 ] ) { |
| 149 | $absolute_path = DIRECTORY_SEPARATOR . $absolute_path; |
| 150 | } |
| 151 | |
| 152 | return $absolute_path; |
| 153 | } |
| 154 | |
| 155 | } |
| 156 |