Commit 9b22df80 authored by Italo Mairo's avatar Italo Mairo Committed by Italo Mairo
Browse files

Issue #3312282 by prudloff, itamair: Cache icon size in setFeatureIconSizesIfEmptyOrInvalid()

parent 426abc40
Loading
Loading
Loading
Loading
+60 −34
Original line number Diff line number Diff line
@@ -62,6 +62,13 @@ class LeafletService {
   */
  protected $requestStack;

  /**
   * Static cache for icon sizes.
   *
   * @var array
   */
  protected $iconSizes = [];

  /**
   * Creates an absolute web-accessible URL string.
   *
@@ -438,11 +445,17 @@ class LeafletService {
   *   The feature.
   */
  public function setFeatureIconSizesIfEmptyOrInvalid(array &$feature) {
    $icon_url = !empty($feature["icon"]["iconUrl"]) ? $this->generateAbsoluteString((string) $feature["icon"]["iconUrl"]) : NULL;
    $icon_url = $feature["icon"]["iconUrl"] ?? NULL;
    if (isset($icon_url) && isset($feature["icon"]["iconSize"])
      && (empty(intval($feature["icon"]["iconSize"]["x"])) && empty(intval($feature["icon"]["iconSize"]["y"])))
      && (!empty($feature["icon"]["iconUrl"]) && $this->fileExists($icon_url))) {
      && (!empty($feature["icon"]["iconUrl"]))) {

      // Use the cached IconSize is present for this Icon Url.
      if (isset($this->iconSizes[$feature["icon"]["iconUrl"]])) {
        $feature["icon"]["iconSize"]["x"] = $this->iconSizes[$feature["icon"]["iconUrl"]]["x"];
        $feature["icon"]["iconSize"]["y"] = $this->iconSizes[$feature["icon"]["iconUrl"]]["y"];
      }
      elseif ($this->fileExists($feature["icon"]["iconUrl"])) {
        $file_parts = pathinfo($icon_url);
        switch ($file_parts['extension']) {
          case "svg":
@@ -459,13 +472,22 @@ class LeafletService {
              $feature["icon"]["iconSize"]["y"] = $iconSize[1];
            }
        }
        // Cache the IconSize, so we don't fetch the same icon multiple times.
        $this->iconSizes[$feature["icon"]["iconUrl"]] = $feature["icon"]["iconSize"];
      }
    }

    $shadow_url = !empty($feature["icon"]["shadowUrl"]) ? $this->generateAbsoluteString($feature["icon"]["shadowUrl"]) : NULL;
    $shadow_url = $feature["icon"]["shadowUrl"] ?? NULL;
    if (isset($shadow_url) && isset($feature["icon"]["shadowSize"])
      && (empty(intval($feature["icon"]["shadowSize"]["x"])) && empty(intval($feature["icon"]["shadowSize"]["y"])))
      && (!empty($feature["icon"]["shadowUrl"]) && $this->fileExists($shadow_url))) {
      && (!empty($feature["icon"]["shadowUrl"]))) {

      // Use the cached Shadow IconSize is present for this Icon Url.
      if (isset($this->iconSizes[$feature["icon"]["shadowUrl"]])) {
        $feature["icon"]["shadowSize"]["x"] = $this->iconSizes[$feature["icon"]["shadowUrl"]]["x"];
        $feature["icon"]["shadowSize"]["y"] = $this->iconSizes[$feature["icon"]["shadowUrl"]]["y"];
      }
      elseif ($this->fileExists($feature["icon"]["shadowUrl"])) {
        $file_parts = pathinfo($shadow_url);
        switch ($file_parts['extension']) {
          case "svg":
@@ -482,6 +504,10 @@ class LeafletService {
              $feature["icon"]["shadowSize"]["y"] = $shadowSize[1];
            }
        }
        // Cache the Shadow IconSize, so we don't fetch the same icon multiple
        // times.
        $this->iconSizes[$feature["icon"]["shadowUrl"]] = $feature["icon"]["shadowSize"];
      }
    }
  }