-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to store snapshots using storage
- Loading branch information
Showing
7 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Bootloader; | ||
|
||
use Spiral\Boot\Bootloader\Bootloader; | ||
use Spiral\Boot\EnvironmentInterface; | ||
use Spiral\Core\FactoryInterface; | ||
use Spiral\Snapshots\SnapshotterInterface; | ||
use Spiral\Snapshots\StorageSnapshooter; | ||
use Spiral\Snapshots\StorageSnapshot; | ||
use Spiral\Storage\Bootloader\StorageBootloader; | ||
|
||
/** | ||
* Depends on environment variables: | ||
* SNAPSHOTS_BUCKET: bucket name | ||
* SNAPSHOTS_DIRECTORY: where snapshots will be stored in the bucket | ||
* SNAPSHOT_VERBOSITY: defaults to {@see \Spiral\Exceptions\Verbosity::VERBOSE} (1) | ||
*/ | ||
final class StorageSnapshotsBootloader extends Bootloader | ||
{ | ||
protected const DEPENDENCIES = [ | ||
StorageBootloader::class, | ||
]; | ||
|
||
protected const SINGLETONS = [ | ||
StorageSnapshot::class => [self::class, 'storageSnapshot'], | ||
SnapshotterInterface::class => StorageSnapshooter::class, | ||
]; | ||
|
||
private function storageSnapshot(EnvironmentInterface $env, FactoryInterface $factory): StorageSnapshot | ||
{ | ||
$bucket = $env->get('SNAPSHOTS_BUCKET'); | ||
|
||
if ($bucket === null) { | ||
throw new \RuntimeException( | ||
'Please, configure a bucket for storing snapshots using the environment variable `SNAPSHOTS_BUCKET`.' | ||
); | ||
} | ||
|
||
return $factory->make(StorageSnapshot::class, [ | ||
'bucket' => $bucket, | ||
'directory' => $env->get('SNAPSHOTS_DIRECTORY', null), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Exceptions\Reporter; | ||
|
||
use Spiral\Exceptions\ExceptionReporterInterface; | ||
use Spiral\Snapshots\StorageSnapshot; | ||
|
||
class StorageReporter implements ExceptionReporterInterface | ||
{ | ||
public function __construct( | ||
private StorageSnapshot $storageSnapshot, | ||
) { | ||
} | ||
|
||
public function report(\Throwable $exception): void | ||
{ | ||
$this->storageSnapshot->create($exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Snapshots; | ||
|
||
final class StorageSnapshooter implements SnapshotterInterface | ||
{ | ||
public function __construct( | ||
private readonly StorageSnapshot $storageSnapshot | ||
) { | ||
} | ||
|
||
public function register(\Throwable $e): SnapshotInterface | ||
{ | ||
return $this->storageSnapshot->create($e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Snapshots; | ||
|
||
use Spiral\Exceptions\ExceptionRendererInterface; | ||
use Spiral\Exceptions\Verbosity; | ||
use Spiral\Storage\StorageInterface; | ||
|
||
class StorageSnapshot | ||
{ | ||
public function __construct( | ||
protected readonly string $bucket, | ||
protected readonly StorageInterface $storage, | ||
protected readonly Verbosity $verbosity, | ||
protected readonly ExceptionRendererInterface $renderer, | ||
protected readonly ?string $directory = null | ||
) { | ||
} | ||
|
||
public function create(\Throwable $e): SnapshotInterface | ||
{ | ||
$snapshot = new Snapshot($this->getID($e), $e); | ||
|
||
$this->saveSnapshot($snapshot); | ||
|
||
return $snapshot; | ||
} | ||
|
||
protected function saveSnapshot(SnapshotInterface $snapshot): void | ||
{ | ||
$filename = $this->getFilename($snapshot, new \DateTime()); | ||
|
||
$this->storage | ||
->bucket($this->bucket) | ||
->create($this->directory !== null ? $this->directory . DIRECTORY_SEPARATOR . $filename : $filename) | ||
->write($this->renderer->render($snapshot->getException(), $this->verbosity)); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
protected function getFilename(SnapshotInterface $snapshot, \DateTimeInterface $time): string | ||
{ | ||
return \sprintf( | ||
'%s-%s.txt', | ||
$time->format('d.m.Y-Hi.s'), | ||
(new \ReflectionClass($snapshot->getException()))->getShortName() | ||
); | ||
} | ||
|
||
protected function getID(\Throwable $e): string | ||
{ | ||
return \md5(\implode('|', [$e->getMessage(), $e->getFile(), $e->getLine()])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Snapshots; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Spiral\Exceptions\ExceptionRendererInterface; | ||
use Spiral\Exceptions\Verbosity; | ||
use Spiral\Snapshots\StorageSnapshot; | ||
use Spiral\Storage\BucketInterface; | ||
use Spiral\Storage\FileInterface; | ||
use Spiral\Storage\StorageInterface; | ||
|
||
final class StorageSnapshotTest extends TestCase | ||
{ | ||
private ExceptionRendererInterface $renderer; | ||
private FileInterface $file; | ||
private BucketInterface $bucket; | ||
private StorageInterface $storage; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->renderer = $this->createMock(ExceptionRendererInterface::class); | ||
$this->renderer | ||
->expects($this->once()) | ||
->method('render') | ||
->willReturn('foo'); | ||
|
||
$this->file = $this->createMock(FileInterface::class); | ||
$this->file | ||
->expects($this->once()) | ||
->method('write') | ||
->with('foo'); | ||
|
||
$this->bucket = $this->createMock(BucketInterface::class); | ||
|
||
$this->storage = $this->createMock(StorageInterface::class); | ||
$this->storage | ||
->expects($this->once()) | ||
->method('bucket') | ||
->willReturn($this->bucket); | ||
} | ||
|
||
public function testCreate(): void | ||
{ | ||
$this->bucket | ||
->expects($this->once()) | ||
->method('create') | ||
->with($this->callback(static fn (string $filename) => \str_contains($filename, 'Error.txt'))) | ||
->willReturn($this->file); | ||
|
||
$e = new \Error('message'); | ||
$s = (new StorageSnapshot('foo', $this->storage, Verbosity::VERBOSE, $this->renderer))->create($e); | ||
|
||
$this->assertSame($e, $s->getException()); | ||
|
||
$this->assertStringContainsString('Error', $s->getMessage()); | ||
$this->assertStringContainsString('message', $s->getMessage()); | ||
$this->assertStringContainsString(__FILE__, $s->getMessage()); | ||
$this->assertStringContainsString('53', $s->getMessage()); | ||
} | ||
|
||
public function testCreateWithDirectory(): void | ||
{ | ||
$this->bucket | ||
->expects($this->once()) | ||
->method('create') | ||
->with($this->callback(static fn (string $filename) => \str_starts_with($filename, 'foo/bar'))) | ||
->willReturn($this->file); | ||
|
||
$e = new \Error('message'); | ||
$s = (new StorageSnapshot('foo', $this->storage, Verbosity::VERBOSE, $this->renderer, 'foo/bar')) | ||
->create($e); | ||
|
||
$this->assertSame($e, $s->getException()); | ||
|
||
$this->assertStringContainsString('Error', $s->getMessage()); | ||
$this->assertStringContainsString('message', $s->getMessage()); | ||
$this->assertStringContainsString(__FILE__, $s->getMessage()); | ||
$this->assertStringContainsString('72', $s->getMessage()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Framework/Bootloader/Exceptions/StorageSnapshotsBootloaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Framework\Bootloader\Exceptions; | ||
|
||
use Spiral\Bootloader\StorageSnapshotsBootloader; | ||
use Spiral\Core\Container; | ||
use Spiral\Snapshots\SnapshotterInterface; | ||
use Spiral\Snapshots\StorageSnapshooter; | ||
use Spiral\Snapshots\StorageSnapshot; | ||
use Spiral\Testing\TestApp; | ||
use Spiral\Testing\TestCase; | ||
|
||
final class StorageSnapshotsBootloaderTest extends TestCase | ||
{ | ||
public const ENV = [ | ||
'SNAPSHOTS_BUCKET' => 'foo', | ||
]; | ||
|
||
public function createAppInstance(Container $container = new Container()): TestApp | ||
{ | ||
return TestApp::create( | ||
directories: $this->defineDirectories( | ||
$this->rootDirectory(), | ||
), | ||
handleErrors: false, | ||
container: $container, | ||
)->withBootloaders([StorageSnapshotsBootloader::class]); | ||
} | ||
|
||
public function testSnapshotterInterfaceBinding(): void | ||
{ | ||
$this->assertContainerBoundAsSingleton(SnapshotterInterface::class, StorageSnapshooter::class); | ||
} | ||
|
||
public function testStorageSnapshotBinding(): void | ||
{ | ||
$this->assertContainerBoundAsSingleton(StorageSnapshot::class, StorageSnapshot::class); | ||
} | ||
} |