Completed
Push — constructionplanless ( e8d891...d7f83f )
by Dominik
01:36
created

Defaults   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A renderComponent() 0 9 2
A loadFunctionsFile() 0 12 3
A renderFile() 0 26 3
1
<?php
2
3
namespace Flynt;
4
5
use Flynt\ComponentManager;
6
use Flynt\Helpers;
7
8
class Defaults
9
{
10
    const CONFIG_DIR = 'config';
11
    const COMPONENT_DIR = 'Components';
12
13
    public static function init()
14
    {
15
        add_filter('Flynt/renderComponent', ['Flynt\Defaults', 'renderComponent'], 999, 3);
16
        add_action('Flynt/registerComponent', ['Flynt\Defaults', 'loadFunctionsFile']);
17
        // TODO: load fields.php
18
    }
19
20
    public static function renderComponent($output, $componentName, $componentData)
21
    {
22
        if (is_null($output)) {
23
            $componentManager = ComponentManager::getInstance();
24
            $filePath = $componentManager->getComponentFilePath($componentName);
25
            $output = self::renderFile($componentData, $areaHtml, $filePath);
0 ignored issues
show
Bug introduced by
The variable $areaHtml does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
26
        }
27
        return $output;
28
    }
29
30
  // this action needs to be removed by the user if they want to overwrite this functionality
31
    public static function loadFunctionsFile($componentName)
32
    {
33
        $componentManager = ComponentManager::getInstance();
34
        $fieldsFilePath = $componentManager->getComponentFilePath($componentName, 'fields.php');
35
        if (false !== $fieldsFilePath) {
36
            require_once $fieldsFilePath;
37
        }
38
        $functionsFilePath = $componentManager->getComponentFilePath($componentName, 'functions.php');
39
        if (false !== $functionsFilePath) {
40
            require_once $functionsFilePath;
41
        }
42
    }
43
44
    protected static function renderFile($componentData, $areaHtml, $filePath)
45
    {
46
        if (!is_file($filePath)) {
47
            trigger_error("Template not found: {$filePath}", E_USER_WARNING);
48
            return '';
49
        }
50
51
        $area = function ($areaName) use ($areaHtml) {
52
            if (array_key_exists($areaName, $areaHtml)) {
53
                return $areaHtml[$areaName];
54
            }
55
        };
56
57
        $data = function () use ($componentData) {
58
            $args = func_get_args();
59
            array_unshift($args, $componentData);
60
            return Helpers::extractNestedDataFromArray($args);
61
        };
62
63
        ob_start();
64
        require $filePath;
65
        $output = ob_get_contents();
66
        ob_get_clean();
67
68
        return $output;
69
    }
70
}
71