-
-
Save Padam87/ad4434502de71e32626fbb4daa7f1b47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php return array ( | |
'#^/oauth/v2/token$#s' => | |
array ( | |
'allow_methods' => | |
array ( | |
0 => 'GET', | |
1 => 'POST', | |
), | |
), | |
'#^/oauth/v2/auth$#s' => | |
array ( | |
'allow_methods' => | |
array ( | |
0 => 'GET', | |
1 => 'POST', | |
), | |
), | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\ApiBundle\HttpKernel; | |
use JMS\DiExtraBundle\Annotation as DI; | |
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer; | |
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | |
use Symfony\Component\Routing\RouterInterface; | |
/** | |
* @DI\Service() | |
* @DI\Tag("kernel.cache_warmer") | |
*/ | |
class CorsPathsCacheWarmer extends CacheWarmer implements CacheWarmerInterface | |
{ | |
/** | |
* @var RouterInterface | |
*/ | |
private $router; | |
/** | |
* @var array | |
*/ | |
private $config; | |
/** | |
* @DI\InjectParams({ | |
* "router" = @DI\Inject("router"), | |
* "config" = @DI\Inject("%nelmio_cors.defaults%") | |
* }) | |
* | |
* @param RouterInterface $router | |
* @param array $config | |
*/ | |
public function __construct(RouterInterface $router, array $config) | |
{ | |
$this->router = $router; | |
$this->config = $config; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function isOptional() | |
{ | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function warmUp($cacheDir) | |
{ | |
$paths = []; | |
foreach ($this->router->getRouteCollection()->all() as $route) { | |
$compiledRoute = $route->compile(); | |
$hostMatch = false; | |
foreach ($this->config['hosts'] as $regex) { | |
if (preg_match('{'.$regex.'}i', $route->getHost())) { | |
$hostMatch = true; | |
break; | |
} | |
} | |
if (!$hostMatch) { | |
continue; | |
} | |
$paths[$compiledRoute->getRegex()]['allow_methods'] = array_merge( | |
isset($paths[$compiledRoute->getRegex()]['allow_methods']) | |
? $paths[$compiledRoute->getRegex()]['allow_methods'] | |
: [], | |
$route->getMethods() | |
); | |
} | |
$this->writeCacheFile($cacheDir . '/cors.cache.php', '<?php return ' . var_export($paths, true) . ';'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\ApiBundle\Service; | |
use JMS\DiExtraBundle\Annotation as DI; | |
use Nelmio\CorsBundle\Options\ProviderInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
/** | |
* @DI\Service() | |
* @DI\Tag("nelmio_cors.options_provider", attributes={ "priority": 1 }) | |
*/ | |
class RouterBasedConfigProvider implements ProviderInterface | |
{ | |
protected $paths; | |
protected $defaults; | |
/** | |
* @DI\InjectParams({ | |
* "cacheDir" = @DI\Inject("%kernel.cache_dir%") | |
* }) | |
*/ | |
public function __construct($cacheDir) | |
{ | |
$this->paths = include $cacheDir . '/cors.cache.php'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getOptions(Request $request) | |
{ | |
$uri = $request->getPathInfo() ?: '/'; | |
foreach ($this->paths as $pathRegexp => $options) { | |
if (preg_match($pathRegexp, $uri)) { | |
return $options; | |
} | |
} | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment