Skip to content

Optimization level 2 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: feat/ux-icons-draft-2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Icons/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.symfony.bundle.yaml export-ignore
/assets/src export-ignore
/assets/test export-ignore
4 changes: 4 additions & 0 deletions src/Icons/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
/composer.lock
/var
/.phpunit.result.cache
3 changes: 3 additions & 0 deletions src/Icons/.symfony.bundle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
branches: ["2.x"]
maintained_branches: ["2.x"]
doc_dir: "doc"
19 changes: 19 additions & 0 deletions src/Icons/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
66 changes: 66 additions & 0 deletions src/Icons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Symfony UX Icons

## Installation

```bash
composer require symfony/ux-icons
```

## Add Icons

No icons are provided by this package. Add your svg icons to the `templates/icons/` directory and commit them.
The name of the file is used as the name of the icon (`name.svg` will be named `name`).

When icons are rendered, any attributes (except `viewBox`) on the file's `<svg>` element will
be removed. This allows you to copy/paste icons from sites like
[heroicons.com](https://heroicons.com/) and not worry about hard-coded attributes interfering with
your design.

## Usage

```twig
{{ ux_icon('user-profile', {class: 'w-4 h-4'}) }} <!-- renders "user-profile.svg" -->

{{ ux_icon('sub-dir:user-profile', {class: 'w-4 h-4'}) }} <!-- renders "sub-dir/user-profile.svg" (sub-directory) -->
```

### HTML Syntax

> [!NOTE]
> `symfony/ux-twig-component` is required to use the HTML syntax.

```html
<twig:UX:Icon name="user-profile" class="w-4 h-4" /> <!-- renders "user-profile.svg" -->

<twig:UX:Icon name="sub-dir:user-profile" class="w-4 h-4" /> <!-- renders "sub-dir/user-profile.svg" (sub-directory) -->
```

> [!TIP]
> The Twig component _name_ can be [configured](#full-default-configuration). For instance, you can change
> it to `Icon` to use `<twig:Icon ...>`.

## Caching

To avoid having to parse icon files on every request, icons are cached.

During container warmup (`cache:warmup` and `cache:clear`), the icon cache is warmed.

> [!NOTE]
> During development, if you change an icon, you will need to clear the cache (`bin/console cache:clear`)
> to see the changes.

## Full Default Configuration

```yaml
ux_icons:
# The local directory where icons are stored.
icon_dir: '%kernel.project_dir%/templates/icons'

# The name of the Twig component to use for rendering icons.
twig_component_name: 'UX:Icon'

# Default attributes to add to all icons.
default_icon_attributes:
# Default:
fill: currentColor
```
52 changes: 52 additions & 0 deletions src/Icons/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "symfony/ux-icons",
"type": "symfony-bundle",
"description": "Twig component to use svg icons in your Symfony app",
"keywords": [
"symfony-ux",
"twig",
"icons"
],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"autoload": {
"psr-4": {
"Symfony\\UX\\Icons\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Symfony\\UX\\Icons\\Tests\\": "tests/"
}
},
"require": {
"php": ">=8.1",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/twig-bundle": "^6.4|7.0"
},
"require-dev": {
"symfony/console": "^6.4|^7.0",
"symfony/phpunit-bridge": "^6.4|^7.0",
"symfony/ux-twig-component": "^2.14",
"zenstruck/console-test": "^1.5"
},
"config": {
"sort-packages": true
},
"conflict": {
"symfony/flex": "<1.13"
},
"extra": {
"thanks": {
"name": "symfony/ux",
"url": "https://github.com/symfony/ux"
}
},
"minimum-stability": "dev"
}
57 changes: 57 additions & 0 deletions src/Icons/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\UX\Icons\IconRenderer;
use Symfony\UX\Icons\Registry\CacheIconRegistry;
use Symfony\UX\Icons\Registry\LocalSvgIconRegistry;
use Symfony\UX\Icons\Twig\UXIconComponent;
use Symfony\UX\Icons\Twig\UXIconComponentListener;
use Symfony\UX\Icons\Twig\UXIconExtension;

return static function (ContainerConfigurator $container): void {
$container->services()
->set('.ux_icons.cache_icon_registry', CacheIconRegistry::class)
->args([
iterator([service('.ux_icons.local_svg_icon_registry')]),
service('cache.system'),
])
->tag('kernel.cache_warmer')

->set('.ux_icons.local_svg_icon_registry', LocalSvgIconRegistry::class)
->args([
abstract_arg('icon_dir'),
])

->alias('.ux_icons.icon_registry', '.ux_icons.cache_icon_registry')

->set('.ux_icons.twig_icon_extension', UXIconExtension::class)
->tag('twig.extension')

->set('.ux_icons.icon_renderer', IconRenderer::class)
->args([
service('.ux_icons.icon_registry'),
])
->tag('twig.runtime')

->set('.ux_icons.twig_component_listener', UXIconComponentListener::class)
->args([
service('.ux_icons.icon_renderer'),
])
->tag('kernel.event_listener', [
'event' => 'Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent',
'method' => 'onPreCreateForRender',
])

->set('.ux_icons.twig_component.icon', UXIconComponent::class)
;
};
32 changes: 32 additions & 0 deletions src/Icons/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="Symfony\UX\Icons\Tests\Fixtures\TestKernel" />
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
</php>

<testsuites>
<testsuite name="symfony/ux-icons Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
49 changes: 49 additions & 0 deletions src/Icons/src/Command/WarmIconCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Icons\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\UX\Icons\Registry\CacheIconRegistry;

/**
* @author Kevin Bond <[email protected]>
*
* @internal
*/
#[AsCommand(
name: 'ux:icons:warm-cache',
description: 'Warm the icon cache',
)]
final class WarmIconCacheCommand extends Command
{
public function __construct(private CacheIconRegistry $registry)
{
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

foreach ($io->progressIterate($this->registry) as $name) {
$this->registry->get($name, refresh: true);
}

$io->success('Icon cache warmed.');

return Command::SUCCESS;
}
}
90 changes: 90 additions & 0 deletions src/Icons/src/DependencyInjection/UXIconsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Icons\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
use Symfony\UX\Icons\Twig\UXIconExtension;

/**
* @author Kevin Bond <[email protected]>
*
* @internal
*/
final class UXIconsExtension extends ConfigurableExtension implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$builder = new TreeBuilder('ux_icons');
$rootNode = $builder->getRootNode();

$rootNode
->children()
->scalarNode('icon_dir')
->info('The local directory where icons are stored.')
->defaultValue('%kernel.project_dir%/templates/icons')
->end()
->scalarNode('twig_component_name')
->info('The name of the Twig component to use for rendering icons.')
->defaultValue('UX:Icon')
->end()
->variableNode('default_icon_attributes')
->info('Default attributes to add to all icons.')
->defaultValue(['fill' => 'currentColor'])
->end()
->end()
;

return $builder;
}

public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
{
return $this;
}

protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void // @phpstan-ignore-line
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services.php');

$container->getDefinition('.ux_icons.local_svg_icon_registry')
->setArguments([
$mergedConfig['icon_dir'],
])
;

$container->getDefinition('.ux_icons.icon_renderer')
->setArgument(1, $mergedConfig['default_icon_attributes'])
;

$container->getDefinition('.ux_icons.twig_icon_extension')
->setArgument(0, $mergedConfig['twig_component_name'])
;

$container->getDefinition('.ux_icons.twig_component_listener')
->setArgument(1, $mergedConfig['twig_component_name'])
;

$container->getDefinition('.ux_icons.twig_component.icon')
->addTag('twig.component', [
'key' => $mergedConfig['twig_component_name'],
'template' => '@UXIcons/Icon.html.twig',
])
;
}
}
Loading