Skip to content

Commit 9def088

Browse files
committed
Fix task
1 parent 85605ab commit 9def088

File tree

7 files changed

+1643
-2684
lines changed

7 files changed

+1643
-2684
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"require": {
2525
"php": ">=7.2",
26-
"dereuromark/cakephp-ide-helper": "^1.2.0"
26+
"dereuromark/cakephp-ide-helper": "^1.3.0"
2727
},
2828
"require-dev": {
2929
"dereuromark/cakephp-tools": "^2.0.1",

src/Tools/Generator/Task/FormatIconFontAwesome4Task.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cake\View\View;
88
use IdeHelper\Generator\Directive\ExpectedArguments;
99
use IdeHelper\Generator\Task\TaskInterface;
10+
use RuntimeException;
1011
use Tools\View\Helper\FormatHelper;
1112

1213
/**
@@ -28,6 +29,9 @@ public function __construct(?string $fontPath = null) {
2829
if ($fontPath === null) {
2930
$fontPath = (string)Configure::readOrFail('Format.fontPath');
3031
}
32+
if ($fontPath && !file_exists($fontPath)) {
33+
throw new RuntimeException('File not found: ' . $fontPath);
34+
}
3135

3236
$this->fontPath = $fontPath;
3337
}
@@ -54,28 +58,35 @@ public function collect(): array {
5458
}
5559

5660
/**
57-
* Fontawesome v4 using fontawesome-webfont.svg file.
61+
* Fontawesome v4 using variables.scss or variables.less file.
5862
*
5963
* Set your custom file path in your app.php:
60-
* 'fontPath' => ROOT . '/webroot/css/fonts/fontawesome-webfont.svg'
64+
* 'fontPath' => ROOT . '/node_modules/.../scss/variables.scss'
6165
*
6266
* @return string[]
6367
*/
6468
protected function collectIcons(): array {
6569
$helper = new FormatHelper(new View());
6670
$configured = $helper->getConfig('fontIcons');
71+
$configured = array_keys($configured);
6772

6873
$fontFile = $this->fontPath;
6974
$icons = [];
7075
if ($fontFile && file_exists($fontFile)) {
7176
$content = file_get_contents($fontFile);
72-
preg_match_all('/glyph-name="([a-z][^"]+)"/', $content, $matches);
73-
$icons = $matches[1];
74-
foreach ($icons as $key => $icon) {
75-
if (strpos($icon, 'uni') === 0 || preg_match('#[a-z]\d[a-z]\d#i', $icon)) {
76-
unset($icons[$key]);
77-
}
77+
$ext = pathinfo($fontFile, PATHINFO_EXTENSION);
78+
switch ($ext) {
79+
case 'less':
80+
preg_match_all('/@fa-var-([0-9a-z-]+):/', $content, $matches);
81+
break;
82+
case 'scss':
83+
preg_match_all('/\$fa-var-([0-9a-z-]+):/', $content, $matches);
84+
break;
85+
default:
86+
throw new RuntimeException('Format not supported: ' . $ext);
7887
}
88+
89+
$icons = !empty($matches[1]) ? $matches[1] : [];
7990
}
8091

8192
$icons = array_merge($configured, $icons);

src/Tools/Generator/Task/FormatIconFontAwesome5Task.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cake\View\View;
88
use IdeHelper\Generator\Directive\ExpectedArguments;
99
use IdeHelper\Generator\Task\TaskInterface;
10+
use RuntimeException;
1011
use Tools\View\Helper\FormatHelper;
1112

1213
class FormatIconFontAwesome5Task implements TaskInterface {
@@ -25,6 +26,9 @@ public function __construct(?string $fontPath = null) {
2526
if ($fontPath === null) {
2627
$fontPath = (string)Configure::readOrFail('Format.fontPath');
2728
}
29+
if ($fontPath && !file_exists($fontPath)) {
30+
throw new RuntimeException('File not found: ' . $fontPath);
31+
}
2832

2933
$this->fontPath = $fontPath;
3034
}
@@ -62,6 +66,7 @@ public function collect(): array {
6266
protected function collectIcons(): array {
6367
$helper = new FormatHelper(new View());
6468
$configured = $helper->getConfig('fontIcons');
69+
$configured = array_keys($configured);
6570

6671
$fontFile = $this->fontPath;
6772
$icons = [];

tests/TestCase/Tools/Generator/Task/FormatIconFontAwesome4TaskTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ public function testIcon(): void {
3535
}
3636

3737
/**
38+
* @dataProvider extensions
39+
*
40+
* @param string $extension
41+
*
3842
* @return void
3943
*/
40-
public function testCollect(): void {
41-
$path = TEST_FILES . 'Tools' . DS . 'fa4' . DS . 'fontawesome-webfont.svg';
44+
public function testCollect(string $extension): void {
45+
$path = TEST_FILES . 'Tools' . DS . 'fa4' . DS . 'variables.' . $extension;
4246
$task = new FormatIconFontAwesome4Task($path);
4347

4448
$result = $task->collect();
@@ -55,8 +59,18 @@ public function testCollect(): void {
5559
return (string)$className;
5660
}, $list);
5761

58-
$this->assertTrue(count($list) > 380);
59-
$this->assertSame('\'smile\'', $list['smile']);
62+
$this->assertTrue(count($list) > 780, 'count of ' . count($list));
63+
$this->assertSame('\'smile-o\'', $list['smile-o']);
64+
}
65+
66+
/**
67+
* @return array
68+
*/
69+
public function extensions(): array {
70+
return [
71+
'scss' => ['scss'],
72+
'less' => ['less'],
73+
];
6074
}
6175

6276
}

0 commit comments

Comments
 (0)