/** * [checkFileExists 检查文件是否存在 和 判断后缀名是否正确 * @param string | array $files 要检查的文件(文件列表) * @param string $ext 是否检查后缀 * @throws \InvalidArgumentException * @throws NotFoundException * @return array|string [type] [description] */ public static function exists($files, $ext = null) { $ext = $ext ? trim($ext, '. ') : null; $files = StrHelper::toArray($files); foreach ($files as $file) { $file = trim($file); if (!file_exists($file)) { throw new NotFoundException("文件 {$file} 不存在!"); } // if ( $ext && strrchr($file,'.') != '.'.$ext ) { if ($ext && preg_match("/\\.({$ext})\$/i", $file)) { throw new \InvalidArgumentException("{$file} 不是 {$ext} 文件!"); } } return $files; }
/** * * how to use language translate ? please see '/doc/language.md' * * @param string $key * @param array $args * @param string $default * @return string */ public function translate($key, $args = [], $default = '') { $key = trim($key, $this->fileSeparator . ' '); if (!$key || !is_string($key)) { throw new \InvalidArgumentException('A lack of parameters or type error.'); } // No separator, get value form default language file. if (($pos = strpos($key, $this->fileSeparator)) === false) { $fileKey = static::DEFAULT_FILE_KEY; // Will try to get the value from the other config file } else { $fileKey = substr($key, 0, $pos); $key = substr($key, $pos + 1); } // translate form current language. if not found, translate form fallback language. $value = $this->findTranslationText($fileKey, $key) ?: $this->tranByFallbackLang($fileKey, $key, $default); if (!$value) { $value = ucfirst(StrHelper::toUnderscoreCase(str_replace(['-', '_'], ' ', $key), ' ')); } $args = $args ? (array) $args : []; // $args is not empty? if ($hasArgs = count($args)) { array_unshift($args, $value); } return $hasArgs ? call_user_func_array('sprintf', $args) : $value; }
/** * 合并编译多个文件 * @param $fileArr * @param $outFile * @param boolean $deleteSpace [description] * @return void [type] [description] */ public static function margePhp($fileArr, $outFile, $deleteSpace = true) { $savePath = dirname($outFile); if (!is_dir($savePath)) { Directory::create($savePath); } if (!is_array($fileArr)) { $fileArr = array($fileArr); } $data = ''; foreach ($fileArr as $v) { #删除注释、空白 if ($deleteSpace) { $data .= StrHelper::deleteStripSpace($v); } else { $o_data = file_get_contents($v); $o_data = substr($o_data, 0, 5) == "<?php" ? substr($o_data, 5) : $o_data; $data .= substr($o_data, -2) == "?>" ? substr($o_data, 0, -2) : $o_data; } } $data = "<?php " . $data . "?>"; file_put_contents($outFile, $data); }