AttributeRouteCollector.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Michel\Attribute;
  3. use InvalidArgumentException;
  4. use LogicException;
  5. use ReflectionAttribute;
  6. use ReflectionClass;
  7. use ReflectionException;
  8. final class AttributeRouteCollector
  9. {
  10. private array $classes;
  11. private ?string $cacheDir;
  12. public function __construct(array $classes, ?string $cacheDir = null)
  13. {
  14. if (PHP_VERSION_ID < 80000) {
  15. throw new LogicException('Attribute routes are only supported in PHP 8.0+');
  16. }
  17. $this->classes = array_unique($classes);
  18. $this->cacheDir = $cacheDir;
  19. if ($this->cacheDir && !is_dir($this->cacheDir)) {
  20. throw new InvalidArgumentException(sprintf(
  21. 'Cache directory "%s" does not exist',
  22. $this->cacheDir
  23. ));
  24. }
  25. }
  26. public function generateCache(): void
  27. {
  28. if (!$this->cacheIsEnabled()) {
  29. throw new LogicException('Cache is not enabled, if you want to enable it, please set the cacheDir on the constructor');
  30. }
  31. $this->collect();
  32. }
  33. public function clearCache(): void
  34. {
  35. if (!$this->cacheIsEnabled()) {
  36. throw new LogicException('Cache is not enabled, if you want to enable it, please set the cacheDir on the constructor');
  37. }
  38. foreach ($this->classes as $class) {
  39. $cacheFile = $this->getCacheFile($class);
  40. if (file_exists($cacheFile)) {
  41. unlink($cacheFile);
  42. }
  43. }
  44. }
  45. /**
  46. * @return array<\Michel\Route
  47. * @throws ReflectionException
  48. */
  49. public function collect(): array
  50. {
  51. $routes = [];
  52. foreach ($this->classes as $class) {
  53. $routes = array_merge($routes, $this->getRoutes($class));
  54. }
  55. return $routes;
  56. }
  57. private function getRoutes(string $class): array
  58. {
  59. if ($this->cacheIsEnabled() && ($cached = $this->get($class))) {
  60. return $cached;
  61. }
  62. $refClass = new ReflectionClass($class);
  63. $routes = [];
  64. $controllerAttr = $refClass->getAttributes(
  65. ControllerRoute::class,
  66. ReflectionAttribute::IS_INSTANCEOF
  67. )[0] ?? null;
  68. $controllerRoute = $controllerAttr ? $controllerAttr->newInstance() : new ControllerRoute('');
  69. foreach ($refClass->getMethods() as $method) {
  70. foreach ($method->getAttributes(
  71. Route::class,
  72. ReflectionAttribute::IS_INSTANCEOF
  73. ) as $attr) {
  74. /**
  75. * @var Route $instance
  76. */
  77. $instance = $attr->newInstance();
  78. $route = new \Michel\Route(
  79. $instance->getName(),
  80. $controllerRoute->getPath() . $instance->getPath(),
  81. [$class, $method->getName()],
  82. $instance->getMethods()
  83. );
  84. $route->format($instance->getFormat() ?: $controllerRoute->getFormat());
  85. foreach ($instance->getOptions() as $key => $value) {
  86. if (!str_starts_with($key, 'where') || $key === 'where') {
  87. throw new InvalidArgumentException(
  88. 'Invalid option "' . $key . '". Options must start with "where".'
  89. );
  90. }
  91. if (is_array($value)) {
  92. $route->$key(...$value);
  93. continue;
  94. }
  95. $route->$key($value);
  96. }
  97. $routes[$instance->getName()] = $route;
  98. }
  99. }
  100. $routes = array_values($routes);
  101. if ($this->cacheIsEnabled()) {
  102. $this->set($class, $routes);
  103. }
  104. return $routes;
  105. }
  106. private function cacheIsEnabled(): bool
  107. {
  108. return $this->cacheDir !== null;
  109. }
  110. private function get(string $class): ?array
  111. {
  112. $cacheFile = $this->getCacheFile($class);
  113. if (!is_file($cacheFile)) {
  114. return null;
  115. }
  116. return require $cacheFile;
  117. }
  118. private function set(string $class, array $routes): void
  119. {
  120. $cacheFile = $this->getCacheFile($class);
  121. $content = "<?php\n\nreturn " . var_export($routes, true) . ";\n";
  122. file_put_contents($cacheFile, $content);
  123. }
  124. private function getCacheFile(string $class): string
  125. {
  126. return rtrim($this->cacheDir, '/') . '/' . md5($class) . '.php';
  127. }
  128. }