Plugin Directory

source: jetpack/trunk/vendor/jetpack-autoloader/class-php-autoloader.php @ 2832426

Last change on this file since 2832426 was 2832426, checked in by wpkaren, 20 months ago

Updating trunk to version 11.7-a.3

File size: 2.8 KB
Line 
1<?php
2/**
3 * This file was automatically generated by automattic/jetpack-autoloader.
4 *
5 * @package automattic/jetpack-autoloader
6 */
7
8namespace Automattic\Jetpack\Autoloader\jpf11009ded9fc4592b6a05b61ce272b3c_jetpack11_7_a_3;
9
10 // phpcs:ignore
11
12/**
13 * This class handles management of the actual PHP autoloader.
14 */
15class PHP_Autoloader {
16
17        /**
18         * Registers the autoloader with PHP so that it can begin autoloading classes.
19         *
20         * @param Version_Loader $version_loader The class loader to use in the autoloader.
21         */
22        public function register_autoloader( $version_loader ) {
23                // Make sure no other autoloaders are registered.
24                $this->unregister_autoloader();
25
26                // Set the global so that it can be used to load classes.
27                global $jetpack_autoloader_loader;
28                $jetpack_autoloader_loader = $version_loader;
29
30                // Ensure that the autoloader is first to avoid contention with others.
31                spl_autoload_register( array( self::class, 'load_class' ), true, true );
32        }
33
34        /**
35         * Unregisters the active autoloader so that it will no longer autoload classes.
36         */
37        public function unregister_autoloader() {
38                // Remove any v2 autoloader that we've already registered.
39                $autoload_chain = spl_autoload_functions();
40                if ( ! $autoload_chain ) {
41                        return;
42                }
43                foreach ( $autoload_chain as $autoloader ) {
44                        // We can identify a v2 autoloader using the namespace.
45                        $namespace_check = null;
46
47                        // Functions are recorded as strings.
48                        if ( is_string( $autoloader ) ) {
49                                $namespace_check = $autoloader;
50                        } elseif ( is_array( $autoloader ) && is_string( $autoloader[0] ) ) {
51                                // Static method calls have the class as the first array element.
52                                $namespace_check = $autoloader[0];
53                        } else {
54                                // Since the autoloader has only ever been a function or a static method we don't currently need to check anything else.
55                                continue;
56                        }
57
58                        // Check for the namespace without the generated suffix.
59                        if ( 'Automattic\\Jetpack\\Autoloader\\jp' === substr( $namespace_check, 0, 32 ) ) {
60                                spl_autoload_unregister( $autoloader );
61                        }
62                }
63
64                // Clear the global now that the autoloader has been unregistered.
65                global $jetpack_autoloader_loader;
66                $jetpack_autoloader_loader = null;
67        }
68
69        /**
70         * Loads a class file if one could be found.
71         *
72         * Note: This function is static so that the autoloader can be easily unregistered. If
73         * it was a class method we would have to unwrap the object to check the namespace.
74         *
75         * @param string $class_name The name of the class to autoload.
76         *
77         * @return bool Indicates whether or not a class file was loaded.
78         */
79        public static function load_class( $class_name ) {
80                global $jetpack_autoloader_loader;
81                if ( ! isset( $jetpack_autoloader_loader ) ) {
82                        return;
83                }
84
85                $file = $jetpack_autoloader_loader->find_class_file( $class_name );
86                if ( ! isset( $file ) ) {
87                        return false;
88                }
89
90                require $file;
91                return true;
92        }
93}
Note: See TracBrowser for help on using the repository browser.