From a0904198f79387ea6d644f8dba0c5b83e07fcc1d Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Wed, 17 Feb 2021 13:34:44 +0100
Subject: [PATCH] feat: quiqqer/package-bricks#123 - Its possible to include
 bricks into the content now

---
 events.xml                |  1 +
 src/QUI/Bricks/Events.php | 55 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/events.xml b/events.xml
index 95423a1..384338e 100644
--- a/events.xml
+++ b/events.xml
@@ -4,4 +4,5 @@
     <event on="onSmartyInit" fire="\QUI\Bricks\Events::onSmartyInit"/>
     <event on="onPackageSetup" fire="\QUI\Bricks\Events::onPackageSetup"/>
     <event on="onDeleteProject" fire="\QUI\Bricks\Events::onDeleteProject"/>
+    <event on="onOutputParseEnd" fire="\QUI\Bricks\Events::onOutputParseEnd"/>
 </events>
diff --git a/src/QUI/Bricks/Events.php b/src/QUI/Bricks/Events.php
index 9542424..13f45d2 100644
--- a/src/QUI/Bricks/Events.php
+++ b/src/QUI/Bricks/Events.php
@@ -301,4 +301,59 @@ public static function onPackageSetup(QUI\Package\Package $Package)
             QUI\System\Log::writeException($Exception);
         }
     }
+
+    //region output filter
+
+    /**
+     * @param $content
+     */
+    public static function onOutputParseEnd(&$content)
+    {
+        if (\strpos($content, '{{brick id=') === false) {
+            return;
+        }
+
+        // search css files
+        $content = \preg_replace_callback(
+            '#{{brick ([^}}]*)}}#',
+            ['QUI\Bricks\Events', "outputParsing"],
+            $content
+        );
+    }
+
+    /**
+     * @param $match
+     */
+    public static function outputParsing($match)
+    {
+        $params = $match[0];
+        $params = \str_replace('{{brick', '', $params);
+        $params = \trim($params, '}}');
+        $params = \trim($params);
+        $params = \explode(' ', $params);
+
+        $attributes = [];
+
+        foreach ($params as $param) {
+            $a = \explode('=', $param);
+
+            $attributes[$a[0]] = $a[1];
+        }
+
+        if (!isset($attributes['id'])) {
+            return $match[0];
+        }
+
+        try {
+            $brickId = (int)$attributes['id'];
+            $Brick   = Manager::init()->getBrickById($brickId);
+
+            return $Brick->create();
+        } catch (\Exception $Exception) {
+        }
+
+        return $match[0];
+    }
+
+    //endregion
 }
-- 
GitLab