Defaults   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 41
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A renderComponent() 0 8 2
A renderFile() 0 14 2
A loadFunctionsFile() 0 6 2
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