Skip to content

Commit

Permalink
Add context support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Rook committed Aug 5, 2021
1 parent 60eafc4 commit eb18286
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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();
Expand Down
61 changes: 40 additions & 21 deletions src/FrostyFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,21 +67,18 @@ 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;

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
Expand All @@ -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();
}
}
3 changes: 2 additions & 1 deletion src/Tags/FrostyTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit eb18286

Please sign in to comment.