Skip to content

Commit

Permalink
WIP [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Nov 27, 2023
1 parent 7ac6d3f commit 12d8d68
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 89 deletions.
50 changes: 50 additions & 0 deletions src/Testing/Actions/ComposerAutoloadAbsoluteAction.php
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;
}
}
16 changes: 10 additions & 6 deletions src/Testing/Actions/GetComposerJsonDataAction.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Actions;

Expand All @@ -13,21 +15,23 @@ public function __construct(
private readonly Filesystem $filesystem,
private readonly GetBasePathForStubsActionContract $getBasePathAction,
private readonly CacheMeServiceContract $cacheMeServiceContract,
)
{
) {
}


public function execute(): mixed
{
return $this->cacheMeServiceContract->get(
key: 'larasctrict.composer.json',
getValue: function (): mixed {
$basePath = $this->getBasePathAction->execute();
return json_decode($this->filesystem->get($basePath . '/composer.json'), true, 512, JSON_THROW_ON_ERROR);
return json_decode(
$this->filesystem->get($basePath . '/composer.json'),
true,
512,
JSON_THROW_ON_ERROR
);
},
strategy: CacheMeStrategy::Memory,
);
}

}
7 changes: 1 addition & 6 deletions src/Testing/Actions/GetNamespaceForStubsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace LaraStrict\Testing\Actions;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use LaraStrict\Testing\Constants\StubConstants;
use LaraStrict\Testing\Contracts\GetNamespaceForStubsActionContract;
use LaraStrict\Testing\Entities\NamespaceEntity;
Expand All @@ -16,14 +15,11 @@ class GetNamespaceForStubsAction implements GetNamespaceForStubsActionContract
final public const ComposerAutoLoadDev = 'autoload-dev';
final public const ComposerPsr4 = 'psr-4';


public function __construct(
private readonly GetComposerJsonDataAction $getComposerJsonDataAction,
)
{
) {
}


public function execute(Command $command, string $inputClass): NamespaceEntity
{
// Ask for which namespace which to use for "tests"
Expand All @@ -50,7 +46,6 @@ public function execute(Command $command, string $inputClass): NamespaceEntity
return new NamespaceEntity($folder, $baseNamespace);
}


private function getComposerDevAutoLoad(array $composer): array
{
if (isset($composer[self::ComposerAutoLoadDev])
Expand Down
38 changes: 38 additions & 0 deletions src/Testing/Actions/PathToClassAction.php
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));
}
}
Loading

0 comments on commit 12d8d68

Please sign in to comment.