|
| 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