Passed
Push — master ( 8fb72e...94d85d )
by Php Easy Api
03:19
created

Application::runningInConsole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Foundation;
4
5
use Resta\Support\Str;
6
use Resta\Config\Config;
7
use DI\NotFoundException;
8
use Resta\Support\Command;
9
use DI\DependencyException;
10
use Resta\Traits\ApplicationPath;
11
use Illuminate\Support\Collection;
12
use Resta\Contracts\ApplicationContracts;
13
use Resta\Environment\EnvironmentProvider;
14
use Resta\Contracts\ConfigProviderContracts;
15
use Resta\Contracts\ApplicationHelpersContracts;
16
use Resta\Foundation\Bootstrapper\Bootstrappers;
17
18
class Application extends Kernel implements ApplicationContracts,ApplicationHelpersContracts
19
{
20
    //get app paths
21
    use ApplicationPath;
22
23
    /**
24
     * The Resta api designer version.
25
     *
26
     * @var string
27
     */
28
    protected const VERSION = '1.0.0';
29
30
    /**
31
     * @var bool
32
     */
33
    protected $console;
34
35
    /**
36
     * Application constructor.
37
     *
38
     * @param bool $console
39
     */
40
    public function __construct($console=false)
41
    {
42
        // get console status for cli
43
        $this->console = $console;
44
45
        // the bootstrapper method is the initial process
46
        // that runs the individual methods that the application initiated.
47
        new Bootstrappers($this);
48
    }
49
50
    /**
51
     * check if the object in binding is available
52
     *
53
     * @param string $object
54
     * @return bool
55
     */
56
    public function checkBindings($object) : bool
57
    {
58
        // the booted objects are saved to the kernel.
59
        // this method checks whether these objects are registered.
60
        return (isset($this['bindings'],$this['bindings'][$object]));
61
    }
62
63
    /**
64
     * handle application command
65
     *
66
     * @param string $command
67
     * @param array $arguments
68
     * @return mixed
69
     *
70
     * @throws DependencyException
71
     * @throws NotFoundException
72
     */
73
    public function command($command, $arguments = array())
74
    {
75
        // the Process class executes a command in a sub-process,
76
        // taking care of the differences between operating system
77
        // and escaping arguments to prevent security issues.
78
        // It replaces PHP functions like exec, passthru, shell_exec and system
79
        return $this->resolve(Command::class,['command'=>$command,'args'=>$arguments])->handle();
80
    }
81
82
    /**
83
     * get kernel command list
84
     *
85
     * @return array
86
     */
87
    public function commandList() : array
88
    {
89
        //get command list from kernel
90
        return $this->commandList;
91
    }
92
93
    /**
94
     * get configuration values
95
     *
96
     * @param null|string $config
97
     * @return mixed|void
98
     *
99
     * @throws DependencyException
100
     * @throws NotFoundException
101
     */
102
    public function config($config=null)
103
    {
104
        if($this->checkBindings(__FUNCTION__)){
105
            return Config::make($config)->get();
0 ignored issues
show
Bug introduced by
It seems like $config can also be of type string; however, parameter $config of Resta\Config\Config::make() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
            return Config::make(/** @scrutinizer ignore-type */ $config)->get();
Loading history...
106
        }
107
108
        // if the environment is not booted,
109
        // it creates a direct missing ring
110
        // and we have to reinstall the environment to remove it.
111
        $this->loadIfNotExistBoot([__FUNCTION__]);
112
        return $this->config($config);
113
    }
114
115
    /**
116
     * console kernel object
117
     *
118
     * @return bool
119
     */
120
    public function console() : bool
121
    {
122
        // controlling the console object is
123
        // intended to make sure that the kernel bootstrap classes do not work.
124
        return $this->console;
125
    }
126
127
    /**
128
     * get core path
129
     *
130
     * @return mixed
131
     */
132
    public function corePath()
133
    {
134
        // get the directory
135
        // where kernel files are running to the kernel object.
136
        return core()->corePath ?: null;
137
    }
138
139
    /**
140
     * get environment variables
141
     *
142
     * @param array $environment
143
     * @return mixed|string
144
     */
145
    public function environment($environment=array())
146
    {
147
        if($this->checkBindings(__FUNCTION__)){
148
149
            $arguments = (isset(func_get_args()[0]))
150
                ? func_get_args()[0] : func_get_args();
151
152
            /** @var EnvironmentProvider $environmentContainer */
153
            $environmentContainer = $this['environment'];
154
155
            return $environmentContainer->environment(
156
                $arguments,$this['environmentVariables']
157
            );
158
        }
159
160
        // if the environment is not booted,
161
        // it creates a direct missing ring
162
        // and we have to reinstall the environment to remove it.
163
        $this->loadIfNotExistBoot([__FUNCTION__]);
164
        return $this->environment();
165
    }
166
167
    /**
168
     * handle application
169
     *
170
     * @return mixed
171
     */
172
    public function handle()
173
    {
174
        // this is the main calling place of your application.
175
        // if you come via http, the kernel response value is appraised.
176
        // if you come via console, the kernel console value is appraised.
177
        return ($this->console()) ? null : $this['result'];
178
    }
179
180
    /**
181
     * Determine if application locale is the given locale.
182
     *
183
     * @return bool
184
     */
185
    public function isLocale() : bool
186
    {
187
        //check environment for local
188
        return $this->environment() === 'local';
189
    }
190
191
    /**
192
     * get all kernel bootstrapper groups keys
193
     *
194
     * @return array
195
     */
196
    public function kernelGroupKeys() : array
197
    {
198
        $properties = [];
199
200
        // with the help of reflection instance,
201
        // we get the kernel properties extended with the application object.
202
        foreach ($this['reflection']($this)->getProperties() as $property){
203
            $properties[] = $property->getName();
204
        }
205
206
        // we get the names of
207
        // the kernel properties ended with groups through the Collection class.
208
        [$groups] = Collection::make($properties)->partition(function($properties){
209
            return Str::endsWith($properties,'Groups');
210
        });
211
212
        //as a result, kernel groups are being returned.
213
        return array_values($groups->all());
214
    }
215
216
    /**
217
     * kernel groups name lists
218
     *
219
     * @return array
220
     */
221
    public function kernelGroupList() : array
222
    {
223
        $list = [];
224
225
        //get kernel group names with manifest method
226
        foreach ($this->kernelGroupKeys() as $groupKey){
227
            $list = array_merge($list,$this->manifest($groupKey));
228
        }
229
230
        return $list;
231
    }
232
233
    /**
234
     * customer configuration loader for core
235
     *
236
     * @param callable $callback
237
     * @return mixed
238
     */
239
    public function loadConfig(callable $callback)
240
    {
241
        // it adds the values in path data specified
242
        // by callback to the configuration values.
243
        if($this['config'] instanceof ConfigProviderContracts){
244
245
            //set your path for config loader
246
            return tap($this['config'],function(ConfigProviderContracts $config) use($callback) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of tap($this['config'], function(...) { /* ... */ }) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
247
                return $config->setConfig(call_user_func($callback));
248
            });
249
        }
250
251
        //set config instance exception for application
252
        exception()->unexpectedValue('config instance is not loaded for application container');
253
    }
254
255
    /**
256
     * load if not exist boot
257
     *
258
     * @param array $loaders
259
     * @return mixed|void
260
     */
261
    public function loadIfNotExistBoot($loaders=array())
262
    {
263
        //get kernel group list from application
264
        $kernelGroupList = $this->kernelGroupList();
265
266
        foreach ($loaders as $loader){
267
268
            // if a service needs another boot service,
269
            // the service is directly installed here and the service needs are resolved.
270
            if(isset($kernelGroupList[$loader]) && $this->checkBindings($loader)===false){
271
272
                //with the boot loader kernel,we get the boot loader method.
273
                $this['closureBootLoader']->call(function() use($loader,$kernelGroupList) {
0 ignored issues
show
Bug introduced by
The method call() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

273
                $this['closureBootLoader']->/** @scrutinizer ignore-call */ call(function() use($loader,$kernelGroupList) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
274
                    $this->bootstrapper = $kernelGroupList[$loader];
0 ignored issues
show
Bug Best Practice introduced by
The property bootstrapper does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
275
                    return $this->boot();
0 ignored issues
show
Bug introduced by
The method boot() does not exist on Resta\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

275
                    return $this->/** @scrutinizer ignore-call */ boot();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
276
                });
277
            }
278
        }
279
    }
280
281
    /**
282
     * get kernel maker from manifest
283
     *
284
     * @param string $maker
285
     * @return mixed
286
     */
287
    public function manifest($maker)
288
    {
289
        //kernel manifest bootstrapper
290
        return $this['bootstrapper']->bootFire(null,$maker);
0 ignored issues
show
Bug introduced by
The method bootFire() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

290
        return $this['bootstrapper']->/** @scrutinizer ignore-call */ bootFire(null,$maker);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
291
    }
292
293
    /**
294
     * check if the request is console
295
     *
296
     * @return bool
297
     */
298
    public function runningInConsole() : bool
299
    {
300
        //Determine if the application is running in the console.
301
        return php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg';
302
    }
303
304
    /**
305
     * get service providers
306
     *
307
     * @return array
308
     */
309
    public function serviceProviders() : array
310
    {
311
        //get project providers from config kernel
312
        $providers = (is_array(config('kernel.providers')))
313
            ? config('kernel.providers')
314
            : [];
315
316
        //core kernel providers and project providers have been merged
317
        return array_merge($this->manifest('providers'),$providers);
318
    }
319
320
    /**
321
     * application structure directory paths changing
322
     *
323
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
324
     * @param null $path
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
325
     * @return mixed|void
326
     */
327
    public function setPaths($name=null,$path=null)
328
    {
329
        // save the globally identified paths to
330
        // the global container object of the resta.
331
        if(!is_null($name) && !is_null($path) && file_exists($path)){
0 ignored issues
show
introduced by
The condition is_null($name) is always true.
Loading history...
332
            $this->register('paths',$name,$path);
333
        }
334
    }
335
336
    /**
337
     * Get the version number of the application.
338
     *
339
     * @return mixed|string
340
     */
341
    public function version()
342
    {
343
        //get resta application version number
344
        return static::VERSION;
345
    }
346
}