kumbia_autoload()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 27
c 5
b 0
f 0
dl 0
loc 43
rs 8.5546
cc 7
nc 7
nop 1
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Core
12
 *
13
 * @copyright  Copyright (c) 2005 - 2023 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
// @see Util
18
require CORE_PATH.'kumbia/util.php';
19
20
// Autocarga de clases
21
function kumbia_autoload($class)
22
{
23
    // Optimizando carga
24
    static $classes;
25
    $classes ??= [
26
        'ActiveRecord'    => APP_PATH.'libs/active_record.php',
27
        'Load'            => CORE_PATH.'kumbia/load.php',
28
        'KumbiaException' => CORE_PATH.'kumbia/kumbia_exception.php',
29
        'KumbiaRouter'    => CORE_PATH.'kumbia/kumbia_router.php',
30
        'KumbiaFacade'    => CORE_PATH.'kumbia/kumbia_facade.php'
31
    ];
32
33
    if (isset($classes[$class])) {
34
        include $classes[$class];
35
        return;
36
    }
37
    // PSR0
38
    if (str_contains($class, '\\')) {
39
        kumbia_autoload_vendor($class);
40
        return;
41
    }
42
    // for legacy apps
43
    if ($class === 'Flash') {
44
        kumbia_autoload_helper('Flash');
45
        return;
46
    }
47
48
    // Convert to smallcase
49
    $sclass = Util::smallcase($class);
50
    if (is_file(APP_PATH."models/$sclass.php")) {
51
        include APP_PATH."models/$sclass.php";
52
        return;
53
    }
54
    if (is_file(APP_PATH."libs/$sclass.php")) {
55
        include APP_PATH."libs/$sclass.php";
56
        return;
57
    }
58
    if (is_file(CORE_PATH."libs/$sclass/$sclass.php")) {
59
        include CORE_PATH."libs/$sclass/$sclass.php";
60
        return;
61
    }
62
    // Perhaps is PEAR,  zend framework 1, ...
63
    kumbia_autoload_vendor($class);
64
}
65
66
function kumbia_autoload_vendor($class): void
67
{
68
    //Autoload PSR0
69
    $psr0 = dirname(APP_PATH, 2).'/vendor/'.str_replace(['_', '\\'], DIRECTORY_SEPARATOR, $class).'.php';
70
    if (is_file($psr0)) {
71
        include $psr0;
72
    }
73
}
74
75
function kumbia_autoload_helper($class): void
76
{
77
    $sclass = Util::smallcase($class);
78
    if (is_file(APP_PATH."extensions/helpers/$sclass.php")) {
79
        include APP_PATH."extensions/helpers/$sclass.php";
80
        return;
81
    }
82
    if (is_file(CORE_PATH."extensions/helpers/$sclass.php")) {
83
        include CORE_PATH."extensions/helpers/$sclass.php";
84
    }
85
}
86
87
// Registrar la autocarga
88
spl_autoload_register('kumbia_autoload');
89