Created
September 7, 2013 00:40
-
-
Save anonymous/6471770 to your computer and use it in GitHub Desktop.
Capture Database Connection error
This file contains 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
<?php | |
namespace Application; | |
use Application\Exception\ParserException; | |
use Application\Model\User; | |
use Application\Schema\SchemaGateway; | |
use Zend\Db\ResultSet\ResultSet; | |
use Zend\EventManager\EventManagerInterface; | |
use Zend\Mvc\ModuleRouteListener; | |
use Zend\Mvc\MvcEvent; | |
use Zend\ServiceManager\ServiceManager; | |
use Zend\Session\Container; | |
use Zend\View\Model\ViewModel; | |
class Module | |
{ | |
public function onBootstrap( MvcEvent $mvcEvent ) | |
{ | |
/** | |
* @var $eventManager EventManagerInterface | |
*/ | |
$eventManager = $mvcEvent->getApplication()->getEventManager(); | |
$this->startSession( $mvcEvent ); | |
$eventManager->attach( | |
MvcEvent::EVENT_DISPATCH_ERROR, function ( MvcEvent $e ) | |
{ | |
if ( $exception = $e->getParam( 'exception' ) ) | |
{ | |
if ( ParserException::create( $exception )->isInvalidQueryException() ) | |
{ | |
$dbAdapter = $e->getApplication()->getServiceManager()->get( 'Zend\Db\Adapter\Adapter' ); | |
$resultSetPrototype = new ResultSet(); | |
$resultSetPrototype->setArrayObjectPrototype( new User() ); | |
$schema = new SchemaGateway( 'user', $dbAdapter, null, $resultSetPrototype ); | |
if ( $schema->isSchemaHealed( $exception ) ) | |
{ | |
/** | |
* @var $controller \Zend\Mvc\Controller\AbstractActionController | |
*/ | |
$controller = $e->getTarget(); | |
$controller->redirect()->refresh(); | |
} | |
} | |
} | |
} | |
); | |
$moduleRouteListener = new ModuleRouteListener(); | |
$moduleRouteListener->attach( $eventManager ); | |
} | |
private function startSession( MvcEvent $mvcEvent ) | |
{ | |
/** | |
* @var $serviceManager ServiceManager | |
*/ | |
$serviceManager = $mvcEvent->getApplication()->getServiceManager(); | |
try | |
{ | |
$dbAdapter = $serviceManager->get( 'Zend\Db\Adapter\Adapter' ); | |
$dbAdapter->getDriver()->getConnection()->connect(); | |
$sessionManager = $serviceManager->get( 'DatabaseSessionManager' ); | |
$sessionManager->start(); | |
Container::setDefaultManager( $sessionManager ); | |
} | |
catch ( \Exception $exc ) | |
{ | |
$ViewModel = $mvcEvent->getViewModel(); | |
$ViewModel->setTemplate( 'layout/error-layout' ); | |
$content = new ViewModel(); | |
$content->setTemplate( 'error/503' ); | |
$ViewModel->setVariable( | |
'content', $serviceManager->get( 'ViewRenderer' )->render( $content ) | |
); | |
exit( $serviceManager->get( 'ViewRenderer' )->render( $ViewModel ) ); | |
} | |
} | |
public function getConfig() | |
{ | |
return include __DIR__ . '/config/module.config.php'; | |
} | |
public function getServiceConfig() | |
{ | |
return array ( | |
'factories' => array ( | |
'Application\Model\UserTable' => 'Application\Factory\UserTableFactory', | |
'Application\Model\UserRegisterLocationTable' => 'Application\Factory\UserRegisterLocationTableFactory', | |
'Application\Model\GeoLocationTable' => 'Application\Factory\GeoLocationTableFactory', | |
'Application\Model\RegionTable' => 'Application\Factory\RegionTableFactory', | |
'Application\Model\CountryTable' => 'Application\Factory\CountryTableFactory', | |
'Application\Model\IpAddressLocationTable' => 'Application\Factory\IpAddressLocationTableFactory', | |
'Application\Model\UserActivityLocationTable' => 'Application\Factory\UserActivityLocationTableFactory', | |
'DatabaseSessionManager' => 'Application\Factory\SessionManagerFactory', | |
), | |
); | |
} | |
public function getAutoLoaderConfig() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment