Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Refactor bootloaders
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin v / vvval committed Oct 4, 2017
1 parent 5f43e1b commit ab6e066
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 59 deletions.
55 changes: 23 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,51 @@ spiral register spiral/snapshotter
],
```

### Include `SnapshotterBootloader`

```php
$this->getBootloader()->bootload([
\Spiral\Snapshotter\Bootloaders\SnapshotterBootloader::class
]);
```

### Select one of provided handlers

Currently there are two supported handlers: `FileHandler` and `AggregationHandler`, choose onf of them and bind it:
```php
$this->getBootloader()->bootload([
\Spiral\Snapshotter\Bootloaders\FileSnapshotterBootloader::class
\Spiral\Snapshotter\Bootloaders\FileHandlerBootloader::class
]);
//OR:
$this->getBootloader()->bootload([
\Spiral\Snapshotter\Bootloaders\AggregationSnapshotterBootloader::class
\Spiral\Snapshotter\Bootloaders\AggregationHandlerBootloader::class
]);
```

## File Handler
File handler stores snapshot files in runtime directory. All you need to do is add `FileSnapshotterBootloader` bootloader.

You can remove standard `SnapshotInterface` binding:
Then you can remove standard `SnapshotInterface` binding (if included):
```php
$this->container->bind(SnapshotInterface::class, Snapshotter\Debug\AggregatedSnapshot::class);
```
Don't worry, it is placed in the bootloader already:
```php
class FileSnapshotterBootloader extends Bootloader
{
/**
* {@inheritdoc}
*/
const BINDINGS = [
HandlerInterface::class => FileHandler::class,
AbstractController::class => SnapshotsController::class,
Debug\SnapshotInterface::class => Debug\Snapshot::class
];
}
//$this->container->bind(SnapshotInterface::class, Snapshotter\Debug\AggregatedSnapshot::class);
```

## File Handler
File handler stores snapshot files in runtime directory.

## Aggregation Handler
Aggregation handler stores snapshots in database. All you need to do is add `AggregationSnapshotterBootloader` bootloader.
Aggregation handler stores snapshots in database. Exception body is gzencoded

### Suppression

Aggregation handler aggregates similar snapshot incidents groping them by snapshot teaser message, it allows you to easily manage snapshots if some of them occurred more than once.
Aggregation handler supports suppression feature: it allows you to save space because new snapshot incidents will be stored with empty exception source. You will see all incidents, no reason to store all sources if you can find it in the last incident. If you want to store sources - just disable suppression.
Aggregation handler aggregates similar snapshot incidents groping them by snapshot teaser message,
it allows you to easily manage snapshots if some of them occurred more than once.
Aggregation handler supports suppression feature:
it allows you to save space because new snapshot incidents will be stored with empty exception source.
You will see all incidents, no reason to store all sources if you can find it in the last incident.
If you want to store sources - just disable suppression.
> After suppression is enabled, only new incidents will be involved, old ones will be kept untouched. Same for disabled suppression.
### Define database connection.

Aggregation handler uses database, by default it is set to `runtime` database:
```php
'snapshots' => [
'connection' => 'runtime',
'tablePrefix' => 'snapshots_'
/*{{databases.snapshotter}}*/
],
```
Aggregation handler uses database, by default it is set as an alias to the `default` database

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
namespace Spiral\Snapshotter\Bootloaders;

use Spiral\Core\Bootloaders\Bootloader;
use Spiral\Debug\SnapshotInterface;
use Spiral\Snapshotter\AbstractController;
use Spiral\Snapshotter\AggregationHandler;
use Spiral\Snapshotter\AggregationHandler\Controllers\SnapshotsController;
use Spiral\Snapshotter\DelegateSnapshot;
use Spiral\Snapshotter\HandlerInterface;

class AggregationSnapshotterBootloader extends Bootloader
class AggregationHandlerBootloader extends Bootloader
{
/**
* {@inheritdoc}
*/
const BINDINGS = [
HandlerInterface::class => AggregationHandler::class,
AbstractController::class => SnapshotsController::class,
SnapshotInterface::class => DelegateSnapshot::class
AbstractController::class => SnapshotsController::class
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
namespace Spiral\Snapshotter\Bootloaders;

use Spiral\Core\Bootloaders\Bootloader;
use Spiral\Debug\SnapshotInterface;
use Spiral\Snapshotter\AbstractController;
use Spiral\Snapshotter\DelegateSnapshot;
use Spiral\Snapshotter\FileHandler;
use Spiral\Snapshotter\FileHandler\Controllers\SnapshotsController;
use Spiral\Snapshotter\HandlerInterface;

class FileSnapshotterBootloader extends Bootloader
class FileHandlerBootloader extends Bootloader
{
/**
* {@inheritdoc}
*/
const BINDINGS = [
HandlerInterface::class => FileHandler::class,
AbstractController::class => SnapshotsController::class,
SnapshotInterface::class => DelegateSnapshot::class
AbstractController::class => SnapshotsController::class
];
}
16 changes: 16 additions & 0 deletions source/Snapshotter/Bootloaders/SnapshotterBootloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Spiral\Snapshotter\Bootloaders;

use Spiral\Core\Bootloaders\Bootloader;
use Spiral\Debug\SnapshotInterface;

class SnapshotterBootloader extends Bootloader
{
/**
* {@inheritdoc}
*/
const BINDINGS = [
SnapshotInterface::class => DelegateSnapshot::class
];
}
9 changes: 3 additions & 6 deletions source/SnapshotterModule.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Spiral;

use Spiral\Core\DirectoriesInterface;
Expand Down Expand Up @@ -33,12 +34,8 @@ public function register(RegistratorInterface $registrator)
]);

//Register database settings
$registrator->configure('databases', 'databases', 'spiral/snapshotter', [
"'snapshotter' => [",
" 'connection' => 'runtime',",
" 'tablePrefix' => 'snapshots_'",
" /*{{databases.snapshotter}}*/",
"],",
$registrator->configure('databases', 'aliases', 'spiral/snapshotter', [
"'snapshots' => 'default',"
]);

//Register controller in navigation config
Expand Down
5 changes: 2 additions & 3 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
use Spiral\Core\Traits\SharedTrait;
use Spiral\Database\Builders\SelectQuery;
use Spiral\Debug\Snapshot;
use Spiral\Snapshotter\Bootloaders\FileSnapshotterBootloader;
use Spiral\Snapshotter\Bootloaders\FileHandlerBootloader;
use Spiral\Snapshotter\DelegateSnapshot;
use Spiral\Snapshotter\AggregationHandler\Database\SnapshotRecord;
use Spiral\Snapshotter\AggregationHandler;
use Spiral\Snapshotter\FileHandler;

/**
* @property \Spiral\Core\MemoryInterface $memory
Expand Down Expand Up @@ -184,7 +183,7 @@ protected function handleSnapshot(Snapshot $snapshot, bool $report = true): Snap
*/
protected function handleFileSnapshot(Snapshot $snapshot, bool $report = true)
{
$this->app->getBootloader()->bootload([FileSnapshotterBootloader::class]);
$this->app->getBootloader()->bootload([FileHandlerBootloader::class]);

/** @var DelegateSnapshot $delegate */
$delegate = $this->factory->make(DelegateSnapshot::class, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Spiral\Tests\Snapshotter\AggregationHandler\Sources;

use Spiral\Snapshotter\AggregationHandler\Database\Sources\SnapshotSource;
use Spiral\Snapshotter\AggregationHandler;
use Spiral\Snapshotter\AggregationHandler\Services\SnapshotService;
use Spiral\Tests\BaseTest;

class SnapshotSourceTest extends BaseTest
Expand All @@ -16,7 +16,7 @@ public function testFindByHash()
$snapshot = $this->makeSnapshot('custom error', 777);
$this->handleSnapshot($snapshot, true);

$hash = AggregationHandler\Services\SnapshotService::makeHash($snapshot);
$hash = SnapshotService::makeHash($snapshot);
$hash2 = 'some second random hash';

/** @var SnapshotSource $source */
Expand All @@ -31,4 +31,49 @@ public function testFindByHash()
$this->assertEmpty($source->findByHash($hash));
$this->assertNotEmpty($source->findByHash($hash2));
}

public function testFindLast()
{
/** @var SnapshotSource $source */
$source = $this->container->get(SnapshotSource::class);

$this->assertEmpty($source->findLast());

$snapshot1 = $this->makeSnapshot('custom error1', 777);
$this->handleSnapshot($snapshot1, true);
$last1 = $source->findLast();
$this->assertNotEmpty($last1);

sleep(1);
$snapshot2 = $this->makeSnapshot('custom error2', 777);
$this->handleSnapshot($snapshot2, true);
$last2 = $source->findLast();
$this->assertNotEmpty($last2);
$this->assertNotSame($last1->primaryKey(), $last2->primaryKey());

sleep(1);
$snapshot3 = $this->makeSnapshot('custom error2', 777);
$this->handleSnapshot($snapshot3, true);
$last3 = $source->findLast();
$this->assertNotEmpty($last3);
$this->assertSame($last2->primaryKey(), $last3->primaryKey());
}

public function testFindWithLastByPK()
{
/** @var SnapshotSource $source */
$source = $this->container->get(SnapshotSource::class);

$record = $source->create();
$record->save();

$this->assertEmpty($source->findWithLastByPK($record->primaryKey()));

$snapshot = $this->makeSnapshot('custom error', 777);
$last = $this->handleSnapshot($snapshot, true);

$this->assertNotEmpty($last);
$this->assertNotSame($last->primaryKey(), $record->primaryKey());
$this->assertNotEmpty($source->findWithLastByPK($last->primaryKey()));
}
}
7 changes: 2 additions & 5 deletions tests/Snapshotter/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
namespace Spiral\Tests\Snapshotter;

use Spiral\Debug\Configs\SnapshotConfig;
use Spiral\Snapshotter\AggregationHandler;
use Spiral\Snapshotter\AggregationHandler\Database\IncidentRecord;
use Spiral\Snapshotter\AggregationHandler\Database\SnapshotRecord;
use Spiral\Snapshotter\Bootloaders\FileSnapshotterBootloader;
use Spiral\Snapshotter\Bootloaders\FileHandlerBootloader;
use Spiral\Snapshotter\DelegateSnapshot;
use Spiral\Tests\BaseTest;

class FileHandlerTest extends BaseTest
{
public function testFileRender()
{
$this->app->getBootloader()->bootload([FileSnapshotterBootloader::class]);
$this->app->getBootloader()->bootload([FileHandlerBootloader::class]);

$snapshot = $this->makeSnapshot('File error', 123);
/** @var DelegateSnapshot $delegate */
Expand Down
2 changes: 1 addition & 1 deletion tests/TestApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TestApplication extends Core

protected function bootstrap()
{
//Nothing to do
//$this->environment->set("DEBUG",true);
}

/**
Expand Down

0 comments on commit ab6e066

Please sign in to comment.