AutoResolvesViews::missingMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JumpGate\ViewResolution\Traits;
4
5
trait AutoResolvesViews
6
{
7
    /**
8
     * @var null|\Illuminate\View\View
9
     */
10
    protected $layout;
11
12
    /**
13
     * Find the view for the called method.
14
     *
15
     * @param null|string $view
16
     * @param null|string $layout
17
     *
18
     * @return $this
19
     */
20
    public function view($view = null, $layout = null)
21
    {
22
        $layoutOptions = $this->getLayoutOptions($layout);
23
24
        // Set up the default view resolution
25
        viewResolver()->setUp($layoutOptions, $view);
26
        $this->setupLayout();
27
    }
28
29
    /**
30
     * Get an array of layout options if available.
31
     *
32
     * @param null|string $layout
33
     *
34
     * @return array|null
35
     */
36
    protected function getLayoutOptions($layout)
37
    {
38
        // If passed a layout, use that for any request.
39
        if (! is_null($layout)) {
40
            return [
41
                'default' => $layout,
42
                'ajax'    => $layout,
43
            ];
44
        }
45
46
        // If a set of layout options is defined, use those.
47
        if (isset($this->layoutOptions)) {
48
            return $this->layoutOptions;
49
        }
50
51
        // Use nothing.
52
        return null;
53
    }
54
55
    /**
56
     * Force the layout for the view.
57
     *
58
     * @param string $layout
59
     */
60
    public function setViewLayout($layout)
61
    {
62
        $this->layoutOptions = [
63
            'default' => $layout,
64
            'ajax'    => $layout,
65
        ];;
66
    }
67
68
    /**
69
     * Master template method
70
     * Sets the template based on location and passes variables to the view.
71
     *
72
     * @return void
73
     */
74
    public function setupLayout()
75
    {
76
        $this->layout = viewResolver()->getLayout();
77
    }
78
79
    /**
80
     * Execute an action on the controller.
81
     *
82
     * Overloading this method to make sure our layout is
83
     * always used.
84
     *
85
     * @param  string $method
86
     * @param  array  $parameters
87
     *
88
     * @return \Symfony\Component\HttpFoundation\Response
89
     */
90
    public function callAction($method, $parameters)
91
    {
92
        $response = call_user_func_array([$this, $method], $parameters);
93
94
        if (is_null($response) && ! is_null($this->layout)) {
95
            $response = $this->layout;
96
        }
97
98
        return $response;
99
    }
100
101
    /**
102
     * Catch a missing method and try to figure out what
103
     * it should be.
104
     *
105
     * @param array $parameters
106
     *
107
     * @return mixed|void
108
     */
109
    public function missingMethod($parameters = [])
110
    {
111
        viewResolver()->missingMethod($parameters);
112
    }
113
114
    /**
115
     * Catch any un-found method and route through
116
     * missing method.
117
     *
118
     * @param string $method
119
     * @param array  $parameters
120
     *
121
     * @return mixed|void
122
     */
123
    public function __call($method, $parameters)
124
    {
125
        $parameters = array_merge((array)$method, $parameters);
126
127
        return $this->missingMethod($parameters);
128
    }
129
}
130