PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.1.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.1.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Core.php

Core.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.1.0, at src/Performance/Core.php

173 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The main plugin class.
4 *
5 * @since 0.1.0
6 *
7 * @package SolidWP\Performance
8 */
9
10 declare( strict_types=1 );
11
12 namespace SolidWP\Performance;
13
14 use InvalidArgumentException;
15 use RuntimeException;
16 use SolidWP\Performance\StellarWP\ContainerContract\ContainerInterface;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * The primary class responsible for booting up the plugin.
24 *
25 * @since 0.1.0
26 *
27 * @package SolidWP\Performance
28 */
29 final class Core {
30
31 public const PLUGIN_FILE = 'solid_performance.plugin_file';
32 public const PLUGIN_DIR = 'solid_performance.plugin_dir';
33 public const PLUGIN_URL = 'solid_performance.plugin_url';
34
35 /**
36 * The server path to the plugin's main file.
37 *
38 * @var string
39 */
40 private string $plugin_file;
41
42 /**
43 * The centralized container.
44 *
45 * @since 0.1.0
46 *
47 * @var Container
48 */
49 private Container $container;
50
51 /**
52 * An array of providers to register within the container.
53 *
54 * @since 0.1.0
55 *
56 * @var array<int,string>
57 */
58 private array $providers = [
59 Config\Provider::class,
60 Assets\Provider::class,
61 View\Provider::class,
62 Admin\Provider::class,
63 API\Provider::class,
64 Notices\Provider::class,
65 Page_Cache\Provider::class,
66 Pipelines\Provider::class,
67 Telemetry\Provider::class,
68 WP_CLI\Provider::class,
69 Shutdown\Provider::class,
70 ];
71
72 /**
73 * The singleton instance for the plugin.
74 *
75 * @var Core
76 */
77 private static self $instance;
78
79 /**
80 * @param string $plugin_file The full server path to the main plugin file.
81 * @param Container $container The container instance.
82 */
83 private function __construct(
84 string $plugin_file,
85 Container $container
86 ) {
87 $this->plugin_file = $plugin_file;
88 $this->container = $container;
89 $this->container->singleton( \SolidWP\Performance\Psr\Container\ContainerInterface::class, $this->container );
90 $this->container->singleton( ContainerInterface::class, $this->container );
91 }
92
93 /**
94 * Get the singleton instance of our plugin.
95 *
96 * @param string|null $plugin_file The full server path to the main plugin file.
97 * @param Container|null $container The container instance.
98 *
99 * @throws InvalidArgumentException If no existing instance and no plugin file or container is provided.
100 *
101 * @return Core
102 */
103 public static function instance( ?string $plugin_file = null, ?Container $container = null ): Core {
104 if ( ! isset( self::$instance ) ) {
105 if ( ! $plugin_file ) {
106 throw new InvalidArgumentException( 'You must provide a $plugin_file path' );
107 }
108
109 if ( ! $container ) {
110 throw new InvalidArgumentException( sprintf( 'You must provide a %s instance!', Container::class ) );
111 }
112
113 self::$instance = new self( $plugin_file, $container );
114 }
115
116 return self::$instance;
117 }
118
119 /**
120 * Initialize the plugin.
121 *
122 * @action plugins_loaded
123 *
124 * @return void
125 */
126 public function init(): void {
127 $this->container->setVar( self::PLUGIN_FILE, $this->plugin_file );
128 $this->container->setVar( self::PLUGIN_DIR, plugin_dir_path( $this->plugin_file ) );
129 $this->container->setVar( self::PLUGIN_URL, plugin_dir_url( $this->plugin_file ) );
130
131 // Register all providers.
132 foreach ( $this->providers as $class ) {
133 $this->container->get( $class )->register( $this->container );
134 }
135 }
136
137 /**
138 * Returns the container instance.
139 *
140 * @return Container
141 */
142 public function container(): Container {
143 return $this->container;
144 }
145
146 /**
147 * Prevent wakeup.
148 *
149 * @throws RuntimeException When attempting to wake up this instance.
150 */
151 public function __wakeup(): void {
152 throw new RuntimeException( 'method not implemented' );
153 }
154
155 /**
156 * Prevent sleep.
157 *
158 * @throws RuntimeException When attempting to sleep this instance.
159 */
160 public function __sleep(): array {
161 throw new RuntimeException( 'method not implemented' );
162 }
163
164 /**
165 * Prevent cloning.
166 *
167 * @throws RuntimeException When attempting to clone this instance.
168 */
169 private function __clone() {
170 throw new RuntimeException( 'Cloning not allowed' );
171 }
172 }
173