Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.18% covered (success)
98.18%
54 / 55
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Container
98.18% covered (success)
98.18%
54 / 55
80.00% covered (warning)
80.00%
4 / 5
9
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 register_shared_dependencies
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 register_dependencies
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
1
 initialize_globals
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/* HEADER */ // phpcs:ignore
3
4/**
5 * This class manages the files and dependencies of the autoloader.
6 */
7class Container {
8
9    /**
10     * Since each autoloader's class files exist within their own namespace we need a map to
11     * convert between the local class and a shared key. Note that no version checking is
12     * performed on these dependencies and the first autoloader to register will be the
13     * one that is utilized.
14     */
15    const SHARED_DEPENDENCY_KEYS = array(
16        Hook_Manager::class => 'Hook_Manager',
17    );
18
19    /**
20     * A map of all the dependencies we've registered with the container and created.
21     *
22     * @var array
23     */
24    protected $dependencies;
25
26    /**
27     * The constructor.
28     */
29    public function __construct() {
30        $this->dependencies = array();
31
32        $this->register_shared_dependencies();
33        $this->register_dependencies();
34        $this->initialize_globals();
35    }
36
37    /**
38     * Gets a dependency out of the container.
39     *
40     * @param string $class The class to fetch.
41     *
42     * @return mixed
43     * @throws \InvalidArgumentException When a class that isn't registered with the container is fetched.
44     */
45    public function get( $class ) {
46        if ( ! isset( $this->dependencies[ $class ] ) ) {
47            throw new \InvalidArgumentException( "Class '$class' is not registered with the container." );
48        }
49
50        return $this->dependencies[ $class ];
51    }
52
53    /**
54     * Registers all of the dependencies that are shared between all instances of the autoloader.
55     */
56    private function register_shared_dependencies() {
57        global $jetpack_autoloader_container_shared;
58        if ( ! isset( $jetpack_autoloader_container_shared ) ) {
59            $jetpack_autoloader_container_shared = array();
60        }
61
62        $key = self::SHARED_DEPENDENCY_KEYS[ Hook_Manager::class ];
63        if ( ! isset( $jetpack_autoloader_container_shared[ $key ] ) ) {
64            require_once __DIR__ . '/class-hook-manager.php';
65            $jetpack_autoloader_container_shared[ $key ] = new Hook_Manager();
66        }
67        $this->dependencies[ Hook_Manager::class ] = &$jetpack_autoloader_container_shared[ $key ];
68    }
69
70    /**
71     * Registers all of the dependencies with the container.
72     */
73    private function register_dependencies() {
74        require_once __DIR__ . '/class-path-processor.php';
75        $this->dependencies[ Path_Processor::class ] = new Path_Processor();
76
77        require_once __DIR__ . '/class-plugin-locator.php';
78        $this->dependencies[ Plugin_Locator::class ] = new Plugin_Locator(
79            $this->get( Path_Processor::class )
80        );
81
82        require_once __DIR__ . '/class-version-selector.php';
83        $this->dependencies[ Version_Selector::class ] = new Version_Selector();
84
85        require_once __DIR__ . '/class-autoloader-locator.php';
86        $this->dependencies[ Autoloader_Locator::class ] = new Autoloader_Locator(
87            $this->get( Version_Selector::class )
88        );
89
90        require_once __DIR__ . '/class-php-autoloader.php';
91        $this->dependencies[ PHP_Autoloader::class ] = new PHP_Autoloader();
92
93        require_once __DIR__ . '/class-manifest-reader.php';
94        $this->dependencies[ Manifest_Reader::class ] = new Manifest_Reader(
95            $this->get( Version_Selector::class )
96        );
97
98        require_once __DIR__ . '/class-plugins-handler.php';
99        $this->dependencies[ Plugins_Handler::class ] = new Plugins_Handler(
100            $this->get( Plugin_Locator::class ),
101            $this->get( Path_Processor::class )
102        );
103
104        require_once __DIR__ . '/class-autoloader-handler.php';
105        $this->dependencies[ Autoloader_Handler::class ] = new Autoloader_Handler(
106            $this->get( PHP_Autoloader::class ),
107            $this->get( Hook_Manager::class ),
108            $this->get( Manifest_Reader::class ),
109            $this->get( Version_Selector::class )
110        );
111
112        require_once __DIR__ . '/class-latest-autoloader-guard.php';
113        $this->dependencies[ Latest_Autoloader_Guard::class ] = new Latest_Autoloader_Guard(
114            $this->get( Plugins_Handler::class ),
115            $this->get( Autoloader_Handler::class ),
116            $this->get( Autoloader_Locator::class )
117        );
118
119        // Register any classes that we will use elsewhere.
120        require_once __DIR__ . '/class-version-loader.php';
121        require_once __DIR__ . '/class-shutdown-handler.php';
122    }
123
124    /**
125     * Initializes any of the globals needed by the autoloader.
126     */
127    private function initialize_globals() {
128        /*
129         * This global was retired in version 2.9. The value is set to 'false' to maintain
130         * compatibility with older versions of the autoloader.
131         */
132        global $jetpack_autoloader_including_latest;
133        $jetpack_autoloader_including_latest = false;
134
135        // Not all plugins can be found using the locator. In cases where a plugin loads the autoloader
136        // but was not discoverable, we will record them in this array to track them as "active".
137        global $jetpack_autoloader_activating_plugins_paths;
138        if ( ! isset( $jetpack_autoloader_activating_plugins_paths ) ) {
139            $jetpack_autoloader_activating_plugins_paths = array();
140        }
141    }
142}