Created
August 12, 2016 11:27
-
-
Save Nyholm/6c345f1f726a9a841f8e0fb1cb5897bc to your computer and use it in GitHub Desktop.
A microservice example of a AppKernel
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 | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\Config\FileLocator; | |
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | |
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; | |
use Psr\Http\Message\RequestInterface; | |
use Happyr\Geocoder\Middleware\Middleware; | |
use Relay\RelayBuilder; | |
use Zend\Diactoros\Response; | |
class AppKernel | |
{ | |
/** | |
* @var bool | |
*/ | |
private $booted = false; | |
/** | |
* @var bool | |
*/ | |
private $debug; | |
/** | |
* @var \Symfony\Component\DependencyInjection\Container | |
*/ | |
private $container; | |
/** | |
* Kernel constructor. | |
* | |
* @param $debug | |
*/ | |
public function __construct($debug = false) | |
{ | |
$this->debug = $debug; | |
} | |
/** | |
* Handle a Request and turn it in to a response. | |
* | |
* @param Request $request | |
* | |
* @return Response | |
*/ | |
public function handle(RequestInterface $request) | |
{ | |
$this->boot(); | |
$relayBuilder = new RelayBuilder(); | |
$relay = $relayBuilder->newInstance([ | |
Middleware::newrelic(), | |
$this->container->get('middleware.cache_lookup'), | |
Middleware::queryParser(), | |
Middleware::router($this->container), | |
$this->container->get('middleware.cache'), | |
]); | |
return $relay($request, new Response()); | |
} | |
/** | |
* Load the container. | |
*/ | |
public function boot() | |
{ | |
if ($this->booted) { | |
return; | |
} | |
$containerDumpFile = __DIR__.'/../app/cache/container.php'; | |
if (!$this->debug && file_exists($containerDumpFile)) { | |
require_once $containerDumpFile; | |
$container = new CachedContainer(); | |
} else { | |
$container = new ContainerBuilder(); | |
$container->setParameter('root_dir', dirname(__DIR__)); | |
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../app/')); | |
$loader->load('services.yml'); | |
if ($this->debug) { | |
$container->setAlias('cache', 'cache.void'); | |
} | |
$container->compile(); | |
if (!$this->debug) { | |
//dump the container | |
$dumper = new PhpDumper($container); | |
file_put_contents($containerDumpFile, $dumper->dump(array('class' => 'CachedContainer'))); | |
} | |
} | |
$this->container = $container; | |
$this->booted = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment