1
\$\begingroup\$

I have created an open source config loader in PHP, but I wish to improve on the loading of the config files, as I think how I handle the loading is good.

At the moment, this is my ConfigLoader class:

<?php

namespace CodeRichard\SimpleConfig;


use CodeRichard\SimpleConfig\Exception\MissingDependencyException;
use CodeRichard\SimpleConfig\Exception\UnsupportedFileTypeException;
use CodeRichard\SimpleConfig\Reader\FileReader;

class ConfigLoader
{
    /**
     * @var string
     */
    private $file;

    /**
     * Initialize a new loader
     *
     * @param string $file The file to load
     */
    public function __construct($file)
    {
        $this->file = $file;
    }

    /**
     * Load the config
     *
     * @param string $file A config file, may be null
     *
     * @return array
     */
    public function loadConfig($file = null)
    {
        $file = $file !== null ? $file : $this->file;

        $reader = $this->getSuitableReader($file);

        try
        {
            $reader->checkDependencies();
        }

        catch(MissingDependencyException $e)
        {
            $message = 'An error occured while trying to load the reader: ' . $e->getMessage();
            trigger_error($message, E_USER_ERROR);
        }

        return $this->readConfig($reader, $file);
    }

    /**
     * Use the config reader to read the file
     *
     * @param \CodeRichard\SimpleConfig\Reader\FileReader $reader
     * @param string                                      $file
     *
     * @return array
     */
    private function readConfig(FileReader $reader, $file)
    {
        return $reader->read($file);
    }

    /**
     * Get the most suitable config reader
     *
     * @param string $file
     *
     * @return Reader\IConfigReader
     * @throws UnsupportedFileTypeException
     */
    private function getSuitableReader($file)
    {
        $ext = pathinfo($file, PATHINFO_EXTENSION);

        if(!ConfigLoaderRegistry::hasRegistered($ext))
        {
            throw new UnsupportedFileTypeException($ext);
        }

        return ConfigLoaderRegistry::getReader($ext);

    }
}

How can I improve this class in particular?

My project can be found here: https://github.com/CodeRichard/simple-config


Edit 14-04

So far, I have moved instantiating and checking if the config reader is supported to the ConfigLoaderRegistry class. Like so:

/**
 * Check whether or not the given file type has a reader registered to it
 *
 * @param string $fileExt The file extension to check on
 *
 * @return bool True if a reader is registered for that file type, false if not
 */
public static function hasRegistered($fileExt)
{
    $fileExt = strtolower($fileExt);
    return isset(self::$registry[$fileExt]);
}

/**
 * Get a reader by its file extension and return an instantiated reader
 *
 * @param string $fileExt The file extension to get the reader from
 *
 * @return FileReader
 */
public static function getReader($fileExt)
{
    $fileExt = strtolower($fileExt);

    $classPath = self::$registry[$fileExt];

    return self::instantiateReader($classPath);
}

/**
 * Instantiate a new reader by its class path
 *
 * @param string $classPath The class path to instantiate from
 *
 * @return FileReader A new instance of the given reader
 */
protected static function instantiateReader($classPath)
{
    $reflClass = new \ReflectionClass($classPath);

    return $reflClass->newInstanceWithoutConstructor();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.