Completed
Push — componentlibrary-sass ( dd5554...c28c92 )
by
unknown
01:35
created

renderComponentsFromFlexibleContent()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
use Flynt;
6
use Twig_Environment;
7
use Twig_Extension;
8
use Twig_SimpleFunction;
9
10
class TwigExtensionFlynt extends Twig_Extension
11
{
12
13
    public function getName()
14
    {
15
        return 'twig_extension_flynt';
16
    }
17
18
    public function getFunctions()
19
    {
20
        return array(
21
            new Twig_SimpleFunction('renderComponent', [$this, 'renderComponent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
22
            new Twig_SimpleFunction('renderComponentsFromFlexibleContent', [$this, 'renderComponentsFromFlexibleContent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
23
        );
24
    }
25
26
    public function renderComponentsFromFlexibleContent(Twig_Environment $env, $context, $fields, $withContext = true, $ignoreMissing = false, $sandboxed = false)
27
    {
28
        $output = '';
29
        foreach ((empty($fields) ? [] : $fields) as $field) {
30
            $output .= $this->renderComponent($env, $context, ucfirst($field['acf_fc_layout']), $field, $withContext, $ignoreMissing, $sandboxed);
31
        }
32
        return $output;
33
    }
34
35
    public function renderComponent(Twig_Environment $env, $context, $componentName, $data = [], $withContext = true, $ignoreMissing = false, $sandboxed = false)
36
    {
37
        $data = $this->getComponentData($data, $componentName);
38
39
        $componentManager = Flynt\ComponentManager::getInstance();
40
        $templateFilename = apply_filters('Flynt/Features/TimberLoader/templateFilename', 'index.twig');
41
        $templateFilename = apply_filters("Flynt/Features/TimberLoader/templateFilename?name=${componentName}", $templateFilename);
42
        $filePath = $componentManager->getComponentFilePath($componentName, $templateFilename);
43
        $relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/');
44
45
        if (!is_file($filePath)) {
46
            trigger_error("Template not found: {$filePath}", E_USER_WARNING);
47
            return $output;
0 ignored issues
show
Bug introduced by
The variable $output seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
48
        }
49
50
        $loader = $env->getLoader();
51
52
        $loaderPaths = $loader->getPaths();
53
54
        $loader->addPath(dirname($filePath));
55
56
        $output = twig_include($env, $context, $relativeFilePath, $data, $withContext, $ignoreMissing, $sandboxed);
57
58
        $loader->setPaths($loaderPaths);
59
60
        $output = apply_filters(
61
            'Flynt/renderComponent',
62
            $output,
63
            $componentName,
64
            $data
65
        );
66
67
        return $output;
68
    }
69
70
    protected function getComponentData($data, $componentName)
71
    {
72
        $data = apply_filters(
73
            'Flynt/addComponentData',
74
            $data,
75
            $componentName
76
        );
77
78
        return $data;
79
    }
80
}
81