1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* 50+ icons for payment systems and methods |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/payment-icons |
6
|
|
|
* @package payment-icons |
7
|
|
|
* @license MIT |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\paymenticons\console; |
12
|
|
|
|
13
|
|
|
use hidev\helpers\FileHelper; |
14
|
|
|
use Yii; |
15
|
|
|
|
16
|
|
|
class DoController extends \yii\console\Controller |
17
|
|
|
{ |
18
|
|
|
private $iconsList; |
19
|
|
|
private $uniqueIconsList; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns list of icons. |
23
|
|
|
* @return array file path => name |
24
|
|
|
*/ |
25
|
|
|
public function getUniqueIconsList() |
26
|
|
|
{ |
27
|
|
|
if ($this->uniqueIconsList === null) { |
28
|
|
|
$this->uniqueIconsList = $this->fetchUniqueIconsList(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->uniqueIconsList; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Fetches list of unique icons. |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function fetchUniqueIconsList() |
39
|
|
|
{ |
40
|
|
|
$md5s = []; |
41
|
|
|
foreach ($this->getIconsList() as $path => $name) { |
42
|
|
|
$hash = md5_file($path); |
43
|
|
|
if (in_array($hash, $md5s, true)) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
$md5s[$path] = $hash; |
47
|
|
|
$list[$path] = $name; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $list; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns list of icons. |
55
|
|
|
* @return array file path => name |
56
|
|
|
*/ |
57
|
|
|
public function getIconsList() |
58
|
|
|
{ |
59
|
|
|
if ($this->iconsList === null) { |
60
|
|
|
$this->iconsList = $this->fetchIconsList(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->iconsList; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Scans directory to prepare list of icons. |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function fetchIconsList() |
71
|
|
|
{ |
72
|
|
|
$dir = Yii::getAlias('@hiqdev/paymenticons/assets/png/xs'); |
73
|
|
|
$files = scandir($dir); |
74
|
|
|
$list = []; |
75
|
|
|
foreach ($files as $file) { |
76
|
|
|
if ($file[0] === '.') { |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
$name = pathinfo($file)['filename']; |
80
|
|
|
$list["$dir/$file"] = $name; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $list; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generates CSS file. |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function genCss() |
91
|
|
|
{ |
92
|
|
|
$sizes = [ |
93
|
|
|
'xs' => 'height: 38px; width: 60px;', |
94
|
|
|
'sm' => 'height: 75px; width: 120px;', |
95
|
|
|
'md' => 'height: 240px; width: 150px;', |
96
|
|
|
'lg' => 'height: 480px; width: 300px;', |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$res = '.pi { display: inline-block;height: 38px;width: 60px; }' . PHP_EOL; |
100
|
|
|
|
101
|
|
|
foreach (array_keys($sizes) as $size) { |
102
|
|
|
$res .= ".pi.pi-$size { $sizes[$size] }" . PHP_EOL; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach (array_keys($sizes) as $size) { |
106
|
|
|
foreach ($this->getIconsList() as $name) { |
107
|
|
|
if ($size === 'xs') { |
108
|
|
|
$res .= ".pi.pi-$size.pi-$name, .pi.pi-$name { background: url('../png/$size/$name.png') no-repeat right; }" . PHP_EOL; |
109
|
|
|
} else { |
110
|
|
|
$res .= ".pi.pi-$size.pi-$name { background: url('../png/$size/$name.png') no-repeat right; }" . PHP_EOL; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
$res .= PHP_EOL; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $res; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function actionGenCss() |
120
|
|
|
{ |
121
|
|
|
echo $this->genCss(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function actionWriteCss() |
125
|
|
|
{ |
126
|
|
|
FileHelper::write('@hiqdev/paymenticons/assets/css/payment-icons.css', $this->genCss()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function actionWritePreviews() |
130
|
|
|
{ |
131
|
|
|
$sizes = ['xs', 'sm', 'md', 'lg']; |
132
|
|
|
|
133
|
|
|
foreach ($sizes as $size) { |
134
|
|
|
$str = ''; |
135
|
|
|
foreach ($this->getUniqueIconsList() as $name) { |
136
|
|
|
$str .= "![$name](https://raw.githubusercontent.com/hiqdev/payment-icons/master/src/assets/png/$size/$name.png)\n"; |
137
|
|
|
} |
138
|
|
|
FileHelper::write('@hiqdev/paymenticons/../docs/Preview' . strtoupper($size) . '.md', $str); |
139
|
|
|
if ($size === 'xs') { |
140
|
|
|
$ps = []; |
141
|
|
|
foreach ($sizes as $s) { |
142
|
|
|
$us = strtoupper($s); |
143
|
|
|
$ps[] = "[$us](docs/Preview$us.md)"; |
144
|
|
|
} |
145
|
|
|
$str .= "\n" . implode(' | ', $ps); |
146
|
|
|
FileHelper::write('@hiqdev/paymenticons/../docs/readme/Preview.md', $str); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.