PluginProbe
Manage – Centralized site maintenance and monitoring / trunk
Manage – Centralized site maintenance and monitoring vtrunk
1.0.9 1.0.8 1.0.7 1.0.6 1.0.5 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4
manage / classes / module-base.php

module-base.php in Manage – Centralized site maintenance and monitoring trunk, at classes/module-base.php

209 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Manage\Classes;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Module Base.
10 *
11 * An abstract class providing the properties and methods needed to
12 * manage and handle modules in inheriting classes.
13 *
14 * @abstract
15 */
16 abstract class Module_Base {
17
18 /**
19 * Module class reflection.
20 *
21 * Holds the information about a class.
22 * @access private
23 *
24 * @var ?\ReflectionClass
25 */
26 private ?\ReflectionClass $reflection = null;
27
28 /**
29 * Module components.
30 *
31 * Holds the module components.
32 * @access private
33 *
34 * @var array
35 */
36 private array $components = [];
37
38 /**
39 * Module instance.
40 *
41 * Holds the module instance.
42 * @access protected
43 *
44 * @var Module_Base[]
45 */
46 protected static array $instances = [];
47
48 /**
49 * Get module name.
50 *
51 * Retrieve the module name.
52 * @access public
53 * @abstract
54 *
55 * @return string Module name.
56 */
57 abstract public function get_name(): string;
58
59 /**
60 * Instance.
61 *
62 * Ensures only one instance of the module class is loaded or can be loaded.
63 * @access public
64 * @static
65 *
66 * @return Module_Base An instance of the class.
67 */
68 public static function instance(): Module_Base {
69 $class_name = static::class_name();
70
71 if ( empty( static::$instances[ $class_name ] ) ) {
72 static::$instances[ $class_name ] = new static(); // @codeCoverageIgnore
73 }
74
75 return static::$instances[ $class_name ];
76 }
77
78 /**
79 * is_active
80 * @access public
81 * @static
82 * @return bool
83 */
84 public static function is_active(): bool {
85 return true;
86 }
87
88 /**
89 * Class name.
90 *
91 * Retrieve the name of the class.
92 * @access public
93 * @static
94 */
95 public static function class_name(): string {
96 return get_called_class();
97 }
98
99 /**
100 * Clone.
101 *
102 * Disable class cloning and throw an error on object clone.
103 *
104 * The whole idea of the singleton design pattern is that there is a single
105 * object. Therefore, we don't want the object to be cloned.
106 *
107 * @access public
108 */
109 public function __clone() {
110 // Cloning instances of the class is forbidden
111 _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'manage' ), '0.0.1' ); // @codeCoverageIgnore
112 }
113
114 /**
115 * Wakeup.
116 *
117 * Disable unserializing of the class.
118 * @access public
119 */
120 public function __wakeup() {
121 // Unserializing instances of the class is forbidden
122 _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'manage' ), '0.0.1' ); // @codeCoverageIgnore
123 }
124
125 /**
126 * @access public
127 */
128 public function get_reflection(): \ReflectionClass {
129 if ( null === $this->reflection ) {
130 try {
131 $this->reflection = new \ReflectionClass( $this );
132 } catch ( \ReflectionException $e ) {
133 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
134 error_log( $e->getMessage() ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
135 }
136 }
137 }
138
139 return $this->reflection;
140 }
141
142 /**
143 * Add module component.
144 *
145 * Add new component to the current module.
146 * @access public
147 *
148 * @param string $id Component ID.
149 * @param mixed $instance An instance of the component.
150 */
151 public function add_component( string $id, $instance ) {
152 $this->components[ $id ] = $instance;
153 }
154
155 /**
156 * @access public
157 * @return array
158 */
159 public function get_components(): array {
160 return $this->components;
161 }
162
163 /**
164 * Get module component.
165 *
166 * Retrieve the module component.
167 * @access public
168 *
169 * @param string $id Component ID.
170 *
171 * @return mixed An instance of the component, or `false` if the component
172 * doesn't exist.
173 * @codeCoverageIgnore
174 */
175 public function get_component( string $id ) {
176 if ( isset( $this->components[ $id ] ) ) {
177 return $this->components[ $id ];
178 }
179
180 return false;
181 }
182
183 /**
184 * Retrieve the namespace of the class
185 *
186 * @access public
187 * @static
188 */
189 public static function namespace_name(): string {
190 $class_name = static::class_name();
191 return substr( $class_name, 0, strrpos( $class_name, '\\' ) );
192 }
193
194 /**
195 * Adds an array of components.
196 * Assumes namespace structure contains `\Components\`
197 *
198 * @param array $components_ids => component's class name.
199 */
200 public function register_components( array $components_ids ) {
201 $namespace = static::namespace_name();
202 foreach ( $components_ids as $component_id ) {
203 $class_name = $namespace . '\\Components\\' . $component_id;
204 $this->add_component( $component_id, new $class_name() );
205 }
206 }
207 }
208
209