Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DX using connections. #159

Merged
merged 39 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3dfde9b
Fix date error on error widget
vluijkx Jan 6, 2024
b650b13
Improve plus icon alignment
vluijkx Jan 6, 2024
dbff7f0
Fix validation for eloquent redirects
vluijkx Jan 6, 2024
34c47b8
Revert "Fix validation for eloquent redirects"
vluijkx Jan 6, 2024
559d653
Merge branch 'fix/plus-icon-alignment'
vluijkx Jan 6, 2024
8675e40
Merge branch 'fix-errors-widget'
vluijkx Jan 6, 2024
c732f1e
Do not use redirect connection for eloquent
vluijkx Jan 6, 2024
bbf504f
Merge branch 'split-connection-config'
vluijkx Jan 6, 2024
397cce2
Small improvements
vluijkx Jan 6, 2024
fe37601
Revert "Small improvements"
vluijkx Jan 7, 2024
217bc70
Split connections
vluijkx Jan 7, 2024
eb035ff
Improve alignment plus icon on errors widget as well
vluijkx Jan 7, 2024
750f3af
Add back right-margins
vluijkx Jan 8, 2024
6822080
Clearify migration names and use seperate publish tag
vluijkx Jan 8, 2024
0e161e0
Update documentation for split connections
vluijkx Jan 8, 2024
306460f
Allow a `default` config value to use laravel default connection
vluijkx Jan 8, 2024
9a30148
Fix styling
vluijkx Jan 8, 2024
43190ed
Fix heading indentation
vluijkx Jan 8, 2024
6540148
Merge branch 'split-connections' of https://github.com/vluijkx/statam…
vluijkx Jan 8, 2024
17c7365
Make quotes more consistent
vluijkx Jan 8, 2024
1a24338
Revert "Make quotes more consistent"
vluijkx Jan 8, 2024
14907ab
Small text improvements
vluijkx Jan 8, 2024
94ce603
Use correct migration names
vluijkx Jan 8, 2024
17cbd61
Remove outdated informatiion.
vluijkx Jan 8, 2024
63168ef
Capitalize `Laravel`
vluijkx Jan 8, 2024
6f7eddb
Text improvement
vluijkx Jan 8, 2024
b28dd2b
Fix typo
vluijkx Jan 8, 2024
7af834d
Rename database from `errors` to `redirect` if needed
vluijkx Jan 8, 2024
67de41d
Merge branch 'main' into split-connections
vluijkx Jan 8, 2024
4e88cf9
Fix tests
vluijkx Jan 8, 2024
e219793
Merge remote-tracking branch 'upstream/main' into split-connections
vluijkx Jan 8, 2024
345f2dc
Merge branch 'fix/plus-icon-alignment' into split-connections
vluijkx Jan 8, 2024
f1d63a2
Add backwards compatibility for `connection` configuration
vluijkx Jan 8, 2024
42f2bcd
Improve backward compatibility
vluijkx Jan 8, 2024
53ccb89
Fix styling
vluijkx Jan 8, 2024
3b99f30
Use singular version which reads better
vluijkx Jan 8, 2024
ba7ee38
Merge branch 'split-connections' of https://github.com/vluijkx/statam…
vluijkx Jan 8, 2024
2ff38e6
Improve readability
vluijkx Jan 9, 2024
e532294
Code style fix
vluijkx Jan 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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