Completed
Pull Request — master (#4)
by Doğa
03:18
created

Api::initDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Flynt;
4
5
use Flynt\ComponentManager;
6
use Dflydev\DotAccessData\Data;
7
8
class Api
9
{
10
    public static function registerComponent($componentName, $componentPath = null)
11
    {
12
        $componentManager = ComponentManager::getInstance();
13
        $componentManager->registerComponent($componentName, $componentPath);
14
    }
15
16
    public static function registerComponentsFromPath($componentBasePath)
17
    {
18
        foreach (glob("{$componentBasePath}/*", GLOB_ONLYDIR) as $componentPath) {
19
            $componentName = basename($componentPath);
20
            self::registerComponent($componentName, $componentPath);
21
        }
22
    }
23
24
    public static function renderComponent($componentName, $data)
25
    {
26
        $data = apply_filters(
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        $data = /** @scrutinizer ignore-call */ apply_filters(
Loading history...
27
            'Flynt/addComponentData',
28
            $data,
29
            $componentName
30
        );
31
        $output = apply_filters(
32
            'Flynt/renderComponent',
33
            null,
34
            $componentName,
35
            $data
36
        );
37
38
        return is_null($output) ? '' : $output;
39
    }
40
41
    public static function registerFields($scope, $fields, $fieldsId = null)
42
    {
43
        global $flyntFields;
44
        $flyntFields = $flyntFields ?? [];
45
        if (empty($fieldsId)) {
46
            $flyntFields[$scope] = $fields;
47
        } else {
48
            $flyntFields[$scope] = $flyntFields[$scope] ?? [];
49
            $flyntFields[$scope][$fieldsId] = $fields;
50
        }
51
        return $fields;
52
    }
53
54
    public static function loadFields($scope, $fieldPath = null)
55
    {
56
        global $flyntFields;
57
        $flyntFields = $flyntFields ?? [];
58
        if (empty($fieldPath)) {
59
            return $flyntFields[$scope];
60
        } else {
61
            $data = new Data($flyntFields[$scope]);
62
            return $data->get($fieldPath);
63
        }
64
    }
65
66
    public static function registerHooks()
67
    {
68
        add_filter('Flynt/renderComponent', function ($output, $componentName, $data) {
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        /** @scrutinizer ignore-call */ 
69
        add_filter('Flynt/renderComponent', function ($output, $componentName, $data) {
Loading history...
69
            return apply_filters(
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            return /** @scrutinizer ignore-call */ apply_filters(
Loading history...
70
                "Flynt/renderComponent?name={$componentName}",
71
                $output,
72
                $componentName,
73
                $data
74
            );
75
        }, 10, 3);
76
77
        add_filter('Flynt/addComponentData', function ($data, $componentName) {
78
            return apply_filters(
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            return /** @scrutinizer ignore-call */ apply_filters(
Loading history...
79
                "Flynt/addComponentData?name={$componentName}",
80
                $data,
81
                $componentName
82
            );
83
        }, 10, 2);
84
    }
85
}
86