Completed
Push — master ( e78af6...b7290a )
by Mihail
03:21
created

App::init()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 27
rs 8.5806
cc 4
eloc 17
nc 2
nop 2
1
<?php
2
3
namespace Ffcms\Core;
4
5
use Ffcms\Core\Exception\ForbiddenException;
6
use Ffcms\Core\Exception\JsonException;
7
use Ffcms\Core\Exception\NativeException;
8
use Ffcms\Core\Exception\NotFoundException;
9
use Ffcms\Core\Exception\SyntaxException;
10
use Ffcms\Core\Managers\BootManager;
11
use Ffcms\Core\Managers\CronManager;
12
use Ffcms\Core\Managers\EventManager;
13
use Ffcms\Core\Helper\Security;
14
use Ffcms\Core\Helper\Type\Obj;
15
use Ffcms\Core\Helper\Type\Str;
16
use Ffcms\Core\I18n\Translate;
17
use Ffcms\Core\Network\Request;
18
use Ffcms\Core\Network\Response;
19
use Ffcms\Core\Arch\View;
20
use Ffcms\Core\Debug\Manager as Debug;
21
use Ffcms\Core\Cache\MemoryObject;
22
23
/**
24
 * Class App. Provide later static callbacks as entry point from any places of ffcms.
25
 * @package Ffcms\Core
26
 */
27
class App
28
{
29
    /** @var \Ffcms\Core\Network\Request */
30
    public static $Request;
31
32
    /** @var \Ffcms\Core\Properties */
33
    public static $Properties;
34
35
    /** @var \Ffcms\Core\Network\Response */
36
    public static $Response;
37
38
    /** @var \Ffcms\Core\Alias */
39
    public static $Alias;
40
41
    /** @var \Ffcms\Core\Arch\View */
42
    public static $View;
43
44
    /** @var \Ffcms\Core\Debug\Manager|null */
45
    public static $Debug;
46
47
    /** @var \Ffcms\Core\Helper\Security */
48
    public static $Security;
49
50
    /** @var \Ffcms\Core\I18n\Translate */
51
    public static $Translate;
52
53
    /** @var \Ffcms\Core\Interfaces\iUser */
54
    public static $User;
55
56
    /** @var \Symfony\Component\HttpFoundation\Session\Session */
57
    public static $Session;
58
59
    /** @var \Illuminate\Database\Capsule\Manager */
60
    public static $Database;
61
62
    /** @var \Ffcms\Core\Cache\MemoryObject */
63
    public static $Memory;
64
65
    /** @var \Swift_Mailer */
66
    public static $Mailer;
67
68
    /** @var \Ffcms\Core\Interfaces\iCaptcha */
69
    public static $Captcha;
70
71
    /** @var \phpFastCache\Drivers\files */
72
    public static $Cache;
73
74
    /** @var EventManager */
75
    public static $Event;
76
77
    /** @var CronManager */
78
    public static $Cron;
79
80
    /**
81
     * Prepare entry-point services
82
     * @param array|null $services
83
     * @param bool|object $loader
84
     * @throws NativeException
85
     * @throws \InvalidArgumentException
86
     */
87
    public static function init(array $services = null, $loader = false)
88
    {
89
        // initialize default services - used in all apps type
90
        self::$Memory = MemoryObject::instance();
91
        self::$Properties = new Properties();
92
        self::$Request = Request::createFromGlobals();
93
        self::$Security = new Security();
94
        self::$Response = new Response();
95
        self::$View = new View();
96
        self::$Translate = new Translate();
97
        self::$Alias = new Alias();
98
        self::$Event = new EventManager();
99
        self::$Cron = new CronManager();
100
101
        // check if debug is enabled and available for current session
102
        if (isset($services['Debug']) && $services['Debug'] === true && Debug::isEnabled() === true) {
103
            self::$Debug = new Debug();
104
        }
105
106
        $objects = App::$Properties->getAll('object');
107
        // pass dynamic initialization
108
        self::dynamicServicePrepare($services, $objects);
109
110
        // Initialize boot manager. This manager allow to auto-execute 'static boot()' methods in apps and widgets
111
        $bootManager = new BootManager($loader);
112
        $bootManager->run();
113
    }
114
115
    /**
116
     * Prepare dynamic services from object anonymous functions
117
     * @param array|null $services
118
     * @param null $objects
119
     * @throws NativeException
120
     */
121
    private static function dynamicServicePrepare(array $services = null, $objects = null)
122
    {
123
        // check if object configuration is passed
124
        if (!Obj::isArray($objects)) {
125
            throw new NativeException('Object configurations is not loaded: /Private/Config/Object.php');
126
        }
127
128
        // each all objects as service_name => service_instance()
129
        foreach ($objects as $name => $instance) {
0 ignored issues
show
Bug introduced by
The expression $objects of type null is not traversable.
Loading history...
130
            // check if definition of object is exist and services list contains it or is null to auto build
131
            if (property_exists(get_called_class(), $name) && $instance instanceof \Closure && (isset($services[$name]) || $services === null)) {
132
                if ($services[$name] === true || $services === null) { // initialize from configs
133
                    self::${$name} = $instance();
134
                } elseif (is_callable($services[$name])) { // raw initialization from App::run()
135
                    self::${$name} = $services[$name]();
136
                }
137
            } elseif (Str::startsWith('_', $name)) { // just anonymous callback without entry-point
138
                @call_user_func($instance);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
139
            }
140
        }
141
    }
142
143
    /**
144
     * Run applications and display output
145
     * @throws \DebugBar\DebugBarException
146
     */
147
    public static function run()
148
    {
149
        $html = null;
150
        // lets try to get html full content to page render
151
        try {
152
            /** @var \Ffcms\Core\Arch\Controller $callClass */
153
            $callClass = null;
154
            $callMethod = 'action' . self::$Request->getAction();
155
156
            // founded callback injection alias
157
            if (self::$Request->getCallbackAlias() !== false) {
158
                $cName = self::$Request->getCallbackAlias();
159 View Code Duplication
                if (class_exists($cName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
                    $callClass = new $cName;
161
                } else {
162
                    throw new NotFoundException('Callback alias of class "' . App::$Security->strip_tags($cName) . '" is not founded');
0 ignored issues
show
Documentation introduced by
$cName is of type boolean, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
                }
164
            } else { // typical parsing of native apps
165
                $cName = '\Apps\Controller\\' . env_name . '\\' . self::$Request->getController();
166
167
                // try to initialize class object
168 View Code Duplication
                if (class_exists($cName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
                    $callClass = new $cName;
170
                } else {
171
                    throw new NotFoundException('Application can not be runned. Initialized class not founded: ' . App::$Security->strip_tags($cName));
172
                }
173
            }
174
175
            // try to call method of founded callback class
176
            if (method_exists($callClass, $callMethod)) {
177
                $actionQuery = [];
178
                // prepare action params for callback
179
                if (!Str::likeEmpty(self::$Request->getID())) {
180
                    $actionQuery[] = self::$Request->getID();
181
                    if (!Str::likeEmpty(self::$Request->getAdd())) {
182
                        $actionQuery[] = self::$Request->getAdd();
183
                    }
184
                }
185
186
                // get controller method arguments count
187
                $reflection = new \ReflectionMethod($callClass, $callMethod);
188
                $argumentCount = 0;
189
                foreach ($reflection->getParameters() as $arg) {
190
                    if (!$arg->isOptional()) {
191
                        $argumentCount++;
192
                    }
193
                }
194
195
                // check method arguments count and current request count to prevent warnings
196
                if (count($actionQuery) < $argumentCount) {
197
                    throw new NotFoundException(__('Arguments for method %method% is not enough. Expected: %required%, got: %current%.', [
198
                        'method' => $callMethod,
199
                        'required' => $argumentCount,
200
                        'current' => count($actionQuery)
201
                    ]));
202
                }
203
204
                // make callback call to action in controller and get response
205
                $actionResponse = call_user_func_array([$callClass, $callMethod], $actionQuery);
206
207
                if ($actionResponse !== null && !Str::likeEmpty($actionResponse)) {
208
                    // set response to controller property object
209
                    $callClass->setResponse($actionResponse);
210
                }
211
212
                // get full compiled response
213
                $html = $callClass->getOutput();
214
            } else {
215
                throw new NotFoundException('Method "' . App::$Security->strip_tags($callMethod) . '()" not founded in "' . get_class($callClass) . '"');
216
            }
217
        } catch (NotFoundException $e) { // catch exceptions and set output
218
            $html = $e->display();
219
        } catch (ForbiddenException $e) {
220
            $html = $e->display();
221
        } catch (SyntaxException $e) {
222
            $html = $e->display();
223
        } catch (JsonException $e) {
224
            $html = $e->display();
225
        } catch (NativeException $e) {
226
            $html = $e->display();
227
        } catch (\Exception $e) { // catch all other exceptions
228
            $html = (new NativeException($e->getMessage()))->display();
229
        }
230
231
        // set full rendered content to response builder
232
        self::$Response->setContent($html);
233
        // echo full response to user via http foundation
234
        self::$Response->send();
235
    }
236
237
}