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

ICanBoogie / Module / 4270677071

pending completion
4270677071

push

github

Olivier Laviale
Tidy

2 of 2 new or added lines in 2 files covered. (100.0%)

73 of 415 relevant lines covered (17.59%)

0.68 hits per line

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

0.0
/lib/Module/Autoconfig/ModuleExtension.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\Module\Autoconfig;
13

14
use DirectoryIterator;
15
use ICanBoogie\Autoconfig\Autoconfig;
16
use ICanBoogie\Autoconfig\AutoconfigGenerator;
17
use ICanBoogie\Autoconfig\ExtensionAbstract;
18

19
use function array_merge;
20
use function getcwd;
21
use function is_dir;
22
use function is_string;
23

24
/**
25
 * Autoconfig extension to handle modules, their config and locale messages.
26
 */
27
final class ModuleExtension extends ExtensionAbstract
28
{
29
    /**
30
     * Identifier for module packages.
31
     */
32
    public const PACKAGE_TYPE_MODULE = "icanboogie-module";
33

34
    /**
35
     * composer.json extra property for modules path. (root-only)
36
     */
37
    public const OPTION_MODULES_PATH = 'modules-path';
38

39
    public function __construct(
40
        private readonly AutoconfigGenerator $generator
41
    ) {
42
        parent::__construct($generator);
×
43
    }
44

45
    /**
46
     * @var string[]
47
     */
48
    private array $modules_directories;
49

50
    /**
51
     * @inheritdoc
52
     */
53
    public function alter_schema(callable $set_property): void
54
    {
55
        $set_property(self::OPTION_MODULES_PATH, [
×
56

57
            'type' => "string",
×
58
            'description' => "(root-only) Path to application's custom modules.",
×
59
            'required' => false,
×
60

61
        ]);
×
62
    }
63

64
    /**
65
     * @inheritdoc
66
     */
67
    public function synthesize(array &$autoconfig): void
68
    {
69
        $modules_directories = $this->modules_directories = $this->collect_modules_directories();
×
70

71
        foreach ($modules_directories as $pathname) {
×
72
            if (is_dir("$pathname/config")) {
×
73
                /** @phpstan-ignore-next-line */
74
                $autoconfig[Autoconfig::ARG_CONFIG_PATHS][] = [
×
75

76
                    $this->find_shortest_path_code("$pathname/config"),
×
77
                    Autoconfig::CONFIG_WEIGHT_MODULE
×
78

79
                ];
×
80
            }
81

82
            if (is_dir("$pathname/locale")) {
×
83
                /** @phpstan-ignore-next-line */
84
                $autoconfig[Autoconfig::ARG_LOCALE_PATHS][] = $this
×
85
                    ->find_shortest_path_code("$pathname/locale");
×
86
            }
87
        }
88
    }
89

90
    public function render(): string
91
    {
92
        return $this->render_array_entry(
×
93
            ModuleAutoconfig::MODULES,
×
94
            $this->modules_directories,
×
95
            function ($directory) {
×
96
                return $this->find_shortest_path_code($directory);
×
97
            }
×
98
        );
×
99
    }
100

101
    /**
102
     * @return string[]
103
     */
104
    private function collect_modules_directories(): array
105
    {
106
        return array_merge(
×
107
            $this->collect_modules_directories_from_packages(),
×
108
            $this->collect_modules_directories_from_root_package()
×
109
        );
×
110
    }
111

112
    /**
113
     * @return string[]
114
     */
115
    private function collect_modules_directories_from_packages(): array
116
    {
117
        $directories = [];
×
118

119
        foreach ($this->generator->packages as $pathname => $package) {
×
120
            if ($package->getType() != self::PACKAGE_TYPE_MODULE) {
×
121
                continue;
×
122
            }
123

124
            $directories[] = $pathname;
×
125
        }
126

127
        return $directories;
×
128
    }
129

130
    /**
131
     * @return string[]
132
     */
133
    private function collect_modules_directories_from_root_package(): array
134
    {
135
        $package = $this->generator->root_package;
×
136
        /** @phpstan-ignore-next-line */
137
        $path = $package->getExtra()['icanboogie'][self::OPTION_MODULES_PATH] ?? null;
×
138

139
        if (!$path) {
×
140
            return [];
×
141
        }
142

143
        assert(is_string($path));
×
144

145
        $directories = [];
×
146
        $iterator = new DirectoryIterator(getcwd() . DIRECTORY_SEPARATOR . $path);
×
147

148
        foreach ($iterator as $file) {
×
149
            if ($file->isDot() || $file->isFile()) {
×
150
                continue;
×
151
            }
152

153
            $directories[] = $file->getPathname();
×
154
        }
155

156
        return $directories;
×
157
    }
158
}
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

© 2025 Coveralls, Inc