welcome back to dyb-tech
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Default CORS configuration provider.
|
||||
*
|
||||
* Uses the bundle's semantic configuration.
|
||||
* Default settings are the lowest priority one, and can be relied upon.
|
||||
*/
|
||||
class ConfigProvider implements ProviderInterface
|
||||
{
|
||||
protected $paths;
|
||||
protected $defaults;
|
||||
|
||||
public function __construct(array $paths, array $defaults = [])
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?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.
|
||||
*/
|
||||
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 origin_regex
|
||||
* - array allow_methods
|
||||
* - array expose_headers
|
||||
* - int max_age
|
||||
*
|
||||
* @return array CORS options
|
||||
*/
|
||||
public function getOptions(Request $request): array;
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
<?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 ProviderInterface[][]
|
||||
*/
|
||||
private $providers;
|
||||
|
||||
/**
|
||||
* @param $providers ProviderInterface[]
|
||||
*/
|
||||
public function __construct(array $providers = [])
|
||||
{
|
||||
$this->providers = $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the options for $request based on {@see $providers} data
|
||||
*
|
||||
* @return array CORS options
|
||||
*/
|
||||
public function getOptions(Request $request): array
|
||||
{
|
||||
$options = [];
|
||||
foreach ($this->providers as $provider) {
|
||||
$options[] = $provider->getOptions($request);
|
||||
}
|
||||
|
||||
return array_merge(...$options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?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;
|
||||
|
||||
interface ResolverInterface
|
||||
{
|
||||
/**
|
||||
* Returns CORS options for $path
|
||||
*
|
||||
* @internal param string $path
|
||||
*/
|
||||
public function getOptions(Request $request): array;
|
||||
}
|
||||
Reference in New Issue
Block a user