/** * Plugin for Smarty * Returns URL for skin asset file * * @param array $aParams * @param Smarty_Internal_Template $oSmartyTemplate * * @return string */ function smarty_function_asset($aParams, $oSmartyTemplate) { if (empty($aParams['skin']) && empty($aParams['file'])) { trigger_error('Asset: missing "file" parameter', E_USER_WARNING); return ''; } if (isset($aParams['file'])) { if (stripos($aParams['file'], 'http://') === 0 || stripos($aParams['file'], 'https://') === 0 || stripos($aParams['file'], 'http://') === 0) { $sUrl = $aParams['file']; } else { $sSkin = !empty($aParams['skin']) ? $aParams['skin'] : E::ModuleViewer()->GetConfigSkin(); // File name has full local path if (F::File_LocalDir($aParams['file'])) { $sFile = $aParams['file']; } else { // Need URL to asset file if (isset($aParams['theme'])) { if (is_bool($aParams['theme'])) { $sTheme = E::ModuleViewer()->GetConfigTheme(); } else { $sTheme = $aParams['theme']; } } else { $sTheme = ''; } if ($sTheme) { $sTheme = 'themes/' . $sTheme . '/'; } if (isset($aParams['plugin'])) { $sFile = Plugin::GetTemplateFile($aParams['plugin'], $aParams['file']); } else { $sFile = Config::Get('path.skins.dir') . '/' . $sSkin . '/' . $sTheme . $aParams['file']; } } if (isset($aParams['prepare'])) { $sAssetName = empty($aParams['asset']) ? $sFile : $aParams['asset']; // Грязноватый хак, но иначе нам не получить ссылку $aFileData = array($sFile => array('name' => md5($sFile), 'prepare' => true)); /** @var ModuleViewerAsset $oLocalViewerAsset */ $oLocalViewerAsset = new ModuleViewerAsset(); $oLocalViewerAsset->AddFiles(F::File_GetExtension($sFile, true), $aFileData, $sAssetName); $oLocalViewerAsset->Prepare(); //$sUrl = $oLocalViewerAsset->AssetFileUrl(F::File_NormPath($sFile)); $aLinks = $oLocalViewerAsset->GetPreparedAssetLinks(); $sUrl = reset($aLinks); } else { $sUrl = E::ModuleViewerAsset()->File2Link($sFile, 'skin/' . $sSkin . '/'); } } } else { // Need URL to asset dir $sUrl = E::ModuleViewer()->GetAssetUrl() . 'skin/' . $aParams['skin'] . '/'; } return $sUrl; }
/** * Plugin for Smarty * Returns URL for skin asset file * * @param array $aParams * @param Smarty_Internal_Template $oSmartyTemplate * * @return string */ function smarty_function_asset($aParams, $oSmartyTemplate) { if (empty($aParams['skin']) && empty($aParams['file'])) { trigger_error('Asset: missing "file" parametr', E_USER_WARNING); return ''; } if (isset($aParams['file'])) { if (stripos($aParams['file'], 'http://') === 0 || stripos($aParams['file'], 'https://') === 0 || stripos($aParams['file'], 'http://') === 0) { $sUrl = $aParams['file']; } else { if (F::File_LocalDir($aParams['file'])) { $sFile = $aParams['file']; } else { // Need URL to asset file if (empty($aParams['skin'])) { $sSkin = E::ModuleViewer()->GetConfigSkin(); } else { $sSkin = $aParams['skin']; } if (isset($aParams['theme'])) { if (is_bool($aParams['theme'])) { $sTheme = E::ModuleViewer()->GetConfigTheme(); } else { $sTheme = $aParams['theme']; } } else { $sTheme = ''; } if ($sTheme) { $sTheme = 'themes/' . $sTheme . '/'; } if (isset($aParams['plugin'])) { $sFile = Plugin::GetTemplateFile($aParams['plugin'], $aParams['file']); } else { $sFile = Config::Get('path.skins.dir') . '/' . $sSkin . '/' . $sTheme . $aParams['file']; } } if (isset($aParams['prepare'])) { /** @var ModuleViewerAsset $oLocalViewerAsset */ $oLocalViewerAsset = new ModuleViewerAsset(); $oLocalViewerAsset->AddFiles(F::File_GetExtension($sFile, true), array($sFile)); $oLocalViewerAsset->Prepare(); $sUrl = $oLocalViewerAsset->AssetFileUrl(F::File_NormPath($sFile)); } else { $sUrl = E::ModuleViewerAsset()->File2Link($sFile, 'skin/' . $sSkin . '/'); } } } else { // Need URL to asset dir $sUrl = E::ModuleViewer()->GetAssetUrl() . 'skin/' . $aParams['skin'] . '/'; } return $sUrl; }
/** * Компилирует файл less и возвращает текст css-файла * * @param $aFile * @param $sCacheDir * @param $sMapPath * @param $aParams * @param $bCompress * @return string */ public function CompileFile($aFile, $sCacheDir, $sMapPath, $aParams, $bCompress) { if (!($sMapPath && $sCacheDir && $aFile)) { return ''; } try { $options = array('sourceMap' => TRUE, 'sourceMapWriteTo' => $sMapPath, 'sourceMapURL' => E::ModuleViewerAsset()->AssetFileUrl($sMapPath), 'cache_dir' => $sCacheDir); if ($bCompress) { $options = array_merge($options, array('compress' => TRUE)); } $sCssFileName = Less_Cache::Get($aFile, $options, $aParams); return file_get_contents($sCacheDir . $sCssFileName); } catch (Exception $e) { E::ModuleMessage()->AddErrorSingle($e->getMessage()); } return ''; }
public function ClearAssetsFiles() { $sDir = F::File_GetAssetDir(); F::File_RemoveDir($sDir); E::ModuleViewerAsset()->ClearAssetsCache(); }
protected function _convertUrlsInCss($sContent, $sSourceDir) { // Есть ли в файле URLs if (!preg_match_all('/(?P<src>src:)?url\\((?P<url>.*?)\\)/is', $sContent, $aMatchedUrl, PREG_OFFSET_CAPTURE)) { return $sContent; } // * Обрабатываем список URLs $aUrls = array(); foreach ($aMatchedUrl['url'] as $nIdx => $aPart) { $sPath = $aPart[0]; //$nPos = $aPart[1]; // * Don't touch data URIs if (strstr($sPath, 'data:')) { continue; } $sPath = str_replace(array('\'', '"'), '', $sPath); // * Если путь является абсолютным, то не обрабатываем if (substr($sPath, 0, 1) == "/" || substr($sPath, 0, 5) == 'http:' || substr($sPath, 0, 6) == 'https:') { continue; } if (($n = strpos($sPath, '?')) || ($n = strpos($sPath, '#'))) { $sPath = substr($sPath, 0, $n); $sFileParam = substr($sPath, $n); } else { $sFileParam = ''; } if (!isset($aUrls[$sPath])) { // if url didn't prepare... $sRealPath = realpath($sSourceDir . $sPath); if ($sRealPath) { $sDestination = F::File_GetAssetDir() . F::Crc32(dirname($sRealPath), true) . '/' . basename($sRealPath); $aUrls[$sPath] = array('source' => $sRealPath, 'destination' => $sDestination, 'url' => E::ModuleViewerAsset()->AssetFileDir2Url($sDestination) . $sFileParam); F::File_Copy($sRealPath, $sDestination); } } } if ($aUrls) { $sContent = str_replace(array_keys($aUrls), F::Array_Column($aUrls, 'url'), $sContent); } return $sContent; }
/** * Создание ресурса из множества файлов * * @param string $sAsset * @param array $aFiles * * @return bool */ public function MakeMerge($sAsset, $aFiles) { $sDestination = F::File_GetAssetDir() . md5($sAsset . serialize($aFiles)) . '.' . $this->sOutType; if (!$this->CheckDestination($sDestination)) { $sContents = ''; $bCompress = $this->bCompress; $bPrepare = null; foreach ($aFiles as $aFileParams) { $sFileContents = trim(F::File_GetContents($aFileParams['file'])); $sContents .= $this->PrepareContents($sFileContents, $aFileParams['file']) . PHP_EOL; if (isset($aFileParams['compress'])) { $bCompress = $bCompress && (bool) $aFileParams['compress']; } // Если хотя бы один файл из набора нужно выводить, то весь набор выводится if ((is_null($bPrepare) || $bPrepare === true) && isset($aFileParams['prepare']) && !$aFileParams['prepare']) { $bPrepare = false; } } if (F::File_PutContents($sDestination, $sContents)) { $aParams = array('file' => $sDestination, 'asset' => $sAsset, 'asset_file' => $sDestination, 'compress' => $bCompress, 'prepare' => is_null($bPrepare) ? false : $bPrepare); $this->AddLink($this->sOutType, E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aParams); } else { F::SysWarning('Can not write asset file "' . $sDestination . '"'); return false; } } else { $aParams = array('file' => $sDestination, 'asset' => $sAsset, 'asset_file' => $sDestination, 'compress' => $this->bCompress, 'prepare' => false); $this->AddLink($this->sOutType, E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aParams); } return true; }