-
Notifications
You must be signed in to change notification settings - Fork 1
/
WidgetBase.php
125 lines (110 loc) · 2.62 KB
/
WidgetBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* WidgetBase.php
* ----------------------------------------------
*
* @author Stanislav Kiryukhin <korsar.zn@gmail.com>
* @copyright Copyright (c) 2014, CKGroup.ru
*
* ----------------------------------------------
* All Rights Reserved.
* ----------------------------------------------
*/
namespace Phalcon\Ext\Widgets;
use Phalcon\Mvc\User\Component;
use Phalcon\Mvc\View\Simple as ViewSimple;
use Phalcon\Mvc\ViewInterface;
/**
* Class WidgetBase
* @package Phalcon\Ext\Widgets
*/
abstract class WidgetBase extends Component implements WidgetInterface
{
const VIEW_DIR_NAME = 'widgets';
/**
* @var array
*/
protected $_options = [];
/**
* @var ViewInterface
*/
protected $_view;
/**
* Create a new Widget component using $options for configuring widget
*
* @param array|null $options
*/
public function __construct($options = null)
{
$this->setOptions($options);
/**
* Called after __construct
*/
if (method_exists($this, 'onConstruct')) {
$this->onConstruct();
}
}
/**
* Set options for configuring widget
*
* @param array|null $options
*
* @return $this
*/
public function setOptions($options)
{
$this->_options = array_merge($this->getDefaultOptions(), (array)$options);
return $this;
}
/**
* Get options for configuring widget
*
* @param string|null $key
* @param mixed|null $default
*
* @return mixed|null
*/
public function getOptions($key = null, $default = null)
{
if ($key !== null) {
return isset($this->_options[$key]) ? $this->_options[$key] : $default;
} else {
return $this->_options;
}
}
/**
* Sets the view service
*
* @param ViewInterface $view
*
* @return $this
*/
public function setView(ViewInterface $view)
{
$this->_view = $view;
return $this;
}
/**
* Gets the view service
*
* @return ViewSimple|ViewInterface
*/
public function getView()
{
if (!$this->_view) {
$defaultViewsDir = $this->getDI()->get('view')->getViewsDir() . '/' . static::VIEW_DIR_NAME . '/';
$this->_view = new ViewSimple();
$this->_view->setViewsDir($this->getOptions('viewsDir', $defaultViewsDir));
}
return $this->_view;
}
/**
* Gets defaults widget options
*
* @return array
*/
protected function getDefaultOptions()
{
return [];
}
}