Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jul 24, 2024
1 parent f25b1ba commit 9deb4f1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/config/GeneralConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class GeneralConfig extends BaseConfig
*
* @group GraphQL
* @since 3.5.0
* @deprecated in 4.11.0 Use \craft\filters\Cors::class instead.
* @deprecated in 4.11.0. [[\craft\filters\Cors]] should be used instead.
* @see https://www.yiiframework.com/doc/api/2.0/yii-filters-cors
*/
public array|null|false $allowedGraphqlOrigins = null;
Expand Down Expand Up @@ -1977,7 +1977,7 @@ class GeneralConfig extends BaseConfig
*
* @group System
* @since 3.6.14
* @deprecated in 4.11.0 Use \craft\filters\Headers::class instead
* @deprecated in 4.11.0. [[\craft\filters\Headers]] should be used instead.
*/
public ?string $permissionsPolicyHeader = null;

Expand Down Expand Up @@ -3472,7 +3472,7 @@ public function allowAdminChanges(bool $value = true): self
* @return self
* @see $allowedGraphqlOrigins
* @since 4.2.0
* @deprecated in 4.11.0 Use \craft\filters\Cors::class instead
* @deprecated in 4.11.0. [[\craft\filters\Cors]] should be used instead.
* @see https://www.yiiframework.com/doc/api/2.0/yii-filters-cors
*/
public function allowedGraphqlOrigins(array|null|false $value): self
Expand Down Expand Up @@ -5370,7 +5370,7 @@ public function pathParam(?string $value): self
* @return self
* @see $permissionsPolicyHeader
* @since 4.2.0
* @deprecated in 4.11.0 Use \craft\filters\Headers::class instead.
* @deprecated in 4.11.0. [[\craft\filters\Headers]] should be used instead.
*/
public function permissionsPolicyHeader(?string $value): self
{
Expand Down
13 changes: 10 additions & 3 deletions src/filters/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@
namespace craft\filters;

use Craft;
use yii\base\ActionFilter;

/**
* Filter for adding arbitrary headers to site responses and handling OPTIONS requests.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.11.0
*/
class Headers extends \yii\base\ActionFilter
class Headers extends ActionFilter
{
use SiteFilterTrait;

/**
* @var array<string,string|string[]> The headers that should be set on responses.
*/
public array $headers = [];

public function beforeAction($action): bool
{
foreach ($this->headers as $name => $value) {
Craft::$app->getResponse()->getHeaders()->set($name, $value);
if (!empty($this->headers)) {
$responseHeaders = Craft::$app->getResponse()->getHeaders();
foreach ($this->headers as $name => $value) {
$responseHeaders->set($name, $value);
}
}

return true;
Expand Down
9 changes: 7 additions & 2 deletions src/filters/SiteFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* Trait to make a filter site-aware.
*
* @property-write int|string|Site|array<int|string|Site> $site
* @see https://www.yiiframework.com/doc/api/2.0/yii-filters-cors
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.11.0
Expand All @@ -24,11 +25,11 @@ trait SiteFilterTrait

protected function isActive(mixed $action): bool
{
if (Craft::$app->getRequest()->isCpRequest || !$this->isCurrentSiteActive()) {
if (!parent::isActive($action)) {
return false;
}

return parent::isActive($action);
return !$this->isCurrentSiteActive();
}

protected function setSite(null|array|int|string|Site $value): void
Expand Down Expand Up @@ -59,6 +60,10 @@ private function getSiteId(int|string|Site $value): int

private function isCurrentSiteActive(): bool
{
if (!Craft::$app->getRequest()->getIsSiteRequest()) {
return false;
}

return $this->siteIds === null || in_array(Craft::$app->getSites()->getCurrentSite()->id, $this->siteIds, true);
}
}
4 changes: 2 additions & 2 deletions src/web/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ private function _processActionRequest(Request $request): ?BaseResponse

// Return the response for OPTIONS requests that return null
// to support the CORS filter: https://www.yiiframework.com/doc/api/2.0/yii-filters-cors
return $response === null && $this->getRequest()->getIsOptions()
? $this->getResponse()
return $request->getIsOptions()
? ($response ?? $this->getResponse())
: $response;
} catch (Throwable $e) {
$this->_unregisterDebugModule();
Expand Down

0 comments on commit 9deb4f1

Please sign in to comment.