prefix = $prefix; $this->abspath = $abspath; spl_autoload_register( array( $this, 'autoloader' ) ); } /** * Autoloader. * * @param string $class_name */ public function autoloader( $class_name ) { if ( ! $this->class_belongs_to_plugin( $class_name ) ) { return; } $path = $this->get_classes_directory() . $this->get_class_path( $class_name ); if ( file_exists( $path ) ) { require_once $path; } } /** * Class belong to plugin. * * @param string $class_name * * @return bool */ protected function class_belongs_to_plugin( $class_name ) { if ( 0 !== strpos( $class_name, $this->vendor . '\\' . $this->prefix . '\\' ) ) { return false; } return true; } /** * Get class path. * * @param string $class_name * * @return string */ protected function get_class_path( $class_name ) { $parts = explode( '\\', strtolower( $class_name ) ); $parts = array_slice( $parts, 2 ); $filename = implode( DIRECTORY_SEPARATOR, $parts ) . '.php'; return str_replace( '_', '-', strtolower( $filename ) ); } /** * Get classes directory. * * @return string */ protected function get_classes_directory() { return $this->abspath . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR; } } }