TwigExtensionFlynt::renderComponent()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 39
rs 9.536
c 0
b 0
f 0
cc 4
nc 8
nop 7
1
<?php
2
3
namespace Flynt\Utils;
4
5
use Flynt\ComponentManager;
6
use Twig_Environment;
7
use Twig_Extension;
8
use Twig\TwigFunction;
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
    public function getName()
13
    {
14
        return 'twig_extension_flynt';
15
    }
16
17
    public function getFunctions()
18
    {
19
        return [
20
            new TwigFunction('renderComponent', [$this, 'renderComponent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
21
        ];
22
    }
23
24
    public function renderComponent(Twig_Environment $env, $context, $componentName, $data = [], $withContext = true, $ignoreMissing = false, $sandboxed = false)
25
    {
26
        $data = $data === false ? [] : $data;
27
28
        if (is_array($componentName)) {
29
            $data = array_merge($componentName, $data);
30
            $componentName = ucfirst($data['acf_fc_layout']);
31
        }
32
        $data = $this->getComponentData($data, $componentName);
33
34
        $componentManager = ComponentManager::getInstance();
35
        $templateFilename = apply_filters('Flynt/TimberLoader/templateFilename', 'index.twig');
36
        $templateFilename = apply_filters("Flynt/TimberLoader/templateFilename?name=${componentName}", $templateFilename);
37
        $filePath = $componentManager->getComponentFilePath($componentName, $templateFilename);
38
        $relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/');
39
40
        if (!is_file($filePath)) {
41
            trigger_error("Template not found: {$filePath}", E_USER_WARNING);
42
            return '';
43
        }
44
45
        $loader = $env->getLoader();
46
47
        $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

47
        /** @scrutinizer ignore-call */ 
48
        $loaderPaths = $loader->getPaths();
Loading history...
48
49
        $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

49
        $loader->/** @scrutinizer ignore-call */ 
50
                 addPath(dirname($filePath));
Loading history...
50
51
        $output = twig_include($env, $context, $relativeFilePath, $data, $withContext, $ignoreMissing, $sandboxed);
52
53
        $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

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