Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
55 / 55 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
FontRepoCompiler | |
100.00% |
55 / 55 |
|
100.00% |
7 / 7 |
23 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getRepository | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
getFilesFromPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parseFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLanguages | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
appendLanguages | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
getFontInfo | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
10 |
1 | <?php |
2 | |
3 | namespace UniversalLanguageSelector; |
4 | |
5 | use OutputPage; |
6 | |
7 | /** |
8 | * This class parses font specification ini files to a central list. |
9 | * |
10 | * @author Niklas Laxström |
11 | * @since 2016.04 |
12 | */ |
13 | class FontRepoCompiler { |
14 | /** @var string */ |
15 | protected $fsPath; |
16 | /** @var string */ |
17 | protected $webPath; |
18 | |
19 | /** |
20 | * @param string $fsPath |
21 | * @param string $webPath |
22 | */ |
23 | public function __construct( $fsPath, $webPath ) { |
24 | $this->fsPath = $fsPath; |
25 | $this->webPath = $webPath; |
26 | } |
27 | |
28 | /** |
29 | * @return array |
30 | */ |
31 | public function getRepository() { |
32 | $files = $this->getFilesFromPath( $this->fsPath ); |
33 | |
34 | $fonts = []; |
35 | $languages = []; |
36 | |
37 | foreach ( $files as $file ) { |
38 | $conf = $this->parseFile( $file ); |
39 | $fontPath = dirname( $file ); |
40 | |
41 | foreach ( $conf as $fontname => $font ) { |
42 | $fontLanguages = $this->getLanguages( $font ); |
43 | $this->appendLanguages( $languages, $fontLanguages, $fontname ); |
44 | $fonts[$fontname] = $this->getFontInfo( $font, $fontPath ); |
45 | } |
46 | } |
47 | |
48 | ksort( $languages ); |
49 | ksort( $fonts ); |
50 | |
51 | return [ |
52 | 'base' => $this->webPath, |
53 | 'languages' => $languages, |
54 | 'fonts' => $fonts |
55 | ]; |
56 | } |
57 | |
58 | /** |
59 | * @param string $fspath |
60 | * @return array|false |
61 | */ |
62 | public function getFilesFromPath( $fspath ) { |
63 | return glob( "$fspath/*/font.ini" ); |
64 | } |
65 | |
66 | /** |
67 | * @param string $filepath |
68 | * @return array|false |
69 | */ |
70 | public function parseFile( $filepath ) { |
71 | return parse_ini_file( $filepath, true ); |
72 | } |
73 | |
74 | /** |
75 | * @param array $font |
76 | * @return array |
77 | */ |
78 | public function getLanguages( array $font ) { |
79 | if ( !isset( $font['languages'] ) ) { |
80 | return []; |
81 | } |
82 | |
83 | $languages = explode( ',', $font['languages'] ); |
84 | $languages = array_map( 'trim', $languages ); |
85 | |
86 | return $languages; |
87 | } |
88 | |
89 | /** |
90 | * @param array &$languages |
91 | * @param array $fontLanguages |
92 | * @param string $fontname |
93 | */ |
94 | public function appendLanguages( &$languages, $fontLanguages, $fontname ) { |
95 | foreach ( $fontLanguages as $rcode ) { |
96 | $code = str_replace( '*', '', $rcode ); |
97 | |
98 | if ( !isset( $languages[$code] ) ) { |
99 | $languages[$code] = [ 'system' ]; |
100 | } |
101 | |
102 | if ( str_contains( $rcode, '*' ) ) { |
103 | if ( $languages[$code][0] === 'system' ) { |
104 | unset( $languages[$code][0] ); |
105 | } |
106 | array_unshift( $languages[$code], $fontname ); |
107 | } else { |
108 | $languages[$code][] = $fontname; |
109 | } |
110 | } |
111 | } |
112 | |
113 | /** |
114 | * @param array $font |
115 | * @param string $fontpath |
116 | * @return array |
117 | */ |
118 | public function getFontInfo( $font, $fontpath ) { |
119 | $info = []; |
120 | $fontdir = basename( $fontpath ); |
121 | |
122 | if ( isset( $font['fontweight'] ) ) { |
123 | $info['fontweight'] = $font['fontweight']; |
124 | } |
125 | |
126 | if ( isset( $font['fontstyle'] ) ) { |
127 | $info['fontstyle'] = $font['fontstyle']; |
128 | } |
129 | |
130 | foreach ( [ 'woff', 'woff2' ] as $format ) { |
131 | if ( isset( $font[$format] ) ) { |
132 | $info[$format] = OutputPage::transformFilePath( $fontdir, $fontpath, $font[$format] ); |
133 | } |
134 | } |
135 | |
136 | // If font formats are not explicitly defined, scan the directory. |
137 | if ( !isset( $info['woff'] ) ) { |
138 | foreach ( glob( "$fontpath/*.{woff,woff2}", GLOB_BRACE ) as $fontfile ) { |
139 | $type = substr( $fontfile, strrpos( $fontfile, '.' ) + 1 ); |
140 | $info[$type] = OutputPage::transformFilePath( $fontdir, $fontpath, basename( $fontfile ) ); |
141 | } |
142 | } |
143 | |
144 | // Font variants |
145 | if ( isset( $font['bold'] ) ) { |
146 | $info['variants']['bold'] = $font['bold']; |
147 | } |
148 | |
149 | if ( isset( $font['bolditalic'] ) ) { |
150 | $info['variants']['bolditalic'] = $font['bolditalic']; |
151 | } |
152 | |
153 | if ( isset( $font['italic'] ) ) { |
154 | $info['variants']['italic'] = $font['italic']; |
155 | } |
156 | |
157 | return $info; |
158 | } |
159 | } |