/**
  * Encode OmnivoreLayers so that the internal used contao.loadLayer method is used.
  *
  * @param EncodeValueEvent $event The subscribed event.
  *
  * @return void
  * @throws EncodeValueFailed If encoding failed.
  */
 public function loadLayer(EncodeValueEvent $event)
 {
     $value = $event->getValue();
     $encoder = $event->getEncoder();
     $template = 'L.contao.load(%s, %s, %s, %s, map);';
     if ($value instanceof OmnivoreLayer) {
         $url = $value->getUrl();
         if ($url instanceof RequestUrl) {
             $url = $url->getHash();
         } elseif (strpos($url, '/') !== false) {
             // Slash found, not a Contao leaflet hash, do not replace encoding.
             return;
         }
         if ($value->getCustomLayer()) {
             $ref = $encoder->encodeReference($value->getCustomLayer());
         } else {
             $template = $encoder->encodeReference($value) . ' = ' . $template;
             $ref = 'null';
         }
         $event->addLine(sprintf($template, $encoder->encodeValue($url), $encoder->encodeValue(strtolower(str_replace('Omnivore.', '', $value->getType()))), $encoder->encodeArray($value->getOptions(), JSON_FORCE_OBJECT), $ref));
         foreach ($value->getMethodCalls() as $call) {
             $event->addLine($call->encode($encoder, Flags::CLOSE_STATEMENT));
         }
     }
 }
Exemple #2
0
 /**
  * Collect all libraries.
  *
  * @param EncodeValueEvent $event The subscribed event.
  *
  * @return void
  */
 public function collect(EncodeValueEvent $event)
 {
     $value = $event->getValue();
     if ($value instanceof Definition) {
         foreach ($value->getRequiredLibraries() as $library) {
             if (isset($this->libraries[$library])) {
                 continue;
             }
             if (isset($this->stylesheets[$library])) {
                 foreach ($this->stylesheets[$library] as $asset) {
                     $this->assets->addStylesheet($asset[0], $asset[1]);
                 }
             }
             if (isset($this->javascripts[$library])) {
                 foreach ($this->javascripts[$library] as $asset) {
                     $this->assets->addJavascript($asset[0], $asset[1]);
                 }
             }
             $this->libraries[$library] = true;
         }
     }
 }
 /**
  * Encode method calls.
  *
  * @param Definition       $definition The current definition.
  * @param Encoder          $encoder    The encoder.
  * @param EncodeValueEvent $event      The event.
  *
  * @return void
  */
 private function handleMethodCalls(Definition $definition, Encoder $encoder, EncodeValueEvent $event)
 {
     $hash = spl_object_hash($definition);
     if (!isset(static::$encodedMethods[$hash])) {
         static::$encodedMethods[$hash] = true;
         foreach ($definition->getMethodCalls() as $method) {
             $event->addLine($method->encode($encoder, Flags::CLOSE_STATEMENT));
         }
     }
 }