From eb182862e21f4a5ec7df899af6e6aac57b43d6f6 Mon Sep 17 00:00:00 2001
From: Michael Rook <michael@rook.net.au>
Date: Thu, 5 Aug 2021 15:12:29 +1000
Subject: [PATCH] Add context support.

---
 README.md              | 12 +++++----
 src/FrostyFetcher.php  | 61 +++++++++++++++++++++++++++---------------
 src/Tags/FrostyTag.php |  3 ++-
 3 files changed, 49 insertions(+), 27 deletions(-)

diff --git a/README.md b/README.md
index 7dcb021..37fef3f 100644
--- a/README.md
+++ b/README.md
@@ -57,19 +57,20 @@ The `\HandmadeWeb\Frosty\FrostyFetcher` class provides a way to easily generate
 
 You can do this by newing up the class.
 ```php
-new FrostyFetcher(string $content = null, $endpoint = null, bool $shouldUseAntlers = false)
+new FrostyFetcher(string $content = null, ?Collection $context = null, string $endpoint = null, bool $shouldUseAntlers = false)
 ```
 Or by using the make method.
 ```php
-FrostyFetcher::make(string $content = null, $endpoint = null, bool $shouldUseAntlers = false)
+FrostyFetcher::make(string $content = null, ?Collection $context = null, string $endpoint = null, bool $shouldUseAntlers = false)
 ```
 
 All parameters are optional on init and can be individually defined later on.
 ```php
 $frosty = FrostyFetcher::make();
-$frosty->withContent($content);
-$frosty->withEndpoint($endpoint);
-$frosty->withAntlers(false); // or true
+$frosty->withContent($content); // string
+$frosty->withContext($context); // \Statamic\Tags\Context or \Illuminate\Support\Collection (Used to provide Cascaded variables to the content)
+$frosty->withEndpoint($endpoint); // string
+$frosty->withAntlers(false); // bool
 ```
 When using the tag, you'll specify if the endpoint is a url or a route, however when using the class directly, the endpoint is assumed to be a url string, if you wish to pass a route to it instead, then you are welcome to do that.
 
@@ -82,6 +83,7 @@ When you are ready to output the content, then you may call the render method.
 ```php
 FrostyFetcher::make()
     ->withContent($content)
+    ->withContext($context)
     ->withEndpoint($endpoint)
     ->withAntlers(false)
     ->render();
diff --git a/src/FrostyFetcher.php b/src/FrostyFetcher.php
index d542daf..255f82c 100644
--- a/src/FrostyFetcher.php
+++ b/src/FrostyFetcher.php
@@ -2,26 +2,59 @@
 
 namespace HandmadeWeb\Frosty;
 
+use Illuminate\Support\Collection;
 use Statamic\Facades\Antlers;
 
 class FrostyFetcher
 {
     protected $content;
+    protected $context;
     protected $endpoint;
     protected $shouldUseAntlers;
 
-    public static function make(string $content = null, string $endpoint = null, bool $shouldUseAntlers = false): static
+    public static function make(string $content = null, ?Collection $context = null, string $endpoint = null, bool $shouldUseAntlers = false): static
     {
-        return new static($content, $endpoint, $shouldUseAntlers);
+        return new static($content, $context, $endpoint, $shouldUseAntlers);
     }
 
-    public function __construct(string $content = null, string $endpoint = null, bool $shouldUseAntlers = false)
+    public function __construct(string $content = null, ?Collection $context = null, string $endpoint = null, bool $shouldUseAntlers = false)
     {
         $this->content = $content;
+        $this->context = $context;
         $this->endpoint = $endpoint;
         $this->shouldUseAntlers = $shouldUseAntlers;
     }
 
+    public function content(): ?string
+    {
+        return $this->shouldUseAntlers() ? Antlers::parse($this->content, $this->context()) : $this->content;
+    }
+
+    public function context(): Collection
+    {
+        if (is_null($this->context)) {
+            $this->context = collect([]);
+        }
+
+        return $this->context;
+    }
+
+    public function endpoint(): ?string
+    {
+        return $this->endpoint;
+    }
+
+    public function render(): string
+    {
+        if ($this->endpoint()) {
+            return view('frosty::fetcher', [
+                'frosty' => $this,
+            ]);
+        }
+
+        return $this->content();
+    }
+
     public function shouldUseAntlers(): bool
     {
         return $this->shouldUseAntlers;
@@ -34,11 +67,6 @@ public function withAntlers(bool $shouldUseAntlers = true): static
         return $this;
     }
 
-    public function content(): ?string
-    {
-        return $this->shouldUseAntlers() ? Antlers::parse($this->content) : $this->content;
-    }
-
     public function withContent(string $content): static
     {
         $this->content = $content;
@@ -46,9 +74,11 @@ public function withContent(string $content): static
         return $this;
     }
 
-    public function endpoint(): ?string
+    public function withContext(Collection $context): static
     {
-        return $this->endpoint;
+        $this->context = $context;
+
+        return $this;
     }
 
     public function withEndpoint(string $endpoint): static
@@ -57,15 +87,4 @@ public function withEndpoint(string $endpoint): static
 
         return $this;
     }
-
-    public function render(): string
-    {
-        if ($this->endpoint()) {
-            return view('frosty::fetcher', [
-                'frosty' => $this,
-            ]);
-        }
-
-        return $this->content();
-    }
 }
diff --git a/src/Tags/FrostyTag.php b/src/Tags/FrostyTag.php
index 54bd4a4..8ca9d92 100644
--- a/src/Tags/FrostyTag.php
+++ b/src/Tags/FrostyTag.php
@@ -29,7 +29,8 @@ public function fetch()
     {
         $frosty = FrostyFetcher::make()
             ->withAntlers()
-            ->withContent($this->content);
+            ->withContent($this->content)
+            ->withContext($this->context);
 
         if ($this->params['url'] ?? false) {
             $frosty->withEndpoint($this->params['url']);