Ejemplo n.º 1
0
 /**
  * Load a configuration file only when offline mode is active
  * @param string $path CDN config file path
  * @return ConfigLoader
  */
 public function offline($path)
 {
     if (!Cdn::isOnline()) {
         return $this;
     }
     return $this->online($path);
 }
Ejemplo n.º 2
0
 /**
  * Get a file id and name
  * @param string|array $file File name | file object
  * @param string $type Section name
  * @param string $tag (optional) Section attribute tag name
  * @throws \yii\base\InvalidParamException when File first param must not string or empty
  * @throws \yii\base\InvalidParamException when File attribute param not string or empty
  * @return array|null Key=>Value pair (ID=>FILENAME) / File skipped
  */
 protected function getFileName($file, $type, $tag = null)
 {
     if (is_string($file)) {
         if (preg_match('/^@component([A-Za-z]+)/i', $file) > 0) {
             return [uniqid('*', false) => ['file' => null, 'url' => $file]];
         }
         return [uniqid('*', false) => ['file' => ltrim(preg_replace('/^@[a-zA-Z]+/', '', $file), '\\/'), 'url' => $this->replaceFileNameTags($file, $type)]];
     }
     if (empty($file[0]) || !is_string($file[0])) {
         throw new InvalidParamException('File first param must be string and not empty');
     }
     $params = ['cdn', 'id'];
     foreach ($params as $p) {
         if (!empty($file['@' . $p]) && !is_string($file['@' . $p])) {
             throw new InvalidParamException("File @{$p} param must be string and cannot be emptied");
         }
     }
     if (array_key_exists('@offline', $file) && $file['@offline'] !== false && Cdn::isOnline()) {
         return null;
     }
     // Check file @cdn exist, use that version,
     $filename = array_key_exists('@cdn', $file) && Cdn::isOnline() ? $file['@cdn'] : $this->replaceFileNameTags($file[0], $type);
     // use offline version
     // Check file ID, if doesn't exist, assign a unique id
     $fileId = array_key_exists('@id', $file) ? trim($file['@id']) : (string) uniqid('*', false);
     if (array_key_exists('@options', $file)) {
         if (!is_array($file['@options'])) {
             throw new InvalidParamException("File @options param should be an array");
         }
         $this->filesAttrs[$type]["@options/{$fileId}"] = $file['@options'];
     }
     $attributes = preg_grep('/^[a-zA-Z]+$/i', array_keys($file));
     if (count($attributes)) {
         foreach ($attributes as $attr => $val) {
             if (in_array($attr, $this->defFileAttrs, true)) {
                 continue;
             }
             $this->filesAttrs[$type]["{$val}/{$fileId}"] = $file[$val];
         }
     }
     return [$fileId => ['file' => $file[0], 'url' => $filename]];
 }