1212 */
1313
1414use CodeIgniter \Cache \CacheInterface ;
15+ use CodeIgniter \CLI \Console ;
1516use CodeIgniter \Config \BaseConfig ;
1617use CodeIgniter \Config \Factories ;
1718use CodeIgniter \Context \Context ;
@@ -127,7 +128,7 @@ function command(string $command)
127128 $ regexString = '([^\s]+?)(?:\s|(?<! \\\\)"|(?<! \\\\) \'|$) ' ;
128129 $ regexQuoted = '(?:"([^" \\\\]*(?: \\\\.[^" \\\\]*)*)"| \'([^ \'\\\\]*(?: \\\\.[^ \'\\\\]*)*) \') ' ;
129130
130- $ args = [];
131+ $ tokens = [];
131132 $ length = strlen ($ command );
132133 $ cursor = 0 ;
133134
@@ -140,9 +141,9 @@ function command(string $command)
140141 if (preg_match ('/\s+/A ' , $ command , $ match , 0 , $ cursor )) {
141142 // nothing to do
142143 } elseif (preg_match ('/ ' . $ regexQuoted . '/A ' , $ command , $ match , 0 , $ cursor )) {
143- $ args [] = stripcslashes (substr ($ match [0 ], 1 , strlen ($ match [0 ]) - 2 ));
144+ $ tokens [] = stripcslashes (substr ($ match [0 ], 1 , strlen ($ match [0 ]) - 2 ));
144145 } elseif (preg_match ('/ ' . $ regexString . '/A ' , $ command , $ match , 0 , $ cursor )) {
145- $ args [] = stripcslashes ($ match [1 ]);
146+ $ tokens [] = stripcslashes ($ match [1 ]);
146147 } else {
147148 // @codeCoverageIgnoreStart
148149 throw new InvalidArgumentException (sprintf (
@@ -155,39 +156,21 @@ function command(string $command)
155156 $ cursor += strlen ($ match [0 ]);
156157 }
157158
158- /** @var array<int|string, string|null> */
159- $ params = [];
160- $ command = array_shift ($ args );
161- $ optionValue = false ;
162-
163- foreach ($ args as $ i => $ arg ) {
164- if (mb_strpos ($ arg , '- ' ) !== 0 ) {
165- if ($ optionValue ) {
166- // if this was an option value, it was already
167- // included in the previous iteration
168- $ optionValue = false ;
169- } else {
170- // add to segments if not starting with '-'
171- // and not an option value
172- $ params [] = $ arg ;
173- }
174-
175- continue ;
176- }
177-
178- $ arg = ltrim ($ arg , '- ' );
179- $ value = null ;
180-
181- if (isset ($ args [$ i + 1 ]) && mb_strpos ($ args [$ i + 1 ], '- ' ) !== 0 ) {
182- $ value = $ args [$ i + 1 ];
183- $ optionValue = true ;
159+ // Don't show the header as it is not needed when running commands from code.
160+ if (! in_array ('--no-header ' , $ tokens , true )) {
161+ if (! in_array ('-- ' , $ tokens , true )) {
162+ $ tokens [] = '--no-header ' ;
163+ } else {
164+ $ index = (int ) array_search ('-- ' , $ tokens , true );
165+ array_splice ($ tokens , $ index , 0 , '--no-header ' );
184166 }
185-
186- $ params [$ arg ] = $ value ;
187167 }
188168
169+ // Prepend an application name, as Console expects one.
170+ array_unshift ($ tokens , 'spark ' );
171+
189172 ob_start ();
190- service ( ' commands ' ) ->run ($ command , $ params );
173+ ( new Console ()) ->run ($ tokens );
191174
192175 return ob_get_clean ();
193176 }
0 commit comments