Skip to content

Commit 9e2493e

Browse files
committed
feature #422 Added an event subscriber to display better error messages in the console (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #422). Discussion ---------- Added an event subscriber to display better error messages in the console I propose this PR because: * It could solve problems like #418 * It allows us to introduce an event subscriber (so far we only have an event listener) If you agree, I'll polish this and add some helpful comments: ![console_exception](https://cloud.githubusercontent.com/assets/73419/21689124/aa8de6a6-d36f-11e6-8f85-222c2bcaeaca.png) Commits ------- ee0536b Added an event subscriber to display better error messages in the console
2 parents a1c83d2 + ee0536b commit 9e2493e

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

app/config/services.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ services:
2222
tags:
2323
- { name: twig.extension }
2424

25+
# Event Listeners are classes that listen to one or more specific events.
26+
# Those events are defined in the tags added to the service definition.
27+
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-listener
2528
app.redirect_to_preferred_locale_listener:
2629
class: AppBundle\EventListener\RedirectToPreferredLocaleListener
2730
arguments: ['@router', '%app_locales%', '%locale%']
2831
tags:
2932
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
3033

34+
# Event subscribers are similar to event listeners but they don't need service tags.
35+
# Instead, the PHP class of the event subscriber includes a method that returns
36+
# the list of events listened by that class.
37+
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
38+
app.console_subscriber:
39+
class: AppBundle\EventListener\ConsoleEventSubscriber
40+
tags:
41+
- { name: kernel.event_subscriber }
42+
3143
# Uncomment the following lines to define a service for the Post Doctrine repository.
3244
# It's not mandatory to create these services, but if you use repositories a lot,
3345
# these services simplify your code:
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace AppBundle\EventListener;
13+
14+
use Symfony\Component\Console\ConsoleEvents;
15+
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
16+
use Symfony\Component\Console\Style\SymfonyStyle;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
19+
/**
20+
* This application uses by default an SQLite database to store its information.
21+
* That's why the 'sqlite3' extension must be enabled in PHP. This event
22+
* subscriber listens to console events and in case of an exception caused by
23+
* a disabled 'sqlite3' extension, it displays a meaningful error message.
24+
*
25+
* @author Javier Eguiluz <[email protected]>
26+
*/
27+
class ConsoleEventSubscriber implements EventSubscriberInterface
28+
{
29+
// Event Subscribers must define this method to declare the events they
30+
// listen to. You can listen to several events, execute more than one method
31+
// for each event and set the priority of each event too.
32+
// See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
33+
public static function getSubscribedEvents()
34+
{
35+
return [
36+
// Exceptions are one of the events defined by the Console. See the
37+
// rest here: http://symfony.com/doc/current/components/console/events.html
38+
ConsoleEvents::EXCEPTION => 'handleDatabaseExceptions',
39+
];
40+
}
41+
42+
/**
43+
* This method checks if there has been an exception in a command related to
44+
* the database and then, it checks if the 'sqlite3' PHP extension is enabled
45+
* or not to display a better error message.
46+
*
47+
* @param ConsoleExceptionEvent $event
48+
*/
49+
public function handleDatabaseExceptions(ConsoleExceptionEvent $event)
50+
{
51+
$commandNames = ['doctrine:fixtures:load', 'doctrine:database:create', 'doctrine:schema:create', 'doctrine:database:drop'];
52+
53+
if (in_array($event->getCommand()->getName(), $commandNames)) {
54+
if (!extension_loaded('sqlite3')) {
55+
$io = new SymfonyStyle($event->getInput(), $event->getOutput());
56+
$io->error('This command requires to have the "sqlite3" PHP extension enabled because, by default, the Symfony Demo application uses SQLite to store its information.');
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)