1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\DBAL\Event\Listeners;
21
22use Doctrine\DBAL\Event\ConnectionEventArgs;
23use Doctrine\DBAL\Events;
24use Doctrine\Common\EventSubscriber;
25
26/**
27 * MySQL Session Init Event Subscriber which allows to set the Client Encoding of the Connection.
28 *
29 * @link       www.doctrine-project.org
30 * @since      1.0
31 * @author     Benjamin Eberlei <kontakt@beberlei.de>
32 * @deprecated Use "charset" option to PDO MySQL Connection instead.
33 */
34class MysqlSessionInit implements EventSubscriber
35{
36    /**
37     * The charset.
38     *
39     * @var string
40     */
41    private $_charset;
42
43    /**
44     * The collation, or FALSE if no collation.
45     *
46     * @var string|boolean
47     */
48    private $_collation;
49
50    /**
51     * Configure Charset and Collation options of MySQL Client for each Connection.
52     *
53     * @param string         $charset   The charset.
54     * @param string|boolean $collation The collation, or FALSE if no collation.
55     */
56    public function __construct($charset = 'utf8', $collation = false)
57    {
58        $this->_charset = $charset;
59        $this->_collation = $collation;
60    }
61
62    /**
63     * @param \Doctrine\DBAL\Event\ConnectionEventArgs $args
64     *
65     * @return void
66     */
67    public function postConnect(ConnectionEventArgs $args)
68    {
69        $collation = ($this->_collation) ? " COLLATE ".$this->_collation : "";
70        $args->getConnection()->executeUpdate("SET NAMES ".$this->_charset . $collation);
71    }
72
73    /**
74     * {@inheritdoc}
75     */
76    public function getSubscribedEvents()
77    {
78        return array(Events::postConnect);
79    }
80}
81