| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Module Base. |
| 11 |
* |
| 12 |
* An abstract class providing the properties and methods needed to |
| 13 |
* manage and handle modules in inheriting classes. |
| 14 |
* |
| 15 |
* @abstract |
| 16 |
*/ |
| 17 |
abstract class Module_Base { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module class reflection. |
| 21 |
* |
| 22 |
* Holds the information about a class. |
| 23 |
* @access private |
| 24 |
* |
| 25 |
* @var ?\ReflectionClass |
| 26 |
*/ |
| 27 |
private ?\ReflectionClass $reflection = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Module components. |
| 31 |
* |
| 32 |
* Holds the module components. |
| 33 |
* @access private |
| 34 |
* |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
private array $components = []; |
| 38 |
|
| 39 |
/** |
| 40 |
* Module routes. |
| 41 |
* |
| 42 |
* Holds the module REST route instances. |
| 43 |
* @access private |
| 44 |
* |
| 45 |
* @var array |
| 46 |
*/ |
| 47 |
private array $routes = []; |
| 48 |
|
| 49 |
/** |
| 50 |
* Module instance. |
| 51 |
* |
| 52 |
* Holds the module instance. |
| 53 |
* @access protected |
| 54 |
* |
| 55 |
* @var Module_Base[] |
| 56 |
*/ |
| 57 |
protected static array $instances = []; |
| 58 |
|
| 59 |
/** |
| 60 |
* Get module name. |
| 61 |
* |
| 62 |
* Retrieve the module name. |
| 63 |
* @access public |
| 64 |
* @abstract |
| 65 |
* |
| 66 |
* @return string Module name. |
| 67 |
*/ |
| 68 |
abstract public function get_name(): string; |
| 69 |
|
| 70 |
/** |
| 71 |
* Instance. |
| 72 |
* |
| 73 |
* Ensures only one instance of the module class is loaded or can be loaded. |
| 74 |
* @access public |
| 75 |
* @static |
| 76 |
* |
| 77 |
* @return Module_Base An instance of the class. |
| 78 |
*/ |
| 79 |
public static function instance(): Module_Base { |
| 80 |
$class_name = static::class_name(); |
| 81 |
|
| 82 |
if ( empty( static::$instances[ $class_name ] ) ) { |
| 83 |
static::$instances[ $class_name ] = new static(); // @codeCoverageIgnore |
| 84 |
} |
| 85 |
|
| 86 |
return static::$instances[ $class_name ]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* is_active |
| 91 |
* @access public |
| 92 |
* @static |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
public static function is_active(): bool { |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Class name. |
| 101 |
* |
| 102 |
* Retrieve the name of the class. |
| 103 |
* @access public |
| 104 |
* @static |
| 105 |
*/ |
| 106 |
public static function class_name(): string { |
| 107 |
return get_called_class(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Clone. |
| 112 |
* |
| 113 |
* Disable class cloning and throw an error on object clone. |
| 114 |
* |
| 115 |
* The whole idea of the singleton design pattern is that there is a single |
| 116 |
* object. Therefore, we don't want the object to be cloned. |
| 117 |
* |
| 118 |
* @access public |
| 119 |
*/ |
| 120 |
public function __clone() { |
| 121 |
// Cloning instances of the class is forbidden |
| 122 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'cookiez' ), '0.0.1' ); // @codeCoverageIgnore |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Wakeup. |
| 127 |
* |
| 128 |
* Disable unserializing of the class. |
| 129 |
* @access public |
| 130 |
*/ |
| 131 |
public function __wakeup() { |
| 132 |
// Unserializing instances of the class is forbidden |
| 133 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'cookiez' ), '0.0.1' ); // @codeCoverageIgnore |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @access public |
| 138 |
*/ |
| 139 |
public function get_reflection(): \ReflectionClass { |
| 140 |
if ( null === $this->reflection ) { |
| 141 |
try { |
| 142 |
$this->reflection = new \ReflectionClass( $this ); |
| 143 |
} catch ( \ReflectionException $e ) { |
| 144 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 145 |
error_log( $e->getMessage() ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $this->reflection; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Add module component. |
| 155 |
* |
| 156 |
* Add new component to the current module. |
| 157 |
* @access public |
| 158 |
* |
| 159 |
* @param string $id Component ID. |
| 160 |
* @param mixed $instance An instance of the component. |
| 161 |
*/ |
| 162 |
public function add_component( string $id, $instance ) { |
| 163 |
$this->components[ $id ] = $instance; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @access public |
| 168 |
* @return array |
| 169 |
*/ |
| 170 |
public function get_components(): array { |
| 171 |
return $this->components; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get module component. |
| 176 |
* |
| 177 |
* Retrieve the module component. |
| 178 |
* @access public |
| 179 |
* |
| 180 |
* @param string $id Component ID. |
| 181 |
* |
| 182 |
* @return mixed An instance of the component, or `false` if the component |
| 183 |
* doesn't exist. |
| 184 |
* @codeCoverageIgnore |
| 185 |
*/ |
| 186 |
public function get_component( string $id ) { |
| 187 |
if ( isset( $this->components[ $id ] ) ) { |
| 188 |
return $this->components[ $id ]; |
| 189 |
} |
| 190 |
|
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Retrieve the namespace of the class |
| 196 |
* |
| 197 |
* @access public |
| 198 |
* @static |
| 199 |
*/ |
| 200 |
public static function namespace_name(): string { |
| 201 |
$class_name = static::class_name(); |
| 202 |
return substr( $class_name, 0, strrpos( $class_name, '\\' ) ); |
| 203 |
} |
| 204 |
|
| 205 |
public static function component_list(): array { |
| 206 |
return []; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Adds an array of components. |
| 211 |
* Assumes namespace structure contains `\Components\` |
| 212 |
* |
| 213 |
* @param array|null $components_ids => component's class name. |
| 214 |
*/ |
| 215 |
public function register_components( ?array $components_ids = null ) { |
| 216 |
$namespace = static::namespace_name(); |
| 217 |
$components_ids = $components_ids ?? static::component_list(); |
| 218 |
foreach ( $components_ids as $component_id ) { |
| 219 |
$class_name = $namespace . '\\Components\\' . $component_id; |
| 220 |
$this->add_component( $component_id, new $class_name() ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* @access public |
| 226 |
* |
| 227 |
* @param string $id Route ID. |
| 228 |
* @param mixed $instance An instance of the route. |
| 229 |
*/ |
| 230 |
public function add_route( string $id, $instance ) { |
| 231 |
$this->routes[ $id ] = $instance; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @access public |
| 236 |
* @return array |
| 237 |
*/ |
| 238 |
public function get_routes(): array { |
| 239 |
return $this->routes; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* @access public |
| 244 |
* |
| 245 |
* @param string $id Route ID. |
| 246 |
* |
| 247 |
* @return mixed An instance of the route, or `false` if the route |
| 248 |
* doesn't exist. |
| 249 |
* @codeCoverageIgnore |
| 250 |
*/ |
| 251 |
public function get_route( string $id ) { |
| 252 |
if ( isset( $this->routes[ $id ] ) ) { |
| 253 |
return $this->routes[ $id ]; |
| 254 |
} |
| 255 |
|
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* @return array Route class name strings. |
| 261 |
*/ |
| 262 |
public static function routes_list(): array { |
| 263 |
return []; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Adds an array of routes. |
| 268 |
* Assumes namespace structure contains `\Rest\` |
| 269 |
*/ |
| 270 |
public function register_routes(): void { |
| 271 |
$namespace = static::namespace_name(); |
| 272 |
$routes_ids = static::routes_list(); |
| 273 |
|
| 274 |
foreach ( $routes_ids as $route_id ) { |
| 275 |
$class_name = $namespace . '\\Rest\\' . $route_id; |
| 276 |
$this->add_route( $route_id, new $class_name() ); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|