Snippets

Dmitriy Kuts Simple Confing.php

Created by Dmitriy Kuts
<?php
 
namespace CodeRichard\Config;
 
use CodeRichard\Error\EnvironmentNotFoundException;
 
class Config
{  
    private static $loaded = false;
   
    private static $configEntries = [];
   
    public static function load($environment)
    {
        $base = __DIR__.'/../../config/';
        $configFile = $base.$environment.'.php';
       
        if(!file_exists($configFile))
        {
            throw new EnvironmentNotFoundException($environment);
        }
       
        if(!self::$loaded)
        {
            self::$configEntries = require $configFile;
        }
    }
   
    public static function get($path, $default = '')
    {
        $arr = self::$configEntries;
        $parts = explode('.', $path);
       
        foreach($parts as $part)
        {
            if(isset($arr[$part]))
            {
                $arr = $arr[$part];
            }
           
            else
            {
                return $default;
            }
        }
       
        return $arr;
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.