-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use getRealPath instead of getPathname to parse the classname
See #92
- Loading branch information
1 parent
556856e
commit 1b9c561
Showing
3 changed files
with
77 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |