View::withStripShortcodes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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