Skip to content

Commit 0dd3719

Browse files
fix cachefilename resolver
1 parent 4fdc784 commit 0dd3719

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/Attribute/AttributeRouteCollector.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace PhpDevCommunity\Attribute;
44

5-
use PhpDevCommunity\Helper;
5+
use InvalidArgumentException;
6+
use LogicException;
7+
use ReflectionAttribute;
8+
use ReflectionClass;
9+
use ReflectionException;
610

711
final class AttributeRouteCollector
812
{
@@ -12,12 +16,12 @@ final class AttributeRouteCollector
1216
public function __construct(array $classes, ?string $cacheDir = null)
1317
{
1418
if (PHP_VERSION_ID < 80000) {
15-
throw new \LogicException('Attribute routes are only supported in PHP 8.0+');
19+
throw new LogicException('Attribute routes are only supported in PHP 8.0+');
1620
}
1721
$this->classes = array_unique($classes);
1822
$this->cacheDir = $cacheDir;
1923
if ($this->cacheDir && !is_dir($this->cacheDir)) {
20-
throw new \InvalidArgumentException(sprintf(
24+
throw new InvalidArgumentException(sprintf(
2125
'Cache directory "%s" does not exist',
2226
$this->cacheDir
2327
));
@@ -27,15 +31,15 @@ public function __construct(array $classes, ?string $cacheDir = null)
2731
public function generateCache(): void
2832
{
2933
if (!$this->cacheIsEnabled()) {
30-
throw new \LogicException('Cache is not enabled, if you want to enable it, please set the cacheDir on the constructor');
34+
throw new LogicException('Cache is not enabled, if you want to enable it, please set the cacheDir on the constructor');
3135
}
3236
$this->collect();
3337
}
3438

3539
public function clearCache(): void
3640
{
3741
if (!$this->cacheIsEnabled()) {
38-
throw new \LogicException('Cache is not enabled, if you want to enable it, please set the cacheDir on the constructor');
42+
throw new LogicException('Cache is not enabled, if you want to enable it, please set the cacheDir on the constructor');
3943
}
4044

4145
foreach ($this->classes as $class) {
@@ -48,7 +52,7 @@ public function clearCache(): void
4852

4953
/**
5054
* @return array<\PhpDevCommunity\Route
51-
* @throws \ReflectionException
55+
* @throws ReflectionException
5256
*/
5357
public function collect(): array
5458
{
@@ -62,37 +66,37 @@ public function collect(): array
6266

6367
private function getRoutes(string $class): array
6468
{
65-
if ($this->cacheIsEnabled() && ( $cached = $this->get($class))) {
69+
if ($this->cacheIsEnabled() && ($cached = $this->get($class))) {
6670
return $cached;
6771
}
68-
$refClass = new \ReflectionClass($class);
72+
$refClass = new ReflectionClass($class);
6973
$routes = [];
7074

7175
$controllerAttr = $refClass->getAttributes(
7276
ControllerRoute::class,
73-
\ReflectionAttribute::IS_INSTANCEOF
77+
ReflectionAttribute::IS_INSTANCEOF
7478
)[0] ?? null;
7579
$controllerRoute = $controllerAttr ? $controllerAttr->newInstance() : new ControllerRoute('');
7680
foreach ($refClass->getMethods() as $method) {
7781
foreach ($method->getAttributes(
7882
Route::class,
79-
\ReflectionAttribute::IS_INSTANCEOF
83+
ReflectionAttribute::IS_INSTANCEOF
8084
) as $attr) {
8185
/**
8286
* @var Route $instance
8387
*/
8488
$instance = $attr->newInstance();
8589
$route = new \PhpDevCommunity\Route(
8690
$instance->getName(),
87-
$controllerRoute->getPath().$instance->getPath(),
91+
$controllerRoute->getPath() . $instance->getPath(),
8892
[$class, $method->getName()],
8993
$instance->getMethods()
9094
);
9195

9296
$route->format($instance->getFormat() ?: $controllerRoute->getFormat());
9397
foreach ($instance->getOptions() as $key => $value) {
9498
if (!str_starts_with($key, 'where') || $key === 'where') {
95-
throw new \InvalidArgumentException(
99+
throw new InvalidArgumentException(
96100
'Invalid option "' . $key . '". Options must start with "where".'
97101
);
98102
}
@@ -138,6 +142,6 @@ private function set(string $class, array $routes): void
138142

139143
private function getCacheFile(string $class): string
140144
{
141-
return $this->cacheDir . '/' .md5($class) . '.php';
145+
return rtrim($this->cacheDir, '/') . '/' . md5($class) . '.php';
142146
}
143147
}

0 commit comments

Comments
 (0)