1<?php
2
3namespace Doctrine\DBAL\Event\Listeners;
4
5use Doctrine\Common\EventSubscriber;
6use Doctrine\DBAL\Event\ConnectionEventArgs;
7use Doctrine\DBAL\Events;
8
9/**
10 * MySQL Session Init Event Subscriber which allows to set the Client Encoding of the Connection.
11 *
12 * @deprecated Use "charset" option to PDO MySQL Connection instead.
13 */
14class MysqlSessionInit implements EventSubscriber
15{
16    /**
17     * The charset.
18     *
19     * @var string
20     */
21    private $charset;
22
23    /**
24     * The collation, or FALSE if no collation.
25     *
26     * @var string|bool
27     */
28    private $collation;
29
30    /**
31     * Configure Charset and Collation options of MySQL Client for each Connection.
32     *
33     * @param string      $charset   The charset.
34     * @param string|bool $collation The collation, or FALSE if no collation.
35     */
36    public function __construct($charset = 'utf8', $collation = false)
37    {
38        $this->charset   = $charset;
39        $this->collation = $collation;
40    }
41
42    /**
43     * @return void
44     */
45    public function postConnect(ConnectionEventArgs $args)
46    {
47        $collation = $this->collation ? ' COLLATE ' . $this->collation : '';
48        $args->getConnection()->executeStatement('SET NAMES ' . $this->charset . $collation);
49    }
50
51    /**
52     * {@inheritdoc}
53     */
54    public function getSubscribedEvents()
55    {
56        return [Events::postConnect];
57    }
58}
59