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,41 +156,25 @@ 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- }
159+ if (! in_array ('--no-header ' , $ tokens , true )) {
160+ // Don't show the header as it is not needed when running commands from code.
161+ $ tokens [] = '--no-header ' ;
162+ }
174163
175- continue ;
176- }
164+ // Prepend an application name, as Console expects one.
165+ array_unshift ( $ tokens , ' spark ' );
177166
178- $ arg = ltrim ($ arg , '- ' );
179- $ value = null ;
167+ ob_start ();
180168
181- if (isset ($ args [$ i + 1 ]) && mb_strpos ($ args [$ i + 1 ], '- ' ) !== 0 ) {
182- $ value = $ args [$ i + 1 ];
183- $ optionValue = true ;
184- }
169+ try {
170+ (new Console ())->run ($ tokens );
185171
186- $ params [$ arg ] = $ value ;
172+ return ob_get_clean ();
173+ } finally {
174+ // `Console::run()` creates a `CLIRequest` instance stored globally, which
175+ // can cause issues if the next code expects a different type of request.
176+ Services::createRequest (config (App::class), is_cli ());
187177 }
188-
189- ob_start ();
190- service ('commands ' )->run ($ command , $ params );
191-
192- return ob_get_clean ();
193178 }
194179}
195180
0 commit comments