Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

85 lines
2.1KB

  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. if(!$this->requestStack->getCurrentRequest()) {
  61. return null;
  62. }
  63. $requestAttributes = $this->requestStack->getCurrentRequest()->attributes->all();
  64. $route = isset($requestAttributes['_route']) ? $requestAttributes['_route'] : null;
  65. return $route;
  66. }
  67. }