vendor/codefog/contao-haste/library/Haste/Ajax/ReloadHelper.php line 33

Open in your IDE?
  1. <?php
  2. namespace Haste\Ajax;
  3. use Haste\Http\Response\JsonResponse;
  4. class ReloadHelper
  5. {
  6.     const TYPE_CONTENT 'ce';
  7.     const TYPE_MODULE 'fmd';
  8.     /**
  9.      * Listeners
  10.      * @var array
  11.      */
  12.     private static $listeners = [];
  13.     /**
  14.      * Response
  15.      * @var array
  16.      */
  17.     private static $response = [];
  18.     /**
  19.      * Subscribe the listener
  20.      *
  21.      * @param string $id
  22.      * @param array  $events
  23.      */
  24.     public static function subscribe($id, array $events)
  25.     {
  26.         foreach ($events as $event) {
  27.             if (!static::$listeners[$event] || !in_array($id, static::$listeners[$event], true)) {
  28.                 static::$listeners[$event][] = $id;
  29.             }
  30.         }
  31.     }
  32.     /**
  33.      * Store the response if applicable
  34.      *
  35.      * @param string $id
  36.      * @param string $event
  37.      * @param string $buffer
  38.      */
  39.     public static function storeResponse($id$event$buffer)
  40.     {
  41.         if (isset(static::$listeners[$event])
  42.             && !isset(static::$response[$id])
  43.             && in_array($id, static::$listeners[$event], true)
  44.         ) {
  45.             static::$response[$id] = $buffer;
  46.         }
  47.     }
  48.     /**
  49.      * Update the buffer
  50.      *
  51.      * @param string $id
  52.      * @param string $buffer
  53.      * @param bool   $isAjax
  54.      *
  55.      * @return string
  56.      */
  57.     public static function updateBuffer($id$buffer$isAjax false)
  58.     {
  59.         $events = [];
  60.         foreach (static::$listeners as $event => $entries) {
  61.             if (in_array($id$entriestrue)) {
  62.                 $events[] = $event;
  63.             }
  64.         }
  65.         if (count($events) > 0) {
  66.             $buffer = static::addDataAttributes($buffer$id$events$isAjax);
  67.         }
  68.         return $buffer;
  69.     }
  70.     /**
  71.      * Return true if there are listeners
  72.      *
  73.      * @return bool
  74.      */
  75.     public static function hasListeners()
  76.     {
  77.         return count(static::$listeners) > 0;
  78.     }
  79.     /**
  80.      * Get the response
  81.      *
  82.      * @return JsonResponse|null
  83.      */
  84.     public static function getResponse()
  85.     {
  86.         if (count(static::$response) < 1) {
  87.             return null;
  88.         }
  89.         $response = new JsonResponse(static::$response);
  90.         $response->setHeader('Vary''Accept');
  91.         return $response;
  92.     }
  93.     /**
  94.      * Return true if the listener is registered
  95.      *
  96.      * @param string $id
  97.      *
  98.      * @return bool
  99.      */
  100.     public static function isRegistered($id)
  101.     {
  102.         foreach (static::$listeners as $entries) {
  103.             if (in_array($id$entriestrue)) {
  104.                 return true;
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.     /**
  110.      * Get the events
  111.      *
  112.      * @param string $id
  113.      *
  114.      * @return array
  115.      */
  116.     public static function getEvents($id)
  117.     {
  118.         $events = [];
  119.         foreach (static::$listeners as $event => $entries) {
  120.             if (in_array($id$entriestrue)) {
  121.                 $events[] = $event;
  122.             }
  123.         }
  124.         return array_unique($events);
  125.     }
  126.     /**
  127.      * Get the unique ID
  128.      *
  129.      * @param string $type
  130.      * @param int    $id
  131.      *
  132.      * @return string
  133.      */
  134.     public static function getUniqid($type$id)
  135.     {
  136.         if (!in_array($type, [self::TYPE_CONTENTself::TYPE_MODULE], true)) {
  137.             throw new \InvalidArgumentException(sprintf('The type "%s" is not supported'$type));
  138.         }
  139.         return $type.$id;
  140.     }
  141.     /**
  142.      * Add the data attributes
  143.      *
  144.      * @param string $buffer
  145.      * @param string $id
  146.      * @param array  $events
  147.      * @param bool   $isAjax
  148.      *
  149.      * @return string
  150.      */
  151.     private static function addDataAttributes($buffer$id, array $events$isAjax false)
  152.     {
  153.         // Merge the data attributes if already present
  154.         preg_replace_callback(
  155.             '/\s?data-haste-ajax-id="[^"]*" data-haste-ajax-listeners="([^"]*)"/',
  156.             function ($matches) use (&$events) {
  157.                 $events array_merge($eventstrimsplit(' '$matches[1]));
  158.                 return '';
  159.             },
  160.             $buffer
  161.         );
  162.         // Remove the HTML comments on AJAX request so they don't appear doubled in the DOM
  163.         if ($isAjax) {
  164.             $buffer preg_replace('/<!--(.*)-->/'''$buffer);
  165.         }
  166.         // Add the necessary attributes to the first wrapping element
  167.         $buffer preg_replace(
  168.             '/<([^>!]+)>/',
  169.             sprintf(
  170.                 '<$1 data-haste-ajax-id="%s" data-haste-ajax-listeners="%s">',
  171.                 $id,
  172.                 implode(' 'array_unique($events))
  173.             ),
  174.             $buffer,
  175.             1
  176.         );
  177.         // Trim the buffer to avoid JS break
  178.         return trim($buffer);
  179.     }
  180. }