Passed
Push — develop ( 73b673...d6f831 )
by Nikolay
05:32
created

Application::main()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 44
rs 9.1768
c 1
b 0
f 0
cc 5
nc 12
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
use MikoPBX\AdminCabinet\Utilities\Debug\PhpError;
21
use MikoPBX\Common\Providers\MainDatabaseProvider;
22
use MikoPBX\Common\Providers\ManagedCacheProvider;
23
use MikoPBX\Common\Providers\ModelsAnnotationsProvider;
24
use MikoPBX\Common\Providers\ModelsCacheProvider;
25
use MikoPBX\Common\Providers\ModelsMetadataProvider;
26
use MikoPBX\Common\Providers\PBXConfModulesProvider;
27
use MikoPBX\Common\Providers\RouterProvider;
28
use MikoPBX\Core\System\SentryErrorLogger;
29
use MikoPBX\Modules\PbxExtensionUtils;
30
use Phalcon\Mvc\Application as BaseApplication;
31
use Whoops\Handler\JsonResponseHandler;
32
use Whoops\Handler\PrettyPageHandler;
33
use Whoops\Run;
34
35
class Application extends BaseApplication
36
{
37
    /**
38
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
39
     */
40
    protected function registerServices()
41
    {
42
43
        $di = new Phalcon\Di\FactoryDefault();
44
45
        /**
46
         * Auto-loader configuration
47
         */
48
        require_once __DIR__ . '/../../src/Common/Config/ClassLoader.php';
49
50
        // Register default services
51
        $providersList = [
52
            // Inject Database connections
53
            ModelsAnnotationsProvider::class,
54
            ModelsMetadataProvider::class,
55
            MainDatabaseProvider::class,
56
57
            // Inject caches
58
            ManagedCacheProvider::class,
59
            ModelsCacheProvider::class,
60
61
            // Inject PBX modules
62
            PBXConfModulesProvider::class,
63
64
            // Inject routers
65
            RouterProvider::class,
66
67
        ];
68
        foreach ($providersList as $provider) {
69
            $di->register(new $provider());
70
        }
71
72
        $this->setDI($di);
73
    }
74
75
    public function main()
76
    {
77
        $this->registerServices();
78
79
        // Attach Sentry error logger
80
        $errorLogger = new SentryErrorLogger('admin-cabinet');
81
        $errorLogger->init();
82
83
        // Enable Whoops error pretty print
84
        $is_ajax = 'xmlhttprequest' === strtolower($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '');
85
        if ($is_ajax) {
86
            $whoopsClass = JsonResponseHandler::class;
87
        } else {
88
            $whoopsClass = PrettyPageHandler::class;
89
        }
90
91
        if (class_exists($whoopsClass)) {
92
            $whoops = new Run();
93
            $whoops->pushHandler(new $whoopsClass());
94
            $whoops->register();
95
        }
96
97
        // Register the default modules
98
        $this->registerModules([
99
            'admin-cabinet' => [
100
                "className" => "MikoPBX\AdminCabinet\Module",
101
                "path"      => __DIR__ . '/../../src/AdminCabinet/Module.php',
102
            ],
103
        ]);
104
        $this->setDefaultModule('admin-cabinet');
105
106
        // Register additional app modules from external enabled modules
107
        PbxExtensionUtils::registerEnabledModulesInApp($this);
108
109
        try {
110
            echo $this->handle($_SERVER['REQUEST_URI'])->getContent();
111
        } catch (Throwable $e) {
112
            $errorLogger->captureException($e);
113
            PhpError::exceptionHandler($e);
114
115
            if (class_exists($whoopsClass)) {
116
                $whoops->handleException($e);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $whoops does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
            } else {
118
                echo $e->getMessage();
119
            }
120
        }
121
    }
122
}
123
124
$application = new Application();
125
$application->main();