Skip to content

Commit 1a87d41

Browse files
committed
Added dispatcher, cmd. exceptions handled by it
1 parent 4da56d8 commit 1a87d41

File tree

7 files changed

+176
-14
lines changed

7 files changed

+176
-14
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Console\Application;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class ShellApplicationSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('PHPCR\Shell\Console\Application\ShellApplication');
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Event;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class CommandExceptionEventSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->shouldHaveType('PHPCR\Shell\Event\CommandExceptionEvent');
14+
}
15+
16+
function let(
17+
\Exception $exception,
18+
OutputInterface $output
19+
) {
20+
$this->beConstructedWith($exception, $output);
21+
}
22+
23+
function it_should_provide_access_to_event_parameters(
24+
\Exception $exception,
25+
OutputInterface $output
26+
) {
27+
$this->getException()->shouldReturn($exception);
28+
$this->getOutput()->shouldReturn($output);
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Listener;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use PHPCR\Shell\Event\CommandExceptionEvent;
9+
10+
class ExceptionListenerSpec extends ObjectBehavior
11+
{
12+
function it_is_initializable()
13+
{
14+
$this->shouldHaveType('PHPCR\Shell\Listener\ExceptionListener');
15+
}
16+
17+
function it_should_output_exception_message_to_console(
18+
CommandExceptionEvent $event,
19+
OutputInterface $output
20+
) {
21+
$exception = new \Exception('This is an exception');
22+
$event->getException()->willReturn($exception);
23+
$event->getOutput()->willReturn($output);
24+
25+
$output->writeln('<error>[Exception] This is an exception</error>')->shouldBeCalled();
26+
27+
$this->handleException($event);
28+
}
29+
}

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
use PHPCR\Shell\Console\Helper\RepositoryHelper;
2626
use PHPCR\Shell\Console\Helper\ResultFormatterHelper;
2727
use PHPCR\Shell\Console\Helper\TextHelper;
28+
29+
use PHPCR\Shell\Subscriber;
30+
use PHPCR\Shell\Event;
2831
use 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
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Event;
4+
5+
use PHPCR\UnsupportedRepositoryOperationException;
6+
use Jackalope\NotImplementedException;
7+
use Symfony\Component\EventDispatcher\Event;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
class CommandExceptionEvent extends Event
11+
{
12+
protected $exception;
13+
protected $output;
14+
15+
public function __construct(\Exception $exception, OutputInterface $output)
16+
{
17+
$this->exception = $exception;
18+
$this->output = $output;
19+
}
20+
21+
public function getException()
22+
{
23+
return $this->exception;
24+
}
25+
26+
public function getOutput()
27+
{
28+
return $this->output;
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Event;
4+
5+
class PhpcrShellEvents
6+
{
7+
const COMMAND_EXCEPTION = 'command_exception';
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Subscriber;
4+
5+
use Jackalope\NotImplementedException;
6+
use PHPCR\Shell\Event\CommandExceptionEvent;
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
use PHPCR\Shell\Event\PhpcrShellEvents;
9+
10+
/**
11+
* Try and better handle exceptions
12+
*
13+
* @author Daniel Leech <daniel@dantleech.com>
14+
*/
15+
class ExceptionSubscriber implements EventSubscriberInterface
16+
{
17+
public static function getSubscribedEvents()
18+
{
19+
return array(
20+
PhpcrShellEvents::COMMAND_EXCEPTION => 'handleException',
21+
);
22+
}
23+
24+
public function handleException(CommandExceptionEvent $event)
25+
{
26+
$exception = $event->getException();
27+
$output = $event->getOutput();
28+
29+
if ($exception instanceof NotImplementedException) {
30+
$output->writeln('<error>Not implemented: ' . $exception->getMessage() . '</error>');
31+
}
32+
33+
$output->writeln('<error>[' . get_class($exception) .'] ' . $exception->getMessage() . '</error>');
34+
}
35+
}

0 commit comments

Comments
 (0)