Overview

Namespaces

  • DSchoenbauer
    • Config

Classes

  • DSchoenbauer\Config\Config
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace DSchoenbauer\Config;
  3: 
  4: use DSchoenbauer\DotNotation\ArrayDotNotation;
  5: 
  6: /**
  7:  * Loads a directory filled with JSON files allowing quick access to data
  8:  *
  9:  * @author David Schoenbauer
 10:  */
 11: class Config
 12: {
 13: 
 14:     protected $arrayDot;
 15:     protected $path;
 16: 
 17:     /**
 18:      * 
 19:      * @param string $path absolute or relative directory path to a folder containing JSON files
 20:      */
 21:     public function __construct($path = null)
 22:     {
 23:         if ($path) {
 24:             $this->load($path);
 25:         }
 26:     }
 27: 
 28:     /**
 29:      * retrieves a value from the amalgamation of all the JSON files data
 30:      * @param string $dotNotation a concatenated string of array keys 
 31:      * @param mixed $defaultValue value to be returned if the dot notation does not find data
 32:      * @return mixed
 33:      */
 34:     public function get($dotNotation, $defaultValue = null)
 35:     {
 36:         return $this->getArrayDot()->get($dotNotation, $defaultValue);
 37:     }
 38: 
 39:     /**
 40:      * retrieves an array of JSON files found in a directory
 41:      * @param string $path absolute or relative directory path to a folder containing JSON files
 42:      * @return array retrieves an array of JSON files found in a directory
 43:      */
 44:     public function getFiles($path)
 45:     {
 46:         $scannedFiles = glob($this->filterPath($path) . "*", GLOB_MARK) ?: [];
 47:         foreach ($scannedFiles as $file) {
 48:             if (is_dir($file)) {
 49:                 $scannedFiles = array_merge($scannedFiles, $this->getFiles($file));
 50:             }
 51:         }
 52:         return array_values(array_filter($scannedFiles, function ($file) {
 53:             return substr(strtolower($file), -4) === 'json';
 54:         }));
 55:     }
 56: 
 57:     /**
 58:      * loads data into the object from a list of JSON files. If run multiple times the data will be continually added to
 59:      * @param array $files list of files to be loaded
 60:      * @return $this
 61:      */
 62:     public function importData(array $files = [])
 63:     {
 64:         $data = $this->getArrayDot()->getData();
 65:         foreach ($files as $file) {
 66:             $data = array_replace_recursive($data, \json_decode(file_get_contents($file) ?: [], true));
 67:         }
 68:         $this->getArrayDot()->setData($data);
 69:         return $this;
 70:     }
 71: 
 72:     /**
 73:      * loads JSON files from a directory path
 74:      * @param string $path  absolute or relative directory path to a folder containing JSON files
 75:      * @return $this
 76:      */
 77:     public function load($path)
 78:     {
 79:         $this->importData($this->getFiles($path));
 80:         return $this;
 81:     }
 82: 
 83:     /**
 84:      * Array dot notation allows for quick and easy access to a complicated data structure
 85:      * @return ArrayDotNotation
 86:      */
 87:     public function getArrayDot()
 88:     {
 89:         if (!$this->arrayDot) {
 90:             $this->setArrayDot(new ArrayDotNotation([]));
 91:         }
 92:         return $this->arrayDot;
 93:     }
 94: 
 95:     /**
 96:      * Array dot notation allows for quick and easy access to a complicated data structure
 97:      * @param ArrayDotNotation $arrayDot
 98:      * @return $this
 99:      */
100:     public function setArrayDot(ArrayDotNotation $arrayDot)
101:     {
102:         $this->arrayDot = $arrayDot;
103:         return $this;
104:     }
105:     
106:     /**
107:      * Cleans a string so that it is truly a path relevant to the class.
108:      * @param string $path  absolute or relative directory path to a folder containing JSON files
109:      * @return string
110:      */
111:     public function filterPath($path)
112:     {
113:         return str_replace(['/','\\'], DIRECTORY_SEPARATOR, trim($path, '\\/') . DIRECTORY_SEPARATOR);
114:     }
115: }
116: 
API documentation generated by ApiGen