Repository navigation
Expand file tree
/
Copy pathGeoIp.php
More file actions
63 lines (50 loc) · 1.6 KB
/
Copy pathGeoIp.php
File metadata and controls
63 lines (50 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* This file is part of the StackGeoIp 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\Stack;
use Geocoder\Stack\GeoIp\ContainerConfig;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* @author Andy Leon <acleon@acleon.co.uk>
*/
class GeoIp implements HttpKernelInterface
{
private $app;
private $container;
public function __construct(HttpKernelInterface $app, array $options = [])
{
$this->app = $app;
$this->container = $this->setupContainer($options);
}
/**
* {@inheritDoc}
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
if ($type !== HttpKernelInterface::MASTER_REQUEST) {
return $this->app->handle($request, $type, $catch);
}
$geocoder = $this->container['geocoder'];
$results = $geocoder->geocode($request->getClientIp());
if ($country = $results->getCountryCode()) {
$request->headers->set($this->container['header'], $country, true);
}
return $this->app->handle($request, $type, $catch);
}
private function setupContainer(array $options)
{
$container = new \Pimple();
$config = new ContainerConfig();
$config->process($container);
foreach ($options as $name => $value) {
$container[$name] = $value;
}
return $container;
}
}