diff --git a/src/ActionManager.php b/src/ActionManager.php index 5efbf78..3bf02c5 100755 --- a/src/ActionManager.php +++ b/src/ActionManager.php @@ -117,24 +117,12 @@ public function identifyFromBacktrace($usedTraits, $limit = 10, BacktraceFrame & public function registerRoutes($paths = 'app/Actions'): void { - $paths = Collection::wrap($paths) - ->map(function (string $path) { - return Str::startsWith($path, DIRECTORY_SEPARATOR) ? $path : base_path($path); - }) - ->unique() - ->filter(function (string $path) { - return is_dir($path); - }) - ->values(); - - if ($paths->isEmpty()) { + if (empty($paths = Util::getAbsoluteDirectories($paths))) { return; } - foreach ((new Finder)->in($paths->toArray())->files() as $file) { - $this->registerRoutesForAction( - $this->getClassnameFromPathname($file->getPathname()) - ); + foreach ((new Finder)->in($paths)->files() as $file) { + $this->registerRoutesForAction(Util::getClassnameFromFile($file)); } } @@ -154,13 +142,4 @@ public function registerRoutesForAction(string $className): void $className::routes($this->app->make(Router::class)); } - - protected function getClassnameFromPathname(string $pathname): string - { - return $this->app->getNamespace() . str_replace( - ['/', '.php'], - ['\\', ''], - Str::after($pathname, realpath(app_path()).DIRECTORY_SEPARATOR) - ); - } } diff --git a/src/Util.php b/src/Util.php new file mode 100644 index 0000000..c5646b5 --- /dev/null +++ b/src/Util.php @@ -0,0 +1,43 @@ +map(function (string $path) { + return Str::startsWith($path, DIRECTORY_SEPARATOR) ? $path : base_path($path); + }) + ->unique() + ->filter(function (string $path) { + return is_dir($path); + }) + ->values() + ->toArray(); + } + + public static function getClassnameFromFile(SplFileInfo $file): string + { + return static::getClassnameFromRealpath($file->getRealPath()); + } + + public static function getClassnameFromRealpath(string $realpath): string + { + return Container::getInstance()->getNamespace() . str_replace( + ['/', '.php'], + ['\\', ''], + Str::after($realpath, realpath(app_path()).DIRECTORY_SEPARATOR) + ); + } +} diff --git a/tests/UtilTest.php b/tests/UtilTest.php new file mode 100644 index 0000000..f03aec5 --- /dev/null +++ b/tests/UtilTest.php @@ -0,0 +1,31 @@ +toBe([ + base_path('app/Models'), + ]); +}); + +it('parses a realpath into a class name', function () { + // Given the realpath of a file in the app directory. + $realpath = realpath(app_path()) . DIRECTORY_SEPARATOR . 'MyClass.php'; + + // When we parse its classname. + $className = Util::getClassnameFromRealpath($realpath); + + // Then we get the expected namespaced result. + expect($className)->toBe('App\MyClass'); +});