Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Extend/Core/Arch/FrontAppController.php (1 issue)

Labels
1
<?php
2
3
namespace Extend\Core\Arch;
4
5
use Apps\ActiveRecord\App as AppRecord;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Controller;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Extend\Core\Arch\Controller. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Ffcms\Core\Exception\ForbiddenException;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Obj;
11
use Ffcms\Core\Helper\Type\Str;
12
13
/**
14
 * Class FrontAppController. Extended controller for front applications.
15
 * This controller allow to control off/on status of controller application and support fast configuration usage.
16
 * @package Extend\Core\Arch
17
 */
18
class FrontAppController extends Controller
19
{
20
    // information about application from table apps
21
    public $application;
22
    private $configs;
23
24
    /**
25
     * FrontAppController constructor. Check if app is enabled in database
26
     * @throws ForbiddenException
27
     */
28
    public function __construct()
29
    {
30
        if (!$this->isEnabled()) {
31
            throw new ForbiddenException(__('This application is disabled or not installed!'));
32
        }
33
34
        // add localizations
35
        App::$Translate->append(App::$Alias->currentViewPath . '/I18n/' . App::$Request->getLanguage() . '.php');
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Check is current instance of application is enabled and can be executed
41
     * @return bool
42
     */
43
    public function isEnabled(): bool
44
    {
45
        $appName = App::$Request->getController();
46
        // if app class extend current class we can get origin name
47
        $nativeName = Str::lastIn(get_class($this), '\\', true);
48
        // check if this controller is enabled
49
        $this->application = AppRecord::getItem('app', [$appName, $nativeName]);
50
51
        // not exist? false
52
        if (!$this->application) {
53
            return false;
54
        }
55
56
        // check if disabled (0 = enabled, anything else = on)
57
        return !(bool)$this->application->disabled;
58
    }
59
60
    /**
61
     * Get current application configs as array
62
     * @return array|null
63
     */
64
    public function getConfigs(): ?array
65
    {
66
        if ($this->configs !== null) {
67
            return $this->configs;
68
        }
69
70
        $configs = (array)$this->application->configs;
71
        foreach ($configs as $cfg => $value) {
72
            if (Any::isInt($value)) {
73
                $configs[$cfg] = $value;
74
            }
75
        }
76
        $this->configs = $configs;
77
78
        return $this->configs;
79
    }
80
}
81