Passed
Push — master ( 7130c9...553534 )
by
unknown
06:43 queued 02:49
created

TwigExtensionFlynt::renderFlexibleContent()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 6
1
<?php
2
3
namespace Flynt\Utils;
4
5
use Flynt\ComponentManager;
6
use Twig_Environment;
7
use Twig_Extension;
8
use Twig_SimpleFunction;
9
10
class TwigExtensionFlynt extends Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

10
class TwigExtensionFlynt extends /** @scrutinizer ignore-deprecated */ Twig_Extension
Loading history...
11
{
12
13
    public function getName()
14
    {
15
        return 'twig_extension_flynt';
16
    }
17
18
    public function getFunctions()
19
    {
20
        return [
21
            new Twig_SimpleFunction('renderComponent', [$this, 'renderComponent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
            /** @scrutinizer ignore-deprecated */ new Twig_SimpleFunction('renderComponent', [$this, 'renderComponent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
Loading history...
22
        ];
23
    }
24
25
    public function renderComponent(Twig_Environment $env, $context, $componentName, $data = [], $withContext = true, $ignoreMissing = false, $sandboxed = false)
26
    {
27
        $data = $data === false ? [] : $data;
28
29
        if (is_array($componentName)) {
30
            $data = array_merge($componentName, $data);
31
            $componentName = ucfirst($data['acf_fc_layout']);
32
        }
33
        $data = $this->getComponentData($data, $componentName);
34
35
        $componentManager = ComponentManager::getInstance();
36
        $templateFilename = apply_filters('Flynt/TimberLoader/templateFilename', 'index.twig');
37
        $templateFilename = apply_filters("Flynt/TimberLoader/templateFilename?name=${componentName}", $templateFilename);
38
        $filePath = $componentManager->getComponentFilePath($componentName, $templateFilename);
39
        $relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/');
40
41
        if (!is_file($filePath)) {
42
            trigger_error("Template not found: {$filePath}", E_USER_WARNING);
43
            return '';
44
        }
45
46
        $loader = $env->getLoader();
47
48
        $loaderPaths = $loader->getPaths();
0 ignored issues
show
Bug introduced by
The method getPaths() does not exist on Twig\Loader\LoaderInterface. It seems like you code against a sub-type of Twig\Loader\LoaderInterface such as Twig\Loader\FilesystemLoader or Twig\Loader\FilesystemLoader or Twig\Loader\FilesystemLoader. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        /** @scrutinizer ignore-call */ 
49
        $loaderPaths = $loader->getPaths();
Loading history...
49
50
        $loader->addPath(dirname($filePath));
0 ignored issues
show
Bug introduced by
The method addPath() does not exist on Twig\Loader\LoaderInterface. It seems like you code against a sub-type of Twig\Loader\LoaderInterface such as Twig\Loader\FilesystemLoader or Twig\Loader\FilesystemLoader or Twig\Loader\FilesystemLoader. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $loader->/** @scrutinizer ignore-call */ 
51
                 addPath(dirname($filePath));
Loading history...
51
52
        $output = twig_include($env, $context, $relativeFilePath, $data, $withContext, $ignoreMissing, $sandboxed);
53
54
        $loader->setPaths($loaderPaths);
0 ignored issues
show
Bug introduced by
The method setPaths() does not exist on Twig\Loader\LoaderInterface. It seems like you code against a sub-type of Twig\Loader\LoaderInterface such as Twig\Loader\FilesystemLoader or Twig\Loader\FilesystemLoader or Twig\Loader\FilesystemLoader. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $loader->/** @scrutinizer ignore-call */ 
55
                 setPaths($loaderPaths);
Loading history...
55
56
        $output = apply_filters(
57
            'Flynt/renderComponent',
58
            $output,
59
            $componentName,
60
            $data
61
        );
62
63
        return $output;
64
    }
65
66
    protected function getComponentData($data, $componentName)
67
    {
68
        $data = apply_filters(
69
            'Flynt/addComponentData',
70
            $data,
71
            $componentName
72
        );
73
74
        return $data;
75
    }
76
}
77