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

sanmai / console / 15953611618

29 Jun 2025 09:07AM UTC coverage: 98.551% (-1.4%) from 100.0%
15953611618

Pull #10

github

web-flow
Merge b91fbae54 into 4a336afed
Pull Request #10: Add an option to specify custom command providers

11 of 12 new or added lines in 2 files covered. (91.67%)

68 of 69 relevant lines covered (98.55%)

8.12 hits per line

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

95.24
/src/ConfigLoader.php
1
<?php
2

3
/**
4
 * Copyright 2025 Alexey Kopytko <alexey@kopytko.com>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18

19
declare(strict_types=1);
20

21
namespace ConsoleApp;
22

23
use Composer\InstalledVersions;
24
use SplFileObject;
25
use Composer\Autoload\ClassLoader;
26

27
use function json_decode;
28
use function spl_autoload_functions;
29
use function count;
30

31
use const JSON_THROW_ON_ERROR;
32

33
final class ConfigLoader
34
{
35
    /** @var array{install_path: string, ...} */
36
    private readonly array $rootPackage;
37

38
    private ?object $config = null;
39

40
    /**
41
     * @param array{install_path: string}|null $rootPackage
42
     */
43
    public function __construct(
44
        readonly private ClassLoader $classLoader,
45
        ?array $rootPackage = null,
46
    ) {
47
        $this->rootPackage = $rootPackage ?? InstalledVersions::getRootPackage();
32✔
48
    }
49

50
    protected function readFile(string $path): string|false
51
    {
52
        $file = new SplFileObject($path, 'r');
16✔
53
        return $file->fread($file->getSize());
16✔
54
    }
55

56
    private function getConfig(): object
57
    {
58
        if (null !== $this->config) {
20✔
NEW
59
            return $this->config;
×
60
        }
61

62
        $path = $this->rootPackage['install_path'] . '/composer.json';
20✔
63

64
        /** @var object */
65
        $config = json_decode(
20✔
66
            json: (string) $this->readFile($path),
20✔
67
            associative: false,
20✔
68
            flags: JSON_THROW_ON_ERROR,
20✔
69
        );
20✔
70

71
        $this->config = $config;
16✔
72

73
        return $this->config;
16✔
74
    }
75

76
    /**
77
     * Returns the path to the bootstrap file.
78
     */
79
    public function getBootstrapPath(): string
80
    {
81
        // @phpstan-ignore-next-line
82
        return $this->getConfig()->extra->console->bootstrap ?? '';
12✔
83
    }
84

85
    /**
86
     * Returns the class name of the command provider.
87
     * @return class-string<CommandProviderInterface>|null
88
     */
89
    public function getProviderClass(): ?string
90
    {
91
        // @phpstan-ignore-next-line
92
        return $this->getConfig()->extra->console->provider ?? null;
8✔
93
    }
94

95
    public function handleAutoloader(callable $callback): void
96
    {
97
        $beforeCount = $this->getAutoloaderCount();
8✔
98

99
        $callback();
8✔
100

101
        if ($this->getAutoloaderCount() <= $beforeCount) {
8✔
102
            return;
4✔
103
        }
104

105
        // If the autoloader was registered, we need to unregister it
106
        $this->classLoader->unregister();
4✔
107
    }
108

109
    public function getAutoloaderCount(): int
110
    {
111
        return count(spl_autoload_functions());
4✔
112
    }
113
}
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