Skip to content

Commit

Permalink
Merge pull request #13 from TheDragonCode/2.x
Browse files Browse the repository at this point in the history
Added ignoring certain URL keys
  • Loading branch information
Andrey Helldar committed Nov 17, 2021
2 parents 7c597fa + 8ab1ba7 commit 0b0ed6f
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"doctrine/dbal": "^2.6|^3.0",
"dragon-code/laravel-cache": "^2.3",
"dragon-code/simple-data-transfer-object": "^2.0",
"dragon-code/support": "^5.0",
"dragon-code/support": "^5.2",
"illuminate/console": "^6.0|^7.0|^8.0",
"illuminate/database": "^6.0|^7.0|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0",
Expand Down
24 changes: 24 additions & 0 deletions config/last_modified.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,28 @@

'ttl' => 43200,
],

/*
* This option contains settings for working with requests.
*/

'requests' => [

/*
* This option contains ignore settings.
*/

'ignore' => [

/*
* This option contains key patterns to be ignored when retrieving the hash.
*
* This is especially useful for SEO.
*
* For example,
* ['qwe', '*led', 'dat*', '*ifi*']
*/
'keys' => [],
],
],
];
10 changes: 4 additions & 6 deletions src/Concerns/Urlable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@

namespace DragonCode\LastModified\Concerns;

use DragonCode\Support\Facades\Http\Builder;
use DragonCode\LastModified\Facades\Url;
use DragonCode\Support\Helpers\Http\Builder as BuilderService;

trait Urlable
{
protected function parseUrl($url): BuilderService
{
return Builder::parse($url);
return Url::parse($url);
}

protected function hashUrl($uri): string
protected function hashUrl($url): string
{
$url = $this->parseUrl($uri)->toUrl();

return md5($url);
return Url::hash($url);
}
}
1 change: 1 addition & 0 deletions src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static array requestIgnoreKeys()
* @method static bool disabled()
* @method static bool enabled()
* @method static int cacheTtl()
Expand Down
37 changes: 37 additions & 0 deletions src/Facades/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the "dragon-code/last-modified" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/last-modified
*/

declare(strict_types=1);

namespace DragonCode\LastModified\Facades;

use DragonCode\LastModified\Support\Url as Support;
use DragonCode\Support\Helpers\Http\Builder;
use Illuminate\Support\Facades\Facade;
use Psr\Http\Message\UriInterface;

/**
* @method static Builder parse(string $url)
* @method static string hash(UriInterface|string $url)
*/
class Url extends Facade
{
protected static function getFacadeAccessor(): string
{
return Support::class;
}
}
5 changes: 5 additions & 0 deletions src/Support/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function cacheTtl(): int
return $this->getInteger($value, 43200);
}

public function requestIgnoreKeys(): array
{
return config('last_modified.requests.ignore.keys', []);
}

protected function getInteger(int $value, int $default): int
{
return abs($value) > 1 ? abs($value) : $default;
Expand Down
63 changes: 63 additions & 0 deletions src/Support/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the "dragon-code/last-modified" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/last-modified
*/

declare(strict_types=1);

namespace DragonCode\LastModified\Support;

use DragonCode\LastModified\Facades\Config as ConfigSupport;
use DragonCode\Support\Facades\Helpers\Ables\Arrayable;
use DragonCode\Support\Facades\Helpers\Str;
use DragonCode\Support\Facades\Http\Builder;
use DragonCode\Support\Helpers\Http\Builder as HttpBuilder;

class Url
{
public function parse(string $url): HttpBuilder
{
return Builder::parse($url);
}

public function hash($url): string
{
$uri = $this->parse($url);

$query = $this->filterQuery($uri);

$uri->withQuery($query);

return md5($uri->toUrl());
}

protected function filterQuery(HttpBuilder $uri): array
{
if ($keys = $this->getIgnoreKeys()) {
return Arrayable::of($uri->getQueryArray())
->filter(static function ($key) use ($keys) {
return ! Str::is($keys, $key);
}, ARRAY_FILTER_USE_KEY)
->get();
}

return [];
}

protected function getIgnoreKeys(): array
{
return ConfigSupport::requestIgnoreKeys();
}
}
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
use DragonCode\LastModified\ServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Tests\Concerns\Fakeable;
use Tests\Concerns\Asserts;
use Tests\Concerns\Fakeable;
use Tests\Concerns\Requests;
use Tests\Concerns\Urlable;
use Tests\fixtures\Providers\TestServiceProvider;
Expand Down Expand Up @@ -82,5 +82,7 @@ protected function setConfig($app): void
$config->set('last_modified.database.chunk', 20);

$config->set('last_modified.enabled', $this->enabled);

$config->set('last_modified.requests.ignore.keys', ['qwe', '*led', 'dat*', '*ifi*']);
}
}
58 changes: 58 additions & 0 deletions tests/WhenDisabled/Support/UrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the "dragon-code/last-modified" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/last-modified
*/

declare(strict_types=1);

namespace Tests\WhenDisabled\Support;

use DragonCode\LastModified\Facades\Url;
use Tests\WhenEnabled\TestCase;

class UrlTest extends TestCase
{
public function testParse()
{
$url = 'https://example.com/foo/bar?id=1&qwe=rty';

$value = Url::parse($url);

$this->assertSame($url, $value->toUrl());
}

public function testHash()
{
$items = [
'https://example.com/foo/bar?id=1&foo=bar' => 'ce620bb9be7299260fd7144652d421f9',
'https://example.com/foo/bar?id=1&qwe=rty&amoleds=123' => '88a3e09cfc343a4d63671da376463579',
'https://example.com/foo/bar?id=1&qwe=rty&details=123' => '3217e42fb45cf73fe6d7fb2a3a4a77f8',

'https://example.com/foo/bar?id=1&qwe=rty&modified=123' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1&qwe=rty&database=123' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1&qwe=rty&amoled=123' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1&qwe=rty' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1' => '66691dbad27d95fa743d5c09761fd0cd',

'https://example.com/foo/bar' => 'f6c18df6c8b2aa33d62818079fe6815d',
];

foreach ($items as $url => $hash) {
$message = sprintf('Url %s has an invalid hash', $url);

$this->assertSame($hash, Url::hash($url), $message);
}
}
}
58 changes: 58 additions & 0 deletions tests/WhenEnabled/Support/UrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the "dragon-code/last-modified" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@ai-rus.com>
*
* @copyright 2021 Andrey Helldar
*
* @license MIT
*
* @see https://github.com/TheDragonCode/last-modified
*/

declare(strict_types=1);

namespace Tests\WhenEnabled\Support;

use DragonCode\LastModified\Facades\Url;
use Tests\WhenEnabled\TestCase;

class UrlTest extends TestCase
{
public function testParse()
{
$url = 'https://example.com/foo/bar?id=1&qwe=rty';

$value = Url::parse($url);

$this->assertSame($url, $value->toUrl());
}

public function testHash()
{
$items = [
'https://example.com/foo/bar?id=1&foo=bar' => 'ce620bb9be7299260fd7144652d421f9',
'https://example.com/foo/bar?id=1&qwe=rty&amoleds=123' => '88a3e09cfc343a4d63671da376463579',
'https://example.com/foo/bar?id=1&qwe=rty&details=123' => '3217e42fb45cf73fe6d7fb2a3a4a77f8',

'https://example.com/foo/bar?id=1&qwe=rty&modified=123' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1&qwe=rty&database=123' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1&qwe=rty&amoled=123' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1&qwe=rty' => '66691dbad27d95fa743d5c09761fd0cd',
'https://example.com/foo/bar?id=1' => '66691dbad27d95fa743d5c09761fd0cd',

'https://example.com/foo/bar' => 'f6c18df6c8b2aa33d62818079fe6815d',
];

foreach ($items as $url => $hash) {
$message = sprintf('Url %s has an invalid hash', $url);

$this->assertSame($hash, Url::hash($url), $message);
}
}
}

0 comments on commit 0b0ed6f

Please sign in to comment.