PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-autoloader.php

class-autoloader.php in ActivityPub trunk, at includes/class-autoloader.php

107 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Autoloader for Activitypub.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub;
9
10 /**
11 * An Autoloader that respects WordPress's filename standards.
12 */
13 class Autoloader {
14
15 /**
16 * Namespace separator.
17 */
18 const NS_SEPARATOR = '\\';
19
20 /**
21 * The prefix to compare classes against.
22 *
23 * @var string
24 * @access protected
25 */
26 protected $prefix;
27
28 /**
29 * Length of the prefix string.
30 *
31 * @var int
32 * @access protected
33 */
34 protected $prefix_length;
35
36 /**
37 * Path to the file to be loaded.
38 *
39 * @var string
40 * @access protected
41 */
42 protected $path;
43
44 /**
45 * Constructor.
46 *
47 * @param string $prefix Namespace prefix all classes have in common.
48 * @param string $path Path to the files to be loaded.
49 */
50 public function __construct( $prefix, $path ) {
51 $this->prefix = $prefix;
52 $this->prefix_length = \strlen( $prefix );
53 $this->path = \rtrim( $path . '/' );
54 }
55
56 /**
57 * Registers Autoloader's autoload function.
58 *
59 * @throws \Exception When autoload_function cannot be registered.
60 *
61 * @param string $prefix Namespace prefix all classes have in common.
62 * @param string $path Path to the files to be loaded.
63 */
64 public static function register_path( $prefix, $path ) {
65 $loader = new self( $prefix, $path );
66 \spl_autoload_register( array( $loader, 'load' ) );
67 }
68
69 /**
70 * Loads a class if its namespace starts with `$this->prefix`.
71 *
72 * @param string $class_name The class to be loaded.
73 */
74 public function load( $class_name ) {
75 if ( \strpos( $class_name, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
76 return;
77 }
78
79 // Strip prefix from the start (ala PSR-4).
80 $class_name = \substr( $class_name, $this->prefix_length + 1 );
81 $class_name = \strtolower( $class_name );
82 $dir = '';
83
84 $last_ns_pos = \strripos( $class_name, self::NS_SEPARATOR );
85 if ( false !== $last_ns_pos ) {
86 $namespace = \substr( $class_name, 0, $last_ns_pos );
87 $namespace = \str_replace( '_', '-', $namespace );
88 $class_name = \substr( $class_name, $last_ns_pos + 1 );
89 $dir = \str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
90 }
91
92 $path = $this->path . $dir . 'class-' . \str_replace( '_', '-', $class_name ) . '.php';
93
94 if ( ! \file_exists( $path ) ) {
95 $path = $this->path . $dir . 'interface-' . \str_replace( '_', '-', $class_name ) . '.php';
96 }
97
98 if ( ! \file_exists( $path ) ) {
99 $path = $this->path . $dir . 'trait-' . \str_replace( '_', '-', $class_name ) . '.php';
100 }
101
102 if ( \file_exists( $path ) ) {
103 require_once $path;
104 }
105 }
106 }
107