1616use Composer \XdebugHandler \Process ;
1717use Composer \XdebugHandler \XdebugHandler ;
1818
19+ /**
20+ * This class runs and provides functionality for the xdebug-handler functional
21+ * test apps.
22+ *
23+ * It sets up the logger and outputs debug and status messages to stdout.
24+ *
25+ * The test apps can be run from the command-line and will show detailed output
26+ * with the --display and --inis options.
27+ */
1928class AppHelper extends AppRunner
2029{
2130 public const ENV_IS_DISPLAY = 'XDEBUG_HANDLER_TEST_DISPLAY ' ;
2231 public const ENV_SHOW_INIS = 'XDEBUG_HANDLER_TEST_INIS ' ;
2332
2433 /** @var string */
2534 private $ appName ;
35+
2636 /** @var string */
2737 private $ envPrefix ;
38+
2839 /** @var non-empty-list<string> */
2940 private $ serverArgv ;
41+
3042 /** @var class-string|null */
3143 private $ className ;
32- /** @var bool */
33- private $ display ;
34- /** @var bool */
35- private $ showInis ;
3644
3745 /** @var Logger */
3846 private $ logger ;
@@ -41,21 +49,19 @@ class AppHelper extends AppRunner
4149 private $ output ;
4250
4351 /** @var Status */
44- private $ status = null ;
52+ private $ status ;
4553
4654 public function __construct (string $ script )
4755 {
4856 parent ::__construct (dirname ($ script ));
4957 $ this ->appName = $ this ->getAppName ($ script );
5058 $ this ->envPrefix = $ this ->getEnvPrefix ($ script );
5159
52- $ this ->display = $ this ->getDisplayFromServer ();
53- $ this ->showInis = $ this ->getInisFromServer ();
54- $ this ->setServerArgv ();
60+ [$ display , $ showInis ] = $ this ->setServerArgvAndOptions ();
5561
56- $ this ->output = new Output ($ this -> display );
62+ $ this ->output = new Output ($ display );
5763 $ this ->logger = new Logger ($ this ->output );
58- $ this ->status = new Status ($ this -> display , $ this -> showInis );
64+ $ this ->status = new Status ($ display , $ showInis );
5965 }
6066
6167 /**
@@ -73,9 +79,10 @@ public function getServerArgv0(): string
7379 }
7480
7581 /**
82+ * Starts an instance of XdebugHandler
7683 *
77- * @param class-string $class
78- * @param array<string, string> $settings
84+ * @param class-string $class Extending class name
85+ * @param array<string, string> $settings XdebugHandler settings
7986 */
8087 public function getXdebugHandler (?string $ class = null , ?array $ settings = null ): XdebugHandler
8188 {
@@ -102,16 +109,40 @@ public function getXdebugHandler(?string $class = null, ?array $settings = null)
102109 return $ xdebug ;
103110 }
104111
112+ /**
113+ * Runs a PHP sub-process
114+ *
115+ * @param string $script The file to run
116+ * @param PhpOptions $options
117+ */
105118 public function runScript (string $ script , ?PhpOptions $ options = null ): void
106119 {
107- parent ::runScript ($ script , $ options );
120+ $ script = $ this ->checkScript ($ script );
121+
122+ if ($ options === null ) {
123+ $ options = new PhpOptions ();
124+ }
125+
126+ // set passthru in child proccesses so output gets collected
127+ $ options ->setPassthru (true );
128+ $ this ->phpExecutor ->run ($ script , $ options , $ this ->scriptDir );
108129 }
109130
131+ /**
132+ * Outputs a message prefixed appname[pid] to stdout
133+ *
134+ * @param string $message
135+ */
110136 public function write (string $ message ): void
111137 {
112138 $ this ->logger ->write ($ message , $ this ->appName );
113139 }
114140
141+ /**
142+ * Outputs the Xdebug loaded status to stdout
143+ *
144+ * More details can be shown with the --display and --inis options.
145+ */
115146 public function writeXdebugStatus (): void
116147 {
117148 $ className = $ this ->className ?? XdebugHandler::class;
@@ -122,10 +153,20 @@ public function writeXdebugStatus(): void
122153 }
123154 }
124155
125- private function setServerArgv (): void
156+ /**
157+ * Checks $_SERVER['argv'] and returns $display and $showInis values
158+ *
159+ * The flags to display the full status and ini file list are passed on the
160+ * command-line, then removed and set in the environment.
161+ *
162+ * @return array<bool, bool>
163+ */
164+ private function setServerArgvAndOptions (): array
126165 {
127166 $ args = [];
128167 $ errors = false ;
168+ $ display = false ;
169+ $ showInis = false ;
129170
130171 if (isset ($ _SERVER ['argv ' ]) && is_array ($ _SERVER ['argv ' ])) {
131172 foreach ($ _SERVER ['argv ' ] as $ value ) {
@@ -134,6 +175,18 @@ private function setServerArgv(): void
134175 break ;
135176 }
136177
178+ if ($ value === '--display ' ) {
179+ $ display = true ;
180+ Process ::setEnv (self ::ENV_IS_DISPLAY , '1 ' );
181+ continue ;
182+ }
183+
184+ if ($ value === '--inis ' ) {
185+ $ showInis = true ;
186+ Process ::setEnv (self ::ENV_SHOW_INIS , '1 ' );
187+ continue ;
188+ }
189+
137190 $ args [] = $ value ;
138191 }
139192 }
@@ -142,46 +195,12 @@ private function setServerArgv(): void
142195 throw new \RuntimeException ('$_SERVER[argv] is not as expected ' );
143196 }
144197
198+ $ _SERVER ['argv ' ] = $ args ;
145199 $ this ->serverArgv = $ args ;
146- }
147-
148- private function getDisplayFromServer (): bool
149- {
150- $ result = false ;
151-
152- if (isset ($ _SERVER ['argv ' ]) && is_array ($ _SERVER ['argv ' ])) {
153- $ key = array_search ('--display ' , $ _SERVER ['argv ' ], true );
154-
155- if (is_int ($ key )) {
156- $ result = true ;
157- Process ::setEnv (self ::ENV_IS_DISPLAY , '1 ' );
158- // @phpstan-ignore argument.type
159- array_splice ($ _SERVER ['argv ' ], $ key , 1 );
160- } else {
161- $ result = false !== getenv (self ::ENV_IS_DISPLAY );
162- }
163- }
164-
165- return $ result ;
166- }
167-
168- private function getInisFromServer (): bool
169- {
170- $ result = false ;
171-
172- if (isset ($ _SERVER ['argv ' ]) && is_array ($ _SERVER ['argv ' ])) {
173- $ key = array_search ('--inis ' , $ _SERVER ['argv ' ], true );
174-
175- if (is_int ($ key )) {
176- $ result = true ;
177- Process ::setEnv (self ::ENV_SHOW_INIS , '1 ' );
178- // @phpstan-ignore argument.type
179- array_splice ($ _SERVER ['argv ' ], $ key , 1 );
180- } else {
181- $ result = false !== getenv (self ::ENV_SHOW_INIS );
182- }
183- }
184200
185- return $ result ;
201+ return [
202+ $ display || false !== getenv (self ::ENV_IS_DISPLAY ),
203+ $ showInis || false !== getenv (self ::ENV_SHOW_INIS ),
204+ ];
186205 }
187206}
0 commit comments