2017-06-22 12:50:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
2017-06-25 05:12:29 +02:00
|
|
|
use SilverStripe\Control\Middleware\HTTPMiddlewareAware;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Application;
|
2019-02-27 02:50:49 +01:00
|
|
|
use SilverStripe\Core\Environment;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Kernel;
|
2019-02-27 02:50:49 +01:00
|
|
|
use SilverStripe\Core\Startup\FlushDiscoverer;
|
|
|
|
use SilverStripe\Core\Startup\CompositeFlushDiscoverer;
|
|
|
|
use SilverStripe\Core\Startup\CallbackFlushDiscoverer;
|
|
|
|
use SilverStripe\Core\Startup\RequestFlushDiscoverer;
|
|
|
|
use SilverStripe\Core\Startup\ScheduledFlushDiscoverer;
|
|
|
|
use SilverStripe\Core\Startup\DeployFlushDiscoverer;
|
2017-06-22 12:50:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes the HTTP application within an ErrorControlChain
|
|
|
|
*/
|
|
|
|
class HTTPApplication implements Application
|
|
|
|
{
|
2017-06-25 08:03:03 +02:00
|
|
|
use HTTPMiddlewareAware;
|
2017-06-22 12:50:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Kernel
|
|
|
|
*/
|
|
|
|
protected $kernel;
|
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
/**
|
|
|
|
* A custom FlushDiscoverer to be kept here
|
|
|
|
*
|
|
|
|
* @var FlushDiscoverer
|
|
|
|
*/
|
|
|
|
private $flushDiscoverer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the application with a kernel instance
|
|
|
|
*
|
|
|
|
* @param Kernel $kernel
|
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public function __construct(Kernel $kernel)
|
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
|
|
|
}
|
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
/**
|
|
|
|
* Override the default flush discovery
|
|
|
|
*
|
|
|
|
* @param FlushDiscoverer $discoverer
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setFlushDiscoverer(FlushDiscoverer $discoverer)
|
|
|
|
{
|
|
|
|
$this->flushDiscoverer = $discoverer;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current flush discoverer
|
|
|
|
*
|
|
|
|
* @param HTTPRequest $request a request to probe for flush parameters
|
|
|
|
*
|
|
|
|
* @return FlushDiscoverer
|
|
|
|
*/
|
|
|
|
public function getFlushDiscoverer(HTTPRequest $request)
|
|
|
|
{
|
|
|
|
if ($this->flushDiscoverer) {
|
|
|
|
return $this->flushDiscoverer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CompositeFlushDiscoverer([
|
|
|
|
new ScheduledFlushDiscoverer($this->kernel),
|
|
|
|
new DeployFlushDiscoverer($this->kernel),
|
|
|
|
new RequestFlushDiscoverer($request, $this->getEnvironmentType())
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current environment type (dev, test or live)
|
|
|
|
* Only checks Kernel and Server ENV as we
|
|
|
|
* don't have sessions initialized yet
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getEnvironmentType()
|
|
|
|
{
|
|
|
|
$kernel_env = $this->kernel->getEnvironment();
|
|
|
|
$server_env = Environment::getEnv('SS_ENVIRONMENT_TYPE');
|
|
|
|
|
|
|
|
$env = !is_null($kernel_env) ? $kernel_env : $server_env;
|
|
|
|
|
|
|
|
return $env;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Get the kernel for this application
|
|
|
|
*
|
|
|
|
* @return Kernel
|
|
|
|
*/
|
|
|
|
public function getKernel()
|
|
|
|
{
|
|
|
|
return $this->kernel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the given HTTP request
|
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
|
|
|
public function handle(HTTPRequest $request)
|
|
|
|
{
|
2019-02-27 02:50:49 +01:00
|
|
|
$flush = (bool) $this->getFlushDiscoverer($request)->shouldFlush();
|
2017-06-22 12:50:45 +02:00
|
|
|
|
|
|
|
// Ensure boot is invoked
|
2019-02-27 02:50:49 +01:00
|
|
|
return $this->execute($request, static function (HTTPRequest $request) {
|
2017-06-25 05:12:29 +02:00
|
|
|
return Director::singleton()->handleRequest($request);
|
2017-06-22 12:50:45 +02:00
|
|
|
}, $flush);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely boot the application and execute the given main action
|
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @param callable $callback
|
|
|
|
* @param bool $flush
|
2019-02-27 02:50:49 +01:00
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
|
|
|
public function execute(HTTPRequest $request, callable $callback, $flush = false)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->callMiddleware($request, function ($request) use ($callback, $flush) {
|
|
|
|
// Pre-request boot
|
|
|
|
$this->getKernel()->boot($flush);
|
|
|
|
return call_user_func($callback, $request);
|
|
|
|
});
|
|
|
|
} catch (HTTPResponse_Exception $ex) {
|
|
|
|
return $ex->getResponse();
|
|
|
|
} finally {
|
|
|
|
$this->getKernel()->shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|