Completed
Branch 2.0.x (e30486)
by Andrew
03:10
created

PageLayoutRenderer::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6667
cc 2
eloc 6
nc 2
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()) {
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