Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4af0c20

Browse files
authored
Adding Plugin support (#742)
* Adding Plugin support * Applied changes from StyleCI * Add deps to main composer.json * Fixed the tests and use stable version of php-cache * Minor
1 parent b0e03ab commit 4af0c20

26 files changed

+1359
-3
lines changed

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"php-http/client-implementation": "^1.0",
2020
"php-http/message-factory": "^1.0.2",
2121
"php-http/httplug": "^1.0",
22-
"php-http/discovery": "^1.0"
22+
"php-http/discovery": "^1.0",
23+
"psr/simple-cache": "^1.0",
24+
"php-http/promise": "^1.0"
2325
},
2426
"require-dev": {
2527
"phpunit/phpunit": "^6.1",
@@ -31,8 +33,9 @@
3133
"php-http/mock-client": "^1.0",
3234
"nyholm/psr7": "^0.2.2",
3335
"nyholm/nsa": "^1.1",
34-
"cache/simple-cache-bridge": "^0.1.1",
35-
"cache/array-adapter": "^0.5.0"
36+
"cache/simple-cache-bridge": "^1.0",
37+
"cache/array-adapter": "^1.0",
38+
"cache/void-adapter": "^1.0"
3639
},
3740
"suggest": {
3841
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",
@@ -52,6 +55,7 @@
5255
"test": "vendor/bin/phpunit"
5356
},
5457
"minimum-stability": "dev",
58+
"prefer-dist": true,
5559
"extra": {
5660
"branch-alias": {
5761
"dev-master": "4.0-dev"

src/Plugin/.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.gitattributes export-ignore
2+
.travis.yml export-ignore
3+
phpunit.xml.dist export-ignore
4+
Tests/ export-ignore

src/Plugin/.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: php
2+
sudo: false
3+
4+
php: 7.0
5+
6+
install:
7+
- composer update --prefer-stable --prefer-dist
8+
9+
script:
10+
- composer test-ci
11+
12+
after_success:
13+
- wget https://scrutinizer-ci.com/ocular.phar
14+
- php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml
15+

src/Plugin/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## 1.0.0
6+
7+
First release of this library.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Plugin\Exception;
14+
15+
use Geocoder\Exception\Exception;
16+
use Geocoder\Query\Query;
17+
18+
/**
19+
* Thrown when the Plugin Client detects an endless loop.
20+
*
21+
* @author Joel Wurtz <[email protected]>
22+
*/
23+
class LoopException extends \RuntimeException implements Exception
24+
{
25+
/**
26+
* @var Query
27+
*/
28+
private $query;
29+
30+
public static function create($message, Query $query)
31+
{
32+
$ex = new self($message);
33+
$ex->query = $query;
34+
35+
return $ex;
36+
}
37+
38+
/**
39+
* @return Query
40+
*/
41+
public function getQuery(): Query
42+
{
43+
return $this->query;
44+
}
45+
}

src/Plugin/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2011 — William Durand <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/Plugin/Plugin.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Plugin;
14+
15+
use Geocoder\Query\Query;
16+
use Http\Promise\Promise;
17+
18+
/**
19+
* A plugin is a middleware to transform the Query and/or the Collection.
20+
*
21+
* The plugin can:
22+
* - break the chain and return a Collection
23+
* - dispatch the Query to the next middleware
24+
* - restart the Query
25+
*
26+
* @author Joel Wurtz <[email protected]>
27+
* @author Tobias Nyholm <[email protected]>
28+
*/
29+
interface Plugin
30+
{
31+
/**
32+
* Handle the Query and return the Collection coming from the next callable.
33+
*
34+
* @param Query $query
35+
* @param callable $next Next middleware in the chain, the query is passed as the first argument
36+
* @param callable $first First middleware in the chain, used to to restart a request
37+
*
38+
* @return Promise Resolves a Collection or fails with an Geocoder\Exception\Exception
39+
*/
40+
public function handleQuery(Query $query, callable $next, callable $first);
41+
}

src/Plugin/Plugin/BoundsPlugin.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Plugin\Plugin;
14+
15+
use Geocoder\Model\Bounds;
16+
use Geocoder\Plugin\Plugin;
17+
use Geocoder\Query\GeocodeQuery;
18+
use Geocoder\Query\Query;
19+
20+
/**
21+
* Add bounds to each GeocoderQuery
22+
*
23+
* @author Tobias Nyholm <[email protected]>
24+
*/
25+
class BoundsPlugin implements Plugin
26+
{
27+
/**
28+
* @var Bounds
29+
*/
30+
private $bounds;
31+
32+
/**
33+
* @param Bounds $bounds
34+
*/
35+
public function __construct(Bounds $bounds)
36+
{
37+
$this->bounds = $bounds;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function handleQuery(Query $query, callable $next, callable $first)
44+
{
45+
if (!$query instanceof GeocodeQuery) {
46+
return $next($query);
47+
}
48+
49+
if (empty($query->getBounds())) {
50+
$query = $query->withBounds($this->bounds);
51+
}
52+
53+
return $next($query);
54+
}
55+
}

src/Plugin/Plugin/CachePlugin.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Plugin\Plugin;
14+
15+
use Geocoder\Plugin\Plugin;
16+
use Geocoder\Query\Query;
17+
use Psr\SimpleCache\CacheInterface;
18+
19+
/**
20+
* Cache the result of a query.
21+
*
22+
* @author Tobias Nyholm <[email protected]>
23+
*/
24+
class CachePlugin implements Plugin
25+
{
26+
/**
27+
* @var CacheInterface
28+
*/
29+
private $cache;
30+
31+
/**
32+
* How log a result is going to be cached.
33+
*
34+
* @var int|null
35+
*/
36+
private $lifetime;
37+
38+
/**
39+
* @param CacheInterface $cache
40+
* @param int $lifetime
41+
*/
42+
public function __construct(CacheInterface $cache, int $lifetime = null)
43+
{
44+
$this->cache = $cache;
45+
$this->lifetime = $lifetime;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function handleQuery(Query $query, callable $next, callable $first)
52+
{
53+
$cacheKey = $this->getCacheKey($query);
54+
if (null !== $cachedResult = $this->cache->get($cacheKey)) {
55+
return $cachedResult;
56+
}
57+
58+
$result = $next($query);
59+
$this->cache->set($cacheKey, $result, $this->lifetime);
60+
61+
return $result;
62+
}
63+
64+
/**
65+
* @param Query $query
66+
*
67+
* @return string
68+
*/
69+
private function getCacheKey(Query $query): string
70+
{
71+
// Include the major version number of the geocoder to avoid issues unserializing.
72+
return 'v4'.sha1((string) $query);
73+
}
74+
}

src/Plugin/Plugin/LimitPlugin.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Plugin\Plugin;
14+
15+
use Geocoder\Plugin\Plugin;
16+
use Geocoder\Query\Query;
17+
18+
/**
19+
* Add limit on the query
20+
*
21+
* @author Tobias Nyholm <[email protected]>
22+
*/
23+
class LimitPlugin implements Plugin
24+
{
25+
/**
26+
* @var int
27+
*/
28+
private $limit;
29+
30+
/**
31+
* @param int $limit
32+
*/
33+
public function __construct(int $limit)
34+
{
35+
$this->limit = $limit;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function handleQuery(Query $query, callable $next, callable $first)
42+
{
43+
if (empty($query->getLocale())) {
44+
$query = $query->withLimit($this->limit);
45+
}
46+
47+
return $next($query);
48+
}
49+
}

0 commit comments

Comments
 (0)