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

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