Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.08% covered (success)
96.08%
49 / 51
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Application
96.08% covered (success)
96.08%
49 / 51
60.00% covered (warning)
60.00%
3 / 5
17
0.00% covered (danger)
0.00%
0 / 1
 load
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 prepare
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getConsole
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 cliError
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 initDb
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
8
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (http://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    http://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Kettle;
16
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@noladev.com>
26 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    3.0.0
29 */
30class Application extends \Pop\Application
31{
32
33    /**
34     * Application name
35     * @var string
36     */
37    const string NAME = 'pop-kettle';
38
39    /**
40     * Application full name
41     * @var string
42     */
43    const string FULL_NAME = 'Pop Kettle';
44
45    /**
46     * Application version
47     * @var string
48     */
49    const string VERSION = '3.0.0';
50
51    /**
52     * Application name
53     * @var ?string
54     */
55    protected ?string $name = self::NAME;
56
57    /**
58     * Application full name
59     * @var ?string
60     */
61    protected ?string $fullName = self::FULL_NAME;
62
63    /**
64     * Version
65     * @var ?string
66     */
67    protected ?string $version = self::VERSION;
68
69    /**
70     * Shared console object
71     * @var ?Console
72     */
73    protected ?Console $console = null;
74
75    /**
76     * Load application
77     *
78     * @return Application
79     */
80    public function load(): Application
81    {
82        $dir = getcwd();
83        if (file_exists($dir . '/app/config/database.php')) {
84            $this->initDb(include $dir . '/app/config/database.php');
85        }
86
87        $this->console = new Console(120);
88        $this->console->write(PHP_EOL . $this->console->header(self::FULL_NAME, '=', null, 'left', true, true));
89
90        $this->on('app.route.pre', function () {
91            Event\Console::maintenanceDisplay($this->console);
92            Event\Console::productionDisplay($this->console);
93        })->on('app.dispatch.post', function() {
94            $this->console->write();
95        });
96
97        return $this;
98    }
99
100    /**
101     * Prepare application
102     *   - determine if the route is a kettle native route or a custom application route
103     *
104     * @return \Pop\Application
105     */
106    public function prepare(): \Pop\Application
107    {
108        $app = $this;
109
110        if (($this->router !== null)) {
111            $this->router->getRouteMatch()->match();
112            $dispatchable = $this->router->getRouteMatch()->getDispatchable();
113
114            // If it's a custom command
115            if (!str_starts_with($dispatchable, 'Pop\Kettle')) {
116                $resolved = (new Model\Application())->resolveAppInstance(getcwd(), $this->autoloader(), $this->config['routes']);
117                if ($resolved !== null) {
118                    $app = $resolved;
119                }
120            }
121        }
122
123        return $app;
124    }
125
126    /**
127     * Get the shared console object
128     *
129     * @return ?Console
130     */
131    public function getConsole(): ?Console
132    {
133        return $this->console;
134    }
135
136    /**
137     * CLI error handler method
138     *
139     * @param  \Exception $exception
140     * @param  bool       $exit
141     * @return void
142     */
143    public function cliError(\Exception $exception, bool $exit = true): void
144    {
145        (new Console())->alertDanger(strip_tags($exception->getMessage()));
146        if ($exit) {
147            exit(127);
148        }
149    }
150
151    /**
152     * Initialize database service
153     *
154     * @param  array $database
155     * @throws \Pop\Db\Adapter\Exception
156     * @return void
157     */
158    public function initDb(array $database): void
159    {
160        foreach ($database as $db => $dbConfig) {
161            if (!empty($dbConfig['adapter']) && !empty($dbConfig['database'])) {
162                $adapter = $dbConfig['adapter'];
163                $options = [
164                    'database' => $dbConfig['database'],
165                    'username' => $dbConfig['username'] ?? null,
166                    'password' => $dbConfig['password'] ?? null,
167                    'host'     => $dbConfig['host'] ?? null,
168                    'type'     => $dbConfig['type'] ?? null
169                ];
170
171                $check = Db\Db::check($adapter, $options);
172
173                if ($check !== true) {
174                    throw new \Pop\Db\Adapter\Exception('Error: ' . $check);
175                }
176
177                $dbService = 'database';
178                if ($db != 'default') {
179                    $dbService .= '_' . $db;
180                }
181
182                $this->services()->set($dbService, [
183                    'call'   => 'Pop\Db\Db::connect',
184                    'params' => [
185                        'adapter' => $adapter,
186                        'options' => $options
187                    ]
188                ]);
189
190                if ($db == 'default') {
191                    if ($this->services()->isAvailable('database')) {
192                        Db\Record::setDb($this->services['database']);
193                    }
194                }
195            }
196        }
197    }
198
199}