• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tochka-developers / jsonrpc / 4135501466

pending completion
4135501466

push

github

darkdarin
Merge remote-tracking branch 'origin/v5.0'

209 of 813 new or added lines in 51 files covered. (25.71%)

233 of 1307 relevant lines covered (17.83%)

1.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Route/ControllerFinder.php
1
<?php
2

3
namespace Tochka\JsonRpc\Route;
4

5
use Illuminate\Support\Str;
6

7
class ControllerFinder
8
{
9
    /** @var array<string, string> */
10
    private ?array $definedNamespaces = null;
11
    private string $appBasePath;
12

13
    /**
14
     * @psalm-suppress PossiblyUnusedMethod
15
     */
16
    public function __construct(string $appBasePath)
17
    {
NEW
18
        $this->appBasePath = $appBasePath;
×
19
    }
20

21
    /**
22
     * @return array<class-string>
23
     * @throws \JsonException
24
     */
25
    public function find(string $namespace, string $suffix = ''): array
26
    {
27
        $namespace = trim($namespace, '\\');
×
28
        $namespaceDirectory = $this->getNamespaceDirectory($namespace);
×
29
        if ($namespaceDirectory === null) {
×
30
            return [];
×
31
        }
32

NEW
33
        $files = scandir($namespaceDirectory, SCANDIR_SORT_ASCENDING);
×
34

35
        $controllers = [];
×
36
        $controllerList = [];
×
37

38
        foreach ($files as $file) {
×
39
            if ($file === '.' || $file === '..') {
×
40
                continue;
×
41
            }
42

43
            $directory = $this->getNamespaceDirectory($namespace . '\\' . $file);
×
44
            if ($directory === null) {
×
45
                continue;
×
46
            }
47

48
            if (is_dir($directory)) {
×
49
                $controllerList[] = $this->find($namespace . '\\' . $file, $suffix);
×
50
            } else {
51
                $className = $namespace . '\\' . str_replace('.php', '', $file);
×
52
                if (Str::endsWith($className, $suffix) && class_exists($className)) {
×
53
                    $controllers[] = $className;
×
54
                }
55
            }
56
        }
57

58
        return array_merge($controllers, ...$controllerList);
×
59
    }
60

61
    /**
62
     * @param string $namespace
63
     *
64
     * @return string|null
65
     * @throws \JsonException
66
     */
67
    protected function getNamespaceDirectory(string $namespace): ?string
68
    {
69
        $composerNamespaces = $this->getDefinedNamespaces();
×
70

71
        $namespaceFragments = explode('\\', $namespace);
×
72
        $undefinedNamespaceFragments = [];
×
73

74
        while ($namespaceFragments) {
×
75
            $possibleNamespace = implode('\\', $namespaceFragments) . '\\';
×
76
            if (array_key_exists($possibleNamespace, $composerNamespaces)) {
×
NEW
77
                $path = $this->appBasePath . DIRECTORY_SEPARATOR . $composerNamespaces[$possibleNamespace] . implode(
×
NEW
78
                    '/',
×
NEW
79
                    array_reverse($undefinedNamespaceFragments)
×
NEW
80
                );
×
81

82
                $realPath = realpath($path);
×
83

84
                return $realPath !== false ? $realPath : null;
×
85
            }
86

87
            $undefinedNamespaceFragments[] = array_pop($namespaceFragments);
×
88
        }
89

90
        return null;
×
91
    }
92

93
    /**
94
     * @return array<string, string>
95
     * @throws \JsonException
96
     */
97
    protected function getDefinedNamespaces(): array
98
    {
99
        if ($this->definedNamespaces === null) {
×
NEW
100
            $composerJsonPath = $this->appBasePath . DIRECTORY_SEPARATOR . 'composer.json';
×
101
            /** @var array{autoload: array{psr-4: array<string, string>}} $composerConfig */
NEW
102
            $composerConfig = json_decode(file_get_contents($composerJsonPath), true, 512, JSON_THROW_ON_ERROR);
×
NEW
103
            $this->definedNamespaces = $composerConfig['autoload']['psr-4'] ?? [];
×
104
        }
105

106
        return $this->definedNamespaces;
×
107
    }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc