Skip to content

Added an event subscriber to display better error messages in the console #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ services:
tags:
- { name: twig.extension }

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

# Event subscribers are similar to event listeners but they don't need service tags.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads confusing as you still need to make use of the kernel.event_subscriber tag.

# Instead, the PHP class of the event subscriber includes a method that returns
# the list of events listened by that class.
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
app.console_subscriber:
class: AppBundle\EventListener\ConsoleEventSubscriber
tags:
- { name: kernel.event_subscriber }

# Uncomment the following lines to define a service for the Post Doctrine repository.
# It's not mandatory to create these services, but if you use repositories a lot,
# these services simplify your code:
Expand Down
60 changes: 60 additions & 0 deletions src/AppBundle/EventListener/ConsoleEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AppBundle\EventListener;

use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* This application uses by default an SQLite database to store its information.
* That's why the 'sqlite3' extension must be enabled in PHP. This event
* subscriber listens to console events and in case of an exception caused by
* a disabled 'sqlite3' extension, it displays a meaningful error message.
*
* @author Javier Eguiluz <[email protected]>
*/
class ConsoleEventSubscriber implements EventSubscriberInterface
{
// Event Subscribers must define this method to declare the events they
// listen to. You can listen to several events, execute more than one method
// for each event and set the priority of each event too.
// See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
public static function getSubscribedEvents()
{
return [
// Exceptions are one of the events defined by the Console. See the
// rest here: http://symfony.com/doc/current/components/console/events.html
ConsoleEvents::EXCEPTION => 'handleDatabaseExceptions',
];
}

/**
* This method checks if there has been an exception in a command related to
* the database and then, it checks if the 'sqlite3' PHP extension is enabled
* or not to display a better error message.
*
* @param ConsoleExceptionEvent $event
*/
public function handleDatabaseExceptions(ConsoleExceptionEvent $event)
{
$commandNames = ['doctrine:fixtures:load', 'doctrine:database:create', 'doctrine:schema:create', 'doctrine:database:drop'];

if (in_array($event->getCommand()->getName(), $commandNames)) {
if (!extension_loaded('sqlite3')) {
$io = new SymfonyStyle($event->getInput(), $event->getOutput());
$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.');
}
}
}
}