Router.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Robots\Controller;
  7. use Magento\Framework\App\ActionFactory;
  8. use Magento\Framework\App\ActionInterface;
  9. use Magento\Framework\App\RequestInterface;
  10. use Magento\Framework\App\Route\ConfigInterface;
  11. use Magento\Framework\App\Router\ActionList;
  12. use Magento\Framework\App\RouterInterface;
  13. /**
  14. * Matches application action in case when robots.txt file was requested
  15. */
  16. class Router implements RouterInterface
  17. {
  18. /**
  19. * @var ActionFactory
  20. */
  21. private $actionFactory;
  22. /**
  23. * @var ActionList
  24. */
  25. private $actionList;
  26. /**
  27. * @var ConfigInterface
  28. */
  29. private $routeConfig;
  30. /**
  31. * @param ActionFactory $actionFactory
  32. * @param ActionList $actionList
  33. * @param ConfigInterface $routeConfig
  34. */
  35. public function __construct(
  36. ActionFactory $actionFactory,
  37. ActionList $actionList,
  38. ConfigInterface $routeConfig
  39. ) {
  40. $this->actionFactory = $actionFactory;
  41. $this->actionList = $actionList;
  42. $this->routeConfig = $routeConfig;
  43. }
  44. /**
  45. * Checks if robots.txt file was requested and returns instance of matched application action class
  46. *
  47. * @param RequestInterface $request
  48. * @return ActionInterface|null
  49. */
  50. public function match(RequestInterface $request)
  51. {
  52. $identifier = trim($request->getPathInfo(), '/');
  53. if ($identifier !== 'robots.txt') {
  54. return null;
  55. }
  56. $modules = $this->routeConfig->getModulesByFrontName('robots');
  57. if (empty($modules)) {
  58. return null;
  59. }
  60. $actionClassName = $this->actionList->get($modules[0], null, 'index', 'index');
  61. $actionInstance = $this->actionFactory->create($actionClassName);
  62. return $actionInstance;
  63. }
  64. }