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

michalsn / codeigniter-module-manager / 21144733500

19 Jan 2026 04:25PM UTC coverage: 69.289% (+0.4%) from 68.889%
21144733500

push

github

michalsn
update module validation

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

10 existing lines in 2 files now uncovered.

828 of 1195 relevant lines covered (69.29%)

11.26 hits per line

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

57.5
/src/Commands/ModulePublish.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Michalsn\CodeIgniterModuleManager\Commands;
6

7
use CodeIgniter\CLI\BaseCommand;
8
use CodeIgniter\CLI\CLI;
9
use CodeIgniter\Publisher\Publisher;
10
use Throwable;
11

12
class ModulePublish extends BaseCommand
13
{
14
    protected $group       = 'Modules';
15
    protected $name        = 'module:publish';
16
    protected $description = 'Publish Module Manager config file and modify Autoload to support modules.';
17

18
    public function run(array $params)
19
    {
20
        $source = service('autoloader')->getNamespace('Michalsn\\CodeIgniterModuleManager')[0];
2✔
21

22
        $publisher = new Publisher($source, APPPATH);
2✔
23

24
        try {
25
            $publisher->addPaths([
2✔
26
                'Config/ModuleManager.php',
2✔
27
            ])->merge(false);
2✔
28
        } catch (Throwable $e) {
×
29
            $this->showError($e);
×
30

31
            return EXIT_ERROR;
×
32
        }
33

34
        // Update published config file
35
        foreach ($publisher->getPublished() as $file) {
2✔
36
            $publisher->replace(
×
37
                $file,
×
38
                [
×
39
                    'namespace Michalsn\\CodeIgniterModuleManager\\Config' => 'namespace Config',
×
40
                    'use CodeIgniter\\Config\\BaseConfig'                  => 'use Michalsn\\CodeIgniterModuleManager\\Config\\ModuleManager as BaseModuleManager',
×
41
                    'class ModuleManager extends BaseConfig'               => 'class ModuleManager extends BaseModuleManager',
×
42
                ],
×
43
            );
×
44
        }
45

46
        CLI::write(CLI::color('  Config Published! ', 'green') . 'You can customize the configuration by editing the "app/Config/ModuleManager.php" file.');
2✔
47
        CLI::newLine();
2✔
48

49
        // Modify Autoload.php to add constructor
50
        return $this->modifyAutoload() ? EXIT_SUCCESS : EXIT_ERROR;
2✔
51
    }
52

53
    private function modifyAutoload(): bool
54
    {
55
        $autoloadFile = APPPATH . 'Config/Autoload.php';
2✔
56

57
        if (! file_exists($autoloadFile)) {
2✔
58
            CLI::error('Autoload.php file not found at: ' . $autoloadFile);
×
59

60
            return false;
×
61
        }
62

63
        $contents = file_get_contents($autoloadFile);
2✔
64

65
        // Check if constructor already exists
66
        if (str_contains($contents, 'public function __construct()')) {
2✔
67
            CLI::write(CLI::color('  Autoload Already Modified! ', 'yellow') . 'Constructor already exists in Autoload.php');
1✔
68

69
            return true;
1✔
70
        }
71

72
        // Find the class definition and add constructor after it
73
        $constructorCode = <<<'CODE'
1✔
74

75
                /**
76
                 * Loads enabled modules from modules_psr4.php
77
                 */
78
                public function __construct()
79
                {
80
                    parent::__construct();
81

82
                    $modulesPsr4 = WRITEPATH . 'modules_psr4.php';
83

84
                    if (file_exists($modulesPsr4)) {
85
                        $modules = include $modulesPsr4;
86

87
                        if (is_array($modules)) {
88
                            foreach ($modules as $namespace => $path) {
89
                                $this->psr4[$namespace] = $path;
90
                            }
91
                        }
92
                    }
93
                }
94
            CODE;
1✔
95

96
        // Insert constructor after the class properties (after the closing bracket of $helpers array)
97
        $pattern = '/(public\s+\$helpers\s*=\s*\[[^\]]*\];)/s';
1✔
98

99
        if (preg_match($pattern, $contents)) {
1✔
100
            $contents = preg_replace($pattern, "$1\n" . $constructorCode, $contents);
1✔
101
            file_put_contents($autoloadFile, $contents);
1✔
102

103
            CLI::write(CLI::color('  Autoload Modified! ', 'green') . 'Constructor added to load enabled modules automatically.');
1✔
104

105
            return true;
1✔
106
        }
107

UNCOV
108
        CLI::error('Could not find the right place to insert constructor in Autoload.php');
×
UNCOV
109
        CLI::write('Please manually add the constructor to Config\\Autoload.php class:', 'yellow');
×
UNCOV
110
        CLI::write($constructorCode);
×
111

UNCOV
112
        return false;
×
113
    }
114
}
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