15 private ?
string $origin;
18 private bool $credentials;
20 private ?array $allowedMethods;
21 private ?array $allowedHeaders;
29 public function __construct(
string $origin =
null,
bool $credentials =
false)
31 $this->origin = $origin;
32 $this->credentials = $credentials;
34 parent::__construct();
39 $this->setCorsHeaders();
44 $this->setCorsHeaders();
49 $this->allowedMethods = $methods;
55 $this->allowedHeaders = $headers;
59 private function setCorsHeaders(): void
61 $context = Main\Context::getCurrent();
67 $response = $context->getResponse();
68 $origin = $this->origin ?: $context->getRequest()->getHeader(
'Origin');
71 $currentHttpHeaders = $response->getHeaders();
72 if (!$currentHttpHeaders->get(
'Access-Control-Allow-Origin'))
74 $currentHttpHeaders->add(
'Access-Control-Allow-Origin', $origin);
76 if ($this->credentials && !$currentHttpHeaders->get(
'Access-Control-Allow-Credentials'))
78 $currentHttpHeaders->add(
'Access-Control-Allow-Credentials',
'true');
80 if (!empty($this->allowedHeaders) && !$currentHttpHeaders->get(
'Access-Control-Allow-Headers'))
82 $currentHttpHeaders->add(
'Access-Control-Allow-Headers', implode(
',', $this->allowedHeaders));
84 if (!empty($this->allowedMethods) && !$currentHttpHeaders->get(
'Access-Control-Allow-Methods'))
86 $currentHttpHeaders->add(
'Access-Control-Allow-Methods', implode(
',', $this->allowedMethods));
__construct(string $origin=null, bool $credentials=false)
onAfterAction(Main\Event $event)
onBeforeAction(Main\Event $event)
setAllowedHeaders(array $headers)
setAllowedMethods(array $methods)