Completed
Pull Request — 2.0.x (#6)
by Andrew
02:53
created

PageLayoutRenderer::setUseLayout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author     Andrew Coulton <[email protected]>
4
 * @copyright  2015 inGenerator Ltd
5
 * @license    http://kohanaframework.org/license
6
 */
7
8
namespace Ingenerator\KohanaView\Renderer;
9
10
11
use Ingenerator\KohanaView\Renderer;
12
use Ingenerator\KohanaView\ViewModel\PageContentView;
13
use Ingenerator\KohanaView\ViewModel\PageLayoutView;
14
15
16
/**
17
 * Renders a PageContentView and - when appropriate - renders the generated output inside a PageLayoutView. By
18
 * default it will render the layout on normal requests (or when no request is present) but not on AJAX requests.
19
 * This behaviour can be customised by calling the setUseLayout method.
20
 *
21
 * For example, from a controller:
22
 *
23
 *    public function action_login()
24
 *    {
25
 *       $layout  = new DefaultPageLayout;
26
 *       $content = new LoginView($layout);
27
 *       $renderer = new PageLayoutRenderer(new HTMLRenderer, $this->request);
28
 *       $this->response->body($renderer->render($content));
29
 *    }
30
 *
31
 * Obviously in real life the creation of the views and renderers would happen in your dependency container.
32
 *
33
 * @package Ingenerator\KohanaView\Renderer
34
 */
35
class PageLayoutRenderer
36
{
37
    /**
38
     * @var bool Whether to force (or not force) embedding the content in the layout
39
     */
40
    protected $use_layout;
41
42
    /**
43
     * @var Renderer
44
     */
45
    protected $view_renderer;
46
47
    /**
48
     * @var \Request
49
     */
50
    protected $current_request;
51
52
    public function __construct(Renderer $view_renderer, \Request $current_request = NULL)
53
    {
54
        $this->view_renderer   = $view_renderer;
55
        $this->current_request = $current_request;
56
    }
57
58
    /**
59
     * @param PageContentView $content_view
60
     *
61
     * @return string
62
     */
63
    public function render(PageContentView $content_view)
64
    {
65
        $content = $this->view_renderer->render($content_view);
66
        if ($this->shouldUseLayout()) {
67
            return $this->renderInLayout($content_view->var_page(), $content);
68
        } else {
69
            return $content;
70
        }
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    protected function shouldUseLayout()
77
    {
78
        if ($this->use_layout !== NULL) {
79
            return $this->use_layout;
80
        }
81
82
        if ($this->current_request AND $this->current_request->is_ajax()) {
1 ignored issue
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
83
            return FALSE;
84
        } else {
85
            return TRUE;
86
        }
87
    }
88
89
    /**
90
     * @param PageLayoutView $layout
91
     * @param string         $content
92
     *
93
     * @return string
94
     */
95
    protected function renderInLayout(PageLayoutView $layout, $content)
96
    {
97
        $layout->setBodyHTML($content);
98
99
        return $this->view_renderer->render($layout);
100
    }
101
102
    /**
103
     * Configure whether to always wrap the content in the layout (TRUE), never (FALSE) or automatically for
104
     * non-AJAX requests (NULL)
105
     *
106
     * @param bool $use_layout
107
     *
108
     * @return void
109
     */
110
    public function setUseLayout($use_layout)
111
    {
112
        $this->use_layout = $use_layout;
113
    }
114
115
}
116