Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions .github/workflows/pest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Pest Tests

on:
pull_request:
branches:
- main
- master

jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [8.3]

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-progress

- name: Run Pest tests
run: vendor/bin/pest
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,46 @@ On this page you can manage the query strings you wish to strip, simply :

You'll see your query string in the list.

### Database Redirects

By default, the addon stores redirects and query strings as YAML files in `content/alt-redirect/`. For containerised deployments or larger sites, you may wish to store these in your database instead.

#### 1. Publish Configuration
If you haven't already, publish the configuration file to your site:

```bash
php artisan vendor:publish --tag=alt-redirect-config
```

#### 2. Run Migrations
While migrations are loaded automatically, you can publish them if you wish to customize them:

```bash
php artisan vendor:publish --tag=alt-redirect-migrations
```

Run the migrations to create the necessary tables:

```bash
php artisan migrate
```

#### 3. Switch Driver
In your `config/alt-redirect.php`, change the `driver` from `file` to `database`:

```php
'driver' => 'database',
```

#### 4. Migrate Existing Data (Optional)
If you already have file-based redirects and want to move them to your database, you can use the following Artisan command:

```bash
php artisan alt-redirect:migrate-file-redirects
```

The command will check if the required tables exist and then migrate all your redirects and query strings.

#### Artisan Command

We have provided an Artisan command to force the creation of defaults.
Expand Down
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"AltDesign\\AltRedirect\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"AltDesign\\AltRedirect\\Tests\\": "tests"
}
},
"require": {
"php": "^8.1",
"statamic/cms": "^6.0"
Expand All @@ -24,7 +29,14 @@
},
"config": {
"allow-plugins": {
"pixelfear/composer-dist-plugin": true
"pixelfear/composer-dist-plugin": true,
"pestphp/pest-plugin": true
}
},
"require-dev": {
"laravel/pint": "^1.29",
"pestphp/pest": "^4.4",
"pestphp/pest-plugin-laravel": "^4.1",
"orchestra/testbench": "^11.1"
}
}
15 changes: 14 additions & 1 deletion config/alt-redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
|
*/

'headers' => []
'headers' => [],

/*
|--------------------------------------------------------------------------
| Driver
|--------------------------------------------------------------------------
|
| Here you may specify the driver to use for storing redirects.
|
| Supported: "file", "database"
|
*/

'driver' => 'file',
// 'driver' => 'database',
];
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_KEY" value="Ackf36aba21d46c79442a47231a670ff"/>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testbench"/>
</php>
</phpunit>
13 changes: 13 additions & 0 deletions resources/blueprints/redirects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ tabs:
hide_display: false
width: 50
default: '301'
-
handle: is_regex
field:
type: toggle
display: 'Is Regex?'
instructions: 'Automatically detected, but can be overridden.'
listable: hidden
instructions_position: above
visibility: visible
replicator_preview: true
hide_display: false
width: 50
default: false
-
handle: sites
field:
Expand Down
3 changes: 2 additions & 1 deletion routes/cp.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

use Illuminate\Support\Facades\Route;

Route::group(['middleware' => ['statamic.cp.authenticated'], 'namespace' => 'AltDesign\AltRedirect\Http\Controllers'], function() {
Route::group(['middleware' => ['statamic.cp.authenticated'], 'namespace' => 'AltDesign\AltRedirect\Http\Controllers'], function () {
// Settings
Route::get('/alt-design/alt-redirect/', 'AltRedirectController@index')->name('alt-redirect.index');
Route::post('/alt-design/alt-redirect/', 'AltRedirectController@create')->name('alt-redirect.create');
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/DefaultQueryStringsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DefaultQueryStringsCommand extends Command
*/
public function handle()
{
if(!$this->confirm('Do you wish to (re)create the list of default query strings?')) {
if (! $this->confirm('Do you wish to (re)create the list of default query strings?')) {
$this->error('User aborted Command');
}

Expand Down
75 changes: 75 additions & 0 deletions src/Console/Commands/MigrateFileRedirectsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace AltDesign\AltRedirect\Console\Commands;

use AltDesign\AltRedirect\Repositories\DatabaseRepository;
use AltDesign\AltRedirect\Repositories\FileRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;

class MigrateFileRedirectsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'alt-redirect:migrate-file-redirects';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrates existing file-based redirects to the database';

/**
* Execute the console command.
*/
public function handle()
{
if (! Schema::hasTable('alt_redirects') || ! Schema::hasTable('alt_query_strings')) {
$this->error('The required database tables do not exist.');
$this->warn('Please run the following commands to set up the database:');
$this->line('1. php artisan vendor:publish --tag=alt-redirect-migrations');
$this->line('2. php artisan migrate');

return 1;
}

if (! $this->confirm('This will migrate all file-based redirects and query strings to the database. Do you wish to continue?')) {
$this->error('User aborted command.');

return 1;
}

$fileRepository = new FileRepository;
$databaseRepository = new DatabaseRepository;

$this->info('Migrating redirects...');
$redirects = $fileRepository->all('redirects');
if (count($redirects) > 0) {
foreach ($redirects as $redirect) {
$databaseRepository->save('redirects', $redirect);
}
$this->info(count($redirects).' redirects migrated.');
} else {
$this->info('No redirects found to migrate.');
}

$this->info('Migrating query strings...');
$queryStrings = $fileRepository->all('query-strings');
if (count($queryStrings) > 0) {
foreach ($queryStrings as $queryString) {
$databaseRepository->save('query-strings', $queryString);
}
$this->info(count($queryStrings).' query strings migrated.');
} else {
$this->info('No query strings found to migrate.');
}

$this->info('Migration completed successfully!');

return 0;
}
}
44 changes: 44 additions & 0 deletions src/Console/Commands/ReScanRegexCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace AltDesign\AltRedirect\Console\Commands;

use AltDesign\AltRedirect\Contracts\RepositoryInterface;
use AltDesign\AltRedirect\Helpers\URISupport;
use Illuminate\Console\Command;

class ReScanRegexCommand extends Command
{
protected $signature = 'alt-redirect:re-scan-regex';

protected $description = 'Re-scans all redirects and updates the is_regex flag based on the latest detection logic.';

public function handle()
{
$repository = app(RepositoryInterface::class);
$redirects = $repository->all('redirects');

$count = 0;
foreach ($redirects as $redirect) {
if (! isset($redirect['from'])) {
$this->error('Redirect missing "from" key: ' . ($redirect['id'] ?? 'unknown ID'));
continue;
}

$isRegexBefore = (bool) ($redirect['is_regex'] ?? false);
$isRegexAfter = URISupport::isRegex($redirect['from']);

if ($isRegexBefore !== $isRegexAfter) {
$redirect['is_regex'] = $isRegexAfter;
$repository->save('redirects', $redirect);
$count++;
}
}

$this->info("Successfully updated $count redirects.");
}

protected function isRegex(string $str): bool
{
return URISupport::isRegex($str);
}
}
18 changes: 18 additions & 0 deletions src/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace AltDesign\AltRedirect\Contracts;

interface RepositoryInterface
{
public function all(string $type): array;

public function getRegex(string $type): array;

public function find(string $type, string $key, $value): ?array;

public function save(string $type, array $data): void;

public function saveAll(string $type, array $data): void;

public function delete(string $type, array $data): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('alt_redirects', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('from')->index();
$table->string('to');
$table->integer('redirect_type')->default(301);
$table->boolean('is_regex')->default(false);
$table->json('sites')->nullable();
$table->timestamps();
});

Schema::create('alt_query_strings', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('query_string')->index();
$table->boolean('strip')->default(false);
$table->json('sites')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('alt_redirects');
Schema::dropIfExists('alt_query_strings');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use AltDesign\AltRedirect\Helpers\DefaultQueryStrings;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
(new DefaultQueryStrings)->makeDefaultQueryStrings();
}

/**
* Reverse the migrations.
*/
public function down(): void
{
// No need to remove them on down, they are part of the table data
}
};
Loading
Loading