Skip to content

Commit

Permalink
Add StackPHP and a basic Exception catching middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dongilbert committed May 16, 2016
1 parent 55d0d60 commit 2458814
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 39 deletions.
51 changes: 51 additions & 0 deletions app/middlewares/CatchExceptionMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Mautic\Middleware;

use Mautic\CoreBundle\Exception\DatabaseConnectionException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class CatchExceptionMiddleware implements HttpKernelInterface
{
/**
* @var HttpKernelInterface
*/
protected $app;

/**
* CatchExceptionMiddleware constructor.
*
* @param HttpKernelInterface $app
*/
public function __construct(HttpKernelInterface $app)
{
$this->app = $app;
}

/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
try {
$response = $this->app->handle($request, $type, $catch);

if ($response instanceof Response) {
return $response;
}
} catch (DatabaseConnectionException $e) {
define('MAUTIC_OFFLINE', 1);
$message = $e->getMessage();
} catch (\Exception $e) {
error_log($e);
define('MAUTIC_OFFLINE', 1);
$message = 'The site is currently offline due to encountering an error. If the problem persists, please contact the system administrator.';
$submessage = 'System administrators, check server logs for errors.';
}

include MAUTIC_ROOT_DIR . '/offline.php';
exit;
}
}
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
"Mautic\\AddonBundle\\Bundle\\":"app/bundles/PluginBundle/Bundle/BC",
"Mautic\\": "app/bundles/",
"MauticPlugin\\": "plugins/",
"MauticAddon\\": "addons/"
}
"Mautic\\Middleware\\": "app/middlewares/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
"require": {
"php": ">=5.3.10",
Expand Down Expand Up @@ -92,7 +96,10 @@
"geoip2/geoip2": "~2.0",
"camspiers/json-pretty": "1.0.*",
"misd/phone-number-bundle": "^1.1",
"ip2location/ip2location-php": "^7.2"
"ip2location/ip2location-php": "^7.2",

"stack/run": "^1.0",
"stack/builder": "^1.0"
},
"require-dev": {
"symfony/web-profiler-bundle": "2.5.*",
Expand Down
103 changes: 101 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 6 additions & 26 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Define Mautic's supported PHP versions
define('MAUTIC_MINIMUM_PHP', '5.3.7');
define('MAUTIC_MAXIMUM_PHP', '5.6.999');
define('MAUTIC_ROOT_DIR', __DIR__);

// Are we running the minimum version?
if (version_compare(PHP_VERSION, MAUTIC_MINIMUM_PHP, '<')) {
Expand Down Expand Up @@ -42,30 +43,9 @@
$apcLoader->register(true);
*/

require_once __DIR__ . '/app/AppKernel.php';
//require_once __DIR__.'/mautic/app/AppCache.php';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();

try {
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);

// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
//Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

} catch (\Mautic\CoreBundle\Exception\DatabaseConnectionException $e) {
define('MAUTIC_OFFLINE', 1);
$message = $e->getMessage();
include __DIR__ . '/offline.php';
} catch (\Exception $e) {
error_log($e);

define('MAUTIC_OFFLINE', 1);
$message = 'The site is currently offline due to encountering an error. If the problem persists, please contact the system administrator.';
$submessage = 'System administrators, check server logs for errors.';
include __DIR__ . '/offline.php';
}
Stack\run((new Stack\Builder)
->push('Mautic\Middleware\CatchExceptionMiddleware')
->resolve($kernel));
12 changes: 4 additions & 8 deletions index_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
// Fix for hosts that do not have date.timezone set, it will be reset based on users settings
date_default_timezone_set ('UTC');

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;

// If you don't want to setup permissions the proper way, just uncomment the following PHP line
Expand All @@ -53,11 +52,8 @@
$loader = require_once __DIR__ . '/vendor/autoload.php';
Debug::enable();

require_once __DIR__.'/app/AppKernel.php';

$kernel = new AppKernel('dev', true);
$kernel = new AppKernel('dev', false);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

Stack\run((new Stack\Builder)
->resolve($kernel));

0 comments on commit 2458814

Please # to comment.