PHP Classes
elePHPant
Icontem

File: docs/files/PdoMysqlLogger.php.txt

Recommend this page to a friend!
  Classes of Kiril Savchev  >  ITE Logger  >  docs/files/PdoMysqlLogger.php.txt  >  Download  
File: docs/files/PdoMysqlLogger.php.txt
Role: Documentation
Content type: text/plain
Description: Documentation
Class: ITE Logger
Log messages to different storage PSR-3 compliant
Author: By
Last change: Change stream logger and strategy loading and leave their init to the concrete stream class
Date: 3 years ago
Size: 4,763 bytes
 

 

Contents

Class file image Download
<?php

/**
 * PdoMysqlLogger file
 *
 * Copyright (c) 2016, Kiril Savchev
 * All rights reserved.
 *
 * @category Libs
 * @package Logger
 *
 * @author Kiril Savchev <k.savchev@gmail.com>
 *
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3 License
 * @link http://ifthenelse.info
 */
namespace Ite\Logger;

/**
 * Basic PDO logger
 *
 * Simple logging log information to the database using the native \PDO lib
 *
 * @version 1.1
 *
 * @author Kiril Savchev <k.savchev@gmail.com>
 */
class PdoMysqlLogger extends AbstractDatabaseLogger {

        /**
         * The PDO object that will writes to the database
         *
         * @var \PDO
         */
        protected $pdo;

        /**
         * Configuration for database connection
         *
         * @var array
         */
        protected $dbConfig;

        /**
         * Database logger using \PDO to writes the log information
         *
         * @param array $dbConfig Database configs (database name, host, etc...)
         * @param array|string $config [Optional] Table configs or default table name
         * @param string $defaultTable [Optional] The default log table name
         * @param \PDO $pdo [Optional] \PDO object to use
         * @throws Exception\InvalidArgumentException
         */
        public function __construct(array $dbConfig = [], $config = null, $defaultTable = '', \PDO $pdo = null) {
                parent::__construct($config, $defaultTable);
                if ($dbConfig) {
                        $this->prepareDbConfig($dbConfig);
                }
                if ($pdo) {
                        $this->pdo = $pdo;
                }
        }

        /**
         * Prepares the database configuration
         * @param array $dbConfig The database connection configs
         * @throws Exception\InvalidArgumentException
         */
        protected function prepareDbConfig(array $dbConfig) {
                if (!array_key_exists('name', $dbConfig)) {
                        throw new Exception\InvalidArgumentException("No database name");
                }
                if (!array_key_exists('host', $dbConfig)) {
                        throw new Exception\InvalidArgumentException("No database host");
                }
                if (!array_key_exists('username', $dbConfig)) {
                        $dbConfig['username'] = null;
                }
                if (!array_key_exists('password', $dbConfig)) {
                        $dbConfig['password'] = null;
                }
                if (!array_key_exists('options', $dbConfig) || !is_array($dbConfig['options'])) {
                        $dbConfig['options'] = [];
                }
                $this->dbConfig = $dbConfig;
        }

        /**
         * Creates the PDO object
         */
        protected function createPdo() {
                $dbConfig = $this->dbConfig;
                $this->pdo = new \PDO('mysql:dbname='.$dbConfig['name'].';host='.$dbConfig['host'], $dbConfig['username'], $dbConfig['password'], $dbConfig['options']);
        }

        /**
         * Writes the prepared sql to the database
         *
         * @param string $sql The SQL query for inserting the log
         * @param array $params [Optional] Query parameters for the statement
         * @param bool $mustPrepare [Optional] Whether or not to prepare the statement
         */
        protected function write($sql, array $params = [], $mustPrepare = false) {
                if (!$this->pdo) {
                        $this->createPdo();
                }
                if (!$mustPrepare) {
                        $this->pdo->query($sql);
                }
                else {
                        $stmt = $this->pdo->prepare($sql);
                        $stmt->execute($params);
                }
        }

        /**
         * Return the PDO object
         *
         * @return \PDO
         */
        public function getPdo() {
                return $this->pdo;
        }

        /**
         * Return the database configuration
         *
         * @return array
         */
        public function getDbConfig() {
                return $this->dbConfig;
        }

        /**
         * Sets the PDO object
         * @param \PDO $pdo
         * @return \Ite\Logger\PdoMysqlLogger
         */
        public function setPdo(\PDO $pdo) {
                $this->pdo = $pdo;
                return $this;
        }

        /**
         * Sets database configuration
         *
         * @param array $dbConfig
         * @return \Ite\Logger\PdoMysqlLogger
         */
        public function setDbConfig(array $dbConfig) {
                $this->prepareDbConfig($dbConfig);
                return $this;
        }

}