-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af6e8cf
Added an event subscriber to display better error messages in the con…
javiereguiluz 50efb0c
Minor CS fix
javiereguiluz 88b4227
Added some help notes
javiereguiluz 7545426
Fixed a minor grammar issue
javiereguiluz ad9a69e
Minor reword
javiereguiluz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.