Overview

Namespaces

  • Charcoal
    • App
      • Action
      • Config
      • Handler
      • Middleware
      • Module
      • Route
      • Script
      • ServiceProvider
      • Template

Classes

  • Charcoal\App\Action\AbstractAction
  • Charcoal\App\App
  • Charcoal\App\AppConfig
  • Charcoal\App\AppContainer
  • Charcoal\App\Config\CacheConfig
  • Charcoal\App\Config\DatabaseConfig
  • Charcoal\App\Config\FilesystemConfig
  • Charcoal\App\Config\LoggerConfig
  • Charcoal\App\Config\MemcacheCacheConfig
  • Charcoal\App\Config\MemcacheCacheServerConfig
  • Charcoal\App\Handler\AbstractHandler
  • Charcoal\App\Handler\Error
  • Charcoal\App\Handler\HandlerConfig
  • Charcoal\App\Handler\NotAllowed
  • Charcoal\App\Handler\NotFound
  • Charcoal\App\Handler\PhpError
  • Charcoal\App\Handler\Shutdown
  • Charcoal\App\Middleware\CacheMiddleware
  • Charcoal\App\Module\AbstractModule
  • Charcoal\App\Module\ModuleConfig
  • Charcoal\App\Module\ModuleManager
  • Charcoal\App\Route\ActionRoute
  • Charcoal\App\Route\ActionRouteConfig
  • Charcoal\App\Route\RouteConfig
  • Charcoal\App\Route\RouteManager
  • Charcoal\App\Route\ScriptRoute
  • Charcoal\App\Route\ScriptRouteConfig
  • Charcoal\App\Route\TemplateRoute
  • Charcoal\App\Route\TemplateRouteConfig
  • Charcoal\App\Script\AbstractScript
  • Charcoal\App\ServiceProvider\AppServiceProvider
  • Charcoal\App\ServiceProvider\CacheServiceProvider
  • Charcoal\App\ServiceProvider\DatabaseServiceProvider
  • Charcoal\App\ServiceProvider\FilesystemServiceProvider
  • Charcoal\App\ServiceProvider\LoggerServiceProvider
  • Charcoal\App\ServiceProvider\ViewServiceProvider
  • Charcoal\App\Template\AbstractTemplate
  • Charcoal\App\Template\AbstractWidget
  • Charcoal\App\Template\WidgetBuilder

Interfaces

  • Charcoal\App\Action\ActionInterface
  • Charcoal\App\AppAwareInterface
  • Charcoal\App\Handler\HandlerInterface
  • Charcoal\App\Module\ModuleInterface
  • Charcoal\App\Route\RouteInterface
  • Charcoal\App\Script\CronScriptInterface
  • Charcoal\App\Script\ScriptInterface
  • Charcoal\App\Template\TemplateInterface
  • Charcoal\App\Template\WidgetInterface

Traits

  • Charcoal\App\AppAwareTrait
  • Charcoal\App\CallableResolverAwareTrait
  • Charcoal\App\Script\ArgScriptTrait
  • Charcoal\App\Script\CronScriptTrait
  • Charcoal\App\Script\PathScriptTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\App\Handler;
  4: 
  5: // Dependencies from PSR-7 (HTTP Messaging)
  6: use Psr\Http\Message\ServerRequestInterface;
  7: use Psr\Http\Message\ResponseInterface;
  8: 
  9: // Dependency from Slim
 10: use Slim\Http\Body;
 11: 
 12: // Local Dependencies
 13: use Charcoal\App\Handler\AbstractHandler;
 14: 
 15: /**
 16:  * Shutdown Handler
 17:  *
 18:  * It outputs a simple message in either JSON, XML, or HTML based on the Accept header.
 19:  *
 20:  * A maintenance mode check is included in the default middleware stack for your application.
 21:  * This is a practical feature to "disable" your application while performing an update
 22:  * or maintenance.
 23:  */
 24: class Shutdown extends AbstractHandler
 25: {
 26:     /**
 27:      * HTTP methods allowed by the current request.
 28:      *
 29:      * @var string $methods
 30:      */
 31:     protected $methods;
 32: 
 33:     /**
 34:      * Invoke "Maintenance" Handler
 35:      *
 36:      * @param  ServerRequestInterface $request  The most recent Request object.
 37:      * @param  ResponseInterface      $response The most recent Response object.
 38:      * @param  string[]               $methods  Allowed HTTP methods.
 39:      * @return ResponseInterface
 40:      */
 41:     public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
 42:     {
 43:         $this->setMethods($methods);
 44: 
 45:         if ($request->getMethod() === 'OPTIONS') {
 46:             $contentType = 'text/plain';
 47:             $output = $this->renderPlainOutput();
 48:         } else {
 49:             $contentType = $this->determineContentType($request);
 50:             switch ($contentType) {
 51:                 case 'application/json':
 52:                     $output = $this->renderJsonOutput();
 53:                     break;
 54: 
 55:                 case 'text/xml':
 56:                 case 'application/xml':
 57:                     $output = $this->renderXmlOutput();
 58:                     break;
 59: 
 60:                 case 'text/html':
 61:                 default:
 62:                     $output = $this->renderHtmlOutput();
 63:                     break;
 64:             }
 65:         }
 66: 
 67:         $body = new Body(fopen('php://temp', 'r+'));
 68:         $body->write($output);
 69: 
 70:         return $response
 71:                 ->withStatus(503)
 72:                 ->withHeader('Content-type', $contentType)
 73:                 ->withBody($body);
 74:     }
 75: 
 76:     /**
 77:      * Set the HTTP methods allowed by the current request.
 78:      *
 79:      * @param  array $methods Case-sensitive array of methods.
 80:      * @return Shutdown Chainable
 81:      */
 82:     protected function setMethods(array $methods)
 83:     {
 84:         $this->methods = implode(', ', $methods);
 85: 
 86:         return $this;
 87:     }
 88: 
 89:     /**
 90:      * Retrieves the HTTP methods allowed by the current request.
 91:      *
 92:      * @return string Returns the allowed request methods.
 93:      */
 94:     public function methods()
 95:     {
 96:         return $this->methods;
 97:     }
 98: 
 99:     /**
100:      * Render Plain/Text Error
101:      *
102:      * @return string
103:      */
104:     protected function renderPlainOutput()
105:     {
106:         $message = $this->translator()->translate('Down for maintenance!');
107: 
108:         return $this->render($message);
109:     }
110: 
111:     /**
112:      * Render JSON Error
113:      *
114:      * @return string
115:      */
116:     protected function renderJsonOutput()
117:     {
118:         $message = $this->translator()->translate('We are currently unavailable. Check back in 15 minutes.');
119: 
120:         return $this->render('{"message":"'.$message.'"}');
121:     }
122: 
123:     /**
124:      * Render XML Error
125:      *
126:      * @return string
127:      */
128:     protected function renderXmlOutput()
129:     {
130:         $message = $this->translator()->translate('We are currently unavailable. Check back in 15 minutes.');
131: 
132:         return $this->render('<root><message>'.$message.'</message></root>');
133:     }
134: 
135:     /**
136:      * Render title of error
137:      *
138:      * @return string
139:      */
140:     public function messageTitle()
141:     {
142:         return $this->translator()->translate('Down for maintenance!');
143:     }
144: 
145:     /**
146:      * Render body of HTML error
147:      *
148:      * @return string
149:      */
150:     public function renderHtmlMessage()
151:     {
152:         $title   = $this->messageTitle();
153:         $notice  = $this->translator()->translate('currently-unavailable');
154:         $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice."</p>\n";
155: 
156:         return $message;
157:     }
158: }
159: 
API documentation generated by ApiGen