Passed
Push — main ( 633b92...66245a )
by Dimitri
12:27
created

Load::providers()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 20
rs 9.6111
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Loader;
13
14
use BlitzPHP\Database\Contracts\ConnectionInterface;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Database\Contracts\ConnectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use BlitzPHP\Exceptions\LoadException;
16
17
class Load
18
{
19
    /**
20
     * Liste des elements deja chargés,
21
     * Si un element est deja chargé, on le renvoie simplement sans avoir besoin de le construire à nouveau
22
     *
23
     * @var array
24
     */
25
    private static $loads = [
26
        'controllers' => [],
27
        'helpers'     => [],
28
        'langs'       => [],
29
        'libraries'   => [],
30
        'models'      => [],
31
    ];
32
33
    /**
34
     * Recupere toutes les definitions des services a injecter dans le container
35
     */
36
    public static function providers(): array
37
    {
38
        $providers = [];
39
40
        // services système
41
        $filename = SYST_PATH . 'Constants' . DS . 'providers.php';
42
        if (! file_exists($filename)) {
43
            throw LoadException::providersDefinitionDontExist($filename);
44
        }
45
        if (! in_array($filename, get_included_files(), true)) {
46
            $providers = array_merge($providers, require $filename);
47
        }
48
49
        // services de l'application
50
        $filename = CONFIG_PATH . 'providers.php';
51
        if (file_exists($filename) && ! in_array($filename, get_included_files(), true)) {
52
            $providers = array_merge($providers, require $filename);
53
        }
54
55
        return $providers;
56
    }
57
58
    /**
59
     * Charge un fichier d'aide
60
     *
61
     * @throws InvalidArgumentException
62
     * @throws LoadException
63
     */
64
    public static function helper(string|array $helpers)
65
    {
66
        if (empty($helpers)) {
67
            throw new LoadException('Veuillez specifier le helper à charger');
68
        }
69
70
        $helpers = (array) $helpers;
71
72
        foreach ($helpers as $helper) {
73
            FileLocator::helper($helper);
74
        }
75
    }
76
77
    /**
78
     * Charge un modele
79
     *
80
     * @throws LoadException
81
     *
82
     * @return object|object[]
83
     */
84
    public static function model(string|array $model, array $options = [], ?ConnectionInterface $connection = null)
85
    {
86
        if (empty($model)) {
87
            throw new LoadException('Veuillez specifier le modele à charger');
88
        }
89
90
        if (is_array($model)) {
0 ignored issues
show
introduced by
The condition is_array($model) is always true.
Loading history...
91
            $models = [];
92
93
            foreach ($model as $value) {
94
                $models[] = self::model($value, $options, $connection);
95
            }
96
97
            return $models;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $models returns an array which contains values of type array<mixed,object> which are incompatible with the documented value type object.
Loading history...
98
        }
99
100
        if (! self::isLoaded('models', $model)) {
101
            self::loaded('models', $model, FileLocator::model($model, $options, $connection));
102
        }
103
104
        return self::getLoaded('models', $model);
105
    }
106
107
    /**
108
     * Charge un fichier de gestion de langue
109
     */
110
    public static function lang(string $file, ?string $locale = null): array
111
    {
112
        $locale ??= config('app.language');
113
114
        if (! self::isLoaded('langs', $file . $locale)) {
115
            self::loaded('langs', $file . $locale, FileLocator::lang($file, $locale));
116
        }
117
118
        return self::getLoaded('langs', $file . $locale);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getLoaded('langs', $file . $locale) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
119
    }
120
121
    /**
122
     * Verifie si un element est chargé dans la liste des modules
123
     *
124
     * @param $element
125
     */
126
    private static function isLoaded(string $module, $element): bool
127
    {
128
        if (! isset(self::$loads[$module]) || ! is_array(self::$loads[$module])) {
129
            return false;
130
        }
131
132
        return in_array($element, self::$loads[$module], true);
133
    }
134
135
    /**
136
     * Ajoute un element aux elements chargés
137
     *
138
     * @param mixed $value
139
     */
140
    private static function loaded(string $module, string $element, $value = null): void
141
    {
142
        self::$loads[$module][$element] = $value;
143
    }
144
145
    /**
146
     * Renvoie un element chargé
147
     *
148
     * @param mixed $element
149
     *
150
     * @return mixed
151
     */
152
    private static function getLoaded(string $module, $element)
153
    {
154
        return self::$loads[$module][$element] ?? null;
155
    }
156
}
157