Completed
Push — componentlibrary ( e8d0dd...512a9e )
by
unknown
01:37
created

Defaults   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A renderComponent() 0 9 2
A loadFunctionsFile() 0 8 2
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
    }
18
19
    public static function renderComponent($output, $componentName, $componentData)
20
    {
21
        if (is_null($output)) {
22
            $componentManager = ComponentManager::getInstance();
23
            $filePath = $componentManager->getComponentFilePath($componentName);
24
            $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...
25
        }
26
        return $output;
27
    }
28
29
  // this action needs to be removed by the user if they want to overwrite this functionality
30
    public static function loadFunctionsFile($componentName)
31
    {
32
        $componentManager = ComponentManager::getInstance();
33
        $functionsFilePath = $componentManager->getComponentFilePath($componentName, 'functions.php');
34
        if (false !== $functionsFilePath) {
35
            require_once $functionsFilePath;
36
        }
37
    }
38
39
    protected static function renderFile($componentData, $areaHtml, $filePath)
40
    {
41
        if (!is_file($filePath)) {
42
            trigger_error("Template not found: {$filePath}", E_USER_WARNING);
43
            return '';
44
        }
45
46
        $area = function ($areaName) use ($areaHtml) {
47
            if (array_key_exists($areaName, $areaHtml)) {
48
                return $areaHtml[$areaName];
49
            }
50
        };
51
52
        $data = function () use ($componentData) {
53
            $args = func_get_args();
54
            array_unshift($args, $componentData);
55
            return Helpers::extractNestedDataFromArray($args);
56
        };
57
58
        ob_start();
59
        require $filePath;
60
        $output = ob_get_contents();
61
        ob_get_clean();
62
63
        return $output;
64
    }
65
}
66