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

codeigniter4 / CodeIgniter4 / 14569795065

21 Apr 2025 07:55AM UTC coverage: 84.402% (+0.007%) from 84.395%
14569795065

Pull #9528

github

web-flow
Merge 4ad1f19d8 into 3d3ba0512
Pull Request #9528: feat: add Time::addCalendarMonths() function

11 of 11 new or added lines in 1 file covered. (100.0%)

136 existing lines in 21 files now uncovered.

20827 of 24676 relevant lines covered (84.4%)

191.03 hits per line

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

87.88
/system/Database/Seeder.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Database;
15

16
use CodeIgniter\CLI\CLI;
17
use CodeIgniter\Exceptions\InvalidArgumentException;
18
use Config\Database;
19
use Faker\Factory;
20
use Faker\Generator;
21

22
/**
23
 * Class Seeder
24
 */
25
class Seeder
26
{
27
    /**
28
     * The name of the database group to use.
29
     *
30
     * @var non-empty-string
31
     */
32
    protected $DBGroup;
33

34
    /**
35
     * Where we can find the Seed files.
36
     *
37
     * @var string
38
     */
39
    protected $seedPath;
40

41
    /**
42
     * An instance of the main Database configuration
43
     *
44
     * @var Database
45
     */
46
    protected $config;
47

48
    /**
49
     * Database Connection instance
50
     *
51
     * @var BaseConnection
52
     */
53
    protected $db;
54

55
    /**
56
     * Database Forge instance.
57
     *
58
     * @var Forge
59
     */
60
    protected $forge;
61

62
    /**
63
     * If true, will not display CLI messages.
64
     *
65
     * @var bool
66
     */
67
    protected $silent = false;
68

69
    /**
70
     * Faker Generator instance.
71
     *
72
     * @deprecated
73
     */
74
    private static ?Generator $faker = null;
75

76
    /**
77
     * Seeder constructor.
78
     */
79
    public function __construct(Database $config, ?BaseConnection $db = null)
80
    {
81
        $this->seedPath = $config->filesPath ?? APPPATH . 'Database/';
714✔
82

83
        if ($this->seedPath === '') {
714✔
84
            throw new InvalidArgumentException('Invalid filesPath set in the Config\Database.');
1✔
85
        }
86

87
        $this->seedPath = rtrim($this->seedPath, '\\/') . '/Seeds/';
713✔
88

89
        if (! is_dir($this->seedPath)) {
713✔
90
            throw new InvalidArgumentException('Unable to locate the seeds directory. Please check Config\Database::filesPath');
1✔
91
        }
92

93
        $this->config = &$config;
712✔
94

95
        $db ??= Database::connect($this->DBGroup);
712✔
96

97
        $this->db    = $db;
712✔
98
        $this->forge = Database::forge($this->DBGroup);
712✔
99
    }
100

101
    /**
102
     * Gets the Faker Generator instance.
103
     *
104
     * @deprecated
105
     */
106
    public static function faker(): ?Generator
107
    {
108
        if (! self::$faker instanceof Generator && class_exists(Factory::class)) {
1✔
109
            self::$faker = Factory::create();
1✔
110
        }
111

112
        return self::$faker;
1✔
113
    }
114

115
    /**
116
     * Loads the specified seeder and runs it.
117
     *
118
     * @return void
119
     *
120
     * @throws InvalidArgumentException
121
     */
122
    public function call(string $class)
123
    {
124
        $class = trim($class);
568✔
125

126
        if ($class === '') {
568✔
127
            throw new InvalidArgumentException('No seeder was specified.');
1✔
128
        }
129

130
        if (! str_contains($class, '\\')) {
567✔
131
            $path = $this->seedPath . str_replace('.php', '', $class) . '.php';
1✔
132

133
            if (! is_file($path)) {
1✔
134
                throw new InvalidArgumentException('The specified seeder is not a valid file: ' . $path);
1✔
135
            }
136

137
            // Assume the class has the correct namespace
138
            // @codeCoverageIgnoreStart
139
            $class = APP_NAMESPACE . '\Database\Seeds\\' . $class;
×
140

UNCOV
141
            if (! class_exists($class, false)) {
×
UNCOV
142
                require_once $path;
×
143
            }
144
            // @codeCoverageIgnoreEnd
145
        }
146

147
        /** @var Seeder $seeder */
148
        $seeder = new $class($this->config);
567✔
149
        $seeder->setSilent($this->silent)->run();
567✔
150

151
        unset($seeder);
567✔
152

153
        if (is_cli() && ! $this->silent) {
567✔
154
            CLI::write("Seeded: {$class}", 'green');
2✔
155
        }
156
    }
157

158
    /**
159
     * Sets the location of the directory that seed files can be located in.
160
     *
161
     * @return $this
162
     */
163
    public function setPath(string $path)
164
    {
165
        $this->seedPath = rtrim($path, '\\/') . '/';
565✔
166

167
        return $this;
565✔
168
    }
169

170
    /**
171
     * Sets the silent treatment.
172
     *
173
     * @return $this
174
     */
175
    public function setSilent(bool $silent)
176
    {
177
        $this->silent = $silent;
711✔
178

179
        return $this;
711✔
180
    }
181

182
    /**
183
     * Run the database seeds. This is where the magic happens.
184
     *
185
     * Child classes must implement this method and take care
186
     * of inserting their data here.
187
     *
188
     * @return void
189
     *
190
     * @codeCoverageIgnore
191
     */
192
    public function run()
193
    {
UNCOV
194
    }
×
195
}
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