Skip to content

Commit 3387540

Browse files
committed
Improved the event subscriber that listens to database exceptions
1 parent 4ab28c9 commit 3387540

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

app/config/services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ services:
5252
# Instead, the PHP class of the event subscriber includes a method that returns
5353
# the list of events listened by that class.
5454
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
55-
app.console_subscriber:
56-
class: AppBundle\EventListener\CheckSQLiteEventSubscriber
55+
app.requirements_subscriber:
56+
class: AppBundle\EventListener\CheckRequirementsSubscriber
5757
arguments: ['@doctrine.orm.entity_manager']
5858
tags:
5959
- { name: kernel.event_subscriber }

src/AppBundle/EventListener/CheckSQLiteEventSubscriber.php renamed to src/AppBundle/EventListener/CheckRequirementsSubscriber.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@
2828
*
2929
* @author Javier Eguiluz <[email protected]>
3030
*/
31-
class CheckSQLiteEventSubscriber implements EventSubscriberInterface
31+
class CheckRequirementsSubscriber implements EventSubscriberInterface
3232
{
33-
/**
34-
* @var EntityManager
35-
*/
33+
/** @var EntityManager */
3634
private $entityManager;
3735

3836
/**
39-
* CheckSQLiteEventSubscriber constructor.
40-
*
4137
* @param EntityManager $entityManager
4238
*/
4339
public function __construct(EntityManager $entityManager)
@@ -54,9 +50,9 @@ public static function getSubscribedEvents()
5450
return [
5551
// Exceptions are one of the events defined by the Console. See the
5652
// rest here: http://symfony.com/doc/current/components/console/events.html
57-
ConsoleEvents::EXCEPTION => 'handleDatabaseExceptions',
58-
// See: http://api.symfony.com/3.2/Symfony/Component/HttpKernel/KernelEvents.html
59-
KernelEvents::EXCEPTION => 'onKernelException',
53+
ConsoleEvents::EXCEPTION => 'handleConsoleException',
54+
// See: http://api.symfony.com/master/Symfony/Component/HttpKernel/KernelEvents.html
55+
KernelEvents::EXCEPTION => 'handleKernelException',
6056
];
6157
}
6258

@@ -67,7 +63,7 @@ public static function getSubscribedEvents()
6763
*
6864
* @param ConsoleExceptionEvent $event
6965
*/
70-
public function handleDatabaseExceptions(ConsoleExceptionEvent $event)
66+
public function handleConsoleException(ConsoleExceptionEvent $event)
7167
{
7268
$commandNames = ['doctrine:fixtures:load', 'doctrine:database:create', 'doctrine:schema:create', 'doctrine:database:drop'];
7369

@@ -80,15 +76,16 @@ public function handleDatabaseExceptions(ConsoleExceptionEvent $event)
8076
}
8177

8278
/**
83-
* This method is triggered when kernel exception occurs. And checks if sqlite extension is enabled.
79+
* This method checks if the triggered exception is related to the database
80+
* and then, it checks if the required 'sqlite3' PHP extension is enabled.
8481
*
8582
* @param GetResponseForExceptionEvent $event
8683
*/
87-
public function onKernelException(GetResponseForExceptionEvent $event)
84+
public function handleKernelException(GetResponseForExceptionEvent $event)
8885
{
8986
$exception = $event->getException();
90-
// Since any exception thrown during a Twig template rendering is wrapped in a Twig_Error_Runtime.
91-
// We must get the original exception.
87+
// Since any exception thrown during a Twig template rendering is wrapped
88+
// in a Twig_Error_Runtime, we must get the original exception.
9289
$previousException = $exception->getPrevious();
9390

9491
// Driver exception may happen in controller or in twig template rendering
@@ -101,14 +98,14 @@ public function onKernelException(GetResponseForExceptionEvent $event)
10198
}
10299

103100
/**
104-
* Check if demo application is configured to use SQLite as database.
101+
* Checks if the application is using SQLite as its database.
105102
*
106103
* @return bool
107104
*/
108105
private function isSQLitePlatform()
109106
{
110107
$databasePlatform = $this->entityManager->getConnection()->getDatabasePlatform();
111108

112-
return $databasePlatform ? $databasePlatform->getName() === 'sqlite' : false;
109+
return $databasePlatform ? 'sqlite' === $databasePlatform->getName() : false;
113110
}
114111
}

0 commit comments

Comments
 (0)