2525use PHPCR \Shell \Console \Helper \RepositoryHelper ;
2626use PHPCR \Shell \Console \Helper \ResultFormatterHelper ;
2727use PHPCR \Shell \Console \Helper \TextHelper ;
28+
29+ use PHPCR \Shell \Subscriber ;
30+ use PHPCR \Shell \Event ;
2831use PHPCR \Shell \PhpcrSession ;
32+ use Symfony \Component \EventDispatcher \EventDispatcher ;
33+ use PHPCR \Shell \Event \PhpcrShellEvents ;
2934
3035/**
3136 * Main application for PHPCRSH
@@ -54,6 +59,14 @@ class ShellApplication extends Application
5459 */
5560 private $ session ;
5661
62+ public function __construct ($ name = 'UNKNOWN ' , $ version = 'UNKNOWN ' )
63+ {
64+ parent ::__construct ($ name , $ verpion );
65+
66+ $ this ->dispatcher = new EventDispatcher ();
67+ }
68+
69+
5770 /**
5871 * The SessionInput is the input used to intialize the shell.
5972 * It contains the connection parameters.
@@ -84,6 +97,7 @@ public function init()
8497 $ this ->initSession ();
8598 $ this ->registerHelpers ();
8699 $ this ->registerCommands ();
100+ $ this ->registerEventListeners ();
87101
88102 $ this ->initialized = true ;
89103 }
@@ -207,6 +221,11 @@ private function registerCommands()
207221 $ this ->add (new CommandShell \ExitCommand ());
208222 }
209223
224+ private function registerEventListeners ()
225+ {
226+ $ this ->dispatcher ->addSubscriber (new Subscriber \ExceptionSubscriber ());
227+ }
228+
210229 /**
211230 * Initialize the PHPCR session
212231 */
@@ -231,6 +250,8 @@ private function initSession()
231250 /**
232251 * Change the current workspace
233252 *
253+ * @todo: Move to session helper?
254+ *
234255 * @param string $workspaceName
235256 */
236257 public function changeWorkspace ($ workspaceName )
@@ -243,6 +264,8 @@ public function changeWorkspace($workspaceName)
243264 /**
244265 * Login (again)
245266 *
267+ * @todo: Move to session helper
268+ *
246269 * @param string $username
247270 * @param string $password
248271 * @param string $workspaceName
@@ -264,7 +287,7 @@ public function relogin($username, $password, $workspaceName = null)
264287 */
265288 private function getTransport ()
266289 {
267- $ transportName = $ sessionInput ->getOption ('transport ' );
290+ $ transportName = $ this -> sessionInput ->getOption ('transport ' );
268291
269292 if (!isset ($ this ->transports [$ transportName ])) {
270293 throw new \InvalidArgumentException (sprintf (
@@ -321,31 +344,24 @@ public function doRun(InputInterface $input, OutputInterface $output)
321344 try {
322345 $ exitCode = parent ::doRun ($ input , $ output );
323346 } catch (\Exception $ e ) {
324- if (!$ e ->getMessage ()) {
325- if ($ e instanceof \PHPCR \UnsupportedRepositoryOperationException) {
326- throw new \Exception ('Unsupported repository operation ' );
327- }
328- }
329-
330- // todo: Handle this with event listener
331- if ($ e instanceof NotImplementedException) {
332- throw new \Exception ('Not implemented: ' . $ e ->getMessage ());
333- }
334-
335- $ output ->writeln ('<error>( ' . get_class ($ e ) .') ' . $ e ->getMessage () . '</error> ' );
347+ $ this ->dispatcher ->dispatch (PhpcrShellEvents::COMMAND_EXCEPTION , new Event \CommandExceptionEvent ($ e , $ output ));
336348 return 1 ;
337349 }
338350
339351 return $ exitCode ;
340352 }
341353
342354 /**
355+ * {@inheritDoc}
356+ *
343357 * Render an exception to the console
344358 *
359+ * @access public
360+ *
345361 * @param \Exception $e $exception
346362 * @param OutputInterface $output
347363 */
348- protected function renderException (\Exception $ exception , OutputInterface $ output )
364+ public function renderException (\Exception $ exception , OutputInterface $ output )
349365 {
350366 $ output ->writeln (sprintf ('<exception>%s</exception ' , $ exception ->getMessage ()));
351367 }
0 commit comments