This commit is contained in:
Marek
2026-03-24 00:04:55 +01:00
commit c5229e48ed
4225 changed files with 511461 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
<?php
/*
* This file is part of the NelmioCorsBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\CorsBundle\Options;
use Symfony\Component\HttpFoundation\Request;
use Nelmio\CorsBundle\DependencyInjection\NelmioCorsExtension;
/**
* Default CORS configuration provider.
*
* Uses the bundle's semantic configuration.
* Default settings are the lowest priority one, and can be relied upon.
*
* @phpstan-import-type CorsCompleteOptions from ProviderInterface
* @phpstan-import-type CorsOptionsPerPath from ProviderInterface
*/
class ConfigProvider implements ProviderInterface
{
/**
* @var CorsOptionsPerPath
*/
protected $paths;
/**
* @var array<string, bool|array<string>|int>
* @phpstan-var CorsCompleteOptions
*/
protected $defaults;
/**
* @param CorsOptionsPerPath $paths
* @param array<string, bool|array<string>|int> $defaults
* @phpstan-param CorsCompleteOptions $defaults
*/
public function __construct(array $paths, ?array $defaults = null)
{
$this->defaults = $defaults === null ? NelmioCorsExtension::DEFAULTS : $defaults;
$this->paths = $paths;
}
public function getOptions(Request $request): array
{
$uri = $request->getPathInfo() ?: '/';
foreach ($this->paths as $pathRegexp => $options) {
if (preg_match('{'.$pathRegexp.'}i', $uri)) {
$options = array_merge($this->defaults, $options);
// skip if the host is not matching
if (count($options['hosts']) > 0) {
foreach ($options['hosts'] as $hostRegexp) {
if (preg_match('{'.$hostRegexp.'}i', $request->getHost())) {
return $options;
}
}
continue;
}
return $options;
}
}
return $this->defaults;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of the NelmioCorsBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\CorsBundle\Options;
use Symfony\Component\HttpFoundation\Request;
/**
* CORS configuration provider interface.
*
* Can override CORS options for a particular path.
*
* @phpstan-type CorsOptions array{hosts?: list<string>, allow_credentials?: bool, allow_origin?: bool|list<string>, allow_headers?: bool|list<string>, allow_private_network?: bool, origin_regex?: bool, allow_methods?: list<string>, expose_headers?: list<string>, max_age?: int, forced_allow_origin_value?: string, skip_same_as_origin?: bool}
* @phpstan-type CorsCompleteOptions array{hosts: list<string>, allow_credentials: bool, allow_origin: bool|list<string>, allow_headers: bool|list<string>, allow_private_network: bool, origin_regex: bool, allow_methods: list<string>, expose_headers: list<string>, max_age: int, forced_allow_origin_value?: string, skip_same_as_origin: bool}
* @phpstan-type CorsOptionsPerPath array<string, CorsOptions>
*/
interface ProviderInterface
{
/**
* Returns CORS options for $request.
*
* Any valid CORS option will overwrite those of the previous ones.
* The method must at least return an empty array.
*
* All keys of the bundle's semantical configuration are valid:
* - bool allow_credentials
* - bool allow_origin
* - bool allow_headers
* - bool allow_private_network
* - bool origin_regex
* - array allow_methods
* - array expose_headers
* - int max_age
*
* @return array<string, bool|array<string>|int> CORS options
* @phpstan-return CorsOptions
*/
public function getOptions(Request $request): array;
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the NelmioCorsBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\CorsBundle\Options;
use Symfony\Component\HttpFoundation\Request;
/**
* CORS options resolver.
*
* Uses Cors providers to resolve options for an HTTP request
*/
class Resolver implements ResolverInterface
{
/**
* CORS configuration providers, indexed by numerical priority
* @var list<ProviderInterface>
*/
private $providers;
/**
* @param list<ProviderInterface> $providers
*/
public function __construct(array $providers = [])
{
$this->providers = $providers;
}
/**
* Resolves the options for $request based on {@see $providers} data
*/
public function getOptions(Request $request): array
{
$options = [];
foreach ($this->providers as $provider) {
$options[] = $provider->getOptions($request);
}
// @phpstan-ignore return.type (the default ConfigProvider will ensure default array is always setting every key)
return array_merge(...$options);
}
}

View File

@@ -0,0 +1,27 @@
<?php
/*
* This file is part of the NelmioCorsBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\CorsBundle\Options;
use Symfony\Component\HttpFoundation\Request;
/**
* @phpstan-import-type CorsCompleteOptions from ProviderInterface
*/
interface ResolverInterface
{
/**
* Returns CORS options for a request's path
*
* @return array<string, bool|array<string>|int> CORS options
* @phpstan-return CorsCompleteOptions
*/
public function getOptions(Request $request): array;
}