Skip to content
Snippets Groups Projects
Commit 189c5d5c authored by David Galeano's avatar David Galeano
Browse files

Resolve #3453127 "Use asinc requests"

parent 6a9a8413
No related branches found
No related tags found
1 merge request!14Resolve #3453127 "Use asinc requests"
Pipeline #199975 passed
......@@ -8,4 +8,4 @@ ckeditor5.plugin.iconify_icons_icons:
sequence:
type: string
label: 'Collection ID'
maxlength: 128
\ No newline at end of file
maxlength: 128
......@@ -82,11 +82,9 @@ class AutocompleteController extends ControllerBase {
// Load the icon data, so we can check for a valid icon.
$icon_data = $this->iconify->getIcons($typed_string, $selected_collections);
// Check each icon to see if it starts with the typed string.
foreach ($icon_data as $icon) {
// Extract collection and name from $icon.
$icons_svg = $this->iconify->generateSvgIcons($icon_data, $parameters);
foreach ($icons_svg as $icon => $icon_svg) {
[$collection, $icon_name] = explode(':', $icon, 2);
$icon_svg = $this->iconify->generateSvg($collection, $icon_name, $parameters);
$results[] = [
'value' => $icon_name . ' (' . $collection . ')',
'label' => $this->getRenderableLabel(
......
......@@ -96,7 +96,7 @@ class Settings extends ConfigFormBase {
'#type' => 'checkboxes',
'#options' => $this->getOptions(),
'#default_value' => $config->get('collections') ?? [],
'#description' => $this->t('Select the collections which are going to provide the set of icons. See @collectionIconsLink list.', [
'#description' => $this->t('Select the collections which are going to provide the set of icons. See @collectionIconsLink list. Leave empty to select all.', [
'@collectionIconsLink' => Link::fromTextAndUrl($this->t('the Iconify icon collections'), Url::fromUri('https://icon-sets.iconify.design/', [
'attributes' => [
'target' => '_blank',
......
......@@ -4,6 +4,7 @@ namespace Drupal\iconify_icons;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\Utils;
use Psr\Log\LoggerInterface;
/**
......@@ -142,7 +143,7 @@ class IconifyService implements IconifyServiceInterface {
/**
* {@inheritdoc}
*/
public function generateSvg(string $collection, string $icon_name, array $parameters = []): string {
public function generateSvgIcon(string $collection, string $icon_name, array $parameters = []): string {
$parameters = $this->setDefaultParameters($parameters);
if ($this->cache->checkIcon($collection, $icon_name, $parameters)) {
return $this->cache->getIcon($collection, $icon_name, $parameters);
......@@ -170,6 +171,53 @@ class IconifyService implements IconifyServiceInterface {
$this->logger->error('JSON decode error: {message}', ['message' => $e->getMessage()]);
return '';
}
}
/**
* {@inheritdoc}
*/
public function generateSvgIcons(array $icons, array $parameters = []): array {
$parameters = $this->setDefaultParameters($parameters);
$results = [];
$promises = [];
foreach ($icons as $icon) {
// Extract collection and name from $icon.
[$collection, $icon_name] = explode(':', $icon, 2);
if ($this->cache->checkIcon($collection, $icon_name, $parameters)) {
$results[$icon] = $this->cache->getIcon($collection, $icon_name, $parameters);
}
else {
$promises[$icon] = $this->httpClient->requestAsync('GET', sprintf($this::DESIGN_DOWNLOAD_API_ENDPOINT, $collection, $icon_name), [
'query' => $parameters,
]);
}
}
try {
// Wait for the all asynchronous request to complete.
$responses = Utils::unwrap($promises);
foreach ($responses as $icon => $response) {
[$collection, $icon_name] = explode(':', $icon, 2);
try {
$icon_svg = $response->getBody()->getContents();
$this->cache->setIcon($collection, $icon_name, $icon_svg, $parameters);
$results[$icon] = $icon_svg;
}
catch (RequestException $e) {
// Handle request exception (e.g., log error, return empty array)
$this->logger->error('Error generating svg icon: ' . $e->getMessage());
$results[$icon] = '';
}
}
}
catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
catch (\Throwable $e) {
}
return $results;
}
/**
......
......@@ -66,7 +66,23 @@ interface IconifyServiceInterface {
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
*/
public function generateSvg(string $collection, string $icon_name, array $parameters = []): string;
public function generateSvgIcon(string $collection, string $icon_name, array $parameters = []): string;
/**
* Generates SVG icons.
*
* @param array $icons
* All the icons that belongs to the same collection.
* @param array $parameters
* The icon parameters (optional).
*
* @return array
* The svg icons array.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
*/
public function generateSvgIcons(array $icons, array $parameters = []): array;
/**
* Gets icon source.
......
......@@ -31,7 +31,7 @@ interface IconsCacheInterface {
* The icon name.
* @param string $icon
* The svg or '' if it doesn't exist yet.
* @param string $parameters
* @param array $parameters
* Query options.
*
* @return bool
......
......@@ -88,7 +88,7 @@ class IconifyIcons extends CKEditor5PluginDefault implements CKEditor5PluginConf
'#type' => 'checkboxes',
'#options' => $this->getOptions(),
'#default_value' => array_keys($this->configuration['collections'] ?? []),
'#description' => $this->t('Select the collections which are going to provide the set of icons. See @collectionIconsLink list.', [
'#description' => $this->t('Select the collections which are going to provide the set of icons. See @collectionIconsLink list. Leave empty to select all.', [
'@collectionIconsLink' => Link::fromTextAndUrl($this->t('the Iconify icon collections'), Url::fromUri('https://icon-sets.iconify.design/', [
'attributes' => [
'target' => '_blank',
......
......@@ -105,7 +105,7 @@ class IconifyIconWidget extends WidgetBase {
'#type' => 'checkboxes',
'#options' => $this->getOptions(),
'#default_value' => $this->getSetting('collections'),
'#description' => $this->t('Select the collections which are going to provide the set of icons. See @collectionIconsLink list.', [
'#description' => $this->t('Select the collections which are going to provide the set of icons. See @collectionIconsLink list. Leave empty to select all.', [
'@collectionIconsLink' => Link::fromTextAndUrl($this->t('the Iconify icon collections'), Url::fromUri('https://icon-sets.iconify.design/', [
'attributes' => [
'target' => '_blank',
......@@ -156,7 +156,7 @@ class IconifyIconWidget extends WidgetBase {
// Extract the icon name and collection name using a regular expression.
if ($iconDetails = $this->extractIconDetails($icon)) {
[$icon_name, $icon_collection] = $iconDetails;
$icon_svg = $this->iconify->generateSvg($icon_collection, $icon_name, $settings);
$icon_svg = $this->iconify->generateSvgIcon($icon_collection, $icon_name, $settings);
}
$element['icon'] = [
......@@ -194,7 +194,7 @@ class IconifyIconWidget extends WidgetBase {
'#maxlength' => 8,
'#min' => 1,
'#max' => 99999,
'#field_suffix' => t('pixels'),
'#field_suffix' => $this->t('pixels'),
'#default_value' => $settings['width'] ?? 50,
];
......@@ -206,7 +206,7 @@ class IconifyIconWidget extends WidgetBase {
'#maxlength' => 8,
'#min' => 1,
'#max' => 99999,
'#field_suffix' => t('pixels'),
'#field_suffix' => $this->t('pixels'),
'#default_value' => $settings['height'] ?? 50,
];
......
......@@ -58,7 +58,7 @@ class IconifyIcon extends AbstractExtension {
if (preg_match('/(.+\\s)\\(([^\\)]+)\\)/', $icon, $matches)) {
$icon_name = trim($matches[1]);
$collection = trim($matches[2]);
return $this->iconify->generateSvg($collection, $icon_name, $settings ?? []);
return $this->iconify->generateSvgIcon($collection, $icon_name, $settings ?? []);
}
return '';
......
......@@ -129,6 +129,7 @@ class IconifyServiceTest extends UnitTestCase {
$ok_response = 'SVG content';
$collection = 'collection_1';
$icon_name = 'icon_1';
$icons = ["$collection:$icon_name"];
$parameters = [];
$stream = $this->createMock(StreamInterface::class);
......@@ -154,8 +155,8 @@ class IconifyServiceTest extends UnitTestCase {
->with('GET', sprintf($this->iconifyService::DESIGN_DOWNLOAD_API_ENDPOINT, $collection, $icon_name))
->willReturn($promise);
$assert = $this->iconifyService->generateSvg($collection, $icon_name, $parameters);
$this->assertEquals($ok_response, $assert);
$assert = $this->iconifyService->generateSvgIcons($icons, $parameters);
$this->assertEquals(["$collection:$icon_name" => $ok_response], $assert);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment