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

sanmai / console / 15932108718

27 Jun 2025 05:18PM UTC coverage: 97.619% (-2.4%) from 100.0%
15932108718

Pull #6

github

web-flow
Merge 0d462ea9d into 33fd65b55
Pull Request #6: Custom bootstrap script

15 of 16 new or added lines in 1 file covered. (93.75%)

41 of 42 relevant lines covered (97.62%)

7.52 hits per line

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

93.75
/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
class ConfigLoader
34
{
35
    /** @var array{install_path: string, ...} */
36
    private readonly array $rootPackage;
37

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

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

54
    public function getBootstrapPath(): string
55
    {
56
        $path = $this->rootPackage['install_path'] . '/composer.json';
12✔
57

58
        $json = json_decode(
12✔
59
            json: (string) $this->readFile($path),
12✔
60
            associative: false,
12✔
61
            flags: JSON_THROW_ON_ERROR,
12✔
62
        );
12✔
63

64
        // @phpstan-ignore-next-line
65
        return $json->extra->console->bootstrap ?? '';
8✔
66
    }
67

68
    public function handleAutoloader(callable $callback): void
69
    {
70
        $beforeCount = $this->getAutoloaderCount();
4✔
71

72
        $callback();
4✔
73

74
        if ($this->getAutoloaderCount() <= $beforeCount) {
4✔
75
            return;
4✔
76
        }
77

78
        // If the autoloader was registered, we need to unregister it
NEW
79
        $this->classLoader->unregister();
×
80
    }
81

82
    public function getAutoloaderCount(): int
83
    {
84
        return count(spl_autoload_functions());
8✔
85
    }
86
}
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