Test Failed
Push — master ( a28393...a400a4 )
by David
06:01 queued 03:02
created

lib/Dwoo/Adapters/Agavi/DwooRenderer.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Copyright (c) 2013-2016
5
 *
6
 * @category  Library
7
 * @package   Dwoo\Adapters\Agavi
8
 * @author    Jordi Boggiano <[email protected]>
9
 * @author    David Sanchez <[email protected]>
10
 * @copyright 2008-2013 Jordi Boggiano
11
 * @copyright 2013-2016 David Sanchez
12
 * @license   http://dwoo.org/LICENSE Modified BSD License
13
 * @version   1.3.0
14
 * @date      2016-09-19
15
 * @link      http://dwoo.org/
16
 */
17
class DwooRenderer extends AgaviRenderer implements AgaviIReusableRenderer
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
{
19
    /**
20
     * @constant   string The directory inside the cache dir where templates will
21
     *                    be stored in compiled form.
22
     */
23
    const COMPILE_DIR = 'templates';
24
25
    /**
26
     * @constant   string The subdirectory inside the compile dir where templates
27
     *                    will be stored in compiled form.
28
     */
29
    const COMPILE_SUBDIR = 'dwoo';
30
31
    /**
32
     * @constant   string The directory inside the cache dir where cached content
33
     *                    will be stored.
34
     */
35
    const CACHE_DIR = 'dwoo';
36
37
    /**
38
     * @var Dwoo Dwoo template engine
39
     */
40
    protected $dwoo = null;
41
42
    /**
43
     * @var string A string with the default template file extension,
44
     *             including the dot
45
     */
46
    protected $defaultExtension = '.html';
47
48
    /**
49
     * stores the (optional) plugin directories to add to the Dwoo_Loader.
50
     */
51
    protected $plugin_dir = null;
52
53
    /**
54
     * Pre-serialization callback.
55
     *
56
     * Excludes the Dwoo instance to prevent excessive serialization load.
57
     */
58
    public function __sleep()
59
    {
60
        $keys = parent::__sleep();
61
        unset($keys[array_search('dwoo', $keys)]);
62
63
        return $keys;
64
    }
65
66
    /**
67
     * Initialize this Renderer.
68
     *
69
     * @param AgaviContext The current application context
70
     * @param array        An associative array of initialization parameters
71
     */
72
    public function initialize(AgaviContext $context, array $parameters = array())
73
    {
74
        parent::initialize($context, $parameters);
75
76
        $this->plugin_dir = $this->getParameter('plugin_dir', $this->plugin_dir);
77
    }
78
79
    /**
80
     * provides a custom compiler to the dwoo renderer with optional settings
81
     * you can set in the agavi output_types.xml config file.
82
     *
83
     * @return Dwoo_Compiler
84
     */
85
    public function compilerFactory()
86
    {
87
        $compiler = Dwoo_Compiler::compilerFactory();
88
        $compiler->setAutoEscape((bool) $this->getParameter('auto_escape', false));
89
90
        return $compiler;
91
    }
92
93
    /**
94
     * Grab a cleaned up dwoo instance.
95
     *
96
     * @return Dwoo A Dwoo instance
97
     */
98
    protected function getEngine()
99
    {
100
        if ($this->dwoo) {
101
            return $this->dwoo;
102
        }
103
104
        // this triggers Agavi autoload
105
        if (!class_exists('Dwoo')) {
106
            if (file_exists(dirname(__FILE__).'/../../../dwooAutoload.php')) {
107
                // file was dropped with the entire dwoo package
108
                include dirname(__FILE__).'/../../../dwooAutoload.php';
109
            } else {
110
                // assume the dwoo package is in the include path
111
                include 'dwooAutoload.php';
112
            }
113
        }
114
115
        $parentMode = fileperms(AgaviConfig::get('core.cache_dir'));
116
117
        $compileDir = AgaviConfig::get('core.cache_dir').DIRECTORY_SEPARATOR.self::COMPILE_DIR.DIRECTORY_SEPARATOR.self::COMPILE_SUBDIR;
118
        AgaviToolkit::mkdir($compileDir, $parentMode, true);
119
120
        $cacheDir = AgaviConfig::get('core.cache_dir').DIRECTORY_SEPARATOR.self::CACHE_DIR;
121
        AgaviToolkit::mkdir($cacheDir, $parentMode, true);
122
123
        $this->dwoo = new Dwoo_Core($compileDir, $cacheDir);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Dwoo_Core($compileDir, $cacheDir) of type object<Dwoo_Core> is incompatible with the declared type object<Dwoo> of property $dwoo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
124
125
        if (!empty($this->plugin_dir)) {
126
            foreach ((array) $this->plugin_dir as $dir) {
127
                $this->dwoo->getLoader()->addDirectory($dir);
128
            }
129
        }
130
131
        $this->dwoo->setDefaultCompilerFactory('file', array($this, 'compilerFactory'));
132
133
        return $this->dwoo;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->dwoo; (Dwoo_Core) is incompatible with the return type documented by DwooRenderer::getEngine of type Dwoo.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
134
    }
135
136
    /**
137
     * Render the presentation and return the result.
138
     *
139
     * @param AgaviTemplateLayer The template layer to render
140
     * @param array              The template variables
141
     * @param array              The slots
142
     * @param array              Associative array of additional assigns
143
     *
144
     * @return string A rendered result
145
     */
146
    public function render(AgaviTemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array())
147
    {
148
        $engine = $this->getEngine();
149
150
        $data = array();
151
        if ($this->extractVars) {
152
            $data = $attributes;
153
        } else {
154
            $data[$this->varName] = &$attributes;
155
        }
156
157
        $data[$this->slotsVarName] = &$slots;
158
159
        foreach ($this->assigns as $key => $getter) {
160
            $data[$key] = $this->getContext()->$getter();
161
        }
162
163
        foreach ($moreAssigns as $key => &$value) {
164
            if (isset($this->moreAssignNames[$key])) {
165
                $key = $this->moreAssignNames[$key];
166
            }
167
            $data[$key] = &$value;
168
        }
169
170
        return $engine->get($layer->getResourceStreamIdentifier(), $data);
171
    }
172
}
173