PHP - Pretty way to load a config array file

So i do not really need help on understanding of anything. I just can’t decide on what is the best way forward for me. So i have a small framework i have been adding to over the years and about a week ago i decided some of the code needed updated. Mainly my issue comes is when trying to load my config files. I just don’t have a pretty way to do it that has hit me as the best and cleanest way it can be done.

config file for paths

return Array
(
	"base"        => getcwd(),
	
	"system"      => "../system",
	
	"config"      => "/config",
	
	"controllers" => "/controllers",
	
	"models"      => "/models",
	
	"views"       => "/views"
)

So i have a bootstrap file that does the initial startup of the framework and setting of the config data. What i would like to do is have a clean way to load the config file from the config directory. which is a subdirectory to my bootstrap script.

So my problem is i would like to have a class manage each config file. So the paths config would have a Paths class that does the management of the config. So should i have a method that takes in the config file path and loads it? or should i pass the config array as a dependency?

example of config of dependency injected

class Path
{
	/**
	* 
	* map of known paths
	* 
	* @var string array
	* 
	*/
	protected $paths = array();
	
	/**
	* excepts the path of the config file to load from
	* 
	* @param array $paths
	* 
	* @return void
	*/
	public function __construct(array $paths = null)
	{
		$this->setArray($paths);	
	}
	
	/**
	* excepts the path of the config file to load from
	* 
	* @param array $paths
	* 
	* @return void
	*/
	public function setArray(array $paths)
	{
		$this->paths = array_merge($this->paths, $paths);
	}
	
	/**
	* returns the path array
	* 
	* @param void
	* 
	* @return array
	*/
	public function getArray()
	{
		return $this->paths;
	}	
	
	/**
	* get an item from the config or false on failure
	* 
	* @param string - alias of the path string
	* 
	* @return string or boolean on failure
	*/
	public function get($alias)
	{
		return isset($this->paths[$alias]) ? $this->paths[$alias] : false;
	}
	
	/**
	* set the config item by alias
	* 
	* @param string - alias of the path string
	* @param string - the path string
	* 
	* @return void
	*/
	public function set($alias, $path)
	{
		$this->paths[$alias] = $path;
	}
	
	/**
	* delete an alias or flush the entire array
	* 
	* @param string - alias of the path string
	* 
	* @return bool
	*/
	public function flush($alias = null)
	{
		if(!is_null($alias) && $this->get($alias) != false)
		{
			unset($this->paths[$alias]);
			return true;
		}
		
		$this->paths = array();
		return true;
	}
}

So if i use the class i wrote from above i must require the config and pass it into this class as an array. Which is all great but kinda seems like it would almost be better to just couple to loading of the config in the path config class? But i cannot decide on what is the better route. I know loose coupling would make more sense but this solution doesn’t seem as clean as having a method that handles the requiring of the config file itself?

What are your thoughts on this? i know probably both ways will work just fine. But i guess what would be considered more standard of practice? If this code was put in front of a new developer would it make sense that the config path class needs the config loaded and passed as a dependency or would it make more sense to hide the loading of the config in a method? So yeah…

here is the loads method i would add.

public function load()
{
	$paths = require "../app/config/paths.php";
	$this->setArray($path);
		
}

So my concern with this is the path to the config file as it would be dependent on knowing the directory and that directory structure not changing.

I always could pass in the path as a param but then again do i need this method at all? when i can just as easily do it in the constructor or use the setArray method and just require as a param such as below.

$config->setArray($array = require "path to teh config")

The load() method makes lot more sense than setArray() with require in arguments.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.