Completed
Push — componentlibrary ( 56c322...de047a )
by Dominik
02:25 queued 12s
created

Flynt.php ➔ loadFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace Flynt;
3
4
use Flynt\Defaults;
5
use Flynt\ComponentManager;
6
use Flynt\Utils\Feature;
7
use Dflydev\DotAccessData\Data;
8
9
function initDefaults()
10
{
11
    Defaults::init();
12
}
13
14
function registerComponent($componentName, $componentPath = null)
15
{
16
    $componentManager = ComponentManager::getInstance();
17
    $componentManager->registerComponent($componentName, $componentPath);
18
}
19
20
function registerComponentsFromPath($componentBasePath)
21
{
22
    foreach (glob("{$componentBasePath}/*", GLOB_ONLYDIR) as $componentPath) {
23
        $componentName = basename($componentPath);
24
        registerComponent($componentName, $componentPath);
25
    }
26
}
27
28
function registerFeaturesFromPath($featureBasePath)
29
{
30
    foreach (glob("{$featureBasePath}/*", GLOB_ONLYDIR) as $featurePath) {
31
        $featureName = basename($featurePath);
32
        Feature::register($featureName, $featureBasePath);
33
    }
34
}
35
36
function renderComponent($componentName, $data)
37
{
38
    // var_dump($componentName, $data);die();
39
    $data = apply_filters(
40
        'Flynt/addComponentData',
41
        $data,
42
        $componentName
43
    );
44
    $output = apply_filters(
45
        'Flynt/renderComponent',
46
        null,
47
        $componentName,
48
        $data
49
    );
50
51
    return is_null($output) ? '' : $output;
52
}
53
54
add_filter('Flynt/renderComponent', function ($output, $componentName, $data) {
55
    return apply_filters(
56
        "Flynt/renderComponent?name={$componentName}",
57
        $output,
58
        $componentName,
59
        $data
60
    );
61
}, 10, 3);
62
63
add_filter('Flynt/addComponentData', function ($data, $componentName) {
64
    return apply_filters(
65
        "Flynt/addComponentData?name={$componentName}",
66
        $data,
67
        $componentName
68
    );
69
}, 10, 2);
70
71
function registerFields($scope, $fields, $fieldsId = null)
72
{
73
    global $flyntFields;
74
    $flyntFields = $flyntFields ?? [];
75
    if (empty($fieldsId)) {
76
        $flyntFields[$scope] = $fields;
77
    } else {
78
        $flyntFields[$scope] = $flyntFields[$scope] ?? [];
79
        $flyntFields[$scope][$fieldsId] = $fields;
80
    }
81
    return $fields;
82
}
83
84
function loadFields($scope, $fieldPath = null)
85
{
86
    global $flyntFields;
87
    $flyntFields = $flyntFields ?? [];
88
    if (empty($fieldPath)) {
89
        return $flyntFields[$scope];
90
    } else {
91
        $data = new Data($flyntFields[$scope]);
92
        return $data->get($fieldPath);
93
    }
94
}
95