Completed
Push — componentlibrary_streamline-ph... ( d7b4f6 )
by Doğa
01:42
created

Api   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 4

8 Methods

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