Icon Loader Bundle

A Symfony bundle that loads, caches and wraps icons.

Installation

Install the bundle via composer:

composer require becklyn/icon-loader

Configuration

Project

You can configure all your icon namespaces in the project configuration:

icon_loader:
    namespaces:
        # all the paths are relative to the project dir
        app: "assets/icon"

        file:
            path: "assets/file-icons"
            class_pattern: "file-icon file-icon-%s"

Reusable Bundle

If you are building a reusable bundle and want to provide icons to the main application, require the package and register your icon namespaces in your bundle class:

use Becklyn\IconLoader\DependencyInjection\RegisterIconNamespacesCompilerPass;

class MyReusableBundle extends Bundle
{
    /**
     * @inheritDoc
     */
    public function build (ContainerBuilder $container)
    {
        $container->addCompilerPass(new RegisterIconNamespacesCompilerPass([
            // just a path, will use the default icon classes
            "my_bundle" => \dirname(__DIR__) . "/icons",

            // customize the class pattern for the wrapper
            "my_bundle_files" => [
                "path" => \dirname(__DIR__) . "/file-icons",
                "class_pattern" => "file-icon file-icon-%s",
            ],
        ]));
    }
}

Usage

Using the bundle means you want to embed an icon somewhere.

In Twig:

{{- icon("app/add") -}}

In Symfony, just get the IconRegistry service:

use Becklyn\IconLoader\Registry\IconRegistry;

class MyController
{
    public function someAction (IconRegistry $iconRegistry) : Response
    {
        // ...
        $addIcon = $iconRegistry->get("app/add");
        // ...
    }
}

Use the console command php bin/console becklyn:icons:list to list all registered namespaces and their icons.

Notable Behavior

  • Missing icons produce an exception if the app is in debug mode and will be an empty string in prod.
  • The same goes for unkown namespaces.
  • If multiple icons with the same name in a single namespace are found, an error is thrown only if these icons have different content.
  • The registry is cached in non-debug mode, so there shouldn’t be any significant performance overhead.