Passed
Push — main ( 4acb40...2e52e3 )
by Dimitri
03:07
created

Load::lang()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A Load::isLoaded() 0 7 3
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\Contracts\Database\ConnectionInterface;
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
     * @return object|object[]
81
     *
82
     * @throws LoadException
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
     * Verifie si un element est chargé dans la liste des modules
109
     *
110
     * @param mixed $element
111
     */
112
    private static function isLoaded(string $module, $element): bool
113
    {
114
        if (! isset(self::$loads[$module]) || ! is_array(self::$loads[$module])) {
115
            return false;
116
        }
117
118
        return in_array($element, self::$loads[$module], true);
119
    }
120
121
    /**
122
     * Ajoute un element aux elements chargés
123
     *
124
     * @param mixed $value
125
     */
126
    private static function loaded(string $module, string $element, $value = null): void
127
    {
128
        self::$loads[$module][$element] = $value;
129
    }
130
131
    /**
132
     * Renvoie un element chargé
133
     *
134
     * @param mixed $element
135
     *
136
     * @return mixed
137
     */
138
    private static function getLoaded(string $module, $element)
139
    {
140
        return self::$loads[$module][$element] ?? null;
141
    }
142
}
143