-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathautoloader.php
More file actions
93 lines (79 loc) · 2.71 KB
/
Copy pathautoloader.php
File metadata and controls
93 lines (79 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* BerlinDB Autoloader.
*
* @package Database
* @subpackage Autoloader
* @copyright 2021-2022 - JJJ and all BerlinDB Contributors
* @license https://opensource.org/licenses/MIT MIT
* @since 2.0.0
*/
/**
* Register a closure to autoload BerlinDB classes.
*/
spl_autoload_register(
/**
* Closure for the autoloader.
*
* @since 2.0.0
* @param string $class_name A fully-qualified class name.
* @return void
*/
static function ( $class_name = '' ) {
$legacy_kern_classes = array(
'BerlinDB\\Database\\Column' => 'BerlinDB\\Database\\Kern\\Column',
'BerlinDB\\Database\\Index' => 'BerlinDB\\Database\\Kern\\Index',
'BerlinDB\\Database\\Query' => 'BerlinDB\\Database\\Kern\\Query',
'BerlinDB\\Database\\Row' => 'BerlinDB\\Database\\Kern\\Row',
'BerlinDB\\Database\\Schema' => 'BerlinDB\\Database\\Kern\\Schema',
'BerlinDB\\Database\\Table' => 'BerlinDB\\Database\\Kern\\Table',
);
if ( isset( $legacy_kern_classes[ $class_name ] ) ) {
$target = $legacy_kern_classes[ $class_name ];
$strip = str_replace( 'BerlinDB\\', '', $target );
$name = str_replace( '\\', DIRECTORY_SEPARATOR, $strip );
$file = sprintf( '%1$s/src/%2$s.php', __DIR__, $name );
if ( is_file( $file ) ) {
require_once $file;
if ( class_exists( $target, false ) && ! class_exists( $class_name, false ) ) {
class_alias( $target, $class_name );
}
}
return;
}
// Project namespace & length.
$root_namespace = 'BerlinDB\\';
$project_namespace = $root_namespace . 'Database\\';
$length = strlen( $project_namespace );
// Bail if class is not in this namespace.
if ( 0 !== strncmp( $project_namespace, $class_name, $length ) ) {
return;
}
// Setup file parts.
$strip = str_replace( $root_namespace, '', $class_name );
$name = str_replace( '\\', DIRECTORY_SEPARATOR, $strip );
// Parse class and namespace to file.
$format = '%1$s/src/%2$s.php';
$path = __DIR__;
$file = sprintf( $format, $path, $name );
// Bail if file does not exist.
if ( ! is_file( $file ) ) {
return;
}
// Require the file.
require_once $file;
/*
* Eagerly register this Kern class's legacy alias, if it has one. PHP's
* instanceof operator does not trigger autoloading, so an `instanceof
* \BerlinDB\Database\Index` check would resolve to false until the alias
* name was loaded some other way. Creating the alias as soon as the Kern
* class loads keeps legacy type checks (instanceof / is_a) correct.
*/
$legacy_alias = array_search( $class_name, $legacy_kern_classes, true );
if ( ( false !== $legacy_alias ) && ! class_exists( $legacy_alias, false ) ) {
class_alias( $class_name, $legacy_alias );
}
},
true,
true
);