1414namespace CodeIgniter \Commands \Utilities ;
1515
1616use CodeIgniter \Cache \FactoriesCache ;
17- use CodeIgniter \CLI \BaseCommand ;
17+ use CodeIgniter \CLI \AbstractCommand ;
18+ use CodeIgniter \CLI \Attributes \Command ;
1819use CodeIgniter \CLI \CLI ;
20+ use CodeIgniter \CLI \Input \Argument ;
1921use CodeIgniter \Config \BaseConfig ;
2022use Config \Optimize ;
2123use Kint \Kint ;
2224
2325/**
2426 * Check the Config values.
25- *
26- * @see \CodeIgniter\Commands\Utilities\ConfigCheckTest
2727 */
28- final class ConfigCheck extends BaseCommand
28+ #[Command(name: 'config:check ' , description: 'Check your config values. ' , group: 'CodeIgniter ' )]
29+ class ConfigCheck extends AbstractCommand
2930{
30- /**
31- * The group the command is lumped under
32- * when listing commands.
33- *
34- * @var string
35- */
36- protected $ group = 'CodeIgniter ' ;
37-
38- /**
39- * The Command's name
40- *
41- * @var string
42- */
43- protected $ name = 'config:check ' ;
44-
45- /**
46- * The Command's short description
47- *
48- * @var string
49- */
50- protected $ description = 'Check your Config values. ' ;
51-
52- /**
53- * The Command's usage
54- *
55- * @var string
56- */
57- protected $ usage = 'config:check <classname> ' ;
58-
59- /**
60- * The Command's arguments
61- *
62- * @var array<string, string>
63- */
64- protected $ arguments = [
65- 'classname ' => 'The config classname to check. Short classname or FQCN. ' ,
66- ];
67-
68- /**
69- * The Command's options
70- *
71- * @var array<string, string>
72- */
73- protected $ options = [];
74-
75- /**
76- * @return int
77- */
78- public function run (array $ params )
31+ protected function configure (): void
7932 {
80- if (! isset ($ params [0 ])) {
81- CLI ::error ('You must specify a Config classname. ' );
82- CLI ::write (' Usage: ' . $ this ->usage );
83- CLI ::write ('Example: config:check App ' );
84- CLI ::write (' config:check \'CodeIgniter\Shield\Config\Auth \'' );
85-
86- return EXIT_ERROR ;
87- }
33+ $ this ->addArgument (new Argument (
34+ name: 'class_name ' ,
35+ description: 'The config class to check. Short name or FQCN. ' ,
36+ required: true ,
37+ ));
38+ }
8839
40+ protected function execute (array $ arguments , array $ options ): int
41+ {
8942 /** @var class-string<BaseConfig> $class */
90- $ class = $ params [0 ];
43+ $ class = $ arguments ['class_name ' ];
44+
45+ $ configCacheEnabled = class_exists (Optimize::class) && (new Optimize ())->configCacheEnabled ;
9146
92- // Load Config cache if it is enabled.
93- $ configCacheEnabled = class_exists (Optimize::class)
94- && (new Optimize ())->configCacheEnabled ;
9547 if ($ configCacheEnabled ) {
96- $ factoriesCache = new FactoriesCache ();
97- $ factoriesCache ->load ('config ' );
48+ (new FactoriesCache ())->load ('config ' );
9849 }
9950
10051 $ config = config ($ class );
10152
10253 if ($ config === null ) {
103- CLI ::error (' No such Config class: ' . $ class );
54+ CLI ::error (sprintf ( ' Config class "%s" not found. ' , $ class) );
10455
10556 return EXIT_ERROR ;
10657 }
10758
108- if (defined ('KINT_DIR ' ) && Kint::$ enabled_mode !== false ) {
109- CLI ::write ($ this ->getKintD ($ config ));
110- } else {
111- CLI ::write (
112- CLI ::color ($ this ->getVarDump ($ config ), 'cyan ' ),
113- );
114- }
59+ CLI ::write ($ this ->getDump ($ config ));
11560
11661 CLI ::newLine ();
117- $ state = CLI ::color ($ configCacheEnabled ? 'Enabled ' : 'Disabled ' , 'green ' );
118- CLI ::write ('Config Caching: ' . $ state );
62+ $ state = CLI ::color ($ configCacheEnabled ? 'enabled ' : 'disabled ' , 'green ' );
63+ CLI ::write (sprintf ( 'Config caching: %s ' , $ state) );
11964
12065 return EXIT_SUCCESS ;
12166 }
12267
68+ /**
69+ * Renders the config object using Kint when available, otherwise var_dump().
70+ */
71+ private function getDump (object $ config ): string
72+ {
73+ if (defined ('KINT_DIR ' ) && Kint::$ enabled_mode !== false ) {
74+ return $ this ->getKintD ($ config );
75+ }
76+
77+ return CLI ::color ($ this ->getVarDump ($ config ), 'cyan ' );
78+ }
79+
12380 /**
12481 * Gets object dump by Kint d()
12582 */
@@ -129,11 +86,7 @@ private function getKintD(object $config): string
12986 d ($ config );
13087 $ output = ob_get_clean ();
13188
132- $ output = trim ($ output );
133-
134- $ lines = explode ("\n" , $ output );
135- array_splice ($ lines , 0 , 3 );
136- array_splice ($ lines , -3 );
89+ $ lines = array_slice (explode ("\n" , trim ($ output )), 3 , -3 );
13790
13891 return implode ("\n" , $ lines );
13992 }
0 commit comments