1 <?php
2
3 4 5 6 7 8
9
10 namespace yii2cdn;
11
12 13 14 15 16 17 18 19 20
21 class ConfigFile {
22
23 24 25 26
27 protected $path;
28
29 30 31 32
33 protected $config = [];
34
35 36 37 38
39 protected $offline = false;
40
41 42 43 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 56 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 71 72
73 protected function getListOf () {
74 if ( Cdn::isOnline() && $this->offline ) {
75 return [];
76 }
77
78
79 $loader = \Yii::createObject( $this->config['configLoaderClass'] );
80 $loader->online($this->getPath());
81
82 return $loader->asArray();
83
84 }
85
86 87 88 89
90 public function getPath() {
91 return \Yii::getAlias( $this->path );
92 }
93
94 95 96 97 98 99
100 public function get ( array $list = [] ) {
101 return $this->getBuiltComponents($list);
102 }
103
104 105 106 107 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
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
141 $postBuiltList = $parserClass::touchComponentTags($prebuiltList);
142
143 foreach ( $postBuiltList as $id=>$data ) {
144
145
146 $component = \Yii::createObject( $this->config['componentClass'], [$data] );
147
148 $builtList[$id] = $component;
149 }
150
151 return $builtList;
152 }
153 }
154