<commit_before><?php
namespace PlasmaCode\GetEnv;

class Env
{
    private $filePath;
    private $fileContents;
    private $error;
    
    public function __construct($filePath)
    {
        $this->filePath = $filePath;
        
        if($this->fileContents() !== true) {
            trigger_error($this->error('fileContents'), E_USER_ERROR);
        }
        
        if($this->setAllEnv() !== true) {
            trigger_error($this->error('setAllEnv'), E_USER_ERROR);
        }
        
        return true;
    }
    
    private function setAllEnv()
    {
        $regex = '/(\w+\s=\s[\'"]\w+[\'"])/';
        
        if(!preg_match_all($regex, $this->fileContents, $matches)) {
            return false;
        }
        
        foreach ($matches[1] as $match) {
            
            //format the matches
            $matchNoSpace = $match = str_replace(' = ', '=', $match);
            $match = str_replace('"', '', $match);
            $match = str_replace("'", "", $match);
            $match = explode('=', $match);

            //set enviromental variables
            putenv($matchNoSpace);
            $_ENV[$match[0]] = $match[1];
            $_SERVER[$match[0]] = $match[1];
            
        }
        
        return true;
    }
    
    private function fileContents()
    {
        if(!is_readable($this->filePath) && strtolower(substr($this->filePath, -4)) != '.env') {
            return false;
        }
        
        $fileContents = file_get_contents($this->filePath);
        $this->fileContents = $fileContents;
        return true;
    }
    
    private function error($methodName)
    {
        switch($methodName) {
            case 'fileContents':
                return 'The file path '.$this->filePath.' is not a correct .env file path.';
            case 'setAllEnv':
                return 'Incorrect env syntax at file path '.$this->filePath;
        }
    }
}<commit_msg>Decoupled<commit_after><?php
namespace PlasmaCode\GetEnv;
use Exception;

class Env
{
    
    public function __construct($filePath)
    {
        if($this->validateFilePath($filePath) === false) {
            throw new Exception('Failed validating file path');
        }
        
        $envContent = $this->getEnvContent($filePath);
        
        if($envContent === false) {
            throw new Exception('Failed parsing the .env file, settings are badly formatted');
        }
        
        $this->setEnvVars($envContent);
    }
    
    private function setEnvVars(array $envContent)
    {
        
        foreach ($envContent as $envValue) {
            
            //format the matches
            $noSpace = $envValue = str_replace(' = ', '=', $envValue);
            $envValue = str_replace('"', '', $envValue);
            $envValue = str_replace("'", "", $envValue);
            $envValue = explode('=', $envValue);
            
            $setting = $envValue[0];
            $value = $envValue[1];

            //set enviromental variables
            putenv($noSpace);
            $_ENV[$setting] = $value;
            $_SERVER[$setting] = $value;
            
        }
    }
    
    //checks to make sure the file path is legit
    private function validateFilePath($filePath)
    {
 
        //last 4 characters to get extension of file path
        $filePathExt = substr($filePath, -4);
        if(!is_readable($filePath) || !file_exists($filePath) || ($filePathExt != '.env' && $filePathExt != '.ENV')) {
            return false;
        }
        
        return true;
    }
    
    private function getEnvContent($filePath)
    {    
        $fileContents = file_get_contents($filePath);
        
        //get all env configurations from file contents
        $regex = '/(\w+\s*=\s*[\'"]\w+[\'"])/';
        if(preg_match_all($regex, $fileContents, $matches) === false) {
            return false;
        }
        
        if($matches[1] == null) {
            return false;
        }
        
        return $matches[1];
    }
}
{'fraction_non_alphanumeric': 0.14317897371714644, 'fraction_numerical': 0.003003754693366708, 'mean_word_length': 1.7128309572301426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1912120', 'n_tokens_mistral': 1141, 'n_tokens_neox': 1131, 'n_words': 239}