Completed
Push — master ( 001983...6d20e6 )
by Asif
02:12
created

View::renderContents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 10
nc 2
nop 0
1
<?php namespace Webwizo\Shortcodes\View;
2
3
use ArrayAccess;
4
use Illuminate\Contracts\Support\Renderable;
5
use Illuminate\View\View as IlluminateView;
6
use Illuminate\View\Engines\EngineInterface;
7
use Webwizo\Shortcodes\Compilers\ShortcodeCompiler;
8
9
class View extends IlluminateView implements ArrayAccess, Renderable
10
{
11
12
    /**
13
     * Short code engine resolver
14
     *
15
     * @var \Webwizo\Shortcodes\Compilers\ShortcodeCompiler
16
     */
17
    public $shortcode;
18
19
    /**
20
     * Create a new view instance.
21
     *
22
     * @param \Illuminate\View\Factory|Factory                           $factory
23
     * @param \Illuminate\View\Compilers\EngineInterface|EngineInterface $engine
24
     * @param  string                                                    $view
25
     * @param  string                                                    $path
26
     * @param  array                                                     $data
27
     * @param \Webwizo\Shortcodes\Compilers\ShortcodeCompiler            $shortcode
28
     */
29
    public function __construct(Factory $factory, EngineInterface $engine, $view, $path, $data = [], ShortcodeCompiler $shortcode)
30
    {
31
        parent::__construct($factory, $engine, $view, $path, $data);
32
        $this->shortcode = $shortcode;
33
    }
34
35
    /**
36
     * Enable the shortcodes
37
     */
38
    public function withShortcodes()
39
    {
40
        $this->shortcode->enable();
41
42
        return $this;
43
    }
44
45
    /**
46
     * Disable the shortcodes
47
     */
48
    public function withoutShortcodes()
49
    {
50
        $this->shortcode->disable();
51
52
        return $this;
53
    }
54
55
    public function withStripShortcodes()
56
    {
57
        $this->shortcode->setStrip(true);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Get the contents of the view instance.
64
     *
65
     * @return string
66
     */
67
    protected function renderContents()
68
    {
69
        // We will keep track of the amount of views being rendered so we can flush
70
        // the section after the complete rendering operation is done. This will
71
        // clear out the sections for any separate views that may be rendered.
72
        $this->factory->incrementRender();
73
        $this->factory->callComposer($this);
74
        $contents = $this->getContents();
75
        if ($this->shortcode->getStrip()) {
76
            // strip content without shortcodes
77
            $contents = $this->shortcode->strip($contents);
78
        } else {
79
            // compile the shortcodes
80
            $contents = $this->shortcode->compile($contents);
81
        }
82
        // Once we've finished rendering the view, we'll decrement the render count
83
        // so that each sections get flushed out next time a view is created and
84
        // no old sections are staying around in the memory of an environment.
85
        $this->factory->decrementRender();
86
87
        return $contents;
88
    }
89
}
90