Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Seed
94.44% covered (success)
94.44%
17 / 18
50.00% covered (danger)
50.00%
1 / 2
3.00
0.00% covered (danger)
0.00%
0 / 1
 run
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 runSeeder
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Dev Commands Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\CLI\Commands;
11
12use Framework\CLI\CLI;
13use Framework\CLI\Command;
14use Framework\MVC\App;
15use ReflectionMethod;
16
17/**
18 * Class Seed.
19 *
20 * @package dev-commands
21 */
22class Seed extends Command
23{
24    protected string $name = 'seed';
25    protected string $description = 'Seeds database.';
26    protected string $usage = 'seed [classname]';
27    protected string $databaseInstance = 'default';
28    protected array $options = [
29        '--instance' => 'Database instance name.',
30    ];
31
32    public function run() : void
33    {
34        // @phpstan-ignore-next-line
35        $this->databaseInstance = $this->getConsole()->getOption('instance') ?? 'default';
36        CLI::write(
37            CLI::style('Database Instance:', CLI::FG_YELLOW, formats: [CLI::FM_BOLD])
38            . ' ' . $this->databaseInstance
39        );
40        CLI::newLine();
41        $start = \microtime(true);
42        $this->runSeeder();
43        $end = \microtime(true);
44        CLI::newLine();
45        CLI::write('Total time of ' . \round($end - $start, 6) . ' seconds.');
46    }
47
48    protected function runSeeder() : void
49    {
50        $class = $this->getConsole()->getArgument(0);
51        if (empty($class)) {
52            CLI::error('First argument must be a class name.');
53        }
54        $class = new $class(App::database($this->databaseInstance));
55        $method = new ReflectionMethod($class, 'runSeed');
56        $method->setAccessible(true);
57        $method->invoke($class, $class);
58    }
59}