Maslosoft Miniview API
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/miniview
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*
* @link http://maslosoft.com/miniview/
*/
namespace Maslosoft\MiniView;
use Exception;
use ReflectionObject;
/**
* Widget
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
abstract class Widget
{
/**
* @var string id of the widget.
*/
private $_id;
/**
* View path
* @var string
*/
private $_path = '';
/**
* Configuration
* @var mixed[]
*/
private $config = [];
/**
* Id counter for automatically generated id's
* @var intr
*/
private static $idCounter = 0;
/**
* Owner, default to current class
* @var Widget
*/
private $owner;
/**
* Create widget with optional config
* @param mixed[] $config
*/
public function __construct($config = [], $owner = null)
{
$class = new ReflectionObject($this);
$this->_path = dirname($class->getFileName());
if (!empty($owner))
{
$this->owner = $owner;
}
else
{
$this->owner = $this;
}
$this->config = $config;
}
/**
* Initializes the widget
*/
abstract public function init();
/**
* Executes the widget.
*/
abstract public function run();
/**
* Forward to owner
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->_owner->$name;
}
/**
* Forward to owner
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
return $this->_owner->$name = $value;
}
/**
* Forward to owner
* @param string $name
* @param mixed[] $arguments
*/
public function __call($name, $arguments)
{
return call_user_func_array([$this->_owner, $name], $arguments);
}
/**
* Forward to owner
* @param string $name
* @return bool
*/
public function __isset($name)
{
return isset($this->_owner->$name);
}
/**
* Forward to owner
* @param string $name
*/
public function __unset($name)
{
unset($this->_owner->$name);
}
/**
* Returns the ID of the widget or generates a new one if not set.
* @return string id of the widget.
*/
public function getId()
{
if ($this->_id === null)
{
$this->_id = sprtinf('msmv-%s', self::$idCounter++);
}
return $this->_id;
}
/**
* Sets the ID of the widget.
* @param string $value id of the widget.
*/
public function setId($value)
{
$this->_id = $value;
}
/**
* Returns the owner/creator of this widget.
* @return object owner/creator of this widget. It could be either a widget or a controller.
*/
public function getOwner()
{
return $this->_owner;
}
/**
* Set views path. This is relative path for view resolving.
* By default it's `views` folder.
* @param string $path
*/
public function setViewsPath($path)
{
$this->_viewsPath = $path;
}
/**
* Render view with data provided.
* View name must not contain `php` extension.
* @param string $view
* @param mixed[] $data
* @param bool $return
* @return string
*/
public function render($view, $data = null, $return = false)
{
$viewFile = sprintf('%s/%s/%s.php', $this->_path, $this->_viewsPath, $view);
return $this->_renderInternal($viewFile, $data, $return);
}
/**
* Render file with data provided.
* @param string $file
* @param mixed[] $data
* @param bool $return
* @return string
*/
public function renderFile($file, $data = null, $return = false)
{
return $this->_renderInternal($file, $data, $return);
}
/**
* Renders a view file.
* This method includes the view file as a PHP script
* and captures the display result if required.
* @param string $_viewFile_ view file
* @param array $_data_ data to be extracted and made available to the view file
* @param boolean $_return_ whether the rendering result should be returned as a string
* @return string the rendering result. Null if the rendering result is not required.
*/
private function _renderInternal($_viewFile_, $_data_ = null, $_return_ = false)
{
// we use special variable names here to avoid conflict when extracting data
if (is_array($_data_))
{
extract($_data_, EXTR_PREFIX_SAME, 'data');
}
else
{
$data = $_data_;
}
if ($_return_)
{
ob_start();
ob_implicit_flush(false);
require($_viewFile_);
return ob_get_clean();
}
else
{
require($_viewFile_);
}
}
/**
* Create and run widget. Use this in templates to properly initialize widgets.
* This must be called from extending class.
* Example:
* ```php
* echo ProgressBar::widget([
* 'percent' => 40
* ]);
* ```
* @param mixed[] $config
* @return string HTML widget
*/
public static function widget($config = [])
{
ob_start();
ob_implicit_flush(false);
/* @var $widget MsWidget */
if (static::class === __CLASS__)
{
throw new WidgetException(sprintf('Method widget must be called from extending class, not from `%s`', __CLASS__));
}
if (is_string($config))
{
$class = $config;
$config = [];
$config['class'] = $class;
}
else
{
$config['class'] = static::class;
}
$widget = EmbeDi::fly()->apply($config);
$widget->init();
$out = $widget->run();
return ob_get_clean() . $out;
}
/**
* This is equivalent of calling ::widget() with config from constructor.
* Could be used for convenient outputting of simple widgets.
* Example:
* ```php
* echo new Flags([], $this);
* echo new Head(['title' => 'foot'], $this);
* ```
* @return string HTML output of widget.
*/
public function __toString()
{
try
{
$class = static::class;
return $class::widget($this->config);
}
catch (Exception $e)
{
return nl2br($e->getTraceAsString());
}
}
}
API documentation generated by ApiGen