forked from mautic/mautic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a sample Middleware to test the new Builder
- Loading branch information
1 parent
4d562c7
commit 2a9390a
Showing
6 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
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
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
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); | ||
} | ||
} |
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
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(); | ||
} |
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
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; | ||
} | ||
} |
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
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