Completed
Pull Request — componentlibrary (#305)
by Dominik
02:19 queued 20s
created

Api::renderComponent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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