1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web; |
4
|
|
|
|
5
|
|
|
use luya\base\Widget; |
6
|
|
|
use luya\Exception; |
7
|
|
|
use luya\helpers\FileHelper; |
8
|
|
|
use luya\helpers\StringHelper; |
9
|
|
|
use luya\traits\CacheableTrait; |
10
|
|
|
use Yii; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Svg "inserter" |
14
|
|
|
* |
15
|
|
|
* This Widget will insert the contents of a given SVG file at the widgets position |
16
|
|
|
* or create an svg>use element for a given file + symbol. |
17
|
|
|
* |
18
|
|
|
* Inline the SVG contents: |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* <?= Svg::widget([ |
22
|
|
|
* 'folder' => "@webroot/images/svg", |
23
|
|
|
* 'file' => 'logos/logo.svg', |
24
|
|
|
* 'cssClass' => 'additional-class-for-css' |
25
|
|
|
* ]); ?> |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* SVG > Use implementation: |
29
|
|
|
* |
30
|
|
|
* ```php |
31
|
|
|
* <?= Svg::widget([ |
32
|
|
|
* 'symbolMode' => true, |
33
|
|
|
* 'symbolName' => 'example' |
34
|
|
|
* 'file' => 'images/svg-sprite.svg', |
35
|
|
|
* 'cssClass' => 'additional-class-for-css' |
36
|
|
|
* ]); ?> |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* @property string $folder The base folder path. Default is "@webroot/svg" |
40
|
|
|
* |
41
|
|
|
* @author Marc Stampfli <[email protected]> |
42
|
|
|
* @since 1.0.0 |
43
|
|
|
*/ |
44
|
|
|
class Svg extends Widget |
45
|
|
|
{ |
46
|
|
|
use CacheableTrait; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool If symbolMode is true the widget will create a svg > use |
50
|
|
|
*/ |
51
|
|
|
public $symbolMode; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string The name of the symbol that will be referenced |
55
|
|
|
*/ |
56
|
|
|
public $symbolName; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string Relative path to the SVG file based on $folder |
60
|
|
|
*/ |
61
|
|
|
public $file; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string Additional css class to add to the SVG element, use only if the SVG itself has no class |
65
|
|
|
*/ |
66
|
|
|
public $cssClass; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string The base folder path. Default is "@webroot/svg" |
70
|
|
|
*/ |
71
|
|
|
private $_folder; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Setter method for $folder. |
75
|
|
|
* |
76
|
|
|
* @param string $folder Set folder for the given string (parsed trough Yii::getAlias()) |
77
|
|
|
*/ |
78
|
|
|
public function setFolder($folder) |
79
|
|
|
{ |
80
|
|
|
$this->_folder = Yii::getAlias($folder); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Getter method for $folder. |
85
|
|
|
* |
86
|
|
|
* @return string Returns the folder string, if none is set it will return the default value "@webroot/svg" |
87
|
|
|
*/ |
88
|
|
|
public function getFolder() |
89
|
|
|
{ |
90
|
|
|
if ($this->_folder === null) { |
91
|
|
|
$this->_folder = Yii::getAlias("@webroot/svg"); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return rtrim($this->_folder, DIRECTORY_SEPARATOR); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function run() |
101
|
|
|
{ |
102
|
|
|
// Cache generated code |
103
|
|
|
return $this->getOrSetHasCache(['svg', $this->folder, $this->file, $this->cssClass], function () { |
104
|
|
|
|
105
|
|
|
// Check if file ends with .svg, if not add the extension |
106
|
|
|
$svgFile = StringHelper::endsWith($this->file, '.svg') ? $this->file : $this->file . '.svg'; |
107
|
|
|
|
108
|
|
|
if ($this->symbolMode) { |
109
|
|
|
$svgPath = Yii::$app->view->publicHtml . '/' . $svgFile; |
110
|
|
|
|
111
|
|
|
return '<svg class="symbol symbol--' . $this->symbolName . '' . ($this->cssClass ? ' ' . $this->cssClass : '') . '"><use class="symbol__use" xlink:href="' . $svgPath . '#' . $this->symbolName . '" /></svg>'; |
112
|
|
|
} else { |
113
|
|
|
// Build the full svg file path |
114
|
|
|
$svgPath = $this->folder . DIRECTORY_SEPARATOR . $svgFile; |
115
|
|
|
|
116
|
|
|
// Get the svg contents |
117
|
|
|
$content = FileHelper::getFileContent($svgPath); |
118
|
|
|
|
119
|
|
|
// If a cssClass string is given, add it to the <svg> tag |
120
|
|
|
if ($this->cssClass && is_string($this->cssClass)) { |
121
|
|
|
$content = preg_replace('/<svg/', '<svg class="' . $this->cssClass . '"', $content); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($content) { |
|
|
|
|
125
|
|
|
return $content; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
throw new Exception('Unable to access SVG File: ' . $svgPath); |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.