-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use LaraStrict\Testing\Contracts\GetBasePathForStubsActionContract; | ||
|
||
final class ComposerAutoloadAbsoluteAction | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
private readonly array $dirs; | ||
|
||
public function __construct( | ||
GetComposerJsonDataAction $getComposerJsonDataAction, | ||
GetBasePathForStubsActionContract $getBasePathForStubsAction, | ||
) { | ||
$this->dirs = $this->makeDirs($getComposerJsonDataAction, $getBasePathForStubsAction); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function execute(): array | ||
{ | ||
return $this->dirs; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function makeDirs( | ||
GetComposerJsonDataAction $getComposerJsonDataAction, | ||
GetBasePathForStubsActionContract $getBasePathForStubsAction | ||
): array { | ||
$basePath = $getBasePathForStubsAction->execute(); | ||
$data = $getComposerJsonDataAction->execute(); | ||
$dirs = []; | ||
|
||
if (isset($data['autoload']['psr-4']) && is_array($data['autoload']['psr-4'])) { | ||
foreach ($data['autoload']['psr-4'] as $ns => $path) { | ||
$dirs[$ns] = $basePath . DIRECTORY_SEPARATOR . trim((string) $path, '\\/'); | ||
} | ||
} | ||
|
||
return $dirs; | ||
} | ||
} |
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
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use Exception; | ||
|
||
final class PathToClassAction | ||
{ | ||
public function __construct( | ||
private readonly ComposerAutoloadAbsoluteAction $composerAutoloadAbsoluteAction, | ||
) { | ||
} | ||
|
||
public function execute(string $path): string | ||
{ | ||
$dirs = $this->composerAutoloadAbsoluteAction->execute(); | ||
foreach ($dirs as $ns => $dir) { | ||
if (str_starts_with($path, $dir) === false) { | ||
continue; | ||
} | ||
|
||
$class = preg_replace_callback( | ||
sprintf('~^%s[/\\\](?<path>.*)\.php$~', preg_quote($dir, '~')), | ||
static fn (array $matches) => $ns . strtr($matches['path'], [ | ||
'/' => '\\', | ||
]), | ||
$path | ||
); | ||
assert(is_string($class)); | ||
|
||
return $class; | ||
} | ||
|
||
throw new Exception(sprintf('Path "%s" not found in composer psr-4.', $path)); | ||
} | ||
} |
Oops, something went wrong.