Commit e6480938 authored by Alasdair Keyes's avatar Alasdair Keyes

Move helper functions into class to fix autoload issue

* helper.php migrated into RedirectTokenHelper class
* Added use of Laravel Aliases
parent 998cff95
...@@ -26,25 +26,34 @@ This redirects website visitors to the URI with a 302 redirect. If an invalid re ...@@ -26,25 +26,34 @@ This redirects website visitors to the URI with a 302 redirect. If an invalid re
`php artisan config:cache` `php artisan config:cache`
* Add the Service Provider **If you're using Laravel >=5.5 you can stop here. The rest of the setup will auto-load.**
For Lumen and Laravel <5.5 do the following...
If you're using Laravel >=5.5 you can stop here. The Service Provider will auto-load. * Add the Service Provider
For Lumen and Laravel <5.5 add the Class to your `app/config.php` config file Add the Class to your `app/config.php` config file
``` ```
return [ 'providers' => [
// Other configurations above... // Other providers above...
RedirectToken\Laravel\Providers\RedirectTokenServiceProvider::class,
// Other providers below...
],
```
'providers' => [ * Add the Aliases
// Other providers above...
RedirectToken\Laravel\Providers\RedirectTokenServiceProvider::class, Add the Alias to your `app/config.php` config file
// Other providers below...
],
// Other configurations below...
];
``` ```
'aliases' => [
// Other aliases above...
'RedirectTokenHelper' => RedirectToken\Laravel\Support\RedirectTokenHelper::class,
// Other aliases below...
],
```
## Example ## Example
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
}, },
"autoload": { "autoload": {
"files": [ "files": [
"src/Support/helpers.php"
], ],
"psr-4": { "psr-4": {
"RedirectToken\\": "src/" "RedirectToken\\": "src/"
...@@ -33,7 +32,10 @@ ...@@ -33,7 +32,10 @@
"laravel": { "laravel": {
"providers": [ "providers": [
"RedirectToken\\Laravel\\Providers\\RedirectTokenServiceProvider" "RedirectToken\\Laravel\\Providers\\RedirectTokenServiceProvider"
] ],
"aliases": {
"RedirectTokenHelper": "RedirectToken\\Laravel\\Support\\RedirectTokenHelper"
}
} }
}, },
"require-dev": { "require-dev": {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "8ae682c6f09a87d9d2db5d41b99610a7", "content-hash": "3963db5f0703db8454d39293a874ca16",
"packages": [ "packages": [
{ {
"name": "alasdairkeyes/redirecttoken", "name": "alasdairkeyes/redirecttoken",
...@@ -4472,20 +4472,21 @@ ...@@ -4472,20 +4472,21 @@
}, },
{ {
"name": "webmozart/assert", "name": "webmozart/assert",
"version": "1.3.0", "version": "1.4.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/webmozart/assert.git", "url": "https://github.com/webmozart/assert.git",
"reference": "0df1908962e7a3071564e857d86874dad1ef204a" "reference": "83e253c8e0be5b0257b881e1827274667c5c17a9"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/webmozart/assert/zipball/0df1908962e7a3071564e857d86874dad1ef204a", "url": "https://api.github.com/repos/webmozart/assert/zipball/83e253c8e0be5b0257b881e1827274667c5c17a9",
"reference": "0df1908962e7a3071564e857d86874dad1ef204a", "reference": "83e253c8e0be5b0257b881e1827274667c5c17a9",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": "^5.3.3 || ^7.0" "php": "^5.3.3 || ^7.0",
"symfony/polyfill-ctype": "^1.8"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^4.6", "phpunit/phpunit": "^4.6",
...@@ -4518,7 +4519,7 @@ ...@@ -4518,7 +4519,7 @@
"check", "check",
"validate" "validate"
], ],
"time": "2018-01-29T19:49:41+00:00" "time": "2018-12-25T11:19:39+00:00"
} }
], ],
"aliases": [], "aliases": [],
......
...@@ -4,6 +4,7 @@ namespace RedirectToken\Laravel\Http\Controllers; ...@@ -4,6 +4,7 @@ namespace RedirectToken\Laravel\Http\Controllers;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use RedirectToken\Laravel\Support\RedirectTokenHelper;
use RedirectToken\Laravel\Enum; use RedirectToken\Laravel\Enum;
use RedirectToken\Laravel\Event\ValidRedirectRequest; use RedirectToken\Laravel\Event\ValidRedirectRequest;
use RedirectToken\Laravel\Event\InvalidRedirectRequest; use RedirectToken\Laravel\Event\InvalidRedirectRequest;
...@@ -27,7 +28,7 @@ class RedirectTokenController extends Controller ...@@ -27,7 +28,7 @@ class RedirectTokenController extends Controller
$token = $request->input(config('redirecttoken.token_query_key')); $token = $request->input(config('redirecttoken.token_query_key'));
// Validate and redirect // Validate and redirect
if (redirectTokenValidate($uriString, $token)) { if (RedirectTokenHelper::redirectTokenValidate($uriString, $token)) {
event(new ValidRedirectRequest($uriString, $token, $request)); event(new ValidRedirectRequest($uriString, $token, $request));
return redirect()->away($uriString); return redirect()->away($uriString);
} }
......
...@@ -37,7 +37,7 @@ class RedirectTokenServiceProvider extends ServiceProvider ...@@ -37,7 +37,7 @@ class RedirectTokenServiceProvider extends ServiceProvider
// Define the redirectUrl() directive // Define the redirectUrl() directive
$this->compiler()->directive('redirectUrl', function ($urlToForwardTo) { $this->compiler()->directive('redirectUrl', function ($urlToForwardTo) {
return '<?php echo redirectTokenGenerateUri(' return '<?php echo RedirectTokenHelper::redirectTokenGenerateUri('
. $urlToForwardTo . ', ' . $urlToForwardTo . ', '
. "config('redirecttoken.path'), " . "config('redirecttoken.path'), "
. "config('redirecttoken.uri_query_key'), " . "config('redirecttoken.uri_query_key'), "
......
<?php
namespace RedirectToken\Laravel\Support;
use GuzzleHttp\Psr7\Uri;
/**
* Helper functions for RedirectToken in Laravel
*/
class RedirectTokenHelper
{
/**
* Helper function to generate a token for a given URI
*
* @param string $urlToForwardTo
* @return string
*/
public static function redirectTokenGenerate($urlToForwardTo)
{
/**
* @var RedirectToken\Generator $generator
*/
$generator = app('redirectTokenGenerator');
$uri = new Uri($urlToForwardTo);
return $generator->generateToken($uri);
}
/**
* Helper function to validate given URI/Token
*
* @param string $urlToForwardTo
* @param string $token
* @return bool
*/
public static function redirectTokenValidate($urlToForwardTo, $token)
{
/**
* @var RedirectToken\Validator $validator
*/
$validator = app('redirectTokenValidator');
$uri = new Uri($urlToForwardTo);
return $validator->validateUriToken($uri, $token);
}
/**
* Generate a the forward path for a URI
*
* @param string $urlToForwardTo
* @return string
*/
public static function redirectTokenGenerateUri($urlToForwardTo)
{
/**
* @var string $token
*/
$token = self::redirectTokenGenerate($urlToForwardTo);
$fwdPath = config('redirecttoken.path');
$uriQueryKey = config('redirecttoken.uri_query_key');
$tokenQueryKey = config('redirecttoken.token_query_key');
return $fwdPath . '?' . http_build_query([ $uriQueryKey => $urlToForwardTo, $tokenQueryKey => $token], '', '&');
}
}
<?php
use GuzzleHttp\Psr7\Uri;
/**
* Helper functions for RedirectToken in Laravel
*/
/**
* Helper function to generate a token for a given URI
*
* @param string $urlToForwardTo
* @return string
*/
function redirectTokenGenerate($urlToForwardTo)
{
/**
* @var RedirectToken\Generator $generator
*/
$generator = app('redirectTokenGenerator');
$uri = new Uri($urlToForwardTo);
return $generator->generateToken($uri);
}
/**
* Helper function to validate given URI/Token
*
* @param string $urlToForwardTo
* @param string $token
* @return bool
*/
function redirectTokenValidate($urlToForwardTo, $token)
{
/**
* @var RedirectToken\Validator $validator
*/
$validator = app('redirectTokenValidator');
$uri = new Uri($urlToForwardTo);
return $validator->validateUriToken($uri, $token);
}
/**
* Generate a the forward path for a URI
*
* @param string $urlToForwardTo
* @return string
*/
function redirectTokenGenerateUri($urlToForwardTo)
{
/**
* @var string $token
*/
$token = redirectTokenGenerate($urlToForwardTo);
$fwdPath = config('redirecttoken.path');
$uriQueryKey = config('redirecttoken.uri_query_key');
$tokenQueryKey = config('redirecttoken.token_query_key');
return $fwdPath . '?' . http_build_query([ $uriQueryKey => $urlToForwardTo, $tokenQueryKey => $token], '', '&');
}
...@@ -13,7 +13,7 @@ class BladeDirectiveTest extends TestCase ...@@ -13,7 +13,7 @@ class BladeDirectiveTest extends TestCase
$compiler = $this->getCompiler(); $compiler = $this->getCompiler();
$actual = $compiler->compileString('@redirectUrl("https:://www.fsf.org/")'); $actual = $compiler->compileString('@redirectUrl("https:://www.fsf.org/")');
$expected = sprintf( $expected = sprintf(
"<?php echo redirectTokenGenerateUri(%s, %s, %s, %s); ?>", "<?php echo RedirectTokenHelper::redirectTokenGenerateUri(%s, %s, %s, %s); ?>",
'"https:://www.fsf.org/"', '"https:://www.fsf.org/"',
"config('redirecttoken.path')", "config('redirecttoken.path')",
"config('redirecttoken.uri_query_key')", "config('redirecttoken.uri_query_key')",
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Test; namespace Test;
use RedirectToken\Laravel\Support\RedirectTokenHelper;
/** /**
* Class HelperTest * Class HelperTest
* @package Tests * @package Tests
...@@ -10,25 +12,25 @@ class HelperTest extends TestCase ...@@ -10,25 +12,25 @@ class HelperTest extends TestCase
{ {
public function testRedirectTokenGenerateHelper() public function testRedirectTokenGenerateHelper()
{ {
$actual = redirectTokenGenerate(self::TEST_REDIRECT_DESTINATION); $actual = RedirectTokenHelper::redirectTokenGenerate(self::TEST_REDIRECT_DESTINATION);
$this->assertSame(self::VALID_URL_TOKEN, $actual); $this->assertSame(self::VALID_URL_TOKEN, $actual);
} }
public function testRedirectTokenValidateHelperValidToken() public function testRedirectTokenValidateHelperValidToken()
{ {
$actual = redirectTokenValidate(self::TEST_REDIRECT_DESTINATION, self::VALID_URL_TOKEN); $actual = RedirectTokenHelper::redirectTokenValidate(self::TEST_REDIRECT_DESTINATION, self::VALID_URL_TOKEN);
$this->assertTrue($actual); $this->assertTrue($actual);
} }
public function testRedirectTokenValidateHelperInvalidToken() public function testRedirectTokenValidateHelperInvalidToken()
{ {
$actual = redirectTokenValidate(self::TEST_REDIRECT_DESTINATION, self::INVALID_URL_TOKEN); $actual = RedirectTokenHelper::redirectTokenValidate(self::TEST_REDIRECT_DESTINATION, self::INVALID_URL_TOKEN);
$this->assertFalse($actual); $this->assertFalse($actual);
} }
public function testRedirectTokenGenerateUri() public function testRedirectTokenGenerateUri()
{ {
$actual = redirectTokenGenerateUri(self::TEST_REDIRECT_DESTINATION); $actual = RedirectTokenHelper::redirectTokenGenerateUri(self::TEST_REDIRECT_DESTINATION);
$this->assertSame( $this->assertSame(
self::VALID_REDIRECTION_URI, self::VALID_REDIRECTION_URI,
$actual $actual
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment