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

sanmai / console / 15926214494

27 Jun 2025 12:24PM UTC coverage: 83.333% (-16.7%) from 100.0%
15926214494

Pull #6

github

web-flow
Merge 493f4cdda into 33fd65b55
Pull Request #6: Custom bootstrap script

24 of 34 new or added lines in 1 file covered. (70.59%)

50 of 60 relevant lines covered (83.33%)

6.33 hits per line

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

70.59
/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\Autoload\ClassLoader;
24
use Composer\InstalledVersions;
25
use RuntimeException;
26

27
use function class_exists;
28
use function file_exists;
29
use function file_get_contents;
30
use function getcwd;
31
use function is_readable;
32
use function is_array;
33
use function json_decode;
34

35
final class ConfigLoader
36
{
37
    public function __construct(
38
        private readonly string $workingDirectory = ''
39
    ) {}
24✔
40

41
    /**
42
     * Loads configuration from the root package composer.json
43
     *
44
     * @return array<string, mixed>
45
     */
46
    public function loadConfig(): array
47
    {
48
        if (!class_exists(InstalledVersions::class)) {
4✔
NEW
49
            return [];
×
50
        }
51

52
        // Get the install path for the root package
53
        $rootPackage = InstalledVersions::getRootPackage();
4✔
54
        $installPath = $rootPackage['install_path'];
4✔
55

56
        $composerJsonPath = $installPath . '/composer.json';
4✔
57

58
        if (!file_exists($composerJsonPath) || !is_readable($composerJsonPath)) {
4✔
NEW
59
            return [];
×
60
        }
61

62
        $content = file_get_contents($composerJsonPath);
4✔
63
        if (false === $content) {
4✔
NEW
64
            return [];
×
65
        }
66

67
        $composer = json_decode($content, true);
4✔
68
        if (!is_array($composer)) {
4✔
NEW
69
            return [];
×
70
        }
71

72
        if (!isset($composer['extra']) || !is_array($composer['extra'])) {
4✔
NEW
73
            return [];
×
74
        }
75

76
        if (!isset($composer['extra']['console']) || !is_array($composer['extra']['console'])) {
4✔
77
            return [];
4✔
78
        }
79

80
        /** @var array<string, mixed> */
NEW
81
        return $composer['extra']['console'];
×
82
    }
83

84
    /**
85
     * Loads a custom bootstrap script
86
     *
87
     * Similar to PHPUnit, bootstrap scripts are included for their side effects.
88
     * They can optionally return a ClassLoader instance to use for command discovery.
89
     */
90
    public function loadCustomInit(
91
        ClassLoader $initialLoader,
92
        string $initScript
93
    ): ?ClassLoader {
94
        $initPath = $this->getWorkingDirectory() . '/' . $initScript;
20✔
95

96
        if (!file_exists($initPath)) {
20✔
97
            throw new RuntimeException("Bootstrap script not found: $initScript");
4✔
98
        }
99

100
        if (!is_readable($initPath)) {
16✔
101
            throw new RuntimeException("Bootstrap script not readable: $initScript");
4✔
102
        }
103

104
        // Load the custom bootstrap script (like PHPUnit does)
105
        $result = require $initPath;
12✔
106

107
        // If bootstrap returns a ClassLoader, use it
108
        // Otherwise, return null to indicate the initial loader should be used
109
        if ($result instanceof ClassLoader) {
8✔
110
            return $result;
4✔
111
        }
112

113
        return null;
4✔
114
    }
115

116
    private function getWorkingDirectory(): string
117
    {
118
        if ($this->workingDirectory) {
20✔
119
            return $this->workingDirectory;
20✔
120
        }
121

NEW
122
        $cwd = getcwd();
×
NEW
123
        if (false === $cwd) {
×
NEW
124
            throw new RuntimeException('Unable to determine current working directory');
×
125
        }
126

NEW
127
        return $cwd;
×
128
    }
129
}
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