Core
3 days ago
Util
3 days ago
Check_Email_Encode_Tab.php
3 days ago
Check_Email_Notify_Tab.php
3 days ago
Check_Email_SMTP_Tab.php
3 days ago
class-check-email-header-parser.php
3 days ago
class-check-email-log-autoloader.php
3 days ago
class-check-email-newsletter.php
3 days ago
deactivate-feedback.php
3 days ago
helper-function.php
3 days ago
install.php
3 days ago
class-check-email-log-autoloader.php
107 lines
| 1 | <?php namespace CheckEmail; |
| 2 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 3 | class Check_Email_Log_Autoloader { |
| 4 | |
| 5 | protected $prefixes = array(); |
| 6 | |
| 7 | protected $files = array(); |
| 8 | |
| 9 | public function register() { |
| 10 | spl_autoload_register( array( $this, 'load_class' ) ); |
| 11 | |
| 12 | // file exists check is already done in `add_file`. |
| 13 | foreach ( $this->files as $file ) { |
| 14 | $this->require_file( $file ); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public function add_namespace( $prefix, $base_dir, $prepend = false ) { |
| 19 | // normalize namespace prefix |
| 20 | $prefix = trim( $prefix, '\\' ) . '\\'; |
| 21 | |
| 22 | // normalize the base directory with a trailing separator |
| 23 | $base_dir = rtrim( $base_dir, DIRECTORY_SEPARATOR ) . '/'; |
| 24 | |
| 25 | // initialize the namespace prefix array |
| 26 | if ( false === isset( $this->prefixes[ $prefix ] ) ) { |
| 27 | $this->prefixes[ $prefix ] = array(); |
| 28 | } |
| 29 | |
| 30 | // retain the base directory for the namespace prefix |
| 31 | if ( $prepend ) { |
| 32 | array_unshift( $this->prefixes[ $prefix ], $base_dir ); |
| 33 | } else { |
| 34 | array_push( $this->prefixes[ $prefix ], $base_dir ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function add_file( $filename ) { |
| 39 | if ( ! in_array( $filename, $this->files, true ) ) { |
| 40 | $this->files[] = $filename; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public function load_class( $class ) { |
| 45 | // the current namespace prefix |
| 46 | $prefix = $class; |
| 47 | // work backwards through the namespace names of the fully-qualified |
| 48 | // class name to find a mapped file name |
| 49 | while ( false !== $pos = strrpos( $prefix, '\\' ) ) { |
| 50 | |
| 51 | // retain the trailing namespace separator in the prefix |
| 52 | $prefix = substr( $class, 0, $pos + 1 ); |
| 53 | |
| 54 | // the rest is the relative class name |
| 55 | $relative_class = substr( $class, $pos + 1 ); |
| 56 | |
| 57 | // try to load a mapped file for the prefix and relative class |
| 58 | $mapped_file = $this->load_mapped_file( $prefix, $relative_class ); |
| 59 | if ( $mapped_file !== false ) { |
| 60 | return $mapped_file; |
| 61 | } |
| 62 | |
| 63 | // remove the trailing namespace separator for the next iteration |
| 64 | // of strrpos() |
| 65 | $prefix = rtrim( $prefix, '\\' ); |
| 66 | } |
| 67 | |
| 68 | // never found a mapped file |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | protected function load_mapped_file( $prefix, $relative_class ) { |
| 73 | // are there any base directories for this namespace prefix? |
| 74 | if ( false === isset( $this->prefixes[ $prefix ] ) ) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | // look through base directories for this namespace prefix |
| 79 | foreach ( $this->prefixes[ $prefix ] as $base_dir ) { |
| 80 | // replace the namespace prefix with the base directory, |
| 81 | // replace namespace separators with directory separators |
| 82 | // in the relative class name, append with .php |
| 83 | $file = $base_dir |
| 84 | . str_replace( '\\', '/', $relative_class ) |
| 85 | . '.php'; |
| 86 | |
| 87 | // if the mapped file exists, require it |
| 88 | if ( $this->require_file( $file ) ) { |
| 89 | // yes, we're done |
| 90 | return $file; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // never found it |
| 95 | return false; |
| 96 | } |
| 97 | protected function require_file( $file ) { |
| 98 | if ( file_exists( $file ) ) { |
| 99 | require_once $file; |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 |