@@ -77,7 +77,8 @@ abstract protected function main(Options $options);
7777 * Execute the CLI program
7878 *
7979 * Executes the setup() routine, adds default options, initiate the options parsing and argument checking
80- * and finally executes main()
80+ * and finally executes main() - Each part is split into their own protected function below, so behaviour
81+ * can easily be overwritten
8182 *
8283 * @throws Exception
8384 */
@@ -87,8 +88,24 @@ public function run()
8788 throw new Exception ('This has to be run from the command line ' );
8889 }
8990
90- // setup
9191 $ this ->setup ($ this ->options );
92+ $ this ->registerDefaultOptions ();
93+ $ this ->parseOptions ();
94+ $ this ->handleDefaultOptions ();
95+ $ this ->setupLogging ();
96+ $ this ->checkArgments ();
97+ $ this ->execute ();
98+
99+ exit (0 );
100+ }
101+
102+ // region run handlers - for easier overriding
103+
104+ /**
105+ * Add the default help, color and log options
106+ */
107+ protected function registerDefaultOptions ()
108+ {
92109 $ this ->options ->registerOption (
93110 'help ' ,
94111 'Display this help screen and exit immeadiately. ' ,
@@ -105,34 +122,61 @@ public function run()
105122 null ,
106123 'level '
107124 );
125+ }
108126
109- // parse
110- $ this ->options ->parseOptions ();
111-
112- // handle defaults
127+ /**
128+ * Handle the default options
129+ */
130+ protected function handleDefaultOptions ()
131+ {
113132 if ($ this ->options ->getOpt ('no-colors ' )) {
114133 $ this ->colors ->disable ();
115134 }
116135 if ($ this ->options ->getOpt ('help ' )) {
117136 echo $ this ->options ->help ();
118137 exit (0 );
119138 }
139+ }
140+
141+ /**
142+ * Handle the logging options
143+ */
144+ protected function setupLogging ()
145+ {
120146 $ level = $ this ->options ->getOpt ('loglevel ' , $ this ->logdefault );
121147 if (!isset ($ this ->loglevel [$ level ])) $ this ->fatal ('Unknown log level ' );
122148 foreach (array_keys ($ this ->loglevel ) as $ l ) {
123149 if ($ l == $ level ) break ;
124150 unset($ this ->loglevel [$ l ]);
125151 }
152+ }
126153
127- // check arguments
154+ /**
155+ * Wrapper around the option parsing
156+ */
157+ protected function parseOptions ()
158+ {
159+ $ this ->options ->parseOptions ();
160+ }
161+
162+ /**
163+ * Wrapper around the argument checking
164+ */
165+ protected function checkArgments ()
166+ {
128167 $ this ->options ->checkArguments ();
168+ }
129169
130- // execute
170+ /**
171+ * Wrapper around main
172+ */
173+ protected function execute ()
174+ {
131175 $ this ->main ($ this ->options );
132-
133- exit (0 );
134176 }
135177
178+ // endregion
179+
136180 // region logging
137181
138182 /**
@@ -286,7 +330,7 @@ public function log($level, $message, array $context = array())
286330 /** @var string $color */
287331 /** @var resource $channel */
288332 list ($ prefix , $ color , $ channel ) = $ this ->loglevel [$ level ];
289- if (!$ this ->colors ->isEnabled ()) $ prefix = '' ;
333+ if (!$ this ->colors ->isEnabled ()) $ prefix = '' ;
290334
291335 $ message = $ this ->interpolate ($ message , $ context );
292336 $ this ->colors ->ptln ($ prefix . $ message , $ color , $ channel );
0 commit comments