Skip to content

Commit

Permalink
Download function added.
Browse files Browse the repository at this point in the history
  • Loading branch information
halilcosdu committed Apr 22, 2024
1 parent 293cd3f commit e79a27f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ $log = LogWeaver::description('User registered')
->log();
```

```php
$response = LogWeaver::download(string $path, $name = null, array $headers = []): StreamedResponse;
```

## Testing

```bash
Expand Down
19 changes: 11 additions & 8 deletions src/Facades/LogWeaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
namespace HalilCosdu\LogWeaver\Facades;

use Illuminate\Support\Facades\Facade;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* @see \HalilCosdu\LogWeaver\LogWeaver
*
* @method static \HalilCosdu\LogWeaver\LogWeaver description(string $description)
* @method static \HalilCosdu\LogWeaver\LogWeaver logResource(string $logResource)
* @method static \HalilCosdu\LogWeaver\LogWeaver level(string $level)
* @method static \HalilCosdu\LogWeaver\LogWeaver content(array $content)
* @method static \HalilCosdu\LogWeaver\LogWeaver disk(string $disk)
* @method static \HalilCosdu\LogWeaver\LogWeaver log(string $path = null)
* @method array toArray()
* @method string toJson()
* @method static \HalilCosdu\LogWeaver\LogWeaver description(string $description): static
* @method static \HalilCosdu\LogWeaver\LogWeaver logResource(string $logResource): static
* @method static \HalilCosdu\LogWeaver\LogWeaver level(string $level): static
* @method static \HalilCosdu\LogWeaver\LogWeaver content(array $content): static
* @method static \HalilCosdu\LogWeaver\LogWeaver disk(string $disk): static
* @method static \HalilCosdu\LogWeaver\LogWeaver directory(string $directory): static
* @method static \HalilCosdu\LogWeaver\LogWeaver log(?string $path = null, bool $wait = false): array
* @method static \HalilCosdu\LogWeaver\LogWeaver download(string $path, $name = null, array $headers = []): StreamedResponse
* @method array toArray(): array
* @method string toJson($options = 0): false|string
*/
class LogWeaver extends Facade
{
Expand Down
6 changes: 6 additions & 0 deletions src/LogWeaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Sleep;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\StreamedResponse;

class LogWeaver implements Arrayable, Jsonable
{
Expand Down Expand Up @@ -175,6 +176,11 @@ private function waitForContentFromDisk(string $path): void
} while ($isFileVisible !== true);
}

public function download(string $path, $name = null, array $headers = []): StreamedResponse
{
return Storage::disk($this->getDisk())->download($path, $name, $headers);
}

/**
* @throws Exception
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/LogWeaverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use HalilCosdu\LogWeaver\LogWeaver;
use Symfony\Component\HttpFoundation\StreamedResponse;

it('can be instantiated', function () {
$logWeaver = new LogWeaver();
Expand Down Expand Up @@ -75,3 +76,27 @@
expect($e->getMessage())->not()->toBeEmpty();
}
});

it('should download log', function () {
$logWeaver = new LogWeaver();
$logWeaver->description('Payment gateway down')
->logResource('event')
->content(['gateway' => 'Stripe', 'status' => 'down'])
->level('critical')
->disk('local')
->log('log.json');

expect($logWeaver->download('log.json'))->toBeInstanceOf(StreamedResponse::class);
});

it('should log', function () {
$logWeaver = new LogWeaver();
$result = $logWeaver->description('Payment gateway down')
->logResource('event')
->content(['gateway' => 'Stripe', 'status' => 'down'])
->level('critical')
->disk('local')
->log();

expect($result)->toMatchArray(['status' => true]);
});

0 comments on commit e79a27f

Please sign in to comment.