ActionList.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Framework\App\Router;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. use Magento\Framework\Serialize\Serializer\Serialize;
  10. use Magento\Framework\Module\Dir\Reader as ModuleReader;
  11. class ActionList
  12. {
  13. /**
  14. * Not allowed string in route's action path to avoid disclosing admin url
  15. */
  16. const NOT_ALLOWED_IN_NAMESPACE_PATH = 'adminhtml';
  17. /**
  18. * List of application actions
  19. *
  20. * @var array
  21. */
  22. protected $actions;
  23. /**
  24. * @var array
  25. */
  26. protected $reservedWords = [
  27. 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const',
  28. 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare',
  29. 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final',
  30. 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'instanceof',
  31. 'insteadof','interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected',
  32. 'public', 'require', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var',
  33. 'while', 'xor', 'void',
  34. ];
  35. /**
  36. * @var SerializerInterface
  37. */
  38. private $serializer;
  39. /**
  40. * @var string
  41. */
  42. private $actionInterface;
  43. /**
  44. * ActionList constructor
  45. *
  46. * @param \Magento\Framework\Config\CacheInterface $cache
  47. * @param ModuleReader $moduleReader
  48. * @param string $actionInterface
  49. * @param string $cacheKey
  50. * @param array $reservedWords
  51. * @param SerializerInterface|null $serializer
  52. */
  53. public function __construct(
  54. \Magento\Framework\Config\CacheInterface $cache,
  55. ModuleReader $moduleReader,
  56. $actionInterface = \Magento\Framework\App\ActionInterface::class,
  57. $cacheKey = 'app_action_list',
  58. $reservedWords = [],
  59. SerializerInterface $serializer = null
  60. ) {
  61. $this->reservedWords = array_merge($reservedWords, $this->reservedWords);
  62. $this->actionInterface = $actionInterface;
  63. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class);
  64. $data = $cache->load($cacheKey);
  65. if (!$data) {
  66. $this->actions = $moduleReader->getActionFiles();
  67. $cache->save($this->serializer->serialize($this->actions), $cacheKey);
  68. } else {
  69. $this->actions = $this->serializer->unserialize($data);
  70. }
  71. }
  72. /**
  73. * Retrieve action class
  74. *
  75. * @param string $module
  76. * @param string $area
  77. * @param string $namespace
  78. * @param string $action
  79. * @return null|string
  80. */
  81. public function get($module, $area, $namespace, $action)
  82. {
  83. if ($area) {
  84. $area = '\\' . $area;
  85. }
  86. if (strpos($namespace, self::NOT_ALLOWED_IN_NAMESPACE_PATH) !== false) {
  87. return null;
  88. }
  89. if (in_array(strtolower($action), $this->reservedWords)) {
  90. $action .= 'action';
  91. }
  92. $fullPath = str_replace(
  93. '_',
  94. '\\',
  95. strtolower(
  96. $module . '\\controller' . $area . '\\' . $namespace . '\\' . $action
  97. )
  98. );
  99. if (isset($this->actions[$fullPath])) {
  100. return is_subclass_of($this->actions[$fullPath], $this->actionInterface) ? $this->actions[$fullPath] : null;
  101. }
  102. return null;
  103. }
  104. }