1<?php
2namespace Wpb\String_Blade_Compiler;
3
4use Illuminate\Contracts\Events\Dispatcher;
5use Illuminate\View\Factory as FactoryParent;
6use Illuminate\View\Engines\EngineResolver;
7use Illuminate\View\ViewFinderInterface;
8use InvalidArgumentException;
9
10class Factory extends FactoryParent
11{
12
13    /**
14     * The engine implementation.
15     *
16     * @var \Illuminate\View\Engines\EngineResolver
17     */
18    protected $engines;
19
20    /**
21     * The view finder implementation.
22     *
23     * @var \Illuminate\View\ViewFinderInterface
24     */
25    protected $finder;
26
27    /**
28     * The event dispatcher instance.
29     *
30     * @var \Illuminate\Contracts\Events\Dispatcher
31     */
32    protected $events;
33
34    /**
35     * The IoC container instance.
36     *
37     * @var \Illuminate\Contracts\Container\Container
38     */
39    protected $container;
40
41    /**
42     * Data that should be available to all templates.
43     *
44     * @var array
45     */
46    protected $shared = [];
47
48    /**
49     * The extension to engine bindings.
50     *
51     * @var array
52     */
53    protected $extensions = [
54        'blade.php' => 'blade',
55        'php' => 'php',
56        'css' => 'file',
57    ];
58
59    /**
60     * The view composer events.
61     *
62     * @var array
63     */
64    protected $composers = [];
65
66    /**
67     * The number of active rendering operations.
68     *
69     * @var int
70     */
71    protected $renderCount = 0;
72
73    /**
74     * Get the evaluated view contents for the given view.
75     *
76     * @param  string|array  $view
77     * @param  array   $data
78     * @param  array   $mergeData
79     * @return \Illuminate\Contracts\View\View|\Wpb\String_Blade_Compiler\StringView
80     */
81    public function make($view, $data = [], $mergeData = [])
82    {
83        $data = array_merge($mergeData, $this->parseData($data));
84
85        // For string rendering
86        if (is_array($view)) {
87            return tap($this->stringViewInstance($view, $data), function ($view) {
88                $this->callCreator($view);
89            });
90        }
91
92        $path = $this->finder->find(
93            $view = $this->normalizeName($view)
94        );
95
96        // Next, we will create the view instance and call the view creator for the view
97        // which can set any data, etc. Then we will return the view instance back to
98        // the caller for rendering or performing other view manipulations on this.
99
100        return tap($this->viewInstance($view, $path, $data), function ($view) {
101            $this->callCreator($view);
102        });
103    }
104
105    /**
106     * Create a new string view instance from the given arguments.
107     *
108     * @param  string|array  $view
109     * @param  array  $data
110     * @return StringView
111     */
112    protected function stringViewInstance($view, $data)
113    {
114        return new StringView($this, $this->engines->resolve('stringblade'), $view, null, $data);
115    }
116
117    /**
118     * Flush all of the section contents if done rendering.
119     *
120     * @return void
121     */
122    public function flushStateIfDoneRendering()
123    {
124        if ($this->doneRendering()) {
125            $this->flushState();
126        }
127    }
128
129    /**
130     * Flush all of the factory state like sections and stacks.
131     *
132     * @return void
133     */
134    public function flushState()
135    {
136        $this->renderCount = 0;
137
138        $this->flushSections();
139        $this->flushStacks();
140    }
141
142    /**
143     * Get the appropriate view engine for the given string key.
144     *
145     * @param  string  $stringkey
146     * @return \Illuminate\Contracts\View\Engine
147     *
148     *  ['file', 'php', 'blade', 'stringblade'] in StringBladeServiceProvider:registerEngineResolver
149     *
150     * @throws \InvalidArgumentException
151     */
152    public function getEngineFromStringKey($stringkey)
153    {
154        // resolve function throws error if $stringkey is not a registered engine
155        return $this->engines->resolve($stringkey);
156    }
157
158}
159