Skip to content

Commit

Permalink
Support for Statamic 5
Browse files Browse the repository at this point in the history
  • Loading branch information
martyf committed May 10, 2024
1 parent 37037d7 commit a9b3b91
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 102 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- statamic:hide -->

![Statamic 4+](https://img.shields.io/badge/Statamic-4+-FF269E?style=for-the-badge&link=https://statamic.com)
![Statamic 5.0](https://img.shields.io/badge/Statamic-5.0-FF269E?style=for-the-badge&link=https://statamic.com)
[![Logger for Statamic on Packagist](https://img.shields.io/packagist/v/mitydigital/statamic-logger?style=for-the-badge)](https://packagist.org/packages/mitydigital/statamic-logger/stats)

---
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"require": {
"php": "^8.2",
"statamic/cms": "^4.0"
"statamic/cms": "^5.0"
},
"extra": {
"statamic": {
Expand Down
50 changes: 25 additions & 25 deletions src/Support/StatamicLoggerReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,6 @@ public function getDates(): Collection
return $logs->sortKeysDesc();
}

protected function read(string $date): bool
{
// get the configured path and filename
$path = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStoragePath();
$filename = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStorageFilename();

// build the path
$fullPath = storage_path($path.DIRECTORY_SEPARATOR.$filename.'-'.$date.'.log');

// if the file doesn't exist, return false
if (! file_exists($fullPath)) {
return false;
}

// load the file
$this->file = new SplFileObject($fullPath);
$this->file->seek($this->file->getSize());

// set the total number of records
$this->total = $this->file->key();

// loaded and total found, return true
return true;
}

public function paginate(string $date, int $page, int $perPage): array|Collection
{
// set the page and per page values
Expand Down Expand Up @@ -104,6 +79,31 @@ public function paginate(string $date, int $page, int $perPage): array|Collectio
return $lines;
}

protected function read(string $date): bool
{
// get the configured path and filename
$path = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStoragePath();
$filename = \MityDigital\StatamicLogger\Facades\StatamicLogger::getStorageFilename();

// build the path
$fullPath = storage_path($path.DIRECTORY_SEPARATOR.$filename.'-'.$date.'.log');

// if the file doesn't exist, return false
if (! file_exists($fullPath)) {
return false;
}

// load the file
$this->file = new SplFileObject($fullPath);
$this->file->seek($this->file->getSize());

// set the total number of records
$this->total = $this->file->key();

// loaded and total found, return true
return true;
}

public function getPage(): int
{
return $this->page;
Expand Down
1 change: 1 addition & 0 deletions tests/Support/StatamicLoggerReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
it('returns the correct page after pagination', function () {
// paginate, then getPage
$this->reader->paginate($this->today, 2, 10);

expect($this->reader->getPage())->toBe(2);

$this->reader->paginate($this->today, 4, 10);
Expand Down
125 changes: 50 additions & 75 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,61 @@

namespace MityDigital\StatamicLogger\Tests;

use Facades\Statamic\Version;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Facades\File;
use MityDigital\StatamicLogger\ServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Statamic\Console\Processes\Composer;
use Statamic\Extend\Manifest;
use Statamic\Providers\StatamicServiceProvider;
use Statamic\Facades\Site;
use Statamic\Statamic;
use Statamic\Testing\AddonTestCase;

abstract class TestCase extends OrchestraTestCase
abstract class TestCase extends AddonTestCase
{
protected $shouldFakeVersion = true;

protected function setUp(): void
protected string $addonServiceProvider = ServiceProvider::class;

protected function getEnvironmentSetUp($app)
{
parent::setUp();
parent::getEnvironmentSetUp($app);

$this->withoutVite();
// assets and asset containers
$app['config']->set('statamic.stache.stores.asset-containers.directory',
$this->getTempDirectory('/content/assets'));

if ($this->shouldFakeVersion) {
Version::shouldReceive('get')
->andReturn(Composer::create(__DIR__.'/../')->installedVersion(Statamic::PACKAGE));
}
}
// collections, entries and collection trees
$app['config']->set('statamic.stache.stores.collections.directory',
$this->getTempDirectory('/content/collections'));
$app['config']->set('statamic.stache.stores.collection-trees.directory',
$this->getTempDirectory('/content/collection-trees'));
$app['config']->set('statamic.stache.stores.entries.directory',
$this->getTempDirectory('/content/collections'));

protected function getPackageProviders($app)
{
return [
StatamicServiceProvider::class,
ServiceProvider::class,
];
}
// taxonomies and terms
$app['config']->set('statamic.stache.stores.taxonomies.directory',
$this->getTempDirectory('/content/taxonomies'));
$app['config']->set('statamic.stache.stores.terms.directory',
$this->getTempDirectory('/content/terms'));

protected function getPackageAliases($app)
{
return [
'Statamic' => Statamic::class,
];
// navigation and nav-trees
$app['config']->set('statamic.stache.stores.navigation.directory',
$this->getTempDirectory('/content/nav'));
$app['config']->set('statamic.stache.stores.nav-trees.directory',
$this->getTempDirectory('/content/nav'));

// globals
$app['config']->set('statamic.stache.stores.globals.directory',
$this->getTempDirectory('/content/globals'));
$app['config']->set('statamic.stache.stores.global-variables.directory',
$this->getTempDirectory('/content/globals'));

// users
$app['config']->set('statamic.stache.stores.users.directory',
$this->getTempDirectory('/content/users'));
}

protected function getEnvironmentSetUp($app)
public function getTempDirectory($suffix = ''): string
{
parent::getEnvironmentSetUp($app);

$app->make(Manifest::class)->manifest = [
'mitydigital/statamic-logger' => [
'id' => 'mitydigital/statamic-logger',
'namespace' => 'MityDigital\\StatamicLogger',
],
];
return __DIR__.'/TestSupport/'.($suffix == '' ? '' : '/'.$suffix);
}

protected function resolveApplicationConfiguration($app)
Expand All @@ -64,7 +68,6 @@ protected function resolveApplicationConfiguration($app)
'cp',
'forms',
'static_caching',
'sites',
'stache',
'system',
'users',
Expand All @@ -73,7 +76,7 @@ protected function resolveApplicationConfiguration($app)
foreach ($configs as $config) {
$app['config']->set(
"statamic.$config",
require(__DIR__."/../vendor/statamic/cms/config/{$config}.php")
require (__DIR__."/../vendor/statamic/cms/config/{$config}.php")
);
}

Expand All @@ -91,39 +94,16 @@ protected function resolveApplicationConfiguration($app)
$app['config']->set('auth.providers.users.driver', 'statamic');
$app['config']->set('statamic.users.repository', 'file');

// assets and asset containers
$app['config']->set('statamic.stache.stores.asset-containers.directory',
$this->getTempDirectory('/content/assets'));

// collections, entries and collection trees
$app['config']->set('statamic.stache.stores.collections.directory',
$this->getTempDirectory('/content/collections'));
$app['config']->set('statamic.stache.stores.collection-trees.directory',
$this->getTempDirectory('/content/collection-trees'));
$app['config']->set('statamic.stache.stores.entries.directory',
$this->getTempDirectory('/content/collections'));

// taxonomies and terms
$app['config']->set('statamic.stache.stores.taxonomies.directory',
$this->getTempDirectory('/content/taxonomies'));
$app['config']->set('statamic.stache.stores.terms.directory',
$this->getTempDirectory('/content/terms'));

// navigation and nav-trees
$app['config']->set('statamic.stache.stores.navigation.directory',
$this->getTempDirectory('/content/nav'));
$app['config']->set('statamic.stache.stores.nav-trees.directory',
$this->getTempDirectory('/content/nav'));

// globals
$app['config']->set('statamic.stache.stores.globals.directory',
$this->getTempDirectory('/content/globals'));
$app['config']->set('statamic.stache.stores.global-variables.directory',
$this->getTempDirectory('/content/globals'));

// users
$app['config']->set('statamic.stache.stores.users.directory',
$this->getTempDirectory('/content/users'));
Statamic::booted(function () {
// configure to be an AU site
Site::setSites([
'default' => [
'name' => config('app.name'),
'locale' => 'en_AU',
'url' => '/',
],
]);
});
}

protected function tearDown(): void
Expand All @@ -132,9 +112,4 @@ protected function tearDown(): void

parent::tearDown();
}

public function getTempDirectory($suffix = ''): string
{
return __DIR__.'/TestSupport/'.($suffix == '' ? '' : '/'.$suffix);
}
}

0 comments on commit a9b3b91

Please sign in to comment.