You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UrlResolver.php 2.0KB

3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @author La clic ! <contact@laclic.fr>
  4. */
  5. namespace Lc\SovBundle\Resolver;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class UrlResolver
  9. {
  10. protected ParameterBagInterface $parameterBag;
  11. protected RequestStack $requestStack;
  12. public function __construct(ParameterBagInterface $parameterBag, RequestStack $requestStack)
  13. {
  14. $this->parameterBag = $parameterBag;
  15. $this->requestStack = $requestStack;
  16. }
  17. public function isServerLocalhost(): bool
  18. {
  19. return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']);
  20. }
  21. public function isBot(): bool
  22. {
  23. if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match(
  24. '/bot|crawl|slurp|spider/i',
  25. $_SERVER['HTTP_USER_AGENT']
  26. )) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. public function isRouteAdmin(): bool
  33. {
  34. return $this->testRoute(['admin_', 'file_manager']);
  35. }
  36. public function isRouteLogin(): bool
  37. {
  38. return $this->testRoute(['sov_login', 'sov_logout', 'frontend_security_login']) ;
  39. }
  40. public function testRoute($mixed, $route = null): bool
  41. {
  42. if(is_null($route)) {
  43. $route = $this->getRouteCurrentRequest();
  44. }
  45. if(!$route) {
  46. return false;
  47. }
  48. if(is_array($mixed)) {
  49. foreach($mixed as $testRoute) {
  50. if($this->testRoute($testRoute, $route)) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. return strpos($route, $mixed) !== false;
  57. }
  58. public function getRouteCurrentRequest(): ?string
  59. {
  60. $requestAttributes = $this->requestStack->getCurrentRequest()->attributes->all();
  61. $route = isset($requestAttributes['_route']) ? $requestAttributes['_route'] : null;
  62. return $route;
  63. }
  64. }