1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @author Xavier Chopin <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App\Event; |
11
|
|
|
|
12
|
|
|
use Jenssegers\Mongodb\Connection; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
16
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
18
|
|
|
use Illuminate\Database\Capsule\Manager; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class LocaleListener |
22
|
|
|
{ |
23
|
|
|
private $container; |
24
|
|
|
|
25
|
|
|
public function __construct(ContainerInterface $container) |
26
|
|
|
{ |
27
|
|
|
$this->container = $container; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
34
|
|
|
{ |
35
|
|
|
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) |
36
|
|
|
return; |
37
|
|
|
|
38
|
|
|
$this->registerLDAP(); |
39
|
|
|
$this->registerCapsule(); |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Registers a LDAP Instance into the container. |
45
|
|
|
*/ |
46
|
|
|
private function registerLDAP() |
47
|
|
|
{ |
48
|
|
|
$ldapInstance = ldap_connect(env('LDAP_HOST'), env('LDAP_PORT')); |
|
|
|
|
49
|
|
|
ldap_set_option($ldapInstance, LDAP_OPT_PROTOCOL_VERSION, 3); |
50
|
|
|
ldap_set_option($ldapInstance, LDAP_OPT_REFERRALS, 0); |
51
|
|
|
$this->container->set('ldap', /** @scrutinizer ignore-type */ $ldapInstance); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets globally and registers the Eloquent ORM's Capsule Manager into the container. |
56
|
|
|
*/ |
57
|
|
|
private function registerCapsule() |
58
|
|
|
{ |
59
|
|
|
$capsule = new Manager(); |
60
|
|
|
$capsule->getDatabaseManager()->extend('mongodb', function($config) { |
61
|
|
|
return new Connection($config); |
62
|
|
|
}); |
63
|
|
|
$capsule->addConnection([ |
64
|
|
|
'driver' => 'mongodb', |
65
|
|
|
'host' => env('DB_HOST'), |
66
|
|
|
'port' => intval(env('DB_PORT')), |
67
|
|
|
'database' => env('DB_NAME'), |
68
|
|
|
'username' => env('DB_USERNAME'), |
69
|
|
|
'password' => env('DB_PASSWORD'), |
70
|
|
|
'options' => [ |
71
|
|
|
'database' => env('DB_NAME'), |
72
|
|
|
] |
73
|
|
|
]); |
74
|
|
|
$capsule->setAsGlobal(); |
75
|
|
|
$capsule->bootEloquent(); |
76
|
|
|
$this->container->set('capsule', $capsule); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function getSubscribedEvents() |
80
|
|
|
{ |
81
|
|
|
return [ KernelEvents::REQUEST => [['onKernelRequest', 200]] ]; |
82
|
|
|
} |
83
|
|
|
} |