PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.8.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.8.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.8.0, at src/Performance/Core.php

257 lines 6.7 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\Contracts\Service_Provider;
17 use SolidWP\Performance\Page_Cache\Cache_Path;
18 use SolidWP\Performance\Psr\Container\ContainerInterface;
19 use SolidWP\Performance\StellarWP\ContainerContract\ContainerInterface as StellarContainerInterface;
20
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit;
23 }
24
25 /**
26 * The primary class responsible for booting up the plugin.
27 *
28 * @since 0.1.0
29 *
30 * @package SolidWP\Performance
31 */
32 final class Core {
33
34 public const PLUGIN_FILE = 'solid_performance.plugin_file';
35 public const PLUGIN_DIR = 'solid_performance.plugin_dir';
36 public const PLUGIN_URL = 'solid_performance.plugin_url';
37 public const PLUGIN_BASENAME = 'solid_performance.plugin_basename';
38 public const PLUGIN_VERSION = 'solid_performance.plugin_version';
39 public const ADVANCED_CACHE_PATH = 'solid_performance.advanced_cache_path';
40
41 /**
42 * The server path to the plugin's main file.
43 *
44 * @var string
45 */
46 private string $plugin_file;
47
48 /**
49 * The centralized container.
50 *
51 * @since 0.1.0
52 *
53 * @var Container
54 */
55 private Container $container;
56
57 /**
58 * An array of providers to register within the container before
59 * WordPress is fully bootstrapped, e.g. in advanced-cache.php.
60 *
61 * @var array<int, class-string<Service_Provider>>
62 */
63 private array $early_providers = [
64 Log\Provider::class,
65 Memoize\Provider::class,
66 Config\Provider::class,
67 View\Provider::class,
68 Cache_Delivery\Provider::class,
69 Page_Cache\Meta\Provider::class,
70 Page_Cache\Provider::class,
71
72 // This should always be last, to ensure dependencies have been built.
73 Update\Provider::class,
74 ];
75
76 /**
77 * An array of providers to register within the container after
78 * WordPress is fully bootstrapped.
79 *
80 * @since 0.1.0
81 *
82 * @var array<int, class-string<Service_Provider>>
83 */
84 private array $providers = [
85 Database\Provider::class,
86 Storage\Provider::class,
87 Lock\Provider::class,
88 Assets\Provider::class,
89 Page_Cache\Purge\Provider::class,
90 Preload\Provider::class,
91 Admin\Provider::class,
92 API\Provider::class,
93 Notices\Provider::class,
94 Pipelines\Provider::class,
95 Telemetry\Provider::class,
96 Image_Transformation\Provider::class,
97 WP_CLI\Provider::class,
98 Shutdown\Provider::class,
99 Dom\Provider::class,
100 Cron\Provider::class,
101 ];
102
103 /**
104 * The singleton instance for the plugin.
105 *
106 * @var Core
107 */
108 private static self $instance;
109
110 /**
111 * @param string $plugin_file The full server path to the main plugin file.
112 * @param Container $container The container instance.
113 */
114 private function __construct(
115 string $plugin_file,
116 Container $container
117 ) {
118 $this->plugin_file = $plugin_file;
119 $this->container = $container;
120 $this->container->singleton( ContainerInterface::class, $this->container );
121 $this->container->singleton( StellarContainerInterface::class, $this->container );
122
123 // Set container variables available to pre bootstrap providers.
124 $this->container->setVar( self::PLUGIN_FILE, $this->plugin_file );
125 $this->container->setVar( self::PLUGIN_VERSION, $this->get_plugin_version() );
126 $this->container->setVar( self::ADVANCED_CACHE_PATH, WP_CONTENT_DIR . '/advanced-cache.php' );
127
128 // Set the cache dir early so all providers can use it.
129 $this->register_mandatory_definitions();
130
131 // Register pre bootstrap providers early.
132 foreach ( $this->early_providers as $provider ) {
133 $this->container->get( $provider )->register();
134 }
135 }
136
137 /**
138 * Get the singleton instance of our plugin.
139 *
140 * @param string|null $plugin_file The full server path to the main plugin file.
141 * @param Container|null $container The container instance.
142 *
143 * @throws InvalidArgumentException If no existing instance and no plugin file or container is provided.
144 *
145 * @return Core
146 */
147 public static function instance( ?string $plugin_file = null, ?Container $container = null ): Core {
148 if ( ! isset( self::$instance ) ) {
149 if ( ! $plugin_file ) {
150 throw new InvalidArgumentException( 'You must provide a $plugin_file path' );
151 }
152
153 if ( ! $container ) {
154 throw new InvalidArgumentException( sprintf( 'You must provide a %s instance!', Container::class ) );
155 }
156
157 self::$instance = new self( $plugin_file, $container );
158 }
159
160 return self::$instance;
161 }
162
163 /**
164 * Initialize the plugin.
165 *
166 * @action plugins_loaded
167 *
168 * @return Core
169 */
170 public function init(): Core {
171 $this->register_mandatory_definitions();
172
173 // Set remaining container variables now that WordPress is fully bootstrapped.
174 $this->container->setVar( self::PLUGIN_DIR, plugin_dir_path( $this->plugin_file ) );
175 $this->container->setVar( self::PLUGIN_URL, plugin_dir_url( $this->plugin_file ) );
176 $this->container->setVar( self::PLUGIN_BASENAME, plugin_basename( $this->plugin_file ) );
177
178 // Register remaining providers now that WP is fully bootstrapped.
179 foreach ( $this->providers as $class ) {
180 $this->container->get( $class )->register( $this->container );
181 }
182
183 return self::$instance;
184 }
185
186 /**
187 * Returns the container instance.
188 *
189 * @return Container
190 */
191 public function container(): Container {
192 return $this->container;
193 }
194
195 /**
196 * Prevent wakeup.
197 *
198 * @throws RuntimeException When attempting to wake up this instance.
199 */
200 public function __wakeup(): void {
201 throw new RuntimeException( 'method not implemented' );
202 }
203
204 /**
205 * Prevent sleep.
206 *
207 * @throws RuntimeException When attempting to sleep this instance.
208 */
209 public function __sleep(): array {
210 throw new RuntimeException( 'method not implemented' );
211 }
212
213 /**
214 * Prevent cloning.
215 *
216 * @throws RuntimeException When attempting to clone this instance.
217 */
218 private function __clone() {
219 throw new RuntimeException( 'Cloning not allowed' );
220 }
221
222 /**
223 * Mandatory container definitions that run both early and on init.
224 *
225 * @return void
226 */
227 private function register_mandatory_definitions(): void {
228 $this->container->when( Cache_Path::class )
229 ->needs( '$cache_dir' )
230 ->give( WP_CONTENT_DIR . '/cache/solid-performance' );
231 }
232
233 /**
234 * Get the plugin's current version.
235 *
236 * @throws RuntimeException If we are unable to retrieve the plugin version.
237 *
238 * @return string
239 */
240 private function get_plugin_version(): string {
241 $content = file_get_contents( $this->plugin_file, false, null, 0, 8 * KB_IN_BYTES );
242
243 // Prevent race condition if the solid-performance file is already deleted.
244 if ( ! $content ) {
245 return '0.0.0';
246 }
247
248 $pattern = '/^ \* Version:\s*(.+)$/m';
249
250 if ( preg_match( $pattern, $content, $matches ) ) {
251 return trim( $matches[1] );
252 }
253
254 throw new RuntimeException( sprintf( 'Unable to read plugin version from "%s"', $this->plugin_file ) );
255 }
256 }
257