silverstripe /
silverstripe-framework
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\Control; |
||
| 4 | |||
| 5 | use SilverStripe\Control\Middleware\HTTPMiddlewareAware; |
||
| 6 | use SilverStripe\Core\Application; |
||
| 7 | use SilverStripe\Core\Environment; |
||
| 8 | use SilverStripe\Core\Kernel; |
||
| 9 | use SilverStripe\Core\Startup\FlushDiscoverer; |
||
| 10 | use SilverStripe\Core\Startup\CompositeFlushDiscoverer; |
||
| 11 | use SilverStripe\Core\Startup\CallbackFlushDiscoverer; |
||
| 12 | use SilverStripe\Core\Startup\RequestFlushDiscoverer; |
||
| 13 | use SilverStripe\Core\Startup\ScheduledFlushDiscoverer; |
||
| 14 | use SilverStripe\Core\Startup\DeployFlushDiscoverer; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Invokes the HTTP application within an ErrorControlChain |
||
| 18 | */ |
||
| 19 | class HTTPApplication implements Application |
||
| 20 | { |
||
| 21 | use HTTPMiddlewareAware; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Kernel |
||
| 25 | */ |
||
| 26 | protected $kernel; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * A custom FlushDiscoverer to be kept here |
||
| 30 | * |
||
| 31 | * @var FlushDiscoverer |
||
| 32 | */ |
||
| 33 | private $flushDiscoverer = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Initialize the application with a kernel instance |
||
| 37 | * |
||
| 38 | * @param Kernel $kernel |
||
| 39 | */ |
||
| 40 | public function __construct(Kernel $kernel) |
||
| 41 | { |
||
| 42 | $this->kernel = $kernel; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Override the default flush discovery |
||
| 47 | * |
||
| 48 | * @param FlushDiscoverer $discoverer |
||
| 49 | * |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | public function setFlushDiscoverer(FlushDiscoverer $discoverer) |
||
| 53 | { |
||
| 54 | $this->flushDiscoverer = $discoverer; |
||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns the current flush discoverer |
||
| 60 | * |
||
| 61 | * @param HTTPRequest $request a request to probe for flush parameters |
||
| 62 | * |
||
| 63 | * @return FlushDiscoverer |
||
| 64 | */ |
||
| 65 | public function getFlushDiscoverer(HTTPRequest $request) |
||
| 66 | { |
||
| 67 | if ($this->flushDiscoverer) { |
||
| 68 | return $this->flushDiscoverer; |
||
| 69 | } |
||
| 70 | |||
| 71 | return new CompositeFlushDiscoverer([ |
||
| 72 | new ScheduledFlushDiscoverer($this->kernel), |
||
| 73 | new DeployFlushDiscoverer($this->kernel), |
||
| 74 | new RequestFlushDiscoverer($request, $this->getEnvironmentType()) |
||
| 75 | ]); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Return the current environment type (dev, test or live) |
||
| 80 | * Only checks Kernel and Server ENV as we |
||
| 81 | * don't have sessions initialized yet |
||
| 82 | * |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | protected function getEnvironmentType() |
||
| 86 | { |
||
| 87 | $kernel_env = $this->kernel->getEnvironment(); |
||
| 88 | $server_env = Environment::getEnv('SS_ENVIRONMENT_TYPE'); |
||
| 89 | |||
| 90 | $env = !is_null($kernel_env) ? $kernel_env : $server_env; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 91 | |||
| 92 | return $env; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the kernel for this application |
||
| 97 | * |
||
| 98 | * @return Kernel |
||
| 99 | */ |
||
| 100 | public function getKernel() |
||
| 101 | { |
||
| 102 | return $this->kernel; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Handle the given HTTP request |
||
| 107 | * |
||
| 108 | * @param HTTPRequest $request |
||
| 109 | * @return HTTPResponse |
||
| 110 | */ |
||
| 111 | public function handle(HTTPRequest $request) |
||
| 112 | { |
||
| 113 | $flush = (bool) $this->getFlushDiscoverer($request)->shouldFlush(); |
||
| 114 | |||
| 115 | // Ensure boot is invoked |
||
| 116 | return $this->execute($request, static function (HTTPRequest $request) { |
||
| 117 | return Director::singleton()->handleRequest($request); |
||
| 118 | }, $flush); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Safely boot the application and execute the given main action |
||
| 123 | * |
||
| 124 | * @param HTTPRequest $request |
||
| 125 | * @param callable $callback |
||
| 126 | * @param bool $flush |
||
| 127 | * |
||
| 128 | * @return HTTPResponse |
||
| 129 | */ |
||
| 130 | public function execute(HTTPRequest $request, callable $callback, $flush = false) |
||
| 131 | { |
||
| 132 | try { |
||
| 133 | return $this->callMiddleware($request, function ($request) use ($callback, $flush) { |
||
| 134 | // Pre-request boot |
||
| 135 | $this->getKernel()->boot($flush); |
||
| 136 | return call_user_func($callback, $request); |
||
| 137 | }); |
||
| 138 | } catch (HTTPResponse_Exception $ex) { |
||
| 139 | return $ex->getResponse(); |
||
| 140 | } finally { |
||
| 141 | $this->getKernel()->shutdown(); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 |