Overview
  • Namespace
  • Class

Namespaces

  • yii2cdn

Classes

  • yii2cdn\Cdn
  • yii2cdn\Component
  • yii2cdn\ConfigFile
  • yii2cdn\ConfigLoader
  • yii2cdn\ConfigParser
  • yii2cdn\File
  • yii2cdn\Section
  1 <?php
  2 
  3 /**
  4  * @copyright Copyright (c) 2016 Junaid Atari
  5  * @link http://junaidatari.com Website
  6  * @see http://www.github.com/blacksmoke26/yii2-cdn
  7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8  */
  9 
 10 namespace yii2cdn;
 11 
 12 use yii\base\InvalidConfigException;
 13 use yii\base\InvalidValueException;
 14 use yii\helpers\ArrayHelper;
 15 
 16 /**
 17  * Yii CDN Configuration File Loader
 18  *
 19  * @package yii2cdn
 20  * @author Junaid Atari <mj.atari@gmail.com>
 21  *
 22  * @access public
 23  * @version 0.1
 24  */
 25 class ConfigLoader {
 26 
 27     /**
 28      * Configuration object
 29      * @var array
 30      */
 31     protected $configs = [];
 32 
 33     /**
 34      * Constructor function
 35      * @param array $config (optional) Default configuration
 36      */
 37     public function __construct( array $config = [] ) {
 38         $this->configs = $config;
 39     }
 40 
 41     /**
 42      * Append config using file path
 43      * @param string $path Config file path
 44      * @throws \yii\base\InvalidConfigException When file not found
 45      * @throws \yii\base\InvalidConfigException When file is unreadable
 46      * @return ConfigLoader
 47      */
 48     protected function appendWithFromFile ( $path ) {
 49         $path = \Yii::getAlias($path);
 50 
 51         if ( !is_file($path) ) {
 52             throw new InvalidConfigException("File '{$path}' not found");
 53         }
 54 
 55         if ( !is_readable( $path) ) {
 56             throw new InvalidConfigException ("File '{$path}' not readable");
 57         }
 58 
 59         $this->appendWith( require($path) );
 60         return $this;
 61     }
 62 
 63     /**
 64      * Append array with current config
 65      * @param array $config Config object
 66      * @return ConfigLoader
 67      */
 68     public function appendWith ( array $config = [] ) {
 69         $this->configs = ArrayHelper::merge( $this->configs, $config );
 70         return $this;
 71     }
 72 
 73     /**
 74      * Load a configuration file only when offline mode is active
 75      * @param string $path CDN config file path
 76      * @return ConfigLoader
 77      */
 78     public function offline ( $path ) {
 79         if ( !Cdn::isOnline() ) {
 80             return $this;
 81         }
 82 
 83         return $this->online( $path );
 84     }
 85 
 86     /**
 87      * Load a configuration file
 88      * @param string $path CDN config file path
 89      * @return ConfigLoader
 90      */
 91     public function online ( $path ) {
 92         return $this->appendWithFromFile( $path );
 93     }
 94 
 95     /**
 96      * Load a files list and merge with configuration<br>
 97      * Usage:
 98      *     1. '...' : main file path
 99      *     2. ['...'] : main file path
100      *     3. ['...', 'offline'=false] : online cdn file path
101      *     4. ['...', 'offline'=true] : offline cdn file path
102      * @param array $filesPath Config files list
103      * @throws \InvalidArgumentException
104      */
105     public function loadConfig ( array $filesPath ) {
106         if ( !is_array( $filesPath) || !count( $filesPath) ) {
107             throw new InvalidValueException ('Files list is empty');
108         }
109 
110         foreach ( $filesPath as $path ) {
111             if ( is_string($path) ) {
112                 $this->online($path);
113                 continue;
114             }
115 
116             if ( !is_array($path) || !count($path) ) {
117                 throw new InvalidValueException ('Path Value in not array nor string given');
118             }
119 
120             if ( count($path) === 1 ) {
121                 $this->online($path[0]);
122                 continue;
123             }
124 
125             if ( isset($path['offline']) && $path['offline'] ) {
126                 $this->offline($path[0]);
127                 continue;
128             }
129 
130             continue;
131         }
132     }
133 
134     /**
135      * Get configuration object as array
136      * @return array
137      */
138     public function asArray () {
139         return $this->configs;
140     }
141 }
142 
API documentation generated by ApiGen