Passed
Push — main ( 3399b6...dd4eea )
by Dimitri
11:50
created

Load::helper()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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
    /**
35
     * Charge un fichier d'aide
36
     *
37
     * @throws InvalidArgumentException
38
     * @throws LoadException
39
     */
40
    public static function helper(string|array $helpers)
41
    {
42
        if (empty($helpers)) {
43
            throw new LoadException('Veuillez specifier le helper à charger');
44
        }
45
46
        $helpers = (array) $helpers;
47
48
        foreach ($helpers as $helper) {
49
            FileLocator::helper($helper);
50
        }
51
    }
52
53
    /**
54
     * Charge un modele
55
     *
56
     * @return object|object[]
57
     *
58
     * @throws LoadException
59
     */
60
    public static function model(string|array $model, ?ConnectionInterface $connection = null)
61
    {
62
        if (empty($model)) {
63
            throw new LoadException('Veuillez specifier le modele à charger');
64
        }
65
66
        if (is_array($model)) {
0 ignored issues
show
introduced by
The condition is_array($model) is always true.
Loading history...
67
            $models = [];
68
69
            foreach ($model as $value) {
70
                $models[] = self::model($value, $connection);
71
            }
72
73
            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...
74
        }
75
76
        if (! self::isLoaded('models', $model)) {
77
            self::loaded('models', $model, FileLocator::model($model, $connection));
78
        }
79
80
        return self::getLoaded('models', $model);
81
    }
82
83
    /**
84
     * Verifie si un element est chargé dans la liste des modules
85
     *
86
     * @param mixed $element
87
     */
88
    private static function isLoaded(string $module, $element): bool
89
    {
90
        if (! isset(self::$loads[$module]) || ! is_array(self::$loads[$module])) {
91
            return false;
92
        }
93
94
        return in_array($element, self::$loads[$module], true);
95
    }
96
97
    /**
98
     * Ajoute un element aux elements chargés
99
     *
100
     * @param mixed $value
101
     */
102
    private static function loaded(string $module, string $element, $value = null): void
103
    {
104
        self::$loads[$module][$element] = $value;
105
    }
106
107
    /**
108
     * Renvoie un element chargé
109
     *
110
     * @param mixed $element
111
     *
112
     * @return mixed
113
     */
114
    private static function getLoaded(string $module, $element)
115
    {
116
        return self::$loads[$module][$element] ?? null;
117
    }
118
}
119