PHP Classes

File: src/BaseConfig.php

Recommend this page to a friend!
  Classes of Joseluis Laso   Simple Logger   src/BaseConfig.php   Download  
File: src/BaseConfig.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Simple Logger
Log messages and view log filtered files
Author: By
Last change: Update of src/BaseConfig.php
Date: 1 year ago
Size: 2,630 bytes
 

Contents

Class file image Download
<?php

namespace JLaso\SimpleLogger;

use
Symfony\Component\Yaml\Yaml;

abstract class
BaseConfig
{
   
/** @var array */
   
protected $config;
   
/** @var string */
   
protected $logFile;
   
/** @var string */
   
protected $projectDir;
   
/** @var array */
   
protected $levels;

    public function
__construct()
    {
       
// vendor/jlaso/simple-logger/src
       
$this->projectDir =
           
preg_match("~vendor/jlaso/simple-logger/src$~i", __DIR__) ?
               
realpath(__DIR__ . '/../../../../') :
               
realpath(__DIR__ . '/../')
        ;

       
$this->readConfig();
    }

   
/**
     * @return array
     */
   
public function getConfig()
    {
        return
$this->config;
    }

   
/**
     * @return string
     */
   
public function getProjectDir()
    {
        return
$this->projectDir;
    }

    protected function
readConfig()
    {
       
$this->config = array_merge(
            array(
               
'logger' => array(
                   
'path' => '%project_dir%/app/cache/logger-%date%.log',
                   
'level' => 'info,debug,error',
                   
'date_format' => 'Y-m-d',
                ),
            ),
           
Yaml::parse(file_get_contents($this->getConfigFile()))
        );
       
$format = $this->config['logger']['date_format'];
       
$this->logFile = str_replace(
            array(
'%project_dir%', '%date%'),
            array(
$this->projectDir, date($format)),
           
$this->config['logger']['path']
        );
       
$this->levels = explode(',', trim(strtolower($this->config['logger']['level'])));
    }

   
/**
     * @return string
     * @throws \Exception
     */
   
protected function getConfigFile()
    {
       
$configFile = $this->projectDir.'/config-simple-logger.yml';
        if(!
file_exists($configFile)) {
           
$configFile = dirname(__DIR__) . '/config-simple-logger.yml.dist';
            if (!
file_exists($configFile)) {
                throw new \
Exception("Configuration file {$configFile} not found");
            }
        }
        if(!
is_readable($configFile)){
            throw new \
Exception("File {$configFile} is not readable!");
        }

        return
$configFile;
    }

   
/**
     * @return string
     */
   
public function getLogFile()
    {
        return
$this->logFile;
    }

   
/**
     * @return array
     */
   
public function getLevels()
    {
        return
$this->levels;
    }

   
/**
     * @param string $level
     * @return bool
     */
   
public function isLevel($level)
    {
        return
in_array(trim(strtolower($level)), $this->levels);
    }

}