Skip to content

Commit 37b6dfc

Browse files
committed
Fix up config for icon
1 parent 2da847c commit 37b6dfc

File tree

12 files changed

+112
-131
lines changed

12 files changed

+112
-131
lines changed

config/Migrations/20200430170235_MigrationToolsTokens.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
use Migrations\AbstractMigration;
34

45
class MigrationToolsTokens extends AbstractMigration {

docs/Helper/Icon.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,18 @@ E.g.
3333
For some Icon classes, there is additional configuration available:
3434
- `namespace`: Some fonts offer different traits (light, bold, round, ...)
3535

36-
E.g.
36+
In this case make sure to use an array instead of just the class string:
3737
```php
3838
'Icon' => [
39-
'config' => [
39+
'sets' => [
4040
'material' => [
41+
'class' => \Tools\View\Icon\MaterialIcon::class,
4142
'namespace' => 'material-symbols-round',
4243
],
4344
...
4445
],
4546
],
4647
```
47-
Make sure to use the same keys here as for the `sets` definition, otherwise the collector won't find your
48-
file configuration here.
4948

5049
## Usage
5150

@@ -69,13 +68,13 @@ echo $this->Html->link(
6968
You can alias them via Configure for more usability:
7069
```php
7170
// In app.php
72-
'Icon' => [
73-
'map' => [
74-
'view' => 'bs:eye',
75-
'translate' => 'bs:translate',
76-
...
77-
],
71+
'Icon' => [
72+
'map' => [
73+
'view' => 'bs:eye',
74+
'translate' => 'bs:translate',
75+
...
7876
],
77+
],
7978

8079
// in the template
8180
echo $this->Icon->render('translate', [], ['title' => 'Translate this']);
@@ -88,27 +87,29 @@ You can get a nested list of all configured and available icons.
8887
For this make sure to set up the path config to the icon meta files as per each collector.
8988
E.g.:
9089
```php
91-
'Icon' => [
92-
// For being able to parse the available icons
93-
'config' => [
94-
'fa' => [
95-
'path' => '/path/to/font-awesome/less/variables.less',
96-
],
97-
'bs' => [
98-
'path' => '/path/to/bootstrap-icons/font/bootstrap-icons.json',
99-
],
100-
'feather' => [
101-
'path' => '/path/to/feather-icons/dist/icons.json',
102-
],
103-
'material' => [
104-
'path' => '/path/to/material-symbols/index.d.ts',
105-
],
90+
'Icon' => [
91+
// For being able to parse the available icons
92+
'sets' => [
93+
'fa' => [
10694
...
95+
'path' => '/path/to/font-awesome/less/variables.less',
10796
],
97+
'bs' => [
98+
...
99+
'path' => '/path/to/bootstrap-icons/font/bootstrap-icons.json',
100+
],
101+
'feather' => [
102+
...
103+
'path' => '/path/to/feather-icons/dist/icons.json',
104+
],
105+
'material' => [
106+
...
107+
'path' => '/path/to/material-symbols/index.d.ts',
108+
],
109+
...
108110
],
111+
],
109112
```
110-
Make sure to use the same keys here as for the `sets` definition, otherwise the collector won't find your
111-
file configuration here.
112113

113114
You can then use this to iterate over all of them for display:
114115
```php

src/View/Icon/AbstractIcon.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Tools\View\Icon;
4+
5+
use Cake\View\StringTemplate;
6+
use RuntimeException;
7+
8+
abstract class AbstractIcon implements IconInterface {
9+
10+
/**
11+
* @var \Cake\View\StringTemplate
12+
*/
13+
protected $template;
14+
15+
/**
16+
* @var array<string, mixed>
17+
*/
18+
protected $config;
19+
20+
/**
21+
* @param array<string, mixed> $config
22+
*/
23+
public function __construct(array $config = []) {
24+
$this->template = new StringTemplate(['icon' => $config['template']]);
25+
$this->config = $config;
26+
}
27+
28+
/**
29+
* @return string
30+
*/
31+
protected function path(): string {
32+
$path = $this->config['path'] ?? null;
33+
if (!$path) {
34+
throw new RuntimeException('You need to define a meta data file path for `' . static::class . '` in order to get icon names.');
35+
}
36+
if (!file_exists($path)) {
37+
throw new RuntimeException('Cannot find meta data file path `' . $path . '` for `' . static::class . '`.');
38+
}
39+
40+
return $path;
41+
}
42+
43+
}

src/View/Icon/BootstrapIcon.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
namespace Tools\View\Icon;
44

5-
use Cake\View\StringTemplate;
65
use Tools\View\Icon\Collector\BootstrapIconCollector;
76

8-
class BootstrapIcon implements IconInterface {
9-
10-
/**
11-
* @var \Cake\View\StringTemplate
12-
*/
13-
protected $template;
7+
class BootstrapIcon extends AbstractIcon {
148

159
/**
1610
* @param array<string, mixed> $config
@@ -20,15 +14,15 @@ public function __construct(array $config = []) {
2014
'template' => '<span class="{{class}}"{{attributes}}></span>',
2115
];
2216

23-
$this->template = new StringTemplate(['icon' => $config['template']]);
17+
parent::__construct($config);
2418
}
2519

2620
/**
27-
* @param string $path
28-
*
2921
* @return array<string>
3022
*/
31-
public function names(string $path): array {
23+
public function names(): array {
24+
$path = $this->path();
25+
3226
return BootstrapIconCollector::collect($path);
3327
}
3428

src/View/Icon/FeatherIcon.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
namespace Tools\View\Icon;
44

5-
use Cake\View\StringTemplate;
65
use Tools\View\Icon\Collector\FeatherIconCollector;
76

8-
class FeatherIcon implements IconInterface {
9-
10-
/**
11-
* @var \Cake\View\StringTemplate
12-
*/
13-
protected $template;
7+
class FeatherIcon extends AbstractIcon {
148

159
/**
1610
* @param array<string, mixed> $config
@@ -20,15 +14,15 @@ public function __construct(array $config = []) {
2014
'template' => '<span data-feather="{{name}}"{{attributes}}></span>',
2115
];
2216

23-
$this->template = new StringTemplate(['icon' => $config['template']]);
17+
parent::__construct($config);
2418
}
2519

2620
/**
27-
* @param string $path
28-
*
2921
* @return array<string>
3022
*/
31-
public function names(string $path): array {
23+
public function names(): array {
24+
$path = $this->path();
25+
3226
return FeatherIconCollector::collect($path);
3327
}
3428

src/View/Icon/FontAwesome4Icon.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
namespace Tools\View\Icon;
44

5-
use Cake\View\StringTemplate;
65
use Tools\View\Icon\Collector\FontAwesome4IconCollector;
76

8-
class FontAwesome4Icon implements IconInterface {
9-
10-
/**
11-
* @var \Cake\View\StringTemplate
12-
*/
13-
protected $template;
7+
class FontAwesome4Icon extends AbstractIcon {
148

159
/**
1610
* @param array<string, mixed> $config
@@ -20,15 +14,15 @@ public function __construct(array $config = []) {
2014
'template' => '<span class="{{class}}"{{attributes}}></span>',
2115
];
2216

23-
$this->template = new StringTemplate(['icon' => $config['template']]);
17+
parent::__construct($config);
2418
}
2519

2620
/**
27-
* @param string $path
28-
*
2921
* @return array<string>
3022
*/
31-
public function names(string $path): array {
23+
public function names(): array {
24+
$path = $this->path();
25+
3226
return FontAwesome4IconCollector::collect($path);
3327
}
3428

src/View/Icon/FontAwesome5Icon.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,28 @@
22

33
namespace Tools\View\Icon;
44

5-
use Cake\View\StringTemplate;
65
use Tools\View\Icon\Collector\FontAwesome5IconCollector;
76

8-
class FontAwesome5Icon implements IconInterface {
9-
10-
/**
11-
* @var \Cake\View\StringTemplate
12-
*/
13-
protected $template;
14-
15-
/**
16-
* @var string
17-
*/
18-
protected $namespace;
7+
class FontAwesome5Icon extends AbstractIcon {
198

209
/**
2110
* @param array<string, mixed> $config
2211
*/
2312
public function __construct(array $config = []) {
2413
$config += [
2514
'template' => '<span class="{{class}}"{{attributes}}></span>',
15+
'namespace' => 'fas',
2616
];
2717

28-
$this->template = new StringTemplate(['icon' => $config['template']]);
29-
$this->namespace = $config['namespace'] ?? 'fas';
18+
parent::__construct($config);
3019
}
3120

3221
/**
33-
* @param string $path
34-
*
3522
* @return array<string>
3623
*/
37-
public function names(string $path): array {
24+
public function names(): array {
25+
$path = $this->path();
26+
3827
return FontAwesome5IconCollector::collect($path);
3928
}
4029

@@ -50,7 +39,7 @@ public function render(string $icon, array $options = [], array $attributes = []
5039
];
5140

5241
$class = [
53-
$this->namespace,
42+
$this->config['namespace'],
5443
];
5544
if (!empty($options['extra'])) {
5645
foreach ($options['extra'] as $i) {

src/View/Icon/FontAwesome6Icon.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,28 @@
22

33
namespace Tools\View\Icon;
44

5-
use Cake\View\StringTemplate;
65
use Tools\View\Icon\Collector\FontAwesome6IconCollector;
76

8-
class FontAwesome6Icon implements IconInterface {
9-
10-
/**
11-
* @var \Cake\View\StringTemplate
12-
*/
13-
protected $template;
14-
15-
/**
16-
* @var string
17-
*/
18-
protected $namespace;
7+
class FontAwesome6Icon extends AbstractIcon {
198

209
/**
2110
* @param array<string, mixed> $config
2211
*/
2312
public function __construct(array $config = []) {
2413
$config += [
2514
'template' => '<span class="{{class}}"{{attributes}}></span>',
15+
'namespace' => 'solid',
2616
];
2717

28-
$this->template = new StringTemplate(['icon' => $config['template']]);
29-
$this->namespace = $config['namespace'] ?? 'solid';
18+
parent::__construct($config);
3019
}
3120

3221
/**
33-
* @param string $path
34-
*
3522
* @return array<string>
3623
*/
37-
public function names(string $path): array {
24+
public function names(): array {
25+
$path = $this->path();
26+
3827
return FontAwesome6IconCollector::collect($path);
3928
}
4029

@@ -49,7 +38,7 @@ public function render(string $icon, array $options = [], array $attributes = []
4938
$formatOptions = $attributes + [
5039
];
5140

52-
$namespace = 'fa-' . $this->namespace;
41+
$namespace = 'fa-' . $this->config['namespace'];
5342

5443
$class = [
5544
$namespace,

src/View/Icon/IconCollection.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,7 @@ public function __construct(array $config = []) {
6666
public function names(): array {
6767
$names = [];
6868
foreach ($this->iconSets as $name => $set) {
69-
$path = $this->_config['config'][$name]['path'] ?? null;
70-
if ($path === null) {
71-
continue;
72-
}
73-
if (!file_exists($path)) {
74-
throw new RuntimeException('Cannot find file path `' . $path . '` for icon set `' . $name . '`');
75-
}
76-
77-
$iconNames = $set->names($path);
69+
$iconNames = $set->names();
7870
$names[$name] = $iconNames;
7971
}
8072

src/View/Icon/IconInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
interface IconInterface {
66

77
/**
8-
* @param string $path
9-
*
108
* @return array<string>
119
*/
12-
public function names(string $path): array;
10+
public function names(): array;
1311

1412
/**
1513
* Icon formatting using the specific engine.

0 commit comments

Comments
 (0)