Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Token class changes #9964

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Contracts/Tokens/TokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Statamic\Contracts\Tokens;

interface TokenRepository
{
public function make(?string $token, string $handler, array $data = []): Token;

public function find(string $token): ?Token;

public function save(Token $token): bool;

public function delete(Token $token): bool;

public function collectGarbage(): void;
}
11 changes: 10 additions & 1 deletion src/Facades/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Tokens\TokenRepository;
use Statamic\Contracts\Tokens\TokenRepository;

/**
* @method static \Statamic\Contracts\Tokens\Token make(?string $token, string $handler, array $data = [])
* @method static \Statamic\Contracts\Tokens\Token find(string $token)
* @method static bool save(\Statamic\Contracts\Tokens\Token $token)
* @method static bool delete(\Statamic\Contracts\Tokens\Token $token)
* @method static void collectGarbage()
*
* @see \Statamic\Tokens\TokenRepository
*/
class Token extends Facade
{
protected static function getFacadeAccessor()
Expand Down
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function register()
\Statamic\Contracts\Assets\AssetRepository::class => \Statamic\Assets\AssetRepository::class,
\Statamic\Contracts\Forms\FormRepository::class => \Statamic\Forms\FormRepository::class,
\Statamic\Contracts\Forms\SubmissionRepository::class => \Statamic\Stache\Repositories\SubmissionRepository::class,
\Statamic\Contracts\Tokens\TokenRepository::class => \Statamic\Tokens\FileTokenRepository::class,
])->each(function ($concrete, $abstract) {
if (! $this->app->bound($abstract)) {
Statamic::repository($abstract, $concrete);
Expand Down
78 changes: 0 additions & 78 deletions src/Tokens/AbstractToken.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Tokens/FileToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Statamic\Tokens;

use Statamic\Data\ExistsAsFile;

class FileToken extends Token
{
use ExistsAsFile;

public function path()
{
return storage_path('statamic/tokens/'.$this->token().'.yaml');
}

public function fileData()
{
return [
'handler' => $this->handler,
'expires_at' => $this->expiry->timestamp,
'data' => $this->data->all(),
];
}
}
67 changes: 67 additions & 0 deletions src/Tokens/FileTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Statamic\Tokens;

use Illuminate\Support\Carbon;
use Statamic\Contracts\Tokens\Token as TokenContract;
use Statamic\Facades\File;
use Statamic\Facades\YAML;

class FileTokenRepository extends TokenRepository
{
public function make(?string $token, string $handler, array $data = []): TokenContract
{
return app()->makeWith(TokenContract::class, compact('token', 'handler', 'data'));
}

public function find(string $token): ?TokenContract
{
$path = storage_path('statamic/tokens/'.$token.'.yaml');

if (! File::exists($path)) {
return null;
}

return $this->makeFromPath($path);
}

public function save(TokenContract $token): bool
{
File::put(storage_path('statamic/tokens/'.$token->token().'.yaml'), $token->fileContents());

return true;
}

public function delete(TokenContract $token): bool
{
File::delete(storage_path('statamic/tokens/'.$token->token().'.yaml'));

return true;
}

public function collectGarbage(): void
{
File::getFilesByType(storage_path('statamic/tokens'), 'yaml')
->map(fn ($path) => $this->makeFromPath($path))
->filter->hasExpired()
->each->delete();
}

private function makeFromPath(string $path): FileToken
{
$yaml = YAML::file($path)->parse();

$token = basename($path, '.yaml');

return $this
->make($token, $yaml['handler'], $yaml['data'] ?? [])
->expireAt(Carbon::createFromTimestamp($yaml['expires_at']));
}

public static function bindings(): array
{
return [
TokenContract::class => FileToken::class,
];
}
}
76 changes: 65 additions & 11 deletions src/Tokens/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,77 @@

namespace Statamic\Tokens;

use Statamic\Data\ExistsAsFile;
use Closure;
use Facades\Statamic\Tokens\Generator;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Statamic\Contracts\Tokens\Token as Contract;
use Statamic\Facades\Token as Tokens;

class Token extends AbstractToken
abstract class Token implements Contract
{
use ExistsAsFile;
protected $token;
protected $handler;
protected $data;
protected $expiry;

public function path()
public function __construct(?string $token, string $handler, array $data = [])
{
return storage_path('statamic/tokens/'.$this->token().'.yaml');
$this->token = $token ?? Generator::generate();
$this->handler = $handler;
$this->data = collect($data);
$this->expiry = Carbon::now()->addHour();
}

public function fileData()
public function token(): string
{
return [
'handler' => $this->handler,
'expires_at' => $this->expiry->timestamp,
'data' => $this->data->all(),
];
return $this->token;
}

public function handler(): string
{
return $this->handler;
}

public function data(): Collection
{
return $this->data;
}

public function get(string $key)
{
return $this->data->get($key);
}

public function save()
{
return Tokens::save($this);
}

public function delete()
{
return Tokens::delete($this);
}

public function handle($request, Closure $next)
{
return app($this->handler)->handle($this, $request, $next);
}

public function expiry(): Carbon
{
return $this->expiry;
}

public function expireAt(Carbon $expiry): self
{
$this->expiry = $expiry;

return $this;
}

public function hasExpired(): bool
{
return $this->expiry->isPast();
}
}
55 changes: 6 additions & 49 deletions src/Tokens/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,15 @@

namespace Statamic\Tokens;

use Illuminate\Support\Carbon;
use Statamic\Facades\File;
use Statamic\Facades\YAML;
use Statamic\Contracts\Tokens\Token as TokenContract;
use Statamic\Contracts\Tokens\TokenRepository as Contract;

class TokenRepository
abstract class TokenRepository implements Contract
{
public function make(?string $token, string $handler, array $data = []): Token
public function make(?string $token, string $handler, array $data = []): TokenContract
{
return new Token($token, $handler, $data);
return app()->makeWith(TokenContract::class, compact('token', 'handler', 'data'));
}

public function find(string $token)
{
$path = storage_path('statamic/tokens/'.$token.'.yaml');

if (! File::exists($path)) {
return null;
}

return $this->makeFromPath($path);
}

public function save(Token $token)
{
File::put(storage_path('statamic/tokens/'.$token->token().'.yaml'), $token->fileContents());

return true;
}

public function delete(Token $token)
{
File::delete(storage_path('statamic/tokens/'.$token->token().'.yaml'));

return true;
}

public function collectGarbage()
{
File::getFilesByType(storage_path('statamic/tokens'), 'yaml')
->map(fn ($path) => $this->makeFromPath($path))
->filter->hasExpired()
->each->delete();
}

private function makeFromPath(string $path): Token
{
$yaml = YAML::file($path)->parse();

$token = basename($path, '.yaml');

return $this
->make($token, $yaml['handler'], $yaml['data'] ?? [])
->expireAt(Carbon::createFromTimestamp($yaml['expires_at']));
}
abstract public static function bindings(): array;
}
4 changes: 2 additions & 2 deletions tests/Tokens/TokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Collection;
use Statamic\Contracts\Tokens\Token;
use Statamic\Facades\File;
use Statamic\Tokens\TokenRepository;
use Statamic\Tokens\FileTokenRepository;
use Tests\TestCase;

class TokenRepositoryTest extends TestCase
Expand All @@ -18,7 +18,7 @@ public function setUp(): void
{
parent::setUp();

$this->tokens = new TokenRepository;
$this->tokens = new FileTokenRepository;
}

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion tests/Tokens/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Statamic\Facades;
use Statamic\Tokens\Token;
use Statamic\Tokens\FileToken as Token;
use Tests\TestCase;

class TokenTest extends TestCase
Expand Down
Loading