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

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