Completed
Pull Request — master (#56)
by
unknown
02:14
created

Widget   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 67.86%

Importance

Changes 0
Metric Value
wmc 12
eloc 22
dl 0
loc 169
ccs 19
cts 28
cp 0.6786
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A renderFile() 0 3 1
A run() 0 10 2
A getView() 0 3 1
A __construct() 0 4 1
A __toString() 0 3 1
A init() 0 2 1
A getContent() 0 2 1
A beforeRun() 0 6 1
A afterRun() 0 6 1
A getViewPath() 0 5 1
A render() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Yiisoft\Widget;
5
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use ReflectionClass;
8
use Yiisoft\View\ViewContextInterface;
9
use Yiisoft\View\WebView;
10
use Yiisoft\Widget\Event\AfterRun;
11
use Yiisoft\Widget\Event\BeforeRun;
12
13
/**
14
 * Widget is the base class for widgets.
15
 *
16
 * For more details and usage information on Widget, see the [guide article on widgets](guide:structure-widgets).
17
 */
18
class Widget implements ViewContextInterface
19
{
20
    /**
21
     * @var EventDispatcherInterface event handler.
22
     */
23
    protected $eventDispatcher;
24
25
    /**
26
     * @var WebView $view
27
     */
28
    protected $webView;
29
30 25
    public function __construct(EventDispatcherInterface $eventDispatcher, WebView $webView)
31
    {
32 25
        $this->eventDispatcher = $eventDispatcher;
33 25
        $this->webView = $webView;
34
    }
35
36
    /**
37
     * Returns the view object that can be used to render views or view files.
38
     *
39
     * The {@see render()} and {@see renderFile()} methods will use this view object to implement the actual view
40
     * rendering. If not set, it will default to the "view" application component.
41
     */
42 2
    public function getView(): WebView
43
    {
44 2
        return $this->webView;
45
    }
46
47
    public function init(): void
48
    {
49
    }
50
51
    public function getContent(): string
52
    {
53
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
54
55
    /**
56
     * Executes the widget.
57
     *
58
     * @return string the result of widget execution to be outputted.
59
     */
60 16
    public function run(): string
61
    {
62 16
        $out = '';
63
64 16
        if ($this->beforeRun()) {
65 16
            $result = $this->getContent();
66 16
            $out = $this->afterRun($result);
67
        }
68
69 16
        return $out;
70
    }
71
72
    /**
73
     * Renders a view.
74
     *
75
     * The view to be rendered can be specified in one of the following formats:
76
     *
77
     * - [path alias](guide:concept-aliases) (e.g. "@app/views/site/index");
78
     * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
79
     * - absolute path within module (e.g. "/site/index"): the view name starts with a single slash.
80
     * - relative path (e.g. "index"): the actual view file will be looked for under {@see viewPath}.
81
     *
82
     * If the view name does not contain a file extension, it will use the default one `.php`.
83
     *
84
     * @param string $view the view name.
85
     * @param array $params the parameters (name-value pairs) that should be made available in the view.
86
     *
87
     * @return string the rendering result.
88
     */
89
    public function render(string $view, array $params = []): string
90
    {
91
        return $this->getView()->render($view, $params, $this);
92
    }
93
94
    /**
95
     * Renders a view file.
96
     *
97
     * @param string $file the view file to be rendered. This can be either a file path or a [path alias](guide:concept-aliases).
98
     * @param array $params the parameters (name-value pairs) that should be made available in the view.
99
     *
100
     * @return string the rendering result.
101
     * @throws \Throwable
102
     */
103
    public function renderFile(string $file, array $params = []): string
104
    {
105
        return $this->getView()->renderFile($file, $params, $this);
106
    }
107
108
    /**
109
     * Returns the directory containing the view files for this widget.
110
     * The default implementation returns the 'views' subdirectory under the directory containing the widget class file.
111
     *
112
     * @return string the directory containing the view files for this widget.
113
     *
114
     * @throws \InvalidArgumentException
115
     * @throws \ReflectionException
116
     */
117
    public function getViewPath(): string
118
    {
119
        $class = new ReflectionClass($this);
120
121
        return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
122
    }
123
124
    /**
125
     * This method is invoked right before the widget is executed.
126
     *
127
     * The method will trigger the {@see BeforeRun()} event. The return value of the method will determine whether the
128
     * widget should continue to run.
129
     *
130
     * When overriding this method, make sure you call the parent implementation like the following:
131
     *
132
     * ```php
133
     * public function beforeRun()
134
     * {
135
     *     if (!parent::beforeRun()) {
136
     *         return false;
137
     *     }
138
     *
139
     *     // your custom code here
140
     *
141
     *     return true; // or false to not run the widget
142
     * }
143
     * ```
144
     *
145
     * @return bool whether the widget should continue to be executed.
146
     */
147 23
    public function beforeRun(): bool
148
    {
149 23
        $event = new BeforeRun();
150 23
        $event = $this->eventDispatcher->dispatch($event);
151
152 23
        return !$event->isPropagationStopped();
153
    }
154
155
    /**
156
     * This method is invoked right after a widget is executed.
157
     *
158
     * The method will trigger the {@see {AfterRun()} event. The return value of the method will be used as the widget
159
     * return value.
160
     *
161
     * If you override this method, your code should look like the following:
162
     *
163
     * ```php
164
     * public function afterRun($result)
165
     * {
166
     *     $result = parent::afterRun($result);
167
     *     // your custom code here
168
     *     return $result;
169
     * }
170
     * ```
171
     *
172
     * @param mixed $result the widget return result.
173
     *
174
     * @return mixed the processed widget result.
175
     */
176 23
    public function afterRun($result)
177
    {
178 23
        $event = new AfterRun($result);
179 23
        $event = $this->eventDispatcher->dispatch($event);
180
181 23
        return $event->getResult();
182
    }
183
184
    public function __toString()
185
    {
186
        return $this->run();
187
    }
188
}
189