Skip to content

Commit

Permalink
Merge pull request #99 from ash-jc-allen/v6.0.0
Browse files Browse the repository at this point in the history
V6.0.0
  • Loading branch information
ash-jc-allen authored Oct 21, 2021
2 parents 1d32773 + 5dc1b4b commit ac3ab70
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 20 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/ci-phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 7.3, 7.4, 8.0 ]
laravel: [ 8.*, 7.*, 6.* ]
php: [8.0]
laravel: [8.*]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*

name: PHP${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.3, 7.4, 8.0]
laravel: [8.*, 7.*, 6.*]
php: [8.0]
laravel: [8.*]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*

name: PHP${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

**v6.0.0 (released 2021-10-21):**
- Added the ability to forward query parameters to the destination URL. [#94](https://github.com/ash-jc-allen/short-url/pull/94)
- Dropped support for Laravel 6, 7. [#96](https://github.com/ash-jc-allen/short-url/pull/96), [#98](https://github.com/ash-jc-allen/short-url/pull/98)
- Dropped support for PHP 7.3, 7.4. [#85](https://github.com/ash-jc-allen/short-url/pull/85)

**v5.2.0 (released 2021-09-21):**
- Updated the migration for the `short_urls` table so that `url_key` is now unique and `destination_url` is now a TEXT field rather than varchar. [#80](https://github.com/ash-jc-allen/short-url/pull/80)
- Added the ability to configure the alphabet used for generating keys with `hashids`. [#77](https://github.com/ash-jc-allen/short-url/pull/77)
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [Tracking Referer URL](#tracking-referer-url)
- [Single Use](#single-use)
- [Enforce HTTPS](#enforce-https)
- [Forwarding Query Parameters](#forwarding-query-parameters)
- [Redirect Status Code](#redirect-status-code)
- [Activation and Deactivation Times](#activation-and-deactivation-times)
- [Facade](#facade)
Expand Down Expand Up @@ -70,8 +71,8 @@ A Laravel package that can be used for adding shortened URLs to your existing we
### Requirements
The package has been developed and tested to work with the following minimum requirements:

- PHP 7.2
- Laravel 6.0
- PHP 8.0
- Laravel 8.0

Short URL requires either the [BC Math](https://secure.php.net/manual/en/book.bc.php) or [GMP](https://secure.php.net/manual/en/book.gmp.php) PHP extensions in order to work.

Expand Down Expand Up @@ -261,6 +262,19 @@ $shortURLObject = $builder->destinationUrl('http://destination.com')->secure()->
// Destination URL: https://destination.com
```

#### Forwarding Query Parameters
When building a short URL, you might want to forward the query parameters sent in the request to destination URL. By default, this functionality is disabled, but can be enabled by setting the `forward_query_params` config option to `true`.

Alternatively, you can also use the `->forwardQueryParams()` method when building your shortened URL, as shown in the example below:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();

$shortURLObject = $builder->destinationUrl('http://destination.com?param1=test')->forwardQueryParams()->make();
```

Based on the example above, assuming that the original short URL's `destination_url` was `https://destination.com`, making a request to `https://webapp.com/short/xxx?param1=abc&param2=def` would redirect to `https://destination.com?param1=test&param2=def`

#### Redirect Status Code

By default, all short URLs are redirected with a ``` 301 ``` HTTP status code. But, this can be overridden when building
Expand Down Expand Up @@ -533,6 +547,7 @@ Note: A contribution guide will be added soon.
- [Nathan Giesbrecht](https://github.com/NathanGiesbrecht)
- [Carlos A. Escobar](https://github.com/carlosjs23)
- [Victor-Emil Rossil Andersen](https://github.com/Victor-emil)
- [Julien Arcin](https://github.com/julienarcin)
- [All Contributors](https://github.com/ash-jc-allen/short-url/graphs/contributors)

## Changelog
Expand Down
42 changes: 41 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
# Upgrade Guide

## Contents
- [Upgrading from 5.* to 6.0.0](#upgrading-from-5-to-600)
- [Upgrading from 4.* to 5.0.0](#upgrading-from-4-to-500)
- [Upgrading from 3.* to 4.0.0](#upgrading-from-3-to-400)
- [Upgrading from 2.* to 3.0.0](#upgrading-from-2-to-300)
- [Upgrading from 1.* to 2.0.0](#upgrading-from-1-to-200)

## Upgrading from 5.* to 6.0.0

### Laravel - Minimum Required Version

As of Short URL v6.0.0, Laravel 6.0 and 7.0 are no longer supported. Therefore, you must be using a minimum of Laravel 8.0 to use this library.

### PHP - Minimum Required Version

As of Short URL v6.0.0, PHP 7.3 and 7.4 are no longer supported. Therefore, you must be using a minimum of PHP 8.0 to use this library.

### New Config Variable and Migration

As of Short URL v6.0.0, you can now forward query parameters from your request onto the destination URL. This feature requires that you run a new migration to add the `forward_query_params` field to your `short_urls` table.

To publish the migration to your own `database/migrations` folder, run the following command in your project root:

```bash
php artisan vendor:publish --tag="short-url-migrations"
```

There is also a new `forward_query_params` config option (that defaults to `false`) for controlling the default behaviour of this feature. If you wish to override this option, you can add the following to your own config:

```php
/*
|--------------------------------------------------------------------------
| Forwards query parameters
|--------------------------------------------------------------------------
|
| Here you can specify if the newly created short URLs will forward
| the query parameters to the destination by default. This option
| can be overridden when creating the short URL with the
| ->forwardQueryParams() method.
|
| eg: https://yoursite.com/short/xxx?a=b => https://destination.com/page?a=b
|
*/
'forward_query_params' => false,
```

## Upgrading from 4.* to 5.0.0

### Publish Migrations
Expand Down Expand Up @@ -106,4 +146,4 @@ You can add these options to your config file like shown below:
'device_type' => true,
],
],
```
```
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
"laravel-package"
],
"require": {
"php": "^7.2|^8.0",
"php": "^8.0",
"nesbot/carbon": "~2.0",
"illuminate/container": "^6.0|^7.0|^8.0",
"illuminate/database": "^6.0|^7.0|^8.0",
"illuminate/container": "^8.0",
"illuminate/database": "^8.0",
"jenssegers/agent": "^2.6",
"hashids/hashids": "^4.0"
},
"require-dev": {
"mockery/mockery": "^1.0",
"orchestra/testbench": "^4.0|^5.0|^6.0",
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^8.2",
"nunomaduro/larastan": "^0.7.12"
},
Expand Down
15 changes: 15 additions & 0 deletions config/short-url.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
*/
'disable_default_route' => false,

/*
|--------------------------------------------------------------------------
| Forwards query parameters
|--------------------------------------------------------------------------
|
| Here you can specify if the newly created short URLs will forward
| the query parameters to the destination by default. This option
| can be overridden when creating the short URL with the
| ->forwardQueryParams() method.
|
| eg: https://yoursite.com/short/xxx?a=b => https://destination.com/page?a=b
|
*/
'forward_query_params' => false,

/*
|--------------------------------------------------------------------------
| Enforce HTTPS in the destination URL
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

class UpdateShortUrlTableAddOptionToForwardQueryParams extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('short_urls', function (Blueprint $table) {
$table->boolean('forward_query_params')->after('single_use')->default(false);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('short_urls', function (Blueprint $table) {
$table->dropColumn(['forward_query_params']);
});
}
}
29 changes: 29 additions & 0 deletions src/Classes/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class Builder
*/
protected $secure;

/**
* Whether or not the short url whould
* forward query params to the
* destination url.
*
* @var bool|null
*/
protected $forwardQueryParams;

/**
* Whether or not if the short URL should track
* statistics about the visitors.
Expand Down Expand Up @@ -209,6 +218,20 @@ public function secure(bool $isSecure = true): self
return $this;
}

/**
* Set whether if the short URL should forward
* query params to the destination URL.
*
* @param bool $shouldForwardQueryParams
* @return Builder
*/
public function forwardQueryParams(bool $shouldForwardQueryParams = true): self
{
$this->forwardQueryParams = $shouldForwardQueryParams;

return $this;
}

/**
* Set whether if the short URL should track some
* statistics of the visitors.
Expand Down Expand Up @@ -434,6 +457,7 @@ protected function insertShortURLIntoDatabase(): ShortURL
'default_short_url' => config('app.url').'/short/'.$this->urlKey,
'url_key' => $this->urlKey,
'single_use' => $this->singleUse,
'forward_query_params' => $this->forwardQueryParams,
'track_visits' => $this->trackVisits,
'redirect_status_code' => $this->redirectStatusCode,
'track_ip_address' => $this->trackIPAddress,
Expand Down Expand Up @@ -476,6 +500,10 @@ private function setOptions(): void
$this->destinationUrl = str_replace('http://', 'https://', $this->destinationUrl);
}

if ($this->forwardQueryParams === null) {
$this->forwardQueryParams = config('short-url.forward_query_params') ?? false;
}

if (! $this->urlKey) {
$this->urlKey = $this->keyGenerator->generateRandom();
}
Expand Down Expand Up @@ -540,6 +568,7 @@ public function resetOptions(): self
$this->urlKey = null;
$this->singleUse = false;
$this->secure = null;
$this->forwardQueryParams = null;
$this->redirectStatusCode = 301;

$this->trackVisits = null;
Expand Down
19 changes: 18 additions & 1 deletion src/Classes/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function validateConfig(): bool
&& $this->validateTrackingOptions()
&& $this->validateDefaultRouteOption()
&& $this->validateKeySalt()
&& $this->validateEnforceHttpsOption();
&& $this->validateEnforceHttpsOption()
&& $this->validateForwardQueryParamsOption();
}

/**
Expand Down Expand Up @@ -126,4 +127,20 @@ protected function validateEnforceHttpsOption(): bool

return true;
}

/**
* Validate that the forward query params option is a boolean.
*
* @return bool
*
* @throws ValidationException
*/
protected function validateForwardQueryParamsOption(): bool
{
if (! is_bool(config('short-url.forward_query_params'))) {
throw new ValidationException('The forward_query_params config variable must be a boolean.');
}

return true;
}
}
25 changes: 25 additions & 0 deletions src/Controllers/ShortURLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ public function __invoke(Request $request, Resolver $resolver, string $shortURLK

$resolver->handleVisit(request(), $shortURL);

if ($shortURL->forward_query_params) {
return redirect($this->forwardQueryParams($request, $shortURL), $shortURL->redirect_status_code);
}

return redirect($shortURL->destination_url, $shortURL->redirect_status_code);
}

/**
* Add the query parameters from the request to the end of the
* destination URL that the user is to be forwarded to.
*
* @param Request $request
* @param ShortURL $shortURL
* @return string
*/
private function forwardQueryParams(Request $request, ShortURL $shortURL): string
{
$queryString = parse_url($shortURL->destination_url, PHP_URL_QUERY);

if (empty($request->query())) {
return $shortURL->destination_url;
}

$separator = $queryString ? '&' : '?';

return $shortURL->destination_url.$separator.http_build_query($request->query());
}
}
4 changes: 4 additions & 0 deletions src/Models/ShortURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\URL;

/**
* Class ShortURL.
Expand All @@ -15,6 +16,7 @@
* @property string $default_short_url
* @property string $url_key
* @property bool $single_use
* @property bool $forward_query_params
* @property bool $track_visits
* @property int $redirect_status_code
* @property bool $track_ip_address
Expand Down Expand Up @@ -48,6 +50,7 @@ class ShortURL extends Model
'default_short_url',
'url_key',
'single_use',
'forward_query_params',
'track_visits',
'redirect_status_code',
'track_ip_address',
Expand Down Expand Up @@ -80,6 +83,7 @@ class ShortURL extends Model
*/
protected $casts = [
'single_use' => 'boolean',
'forward_query_parameters' => 'boolean',
'track_visits' => 'boolean',
'track_ip_address' => 'boolean',
'track_operating_system' => 'boolean',
Expand Down
Loading

0 comments on commit ac3ab70

Please sign in to comment.