| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class is responsible for locating and loading the autoloader file used in the plugin. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace ISC; |
| 7 |
|
| 8 |
/** |
| 9 |
* Autoloader. |
| 10 |
*/ |
| 11 |
class Autoloader { |
| 12 |
|
| 13 |
/** |
| 14 |
* Hold autoloader. |
| 15 |
* |
| 16 |
* @var mixed |
| 17 |
*/ |
| 18 |
private $autoloader; |
| 19 |
|
| 20 |
/** |
| 21 |
* Main instance |
| 22 |
* |
| 23 |
* Ensure only one instance is loaded or can be loaded. |
| 24 |
* |
| 25 |
* @return Autoloader |
| 26 |
*/ |
| 27 |
public static function get(): Autoloader { |
| 28 |
static $instance; |
| 29 |
|
| 30 |
if ( null === $instance ) { |
| 31 |
$instance = new Autoloader(); |
| 32 |
} |
| 33 |
|
| 34 |
return $instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get hold autoloader. |
| 39 |
* |
| 40 |
* @return mixed |
| 41 |
*/ |
| 42 |
public function get_autoloader() { |
| 43 |
return $this->autoloader; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get plugin directory. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_directory(): string { |
| 52 |
return ISCPATH; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Runs this initializer. |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function initialize(): void { |
| 61 |
$this->autoloader = require $this->locate(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Locate the autoload file |
| 66 |
* |
| 67 |
* This function searches for the autoload file in the packages directory and vendor directory. |
| 68 |
* |
| 69 |
* @return bool|string |
| 70 |
*/ |
| 71 |
private function locate() { |
| 72 |
$directory = $this->get_directory(); |
| 73 |
$lib = $directory . 'lib/autoload.php'; |
| 74 |
$vendors = $directory . 'vendor/autoload.php'; |
| 75 |
|
| 76 |
if ( is_readable( $lib ) && ( ! self::is_test() ) ) { |
| 77 |
return $lib; |
| 78 |
} |
| 79 |
|
| 80 |
if ( is_readable( $vendors ) ) { |
| 81 |
return $vendors; |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if this is a test run. |
| 89 |
* |
| 90 |
* @return bool |
| 91 |
*/ |
| 92 |
public static function is_test(): bool { |
| 93 |
return ( isset( $_SERVER['TEST_SITE_USER_AGENT'] ) && $_SERVER['TEST_SITE_USER_AGENT'] === 'ISC_Test' ); // wp-browser unit test |
| 94 |
} |
| 95 |
} |