Skip to content

Improved the event subscriber that listens to database exceptions #452

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

Merged
merged 1 commit into from
Feb 7, 2017
Merged
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
4 changes: 2 additions & 2 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ services:
# 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\CheckSQLiteEventSubscriber
app.requirements_subscriber:
class: AppBundle\EventListener\CheckRequirementsSubscriber
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: kernel.event_subscriber }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@
*
* @author Javier Eguiluz <[email protected]>
*/
class CheckSQLiteEventSubscriber implements EventSubscriberInterface
class CheckRequirementsSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManager
*/
/** @var EntityManager */
private $entityManager;

/**
* CheckSQLiteEventSubscriber constructor.
*
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
Expand All @@ -54,9 +50,9 @@ 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',
// See: http://api.symfony.com/3.2/Symfony/Component/HttpKernel/KernelEvents.html
KernelEvents::EXCEPTION => 'onKernelException',
ConsoleEvents::EXCEPTION => 'handleConsoleException',
// See: http://api.symfony.com/master/Symfony/Component/HttpKernel/KernelEvents.html
Copy link
Member

Choose a reason for hiding this comment

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

can we use current on the API doc or no ?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was my first try, but ...

api_current

Copy link
Member

Choose a reason for hiding this comment

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

then fine with using master

KernelEvents::EXCEPTION => 'handleKernelException',
];
}

Expand All @@ -67,7 +63,7 @@ public static function getSubscribedEvents()
*
* @param ConsoleExceptionEvent $event
*/
public function handleDatabaseExceptions(ConsoleExceptionEvent $event)
public function handleConsoleException(ConsoleExceptionEvent $event)
{
$commandNames = ['doctrine:fixtures:load', 'doctrine:database:create', 'doctrine:schema:create', 'doctrine:database:drop'];

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

/**
* This method is triggered when kernel exception occurs. And checks if sqlite extension is enabled.
* This method checks if the triggered exception is related to the database
* and then, it checks if the required 'sqlite3' PHP extension is enabled.
*
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
public function handleKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
// Since any exception thrown during a Twig template rendering is wrapped in a Twig_Error_Runtime.
// We must get the original exception.
// Since any exception thrown during a Twig template rendering is wrapped
// in a Twig_Error_Runtime, we must get the original exception.
$previousException = $exception->getPrevious();

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

/**
* Check if demo application is configured to use SQLite as database.
* Checks if the application is using SQLite as its database.
*
* @return bool
*/
private function isSQLitePlatform()
{
$databasePlatform = $this->entityManager->getConnection()->getDatabasePlatform();

return $databasePlatform ? $databasePlatform->getName() === 'sqlite' : false;
return $databasePlatform ? 'sqlite' === $databasePlatform->getName() : false;
}
}