Skip to content

Commit

Permalink
Use getRealPath instead of getPathname to parse the classname
Browse files Browse the repository at this point in the history
See #92
  • Loading branch information
lorisleiva committed Mar 30, 2021
1 parent 556856e commit 1b9c561
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
27 changes: 3 additions & 24 deletions src/ActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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)
);
}
}
43 changes: 43 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Lorisleiva\Actions;

use Illuminate\Container\Container;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use SplFileInfo;

class Util
{
/**
* @param string|array $paths
* @return array
*/
public static function getAbsoluteDirectories($paths): array
{
return 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()
->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)
);
}
}
31 changes: 31 additions & 0 deletions tests/UtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Lorisleiva\Actions\Tests;

use Lorisleiva\Actions\Util;

it('parses relative paths into absolute directories that exists', function () {
// When we request the absolute directories from:
// - one relative directory path,
// - one directory that does not exists,
// - and one file.
$result = Util::getAbsoluteDirectories([
'app/Models', 'app/IDontExists', 'composer.json'
]);

// Then we only receive the absolute path of the first directory.
expect($result)->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');
});

0 comments on commit 1b9c561

Please sign in to comment.