Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Module
96.77% covered (success)
96.77%
30 / 31
66.67% covered (warning)
66.67%
2 / 3
10
0.00% covered (danger)
0.00%
0 / 1
 register
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 cliError
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 initDb
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Kettle;
15
16use Pop\Application;
17use Pop\Console\Console;
18use Pop\Db;
19
20/**
21 * Main module class
22 *
23 * @category   Pop\Kettle
24 * @package    Pop\Kettle
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    2.2.0
29 */
30class Module extends \Pop\Module\Module
31{
32
33    /**
34     * Application name
35     * @var ?string
36     */
37    protected ?string $name = 'pop-kettle';
38
39    /**
40     * Application version
41     * @var string
42     */
43    const VERSION = '2.2.0';
44
45    /**
46     * Register module
47     *
48     * @param Application $application
49     * @return static
50     */
51    public function register(Application $application): static
52    {
53        parent::register($application);
54
55        $dir = getcwd();
56        if (file_exists($dir . '/app/config/database.php')) {
57            $this->initDb(include $dir . '/app/config/database.php');
58        }
59
60        if ($this->application->router() !== null) {
61            $this->application->router()->addControllerParams(
62                '*', [
63                    'application' => $this->application,
64                    'console'     => new Console(120, '    ')
65                ]
66            );
67        }
68
69        $this->application->on('app.route.pre', 'Pop\Kettle\Event\Console::header')
70             ->on('app.dispatch.post', 'Pop\Kettle\Event\Console::footer');
71
72        return $this;
73    }
74
75    /**
76     * CLI error handler method
77     *
78     * @param  \Exception $exception
79     * @param  bool       $exit
80     * @return void
81     */
82    public function cliError(\Exception $exception, bool $exit = true): void
83    {
84        (new Console())->alertDanger(strip_tags($exception->getMessage()));
85        if ($exit) {
86            exit(127);
87        }
88    }
89
90    /**
91     * Initialize database service
92     *
93     * @param  array $database
94     * @throws \Pop\Db\Adapter\Exception
95     * @return void
96     */
97    public function initDb(array $database): void
98    {
99        if (isset($database['default']) &&
100            !empty($database['default']['adapter']) && !empty($database['default']['database'])) {
101            $adapter = $database['default']['adapter'];
102            $options = [
103                'database' => $database['default']['database'],
104                'username' => $database['default']['username'] ?? null,
105                'password' => $database['default']['password'] ?? null,
106                'host'     => $database['default']['host'] ?? null,
107                'type'     => $database['default']['type'] ?? null
108            ];
109
110            $check = Db\Db::check($adapter, $options);
111
112            if ($check !== true) {
113                throw new \Pop\Db\Adapter\Exception('Error: ' . $check);
114            }
115
116            Db\Record::setDb(Db\Db::connect($adapter, $options));
117        }
118    }
119
120}