Skip to content

Commit 73bfe1b

Browse files
committed
code improved
1 parent 6015cb3 commit 73bfe1b

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/Loader/DescriptorDirectoryLoader.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,21 +247,12 @@ private function findDescriptors(string $resource) : array
247247
continue;
248248
}
249249

250-
$reflection = new ReflectionClass($class);
251-
252-
if (8 === PHP_MAJOR_VERSION) {
253-
$attribute = $reflection->getAttributes(AttributeRouteDescriptor::class)[0] ?? null;
254-
if (isset($attribute)) {
255-
$descriptors[$class] = $attribute->newInstance();
256-
continue;
257-
}
258-
}
259-
260-
$annotation = $this->annotationReader->getClassAnnotation($reflection, AnnotationRouteDescriptor::class);
261-
if (isset($annotation)) {
262-
$descriptors[$class] = $annotation;
250+
$descriptor = $this->extractDescriptor($class);
251+
if (!isset($descriptor)) {
263252
continue;
264253
}
254+
255+
$descriptors[$class] = $descriptor;
265256
}
266257

267258
uasort($descriptors, function ($a, $b) {
@@ -271,6 +262,32 @@ private function findDescriptors(string $resource) : array
271262
return $descriptors;
272263
}
273264

265+
/**
266+
* Extracts a descriptor from the given class
267+
*
268+
* @param string $class
269+
*
270+
* @return null|RouteDescriptorInterface
271+
*/
272+
private function extractDescriptor(string $class) : ?RouteDescriptorInterface
273+
{
274+
$reflection = new ReflectionClass($class);
275+
276+
if (8 === PHP_MAJOR_VERSION) {
277+
$attribute = $reflection->getAttributes(AttributeRouteDescriptor::class)[0] ?? null;
278+
if (isset($attribute)) {
279+
return $attribute->newInstance();
280+
}
281+
}
282+
283+
$annotation = $this->annotationReader->getClassAnnotation($reflection, AnnotationRouteDescriptor::class);
284+
if (isset($annotation)) {
285+
return $annotation;
286+
}
287+
288+
return null;
289+
}
290+
274291
/**
275292
* Finds classes in the given resource
276293
*
@@ -300,7 +317,6 @@ private function findClasses(string $resource) : array
300317
private function findFiles(string $resource) : array
301318
{
302319
$flags = FilesystemIterator::CURRENT_AS_PATHNAME;
303-
304320
$directory = new RecursiveDirectoryIterator($resource, $flags);
305321
$iterator = new RecursiveIteratorIterator($directory);
306322
$files = new RegexIterator($iterator, '/\.php$/');

0 commit comments

Comments
 (0)