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 /**
 13  * CDN Configuration File handler
 14  *
 15  * @package yii2cdn
 16  * @author Junaid Atari <mj.atari@gmail.com>
 17  *
 18  * @access public
 19  * @version 0.1
 20  */
 21 class ConfigFile {
 22 
 23     /**
 24      * Absolute config file path
 25      * @var string
 26      */
 27     protected $path;
 28 
 29     /**
 30      * Component Configuration
 31      * @var array
 32      */
 33     protected $config = [];
 34 
 35     /**
 36      * File is offline or not
 37      * @var boolean
 38      */
 39     protected $offline = false;
 40 
 41     /**
 42      * Constructor function
 43      * @param object|string $config CDN Config
 44      */
 45     public function __construct ( $config ) {
 46         if ( isset($config['path']) && !empty($config['path']) ) {
 47             $this->load($config['path']);
 48             unset ($config['path']);
 49         }
 50 
 51         $this->config = $config;
 52     }
 53 
 54     /**
 55      * Load the CDN config
 56      * @param string|object $config CDN Config
 57      */
 58     protected function load ( $config ) {
 59         if ( is_string($config) ) {
 60             $this->path = $config;
 61             return;
 62         }
 63 
 64         $this->path = $config[0];
 65         $this->offline = array_key_exists( 'offline', $config )
 66             && $config['offline'];
 67     }
 68 
 69     /**
 70      * Get the configuration
 71      * @return array
 72      */
 73     protected function getListOf () {
 74         if ( Cdn::isOnline() && $this->offline ) {
 75             return [];
 76         }
 77 
 78         /** @var ConfigLoader $loader */
 79         $loader = \Yii::createObject( $this->config['configLoaderClass'] );
 80         $loader->online($this->getPath());
 81 
 82         return $loader->asArray();
 83 
 84     }
 85 
 86     /**
 87      *  Get config file real path
 88      * @return string
 89      */
 90     public function getPath() {
 91         return \Yii::getAlias( $this->path );
 92     }
 93 
 94     /**
 95      * Get the list of cdn components configuration
 96      * @see ConfigFile::getBuiltComponents()
 97      * @param array $list (optional) Components configuration
 98      * @return array
 99      */
100     public function get ( array $list = [] ) {
101         return $this->getBuiltComponents($list);
102     }
103 
104     /**
105      * Get pre built list of components
106      * @param array $list (optional) Components configuration
107      * @throws \yii\base\InvalidConfigException
108      */
109     protected function getBuiltComponents ( array $list = [] ) {
110         $list = count($list) ? $list : $this->getListOf();
111 
112         if ( !count($list) ) {
113             return [];
114         }
115 
116         $prebuiltList = [];
117         $builtList = [];
118 
119         foreach ( $list as $id=>$config ) {
120 
121             /** @var ConfigParser $parser */
122             $parser = \Yii::createObject($this->config['configParserClass'], [[
123                 'id' => $id,
124                 'config' => $config,
125                 'baseUrl' => $this->config['baseUrl'],
126                 'sections' => $this->config['sections'],
127                 'fileClass' => $this->config['fileClass'],
128                 'sectionClass' =>$this->config['sectionClass'],
129             ]]);
130 
131             if ( ($props = $parser->getParsed()) === null ) {
132                 continue;
133             }
134 
135             $prebuiltList[$id] = $props;
136         }
137 
138         $parserClass = $this->config['configParserClass'];
139 
140         // Replace @component* tags from files name
141         $postBuiltList = $parserClass::touchComponentTags($prebuiltList);
142 
143         foreach ( $postBuiltList as $id=>$data ) {
144 
145             /** @var Component $component */
146             $component = \Yii::createObject( $this->config['componentClass'], [$data] );
147 
148             $builtList[$id] = $component;
149         }
150 
151         return $builtList;
152     }
153 }
154 
API documentation generated by ApiGen