Skip to content

Commit

Permalink
Add a sample Middleware to test the new Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dongilbert committed May 17, 2016
1 parent 4d562c7 commit 2a9390a
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 6 deletions.
20 changes: 19 additions & 1 deletion app/middlewares/CatchExceptionMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<?php

/**
* @package Mautic
* @copyright 2016 Mautic Contributors. All rights reserved.
* @author Mautic
* @link http://mautic.org
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

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
class CatchExceptionMiddleware implements HttpKernelInterface, PrioritizedMiddlewareInterface
{
const PRIORITY = 1;

/**
* @var HttpKernelInterface
*/
Expand Down Expand Up @@ -48,4 +58,12 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch =
include MAUTIC_ROOT_DIR . '/offline.php';
exit;
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
return self::PRIORITY;
}
}
52 changes: 52 additions & 0 deletions app/middlewares/MiddlewareBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @package Mautic
* @copyright 2016 Mautic Contributors. All rights reserved.
* @author Mautic
* @link http://mautic.org
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\Middleware;

use Stack\StackedHttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class MiddlewareBuilder
{
protected $specs;

public function __construct()
{
$this->specs = new \SplPriorityQueue();

$middlewares = glob(__DIR__ . '/*Middleware.php');

foreach ($middlewares as $middleware) {
$this->push('Mautic\\Middleware\\' . basename(substr($middleware, 0, -4)));
}
}

public function push($kernelClass)
{
$reflection = new \ReflectionClass($kernelClass);
$priority = $reflection->getConstant('PRIORITY');

$this->specs->insert($reflection, $priority);
}

public function resolve(HttpKernelInterface $app)
{
$middlewares = array($app);

/** @var \ReflectionClass $spec */
foreach ($this->specs as $spec) {
$app = $spec->newInstanceArgs([$app]);

array_unshift($middlewares, $app);
}

return new StackedHttpKernel($app, $middlewares);
}
}
21 changes: 21 additions & 0 deletions app/middlewares/PrioritizedMiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @package Mautic
* @copyright 2016 Mautic Contributors. All rights reserved.
* @author Mautic
* @link http://mautic.org
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\Middleware;

interface PrioritizedMiddlewareInterface
{
/**
* Get the middleware's priority
*
* @return int
*/
public function getPriority();
}
54 changes: 54 additions & 0 deletions app/middlewares/SetMauticHeaderMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* @package Mautic
* @copyright 2016 Mautic Contributors. All rights reserved.
* @author Mautic
* @link http://mautic.org
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\Middleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class SetMauticHeaderMiddleware implements HttpKernelInterface, PrioritizedMiddlewareInterface
{
const PRIORITY = 2;

/**
* @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)
{
$response = $this->app->handle($request, $type, $catch);

$response->headers->set('X-Mautic-Version', \AppKernel::MAJOR_VERSION . '.' . \AppKernel::MINOR_VERSION . '.' . \AppKernel::PATCH_VERSION . \AppKernel::EXTRA_VERSION);

return $response;
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
return self::PRIORITY;
}
}
4 changes: 1 addition & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,4 @@
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();

Stack\run((new Stack\Builder)
->push('Mautic\Middleware\CatchExceptionMiddleware')
->resolve($kernel));
Stack\run((new \Mautic\Middleware\MiddlewareBuilder)->resolve($kernel));
3 changes: 1 addition & 2 deletions index_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@
$kernel = new AppKernel('dev', false);
$kernel->loadClassCache();

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

0 comments on commit 2a9390a

Please # to comment.