Completed
Push — master ( bb1ccc...001983 )
by Asif
02:15
created

View::withShortcodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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