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
|
|
|
|
|
|
|
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);
|
2023-02-28 23:36:08 +01:00
|
|
|
|
|
|
|
// This is the earliest point we can do this and guarantee it's hit exactly once per request.
|
|
|
|
$this->warnAboutDeprecatedSetups();
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
return call_user_func($callback, $request);
|
|
|
|
});
|
|
|
|
} catch (HTTPResponse_Exception $ex) {
|
|
|
|
return $ex->getResponse();
|
|
|
|
} finally {
|
|
|
|
$this->getKernel()->shutdown();
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 23:36:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger deprecation notices for legacy configuration which is deprecated but
|
|
|
|
* doesn't have deprecation notices directly on the relevant API
|
|
|
|
*
|
|
|
|
* Don't remove this method even if it's just a no-op - we'll reuse this mechanism
|
|
|
|
* in the future as needed.
|
2023-03-02 22:34:10 +01:00
|
|
|
*
|
|
|
|
* Example of use going from CMS 4 to CMS 5:
|
|
|
|
*
|
|
|
|
* // TypeCreator is a class unique to GraphQL v3 - we use it in other areas to detect
|
|
|
|
* // which version is being used.
|
|
|
|
* if (class_exists(TypeCreator::class)) {
|
|
|
|
* Deprecation::notice(
|
|
|
|
* '4.13.0',
|
|
|
|
* 'silverstripe/graphql 3.x is deprecated. Upgrade to 4.x instead.'
|
|
|
|
* . ' See https://docs.silverstripe.org/en/4/upgrading/upgrading_to_graphql_4/',
|
|
|
|
* Deprecation::SCOPE_GLOBAL
|
|
|
|
* );
|
|
|
|
* }
|
2023-02-28 23:36:08 +01:00
|
|
|
*/
|
|
|
|
private function warnAboutDeprecatedSetups()
|
|
|
|
{
|
2023-03-02 22:34:10 +01:00
|
|
|
// noop
|
2023-02-28 23:36:08 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
}
|