Completed
Push — master ( 8ba53b...895e90 )
by Tolan
06:16
created

PhpTemplate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 94
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B doRender() 0 31 3
A getRealTemplatePath() 0 13 4
1
<?php
2
/*
3
 * This file is part of the Patternseek ComponentView library.
4
 *
5
 * (c)2016 Tolan Blundell <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PatternSeek\ComponentView\Template;
11
12
use PatternSeek\ComponentView\AbstractViewComponent;
13
use PatternSeek\ComponentView\Response;
14
use PatternSeek\ComponentView\ViewState\ViewState;
15
use Puli\Repository\Api\ResourceRepository;
16
17
/**
18
 * This class allows the use of PHP files as templates.
19
 * Twig templates are generally preferred but in certain
20
 * cases such as porting legacy code a PHP template may
21
 * save a lot of work.
22
 *
23
 * Class PhpTemplate
24
 * @package PatternSeek\ComponentView
25
 */
26
class PhpTemplate extends AbstractTemplate
27
{
28
29
    /**
30
     * @var string A PHP template path
31
     */
32
    protected $templatePath;
33
34
    /**
35
     * @var string A PHP template string
36
     */
37
    protected $templateString;
38
    /**
39
     * @var ResourceRepository
40
     */
41
    protected $repo;
42
43
    /**
44
     * @param AbstractViewComponent $component
45
     * @param string $templatePath
46
     * @param null $templateString
47
     * @param ResourceRepository $repo
48
     */
49
    public function __construct(
50
        AbstractViewComponent $component,
51
        $templatePath = null,
52
        $templateString = null,
53
        ResourceRepository $repo = null
54
    ){
55
        parent::__construct( $component );
56
        $this->templatePath = $templatePath;
57
        $this->templateString = $templateString;
58
        // Optional Puli repo
59
        $this->repo = $repo;
60
    }
61
62
    /**
63
     * @param ViewState $state
64
     * @param array $props
65
     * @return Response
66
     * @throws \Exception
67
     */
68
    protected function doRender( ViewState $state, array $props = [ ] )
69
    {
70
        // Available variables in template file are:
71
        // $state
72
        // $props
73
        // $thisComponent (equivalent of 'this' in TwigTemplates)
74
        // $parent
75
        // $exec
76
77
        /** @noinspection PhpUnusedLocalVariableInspection */
78
        $thisComponent = $this->component;
79
        /** @noinspection PhpUnusedLocalVariableInspection */
80
        $parent = $this->component->getParent();
81
        /** @noinspection PhpUnusedLocalVariableInspection */
82
        $exec = $this->component->exec;
83
        if ($this->templatePath) {
84
            $realPath = $this->getRealTemplatePath( $this->templatePath );
85
            ob_start();
86
            /** @noinspection PhpIncludeInspection */
87
            include( $realPath );
88
            $rendered = ob_get_clean();
89
        }elseif ($this->templateString) {
90
            ob_start();
91
            eval( "?>" . $this->templateString );
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
92
            $rendered = ob_get_clean();
93
        }else {
94
            throw new \Exception( "Neither valid template path no valid template string passed to PhpTemplate." );
95
        }
96
97
        return new Response( "text/html", $rendered );
98
    }
99
100
    /**
101
     * @param $templatePath
102
     * @return null|string
103
     * @throws \Exception
104
     */
105
    private function getRealTemplatePath( $templatePath )
106
    {
107
        if ($this->repo instanceof ResourceRepository) {
0 ignored issues
show
Bug introduced by
The class Puli\Repository\Api\ResourceRepository does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
108
            if ($this->repo->contains( $templatePath )) {
109
                return $this->repo->get( $templatePath )
110
                    ->getPath();
111
            }
112
        }
113
        if (file_exists( $templatePath )) {
114
            return $templatePath;
115
        }
116
        throw new \Exception( "Template path not found in repository or on filesystem: {$templatePath}, in PhpTemplate" );
117
    }
118
119
}
120