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:  * Not Allowed Handler
 17:  *
 18:  * Enhanced version of {@see \Slim\Handlers\NotAllowed}.
 19:  *
 20:  * It outputs a simple message in either JSON, XML,
 21:  * or HTML based on the Accept header.
 22:  */
 23: class NotAllowed extends AbstractHandler
 24: {
 25:     /**
 26:      * HTTP methods allowed by the current request.
 27:      *
 28:      * @var string $methods
 29:      */
 30:     protected $methods;
 31: 
 32:     /**
 33:      * Invoke "Not Allowed" Handler
 34:      *
 35:      * @param  ServerRequestInterface $request  The most recent Request object.
 36:      * @param  ResponseInterface      $response The most recent Response object.
 37:      * @param  string[]               $methods  Allowed HTTP methods.
 38:      * @return ResponseInterface
 39:      */
 40:     public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
 41:     {
 42:         $this->setMethods($methods);
 43: 
 44:         if ($request->getMethod() === 'OPTIONS') {
 45:             $status = 200;
 46:             $contentType = 'text/plain';
 47:             $output = $this->renderPlainOutput();
 48:         } else {
 49:             $status = 405;
 50:             $contentType = $this->determineContentType($request);
 51:             switch ($contentType) {
 52:                 case 'application/json':
 53:                     $output = $this->renderJsonOutput();
 54:                     break;
 55: 
 56:                 case 'text/xml':
 57:                 case 'application/xml':
 58:                     $output = $this->renderXmlOutput();
 59:                     break;
 60: 
 61:                 case 'text/html':
 62:                 default:
 63:                     $output = $this->renderHtmlOutput();
 64:                     break;
 65:             }
 66:         }
 67: 
 68:         $body = new Body(fopen('php://temp', 'r+'));
 69:         $body->write($output);
 70:         $allow = implode(', ', $methods);
 71: 
 72:         return $response
 73:                 ->withStatus($status)
 74:                 ->withHeader('Content-type', $contentType)
 75:                 ->withHeader('Allow', $allow)
 76:                 ->withBody($body);
 77:     }
 78: 
 79:     /**
 80:      * Set the HTTP methods allowed by the current request.
 81:      *
 82:      * @param  array $methods Case-sensitive array of methods.
 83:      * @return NotAllowed Chainable
 84:      */
 85:     protected function setMethods(array $methods)
 86:     {
 87:         $this->methods = implode(', ', $methods);
 88: 
 89:         return $this;
 90:     }
 91: 
 92:     /**
 93:      * Retrieves the HTTP methods allowed by the current request.
 94:      *
 95:      * @return string Returns the allowed request methods.
 96:      */
 97:     public function methods()
 98:     {
 99:         return $this->methods;
100:     }
101: 
102:     /**
103:      * Render Plain/Text Error
104:      *
105:      * @return string
106:      */
107:     protected function renderPlainOutput()
108:     {
109:         $message = $this->translator()->translate('Allowed methods:').' '.$this->methods();
110: 
111:         return $this->render($message);
112:     }
113: 
114:     /**
115:      * Render JSON Error
116:      *
117:      * @return string
118:      */
119:     protected function renderJsonOutput()
120:     {
121:         $message = $this->translator()->translate('Method not allowed. Must be one of:').' '.$this->methods();
122: 
123:         return $this->render('{"message":"'.$message.'"}');
124:     }
125: 
126:     /**
127:      * Render XML Error
128:      *
129:      * @return string
130:      */
131:     protected function renderXmlOutput()
132:     {
133:         $message = $this->translator()->translate('Method not allowed. Must be one of:').' '.$this->methods();
134: 
135:         return $this->render('<root><message>'.$message.'</message></root>');
136:     }
137: 
138:     /**
139:      * Render title of error
140:      *
141:      * @return string
142:      */
143:     public function messageTitle()
144:     {
145:         return $this->translator()->translate('Method not allowed.');
146:     }
147: 
148:     /**
149:      * Render body of HTML error
150:      *
151:      * @return string
152:      */
153:     public function renderHtmlMessage()
154:     {
155:         $title   = $this->messageTitle();
156:         $notice  = $this->translator()->translate('Method not allowed. Must be one of:');
157:         $methods = $this->methods();
158:         $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice.' <strong>'.$methods."</strong></p>\n";
159: 
160:         return $message;
161:     }
162: }
163: 
API documentation generated by ApiGen