Maslosoft /
MiniView
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This software package is licensed under `AGPL, Commercial` license[s]. |
||
| 5 | * |
||
| 6 | * @package maslosoft/miniview |
||
| 7 | * @license AGPL, Commercial |
||
| 8 | * |
||
| 9 | * @copyright Copyright (c) Peter Maselkowski <[email protected]> |
||
| 10 | * |
||
| 11 | * @link http://maslosoft.com/miniview/ |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace Maslosoft\MiniView; |
||
| 15 | |||
| 16 | use Exception; |
||
| 17 | use ReflectionObject; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Widget |
||
| 21 | * |
||
| 22 | * @author Piotr Maselkowski <pmaselkowski at gmail.com> |
||
| 23 | */ |
||
| 24 | abstract class Widget |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string id of the widget. |
||
| 29 | */ |
||
| 30 | private $_id; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * View path |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $_path = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Configuration |
||
| 40 | * @var mixed[] |
||
| 41 | */ |
||
| 42 | private $config = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Id counter for automatically generated id's |
||
| 46 | * @var intr |
||
| 47 | */ |
||
| 48 | private static $idCounter = 0; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Owner, default to current class |
||
| 52 | * @var Widget |
||
| 53 | */ |
||
| 54 | private $owner; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create widget with optional config |
||
| 58 | * @param mixed[] $config |
||
| 59 | */ |
||
| 60 | public function __construct($config = [], $owner = null) |
||
| 61 | { |
||
| 62 | $class = new ReflectionObject($this); |
||
| 63 | $this->_path = dirname($class->getFileName()); |
||
| 64 | if (!empty($owner)) |
||
| 65 | { |
||
| 66 | $this->owner = $owner; |
||
| 67 | } |
||
| 68 | else |
||
| 69 | { |
||
| 70 | $this->owner = $this; |
||
| 71 | } |
||
| 72 | $this->config = $config; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initializes the widget |
||
| 77 | */ |
||
| 78 | abstract public function init(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Executes the widget. |
||
| 82 | */ |
||
| 83 | abstract public function run(); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Forward to owner |
||
| 87 | * @param string $name |
||
| 88 | * @return mixed |
||
| 89 | */ |
||
| 90 | public function __get($name) |
||
| 91 | { |
||
| 92 | return $this->_owner->$name; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Forward to owner |
||
| 97 | * @param string $name |
||
| 98 | * @param mixed $value |
||
| 99 | */ |
||
| 100 | public function __set($name, $value) |
||
| 101 | { |
||
| 102 | return $this->_owner->$name = $value; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Forward to owner |
||
| 107 | * @param string $name |
||
| 108 | * @param mixed[] $arguments |
||
| 109 | */ |
||
| 110 | public function __call($name, $arguments) |
||
| 111 | { |
||
| 112 | return call_user_func_array([$this->_owner, $name], $arguments); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Forward to owner |
||
| 117 | * @param string $name |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function __isset($name) |
||
| 121 | { |
||
| 122 | return isset($this->_owner->$name); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Forward to owner |
||
| 127 | * @param string $name |
||
| 128 | */ |
||
| 129 | public function __unset($name) |
||
| 130 | { |
||
| 131 | unset($this->_owner->$name); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the ID of the widget or generates a new one if not set. |
||
| 136 | * @return string id of the widget. |
||
| 137 | */ |
||
| 138 | public function getId() |
||
| 139 | { |
||
| 140 | if ($this->_id === null) |
||
| 141 | { |
||
| 142 | $this->_id = sprtinf('msmv-%s', self::$idCounter++); |
||
| 143 | } |
||
| 144 | return $this->_id; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Sets the ID of the widget. |
||
| 149 | * @param string $value id of the widget. |
||
| 150 | */ |
||
| 151 | public function setId($value) |
||
| 152 | { |
||
| 153 | $this->_id = $value; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the owner/creator of this widget. |
||
| 158 | * @return object owner/creator of this widget. It could be either a widget or a controller. |
||
| 159 | */ |
||
| 160 | public function getOwner() |
||
| 161 | { |
||
| 162 | return $this->_owner; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set views path. This is relative path for view resolving. |
||
| 167 | * By default it's `views` folder. |
||
| 168 | * @param string $path |
||
| 169 | */ |
||
| 170 | public function setViewsPath($path) |
||
| 171 | { |
||
| 172 | $this->_viewsPath = $path; |
||
|
0 ignored issues
–
show
|
|||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Render view with data provided. |
||
| 177 | * View name must not contain `php` extension. |
||
| 178 | * @param string $view |
||
| 179 | * @param mixed[] $data |
||
| 180 | * @param bool $return |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function render($view, $data = null, $return = false) |
||
| 184 | { |
||
| 185 | $viewFile = sprintf('%s/%s/%s.php', $this->_path, $this->_viewsPath, $view); |
||
|
0 ignored issues
–
show
The property
_viewsPath does not exist on object<Maslosoft\MiniView\Widget>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 186 | return $this->_renderInternal($viewFile, $data, $return); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Render file with data provided. |
||
| 191 | * @param string $file |
||
| 192 | * @param mixed[] $data |
||
| 193 | * @param bool $return |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function renderFile($file, $data = null, $return = false) |
||
| 197 | { |
||
| 198 | return $this->_renderInternal($file, $data, $return); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Renders a view file. |
||
| 203 | * This method includes the view file as a PHP script |
||
| 204 | * and captures the display result if required. |
||
| 205 | * @param string $_viewFile_ view file |
||
| 206 | * @param array $_data_ data to be extracted and made available to the view file |
||
| 207 | * @param boolean $_return_ whether the rendering result should be returned as a string |
||
| 208 | * @return string the rendering result. Null if the rendering result is not required. |
||
| 209 | */ |
||
| 210 | private function _renderInternal($_viewFile_, $_data_ = null, $_return_ = false) |
||
| 211 | { |
||
| 212 | // we use special variable names here to avoid conflict when extracting data |
||
| 213 | if (is_array($_data_)) |
||
| 214 | { |
||
| 215 | extract($_data_, EXTR_PREFIX_SAME, 'data'); |
||
| 216 | } |
||
| 217 | else |
||
| 218 | { |
||
| 219 | $data = $_data_; |
||
| 220 | } |
||
| 221 | if ($_return_) |
||
| 222 | { |
||
| 223 | ob_start(); |
||
| 224 | ob_implicit_flush(false); |
||
| 225 | require($_viewFile_); |
||
| 226 | return ob_get_clean(); |
||
| 227 | } |
||
| 228 | else |
||
| 229 | { |
||
| 230 | require($_viewFile_); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Create and run widget. Use this in templates to properly initialize widgets. |
||
| 236 | * This must be called from extending class. |
||
| 237 | * Example: |
||
| 238 | * ```php |
||
| 239 | * echo ProgressBar::widget([ |
||
| 240 | * 'percent' => 40 |
||
| 241 | * ]); |
||
| 242 | * ``` |
||
| 243 | * @param mixed[] $config |
||
| 244 | * @return string HTML widget |
||
| 245 | */ |
||
| 246 | public static function widget($config = []) |
||
| 247 | { |
||
| 248 | ob_start(); |
||
| 249 | ob_implicit_flush(false); |
||
| 250 | /* @var $widget MsWidget */ |
||
| 251 | if (static::class === __CLASS__) |
||
| 252 | { |
||
| 253 | throw new WidgetException(sprintf('Method widget must be called from extending class, not from `%s`', __CLASS__)); |
||
| 254 | } |
||
| 255 | if (is_string($config)) |
||
| 256 | { |
||
| 257 | $class = $config; |
||
| 258 | $config = []; |
||
| 259 | $config['class'] = $class; |
||
| 260 | } |
||
| 261 | else |
||
| 262 | { |
||
| 263 | $config['class'] = static::class; |
||
| 264 | } |
||
| 265 | $widget = EmbeDi::fly()->apply($config); |
||
| 266 | $widget->init(); |
||
| 267 | $out = $widget->run(); |
||
| 268 | |||
| 269 | return ob_get_clean() . $out; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * This is equivalent of calling ::widget() with config from constructor. |
||
| 274 | * Could be used for convenient outputting of simple widgets. |
||
| 275 | * Example: |
||
| 276 | * ```php |
||
| 277 | * echo new Flags([], $this); |
||
| 278 | * echo new Head(['title' => 'foot'], $this); |
||
| 279 | * ``` |
||
| 280 | * @return string HTML output of widget. |
||
| 281 | */ |
||
| 282 | public function __toString() |
||
| 283 | { |
||
| 284 | try |
||
| 285 | { |
||
| 286 | $class = static::class; |
||
| 287 | return $class::widget($this->config); |
||
| 288 | } |
||
| 289 | catch (Exception $e) |
||
| 290 | { |
||
| 291 | return nl2br($e->getTraceAsString()); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | } |
||
| 296 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.