yassinedoghri/php-iconify

此包已被废弃,不再维护。作者建议使用 yassinedoghri/php-icons 包。

基于 iconify API 的 PHP 库,用于从流行的开源图标集中下载和渲染 svg 图标。

v1.0.0 2024-09-28 18:12 UTC

This package is auto-updated.

Last update: 2024-09-29 17:08:41 UTC


README

PHPIcons 🐘 🙂

一个方便的 PHP 库,用于渲染 svg 图标。

Latest Stable Version Total Downloads codecov License PHP Version Require

直接从您的 PHP 文件中访问超过 200,000 个图标,来自超过 150 个开源图标集

感谢 Iconify ❤️

🧩 集成

🚀 入门

1. 通过 composer 安装

composer require yassinedoghri/php-icons

2. 配置

运行以下命令以初始化配置文件

vendor/bin/php-icons init

这将在您项目的根目录中提示您创建一个 php-icons.php 配置文件。有关更多信息,请参阅 配置参考

3. 任何地方使用

3.1. icon(string $iconKey) 方法

在您的视图文件中使用 icon 方法,将图标键字符串({prefix}:{icon})作为参数

<?php

use PHPIcons\PHPIcons;

$phpicons = new PHPIcons('/path/to/config/file.php');

echo $phpicons->icon('material-symbols:bolt');
// <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
//      <path fill="currentColor" d="m8 22l1-7H4l9-13h2l-1 8h6L10 22z"/>
// </svg>

👉 使用 attr()attributes() 方法添加任何属性

echo $phpicons
        ->icon('material-symbols:bolt')
        ->attr('class', 'text-2xl')
        ->attr('style', 'color: yellow;');
// <svg class="text-2xl" style="color: yellow;" […]>…</svg>

echo $phpicons
        ->icon('material-symbols:bolt')
        ->attributes([
          'class' => 'text-2xl',
          'style' => 'color: yellow;'
        ]);
// <svg class="text-2xl" style="color: yellow;" […]>…</svg>

提示

Iconify 的索引 中查找并复制流行开源图标集的图标键。

3.2. 扫描源文件并加载图标

重要

首次定义图标时,将显示占位符(默认为 )。
请确保运行 scan 命令以加载 SVG。

vendor/bin/php-icons scan

⚙️ 配置参考

您的配置文件由 php-icons CLI 工具和 PHPIcons 类使用,应如下所示

<?php

declare(strict_types=1);

use PHPIcons\Config\PHPIconsConfig;

return PHPIconsConfig::configure()
    ->withPaths([
      __DIR__ . '/src'
    ])
    ->withDefaultPrefix('')
    ->withPlaceholder('');

路径

withPaths([])

源文件路径列表。PHP 文件将被解析并扫描以发现您定义的图标。

API 服务器

withAPIHosts([])

Iconify API 服务器,用于下载 svg 图标。首先查询第一个服务器,其余的作为备份使用。

默认为 Iconify 的公共服务器:["https://api.iconify.design","https://api.simplesvg.com", "https://api.unisvg.com"]

本地图标集

withLocalIconSets([])

如果您有自定义图标,php-icons 可以在您的文件系统中本地查找,而不是调用 Iconify API

重要

php-icons 将在您的本地图标集中查找 {name}.svg 文件

接受一个关联数组,图标集前缀作为键,路径作为值。

示例

my-custom-set/
├── heart.svg
├── rocket.svg
├── star.svg
└── user.svg
// in your config file
->withLocalIconSets([
  'custom' => '/path/to/my-custom-set',
])
// ✅ ALL GOOD
echo $phpicons->icon('custom:heart');
echo $phpicons->icon('custom:rocket');
echo $phpicons->icon('custom:star');
echo $phpicons->icon('custom:user');

// ❌ ICONS NOT FOUND
echo $phpicons->icon('custom:banana');
echo $phpicons->icon('custom:key');

默认前缀

withDefaultPrefix('')

当未设置时使用的默认图标集前缀。

示例

使用 material-symbols 作为默认前缀

// this
echo $phpicons->icon('bolt');

// same as this
echo $phpicons->icon('material-symbols:bolt');

默认图标

withDefaultIcon()

当未找到图标时使用的默认图标。

接受一个图标键 {prefix}:{name}。如果没有设置前缀,则使用默认前缀。

默认图标每个集合

withDefaultIconPerSet([])

当图标集中找不到图标时使用的默认图标。

接收一个关联数组,键为图标集前缀,值为默认图标。

占位符

withPlaceholder('�')

当图标未找到或未知时显示的字符串。

默认为(替换字符)。

标识符

withIdentifiers([])

用于在源文件中识别图标键的函数或方法名称。

默认为['icon']

🖥️ 命令行界面命令

> vendor/bin/php-icons

         _             _
   _ __ | |__  _ __   (_) ___ ___  _ __  ___
  | '_ \| '_ \| '_ \  | |/ __/ _ \| '_ \/ __|
  | |_) | | | | |_) | | | (_| (_) | | | \__ \
  | .__/|_| |_| .__/  |_|\___\___/|_| |_|___/
  |_|         |_|

 A convenient PHP library to render svg icons
----------------------------------------------

PHPIcons, version 1.0.0.0-dev

Commands:
*
  init i    Configure PHPIcons interactively
  scan s    Scans source files and loads icons

Run `<command> --help` for specific help

❤️ 致谢

如果没有Iconify团队和设计人员维护的众多开源图标集的出色工作,这将是无法实现的。

灵感来源于astro-iconblade-iconsrector

📜 许可证

代码在MIT许可证下发布。

版权所有 (c) 2024-present, Yassine Doghri (@yassinedoghri).