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

233 lines 6.1 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 Config\Provider::class,
66 Update\Provider::class,
67 View\Provider::class,
68 Page_Cache\Meta\Provider::class,
69 Page_Cache\Provider::class,
70 ];
71
72 /**
73 * An array of providers to register within the container after
74 * WordPress is fully bootstrapped.
75 *
76 * @since 0.1.0
77 *
78 * @var array<int, class-string<Service_Provider>>
79 */
80 private array $providers = [
81 Database\Provider::class,
82 Storage\Provider::class,
83 Lock\Provider::class,
84 Assets\Provider::class,
85 Page_Cache\Purge\Provider::class,
86 Preload\Provider::class,
87 Admin\Provider::class,
88 API\Provider::class,
89 Notices\Provider::class,
90 Pipelines\Provider::class,
91 Telemetry\Provider::class,
92 WP_CLI\Provider::class,
93 Shutdown\Provider::class,
94 Dom\Provider::class,
95 ];
96
97 /**
98 * The singleton instance for the plugin.
99 *
100 * @var Core
101 */
102 private static self $instance;
103
104 /**
105 * @param string $plugin_file The full server path to the main plugin file.
106 * @param Container $container The container instance.
107 */
108 private function __construct(
109 string $plugin_file,
110 Container $container
111 ) {
112 $this->plugin_file = $plugin_file;
113 $this->container = $container;
114 $this->container->singleton( ContainerInterface::class, $this->container );
115 $this->container->singleton( StellarContainerInterface::class, $this->container );
116
117 // Set container variables available to pre bootstrap providers.
118 $this->container->setVar( self::PLUGIN_FILE, $this->plugin_file );
119 $this->container->setVar( self::PLUGIN_VERSION, $this->get_plugin_version() );
120 $this->container->setVar( self::ADVANCED_CACHE_PATH, WP_CONTENT_DIR . '/advanced-cache.php' );
121
122 // Set the cache dir early so all providers can use it.
123 $this->container->when( Cache_Path::class )
124 ->needs( '$cache_dir' )
125 ->give( WP_CONTENT_DIR . '/cache' );
126
127 // Register pre bootstrap providers early.
128 foreach ( $this->early_providers as $provider ) {
129 $this->container->get( $provider )->register();
130 }
131 }
132
133 /**
134 * Get the singleton instance of our plugin.
135 *
136 * @param string|null $plugin_file The full server path to the main plugin file.
137 * @param Container|null $container The container instance.
138 *
139 * @throws InvalidArgumentException If no existing instance and no plugin file or container is provided.
140 *
141 * @return Core
142 */
143 public static function instance( ?string $plugin_file = null, ?Container $container = null ): Core {
144 if ( ! isset( self::$instance ) ) {
145 if ( ! $plugin_file ) {
146 throw new InvalidArgumentException( 'You must provide a $plugin_file path' );
147 }
148
149 if ( ! $container ) {
150 throw new InvalidArgumentException( sprintf( 'You must provide a %s instance!', Container::class ) );
151 }
152
153 self::$instance = new self( $plugin_file, $container );
154 }
155
156 return self::$instance;
157 }
158
159 /**
160 * Initialize the plugin.
161 *
162 * @action plugins_loaded
163 *
164 * @return void
165 */
166 public function init(): void {
167 // Set remaining container variables now that WordPress is fully bootstrapped.
168 $this->container->setVar( self::PLUGIN_DIR, plugin_dir_path( $this->plugin_file ) );
169 $this->container->setVar( self::PLUGIN_URL, plugin_dir_url( $this->plugin_file ) );
170 $this->container->setVar( self::PLUGIN_BASENAME, plugin_basename( $this->plugin_file ) );
171
172 // Register remaining providers now that WP is fully bootstrapped.
173 foreach ( $this->providers as $class ) {
174 $this->container->get( $class )->register( $this->container );
175 }
176 }
177
178 /**
179 * Returns the container instance.
180 *
181 * @return Container
182 */
183 public function container(): Container {
184 return $this->container;
185 }
186
187 /**
188 * Prevent wakeup.
189 *
190 * @throws RuntimeException When attempting to wake up this instance.
191 */
192 public function __wakeup(): void {
193 throw new RuntimeException( 'method not implemented' );
194 }
195
196 /**
197 * Prevent sleep.
198 *
199 * @throws RuntimeException When attempting to sleep this instance.
200 */
201 public function __sleep(): array {
202 throw new RuntimeException( 'method not implemented' );
203 }
204
205 /**
206 * Prevent cloning.
207 *
208 * @throws RuntimeException When attempting to clone this instance.
209 */
210 private function __clone() {
211 throw new RuntimeException( 'Cloning not allowed' );
212 }
213
214 /**
215 * Get the plugin's current version.
216 *
217 * @throws RuntimeException If we are unable to retrieve the plugin version.
218 *
219 * @return string
220 */
221 private function get_plugin_version(): string {
222 $content = file_get_contents( $this->plugin_file, false, null, 0, 8 * KB_IN_BYTES );
223
224 $pattern = '/^ \* Version:\s*(.+)$/m';
225
226 if ( preg_match( $pattern, $content, $matches ) ) {
227 return trim( $matches[1] );
228 }
229
230 throw new RuntimeException( sprintf( 'Unable to read plugin version from "%s"', $this->plugin_file ) );
231 }
232 }
233