Defaults::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
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
7
class Defaults
8
{
9
    public static function init()
10
    {
11
        add_filter('Flynt/renderComponent', ['Flynt\Defaults', 'renderComponent'], 999, 3);
12
        add_action('Flynt/registerComponent', ['Flynt\Defaults', 'loadFunctionsFile']);
13
    }
14
15
    public static function renderComponent($output, $componentName, $componentData)
16
    {
17
        if (is_null($output)) {
18
            $componentManager = ComponentManager::getInstance();
19
            $filePath = $componentManager->getComponentFilePath($componentName);
20
            $output = self::renderFile($componentData, $filePath);
21
        }
22
        return $output;
23
    }
24
25
    public static function loadFunctionsFile($componentName)
26
    {
27
        $componentManager = ComponentManager::getInstance();
28
        $functionsFilePath = $componentManager->getComponentFilePath($componentName, 'functions.php');
29
        if (false !== $functionsFilePath) {
30
            require_once $functionsFilePath;
31
        }
32
    }
33
34
    protected static function renderFile($componentData, $filePath)
35
    {
36
        _deprecated_function(__METHOD__, '1.4.0');
37
        if (!is_file($filePath)) {
38
            trigger_error("Template not found: {$filePath}", E_USER_WARNING);
39
            return '';
40
        }
41
42
        ob_start();
43
        require $filePath;
44
        $output = ob_get_contents();
45
        ob_get_clean();
46
47
        return $output;
48
    }
49
}
50