55 * @package query-monitor
66 */
77
8- class QM_Component {
8+ /**
9+ * @phpstan-type QM_Component_Array array{
10+ * type: string,
11+ * name: string,
12+ * context: string,
13+ * }
14+ * @property-read string $name
15+ */
16+ class QM_Component implements JsonSerializable {
17+ public const TYPE_ALTIS_VENDOR = 'altis-vendor ' ;
18+ public const TYPE_CORE = 'core ' ;
19+ public const TYPE_DROPIN = 'dropin ' ;
20+ public const TYPE_GO_PLUGIN = 'go-plugin ' ;
21+ public const TYPE_MU_PLUGIN = 'mu-plugin ' ;
22+ public const TYPE_MU_VENDOR = 'mu-vendor ' ;
23+ public const TYPE_OTHER = 'other ' ;
24+ public const TYPE_PHP = 'php ' ;
25+ public const TYPE_PLUGIN = 'plugin ' ;
26+ public const TYPE_STYLESHEET = 'stylesheet ' ;
27+ public const TYPE_TEMPLATE = 'template ' ;
28+ public const TYPE_THEME = 'theme ' ;
29+ public const TYPE_UNKNOWN = 'unknown ' ;
30+ public const TYPE_VIP_CLIENT_MU_PLUGIN = 'vip-client-mu-plugin ' ;
31+ public const TYPE_VIP_PLUGIN = 'vip-plugin ' ;
32+
933 /**
1034 * @var string
1135 */
@@ -14,10 +38,117 @@ class QM_Component {
1438 /**
1539 * @var string
1640 */
17- public $ name ;
41+ public $ context ;
1842
1943 /**
2044 * @var string
2145 */
22- public $ context ;
46+ public $ file ;
47+
48+ public function __construct ( string $ context , string $ file = '' , string $ type = '' ) {
49+ $ this ->context = $ context ;
50+ $ this ->file = $ file ;
51+ $ this ->type = $ type ;
52+ }
53+
54+ public function get_name (): string {
55+ if ( isset ( $ this ->name ) ) {
56+ return $ this ->name ;
57+ }
58+
59+ return sprintf (
60+ $ this ->type ,
61+ $ this ->context
62+ );
63+ }
64+
65+ final public function get_id (): string {
66+ return "{$ this ->type }- {$ this ->context }" ;
67+ }
68+
69+ final public function is_plugin (): bool {
70+ return ( $ this ->type === self ::TYPE_PLUGIN );
71+ }
72+
73+ final public function is_core (): bool {
74+ return ( $ this ->type === self ::TYPE_CORE );
75+ }
76+
77+ /**
78+ * @param QM_Component[] $components
79+ */
80+ final public static function has_non_core ( array $ components ): bool {
81+ foreach ( $ components as $ component ) {
82+ if ( ! $ component ->is_core () ) {
83+ return true ;
84+ }
85+ }
86+
87+ return false ;
88+ }
89+
90+ final public static function from ( string $ type , string $ context = '' , string $ file = '' ): QM_Component {
91+ switch ( $ type ) {
92+ case self ::TYPE_ALTIS_VENDOR :
93+ return new QM_Component_Altis_Vendor ( $ context , $ file , $ type );
94+ case self ::TYPE_PLUGIN :
95+ return new QM_Component_Plugin ( $ context , $ file , $ type );
96+ case self ::TYPE_MU_PLUGIN :
97+ return new QM_Component_MU_Plugin ( $ context , $ file , $ type );
98+ case self ::TYPE_MU_VENDOR :
99+ return new QM_Component_MU_Vendor ( $ context , $ file , $ type );
100+ case self ::TYPE_GO_PLUGIN :
101+ return new QM_Component_Go_Plugin ( $ context , $ file , $ type );
102+ case self ::TYPE_VIP_PLUGIN :
103+ return new QM_Component_VIP_Plugin ( $ context , $ file , $ type );
104+ case self ::TYPE_VIP_CLIENT_MU_PLUGIN :
105+ return new QM_Component_VIP_Client_MU_Plugin ( $ context , $ file , $ type );
106+ case self ::TYPE_STYLESHEET :
107+ return new QM_Component_Stylesheet ( $ context , $ file , $ type );
108+ case self ::TYPE_TEMPLATE :
109+ return new QM_Component_Template ( $ context , $ file , $ type );
110+ case self ::TYPE_OTHER :
111+ return new QM_Component_Other ( $ context , $ file , $ type );
112+ case self ::TYPE_CORE :
113+ return new QM_Component_Core ( $ context , $ file , $ type );
114+ case self ::TYPE_DROPIN :
115+ return new QM_Component_Dropin ( $ context , $ file , $ type );
116+ case self ::TYPE_PHP :
117+ return new QM_Component_PHP ( $ context , $ file , $ type );
118+ }
119+
120+ return new QM_Component_Unknown ( $ context , $ file , $ type );
121+ }
122+
123+ /**
124+ * @return mixed
125+ */
126+ public function __get ( string $ key ) {
127+ if ( 'name ' === $ key ) {
128+ return $ this ->get_name ();
129+ }
130+ }
131+
132+ /**
133+ * @phpstan-return QM_Component_Array
134+ * @return array<string, string>
135+ */
136+ public function toArray (): array {
137+ return array (
138+ 'type ' => $ this ->type ,
139+ 'name ' => $ this ->name ,
140+ 'context ' => $ this ->context ,
141+ );
142+ }
143+ /**
144+ * @phpstan-return QM_Component_Array
145+ * @return array<string, string>
146+ */
147+ public function jsonSerialize (): array {
148+ return $ this ->toArray ();
149+ }
150+
151+ public static function sort ( QM_Component $ a , QM_Component $ b ): int {
152+ return strcasecmp ( $ a ->get_name (), $ b ->get_name () );
153+ }
23154}
0 commit comments