seedPath = $config->filesPath ?? APPPATH.'Database/'; if (empty($this->seedPath)) { throw new \InvalidArgumentException('Invalid filesPath set in the Config\Database.'); } $this->seedPath = rtrim($this->seedPath, '/').'/Seeds/'; if (! is_dir($this->seedPath)) { throw new \InvalidArgumentException('Unable to locate the seeds directory. Please check Config\Database::filesPath'); } $this->config =& $config; if (is_null($db)) { $db = \Config\Database::connect($this->DBGroup); } $this->db =& $db; } //-------------------------------------------------------------------- /** * Loads the specified seeder and runs it. * * @param string $class * * @throws RuntimeException */ public function call(string $class) { if (empty($class)) { throw new \InvalidArgumentException('No Seeder was specified.'); } $path = str_replace('.php', '', $class).'.php'; // If we have namespaced class, simply try to load it. if (strpos($class, '\\') !== false) { $seeder = new $class($this->config); } // Otherwise, try to load the class manually. else { $path = $this->seedPath.$path; if (! is_file($path)) { throw new \InvalidArgumentException('The specified Seeder is not a valid file: '. $path); } if (! class_exists($class, false)) { require $path; } $seeder = new $class($this->config); } $seeder->run(); unset($seeder); if (is_cli() && ! $this->silent) { CLI::write("Seeded: {$class}", 'green'); } } //-------------------------------------------------------------------- /** * Sets the location of the directory that seed files can be located in. * * @param sting $path * * @return $this */ public function setPath(string $path) { $this->seedPath = rtrim($path, '/').'/'; return $this; } //-------------------------------------------------------------------- /** * Sets the silent treatment. * * @param bool $silent * * @return $this */ public function setSilent(bool $silent) { $this->silent = $silent; return $this; } //-------------------------------------------------------------------- /** * Run the database seeds. This is where the magic happens. * * Child classes must implement this method and take care * of inserting their data here. * * @return mixed */ public function run() { } //-------------------------------------------------------------------- }