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

Skip to content

Adding Plugin support #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2017
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
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"php-http/client-implementation": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/httplug": "^1.0",
"php-http/discovery": "^1.0"
"php-http/discovery": "^1.0",
"psr/simple-cache": "^1.0",
"php-http/promise": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.1",
Expand All @@ -31,8 +33,9 @@
"php-http/mock-client": "^1.0",
"nyholm/psr7": "^0.2.2",
"nyholm/nsa": "^1.1",
"cache/simple-cache-bridge": "^0.1.1",
"cache/array-adapter": "^0.5.0"
"cache/simple-cache-bridge": "^1.0",
"cache/array-adapter": "^1.0",
"cache/void-adapter": "^1.0"
},
"suggest": {
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",
Expand All @@ -52,6 +55,7 @@
"test": "vendor/bin/phpunit"
},
"minimum-stability": "dev",
"prefer-dist": true,
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
Expand Down
4 changes: 4 additions & 0 deletions src/Plugin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gitattributes export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
Tests/ export-ignore
15 changes: 15 additions & 0 deletions src/Plugin/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php
sudo: false

php: 7.0

install:
- composer update --prefer-stable --prefer-dist

script:
- composer test-ci

after_success:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml

7 changes: 7 additions & 0 deletions src/Plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 1.0.0

First release of this library.
45 changes: 45 additions & 0 deletions src/Plugin/Exception/LoopException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Plugin\Exception;

use Geocoder\Exception\Exception;
use Geocoder\Query\Query;

/**
* Thrown when the Plugin Client detects an endless loop.
*
* @author Joel Wurtz <[email protected]>
*/
class LoopException extends \RuntimeException implements Exception
{
/**
* @var Query
*/
private $query;

public static function create($message, Query $query)
{
$ex = new self($message);
$ex->query = $query;

return $ex;
}

/**
* @return Query
*/
public function getQuery(): Query
{
return $this->query;
}
}
21 changes: 21 additions & 0 deletions src/Plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2011 — William Durand <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions src/Plugin/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Plugin;

use Geocoder\Query\Query;
use Http\Promise\Promise;

/**
* A plugin is a middleware to transform the Query and/or the Collection.
*
* The plugin can:
* - break the chain and return a Collection
* - dispatch the Query to the next middleware
* - restart the Query
*
* @author Joel Wurtz <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
interface Plugin
{
/**
* Handle the Query and return the Collection coming from the next callable.
*
* @param Query $query
* @param callable $next Next middleware in the chain, the query is passed as the first argument
* @param callable $first First middleware in the chain, used to to restart a request
*
* @return Promise Resolves a Collection or fails with an Geocoder\Exception\Exception
*/
public function handleQuery(Query $query, callable $next, callable $first);
}
55 changes: 55 additions & 0 deletions src/Plugin/Plugin/BoundsPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Plugin\Plugin;

use Geocoder\Model\Bounds;
use Geocoder\Plugin\Plugin;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;

/**
* Add bounds to each GeocoderQuery
*
* @author Tobias Nyholm <[email protected]>
*/
class BoundsPlugin implements Plugin
{
/**
* @var Bounds
*/
private $bounds;

/**
* @param Bounds $bounds
*/
public function __construct(Bounds $bounds)
{
$this->bounds = $bounds;
}

/**
* {@inheritdoc}
*/
public function handleQuery(Query $query, callable $next, callable $first)
{
if (!$query instanceof GeocodeQuery) {
return $next($query);
}

if (empty($query->getBounds())) {
$query = $query->withBounds($this->bounds);
}

return $next($query);
}
}
74 changes: 74 additions & 0 deletions src/Plugin/Plugin/CachePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Plugin\Plugin;

use Geocoder\Plugin\Plugin;
use Geocoder\Query\Query;
use Psr\SimpleCache\CacheInterface;

/**
* Cache the result of a query.
*
* @author Tobias Nyholm <[email protected]>
*/
class CachePlugin implements Plugin
{
/**
* @var CacheInterface
*/
private $cache;

/**
* How log a result is going to be cached.
*
* @var int|null
*/
private $lifetime;

/**
* @param CacheInterface $cache
* @param int $lifetime
*/
public function __construct(CacheInterface $cache, int $lifetime = null)
{
$this->cache = $cache;
$this->lifetime = $lifetime;
}

/**
* {@inheritdoc}
*/
public function handleQuery(Query $query, callable $next, callable $first)
{
$cacheKey = $this->getCacheKey($query);
if (null !== $cachedResult = $this->cache->get($cacheKey)) {
return $cachedResult;
}

$result = $next($query);
$this->cache->set($cacheKey, $result, $this->lifetime);

return $result;
}

/**
* @param Query $query
*
* @return string
*/
private function getCacheKey(Query $query): string
{
// Include the major version number of the geocoder to avoid issues unserializing.
return 'v4'.sha1((string) $query);
}
}
49 changes: 49 additions & 0 deletions src/Plugin/Plugin/LimitPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Plugin\Plugin;

use Geocoder\Plugin\Plugin;
use Geocoder\Query\Query;

/**
* Add limit on the query
*
* @author Tobias Nyholm <[email protected]>
*/
class LimitPlugin implements Plugin
{
/**
* @var int
*/
private $limit;

/**
* @param int $limit
*/
public function __construct(int $limit)
{
$this->limit = $limit;
}

/**
* {@inheritdoc}
*/
public function handleQuery(Query $query, callable $next, callable $first)
{
if (empty($query->getLocale())) {
$query = $query->withLimit($this->limit);
}

return $next($query);
}
}
Loading