Skip to content

Commit

Permalink
Improve DX using connections. (#159)
Browse files Browse the repository at this point in the history
* Fix date error on error widget

* Improve plus icon alignment

* Fix validation for eloquent redirects

* Revert "Fix validation for eloquent redirects"

This reverts commit dbff7f0.

* Do not use redirect connection for eloquent

* Small improvements

* Revert "Small improvements"

This reverts commit 397cce2.

* Split connections

* Improve alignment plus icon on errors widget as well

* Add back right-margins

* Clearify migration names and use seperate publish tag

* Update documentation for split connections

* Allow a `default` config value to use laravel default connection

* Fix styling

* Fix heading indentation

* Make quotes more consistent

* Revert "Make quotes more consistent"

This reverts commit 17c7365.

* Small text improvements

* Use correct migration names

* Remove outdated informatiion.

People can read the up-to-date documentation instead.

* Capitalize `Laravel`

* Text improvement

* Fix typo

* Rename database from `errors` to `redirect` if needed

* Fix tests

* Add backwards compatibility for `connection` configuration

* Improve backward compatibility

* Fix styling

* Use singular version which reads better

* Improve readability

* Code style fix

---------

Co-authored-by: vluijkx <vluijkx@users.noreply.github.com>
  • Loading branch information
vluijkx and vluijkx authored Jan 16, 2024
1 parent ba5c921 commit d2f3d61
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 110 deletions.
36 changes: 19 additions & 17 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Widgets can be added to the dashboard by modifying the widgets array in the `con
```php
// config/statamic/cp.php
'widgets' => [
[
'type' => 'errors',
Expand All @@ -58,44 +58,46 @@ Widgets can be added to the dashboard by modifying the widgets array in the `con

Like the default widgets of Statamic, you can also configure `title`& `width` of the widgets.

### Logging errors
## Logging errors

By default, Statamic Redirect will create error logs, you can disable this by setting the `log_errors` config value to `false`.

### Logging individual hits
## Logging individual hits

By default, Statamic Redirect will also keep individual hits for each error, you can disable this by setting the `log_hits` config value to `false`.

### Create entry redirects
## Create entry redirects

By default, Statamic Redirect will create redirects when the URI of an entry changes. You can disable this by setting the `create_entry_redirects` config value to `false`.

### Deleting entry conflicting redirects
## Deleting entry conflicting redirects

By default, Statamic Redirect will delete redirects when a saved entry's URI conflicts with the redirect. You can disable this by setting the `delete_conflicting_redirects` config value to `false`.

### Cleaning errors
## Cleaning errors

The amount of errors can grow quickly, Statamic Redirect includes a scheduled command that by default cleans up errors older than 1 month.

You can disable the cleaning by setting the `clean_errors` config value to `false`. Or change the date range by changing the `clean_older_than` config value. This accepts a value that is parsed by PHP's [createfromdatestring](http://php.net/manual/en/dateinterval.createfromdatestring.php) function.

> Make sure your [Schedule](https://laravel.com/docs/8.x/scheduling#introduction) is running for error cleaning to work.
> Make sure your [Schedule](https://laravel.com/docs/8.x/scheduling#introduction) is running for error cleaning to work.
### Different storage
## Different storage

If you want to use a different storage method for the Errors or Redirects, you can change them in the config file.
If you want to use a different storage method for errors or redirects, change them in the config file.

These should implement `\Rias\StatamicRedirect\Repositories\ErrorRepository` and `\Rias\StatamicRedirect\Repositories\RedirectRepository` respectively.
### Errors

In the future this addon will come with some extra Repositories by default, without a need to code your own.
By default, errors are stored using the built-in `redirect-sqlite` connection. Picking another connection can be useful when logging a lot of errors & hits. To do this, change the `error_connection` config value. Provide `default` to use the default Laravel connection.

You can switch the storing & retrieving of Errors & Redirects to the Eloquent repositories provided, this can be useful if you're logging a lot of errors & hits.
> [!NOTE]
> When using a connection other than `redirect-sqlite`, make sure to publish (and run) the corresponding migration using `php artisan vendor:publish --tag="statamic-redirect-error-migrations"`.
```php
// config/statamic/redirect.php
### Redirects

'error_repository' => \Rias\StatamicRedirect\Eloquent\Errors\EloquentErrorRepository::class,
By default, errors are stored in the `content/redirects` folder. Update the `redirect_store` config value to use a different folder.

'redirect_repository' => \Rias\StatamicRedirect\Eloquent\Redirects\EloquentRedirectRepository::class,
```
It is also possible to store your redirects in the database. To do this, change the `redirect_connection` config value. Provide `default` to use the default Laravel connection.

> [!NOTE]
> When using a connection other than `stache` or `redirect-sqlite`, make sure to publish (and run) the corresponding migration using `php artisan vendor:publish --tag="statamic-redirect-redirect-migrations"`.
20 changes: 1 addition & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Read more in [the documentation](https://statamic.com/addons/rias/redirect/docs)

## License

Redirect requires a license to be used while on a production site.
Redirect requires a license to be used while on a production site.
You can purchase one at https://statamic.com/addons/rias/redirect.

You may use Redirect without a license while developing locally.
Expand All @@ -32,22 +32,4 @@ Require it using Composer.
composer require rias/statamic-redirect
```

Publish the assets:

```
php artisan vendor:publish --provider="Rias\StatamicRedirect\RedirectServiceProvider"
```

## Using the Eloquent repositories

You can switch the storing & retrieving of Errors & Redirects to the Eloquent repositories provided, this can be useful if you're logging a lot of errors & hits.

```php
// config/statamic/redirect.php

'error_repository' => \Rias\StatamicRedirect\Eloquent\Errors\EloquentErrorRepository::class,

'redirect_repository' => \Rias\StatamicRedirect\Eloquent\Redirects\EloquentRedirectRepository::class,
```

Brought to you by [Rias](https://rias.be)
25 changes: 16 additions & 9 deletions config/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,30 @@
'keep_unique_errors' => 1000,

/*
* The database connection redirect should use to store errors
* by default this is the included 'redirect' connection
* that uses an sqlite database in storage.
* The database connection used to store errors. By default
* this is the included 'redirect-sqlite'. Use
* 'default' to use the Laravel default.
*/
'connection' => 'redirect',
'error_connection' => env('REDIRECT_ERROR_CONNECTION', 'redirect-sqlite'),

/*
* Customize where on filesystem the redirects are being stored
* The database connection used to store redirects. By
* default this is the included 'stache' connection.
* Use 'default' to use the Laravel default.
*/
'redirect_connection' => env('REDIRECT_REDIRECT_CONNECTION', 'stache'),

/*
* Customize where on filesystem the redirects are being stored in stache.
* Useful when using a non-conventional setup where data should
* not be inside the usual content/redirects folder
*/
'redirect_store' => base_path('content/redirects'),

/*
* Customise the redirect repository you want to use for data
* storage. Change to \Rias\StatamicRedirect\Eloquent\Redirects\RedirectRepository::class
* for eloquent storage, or create your own custom repository
* storage. Provide null to automaticly use a repository
* based on the 'redirect_connection'.
*/
'redirect_repository' => \Rias\StatamicRedirect\Stache\Redirects\RedirectRepository::class,
'redirect_repository' => null,
];
30 changes: 0 additions & 30 deletions database/migrations/create_eloquent_redirect_table.php.stub

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Rias\StatamicRedirect\Data\Error;

class CreateRedirectTables extends Migration
class CreateRedirectErrorTables extends Migration
{
public function up()
{
Expand Down
28 changes: 28 additions & 0 deletions database/migrations/create_redirect_redirects_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRedirectRedirectsTable extends Migration
{
public function up()
{
Schema::create('redirects', function (Blueprint $table): void {
$table->uuid('id')->unique()->index();
$table->string('source')->index();
$table->string('destination');
$table->char('match_type', 10);
$table->char('type', 10);
$table->string('site')->index();
$table->integer('order')->nullable();
$table->boolean('enabled');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('redirects');
}
}
10 changes: 9 additions & 1 deletion src/Data/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ class Error extends Model

public function getConnectionName()
{
return config('statamic.redirect.connection', 'redirect');
if (config('statamic.redirect.connection') !== null) {
return config('statamic.redirect.connection');
}

if (config('statamic.redirect.error_connection') === 'default') {
return config('database.default');
}

return config('statamic.redirect.error_connection', 'redirect-sqlite');
}

protected static function booted()
Expand Down
10 changes: 9 additions & 1 deletion src/Data/Hit.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class Hit extends Model

public function getConnectionName()
{
return config('statamic.redirect.connection', 'redirect');
if (config('statamic.redirect.connection') !== null) {
return config('statamic.redirect.connection');
}

if (config('statamic.redirect.error_connection') === 'default') {
return config('database.default');
}

return config('statamic.redirect.error_connection', 'redirect-sqlite');
}
}
14 changes: 11 additions & 3 deletions src/Eloquent/Redirects/RedirectModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
class RedirectModel extends Model
{
use HasUuids;

protected $guarded = [];

protected $casts = [];

protected $table = 'redirects';

public function getConnectionName()
{
return config('statamic.redirect.connection', 'redirect');
if (config('statamic.redirect.connection') !== null) {
return config('statamic.redirect.connection');
}

if (config('statamic.redirect.redirect_connection') === 'default') {
return config('database.default');
}

return config('statamic.redirect.redirect_connection', 'redirect-sqlite');
}
}
Loading

0 comments on commit d2f3d61

Please sign in to comment.