66
77use Steevanb \ParallelProcess \{
88 Console \Application \Theme \DefaultTheme ,
9+ Console \Application \Theme \SummaryTheme ,
910 Console \Application \Theme \ThemeInterface ,
1011 Console \Output \ConsoleBufferedOutput ,
12+ Exception \ParallelProcessException ,
1113 Process \Process ,
1214 Process \ProcessArray
1315};
1416use Symfony \Component \Console \{
1517 Command \SignalableCommandInterface ,
1618 Input \InputInterface ,
19+ Input \InputOption ,
1720 Output \OutputInterface ,
1821 SingleCommandApplication
1922};
@@ -146,13 +149,36 @@ public function handleSignal(int $signal): void
146149 }
147150 }
148151
152+ protected function configure (): void
153+ {
154+ parent ::configure ();
155+
156+ $ this
157+ ->addOption (
158+ 'theme ' ,
159+ 't ' ,
160+ InputOption::VALUE_REQUIRED ,
161+ 'Name or FQCN of the theme to use (examples: default, summary, ' . DefaultTheme::class . ') '
162+ )
163+ ->addOption (
164+ 'refresh-interval ' ,
165+ 'r ' ,
166+ InputOption::VALUE_REQUIRED ,
167+ 'Refresh interval in microseconds (example: 100000 for 100ms) '
168+ );
169+ }
170+
149171 protected function isCanceled (): bool
150172 {
151173 return $ this ->canceled ;
152174 }
153175
154176 protected function runProcessesInParallel (InputInterface $ input , OutputInterface $ output ): int
155177 {
178+ $ this
179+ ->defineThemeFromInput ($ input )
180+ ->defineRefreshIntervalFromInput ($ input );
181+
156182 $ this ->getTheme ()->outputStart ($ output , $ this ->getProcesses ());
157183
158184 $ this
@@ -164,6 +190,53 @@ protected function runProcessesInParallel(InputInterface $input, OutputInterface
164190 return $ this ->getExitCode ();
165191 }
166192
193+ protected function defineThemeFromInput (InputInterface $ input ): self
194+ {
195+ $ theme = $ input ->getOption ('theme ' );
196+ if (is_string ($ theme )) {
197+ switch ($ theme ) {
198+ case 'default ' :
199+ $ this ->setTheme (new DefaultTheme ());
200+ break ;
201+ case 'summary ' :
202+ $ this ->setTheme (new SummaryTheme ());
203+ break ;
204+ default :
205+ if (class_exists ($ theme ) === false ) {
206+ throw new ParallelProcessException ('Theme " ' . $ theme . '" not found. ' );
207+ }
208+
209+ $ themeObject = new $ theme ();
210+ if ($ themeObject instanceof ThemeInterface === false ) {
211+ throw new ParallelProcessException (
212+ 'Theme " ' . $ theme . '" should implements ' . ThemeInterface::class . '. '
213+ );
214+ }
215+
216+ $ this ->setTheme ($ themeObject );
217+ break ;
218+ }
219+ }
220+
221+ return $ this ;
222+ }
223+
224+ protected function defineRefreshIntervalFromInput (InputInterface $ input ): self
225+ {
226+ $ interval = $ input ->getOption ('refresh-interval ' );
227+ if (is_string ($ interval )) {
228+ if (is_numeric ($ interval ) === false || (string ) abs (intval ($ interval )) !== $ interval ) {
229+ throw new ParallelProcessException (
230+ 'Refresh interval " ' . $ interval . '" should be an integer value in microseconds. '
231+ );
232+ }
233+
234+ $ this ->setRefreshInterval ((int ) $ interval );
235+ }
236+
237+ return $ this ;
238+ }
239+
167240 protected function startProcesses (): self
168241 {
169242 return $ this ->startReadyProcesses ();
0 commit comments