mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°2982 - Speed up SCSS themes compilation during setup : fix precompiled css theme generated
This commit is contained in:
@@ -322,10 +322,10 @@ CSS;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$aFiles = $oFindStylesheetObject->GetImports();
|
$aFiles = $oFindStylesheetObject->GetImportPaths();
|
||||||
if (count($aFiles) !== 0) {
|
if (count($aFiles) !== 0) {
|
||||||
foreach ($aFiles as $sFile) {
|
foreach ($aFiles as $sFileURI => $sFilePath) {
|
||||||
$aSignature['imports'][$sFile] = md5_file($sFile);
|
$aSignature['imports'][$sFileURI] = md5_file($sFilePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -736,18 +736,18 @@ CSS;
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 3.0.0 N°2982
|
* @param string $sFileURI
|
||||||
* Find the given file in the list '$aImportsPaths' of directory and all included stylesheets as well
|
|
||||||
* Compute latest timestamp found among all found stylesheets
|
|
||||||
*
|
|
||||||
* @param string $sFile
|
|
||||||
* @param string[] $aImportsPaths
|
* @param string[] $aImportsPaths
|
||||||
* @param FindStylesheetObject $oFindStylesheetObject
|
* @param FindStylesheetObject $oFindStylesheetObject
|
||||||
* @param bool $bImports
|
* @param bool $bImports
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
|
*@since 3.0.0 N°2982
|
||||||
|
* Find the given file in the list '$aImportsPaths' of directory and all included stylesheets as well
|
||||||
|
* Compute latest timestamp found among all found stylesheets
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public static function FindStylesheetFile(string $sFile, array $aImportsPaths, $oFindStylesheetObject, $bImports = false)
|
public static function FindStylesheetFile(string $sFileURI, array $aImportsPaths, $oFindStylesheetObject, $bImports = false)
|
||||||
{
|
{
|
||||||
if (! $bImports) {
|
if (! $bImports) {
|
||||||
$oFindStylesheetObject->ResetLastStyleSheet();
|
$oFindStylesheetObject->ResetLastStyleSheet();
|
||||||
@@ -755,7 +755,7 @@ CSS;
|
|||||||
|
|
||||||
foreach($aImportsPaths as $sPath)
|
foreach($aImportsPaths as $sPath)
|
||||||
{
|
{
|
||||||
$sFilePath = $sPath.'/'.$sFile;
|
$sFilePath = $sPath.'/'.$sFileURI;
|
||||||
$sImportedFile = realpath($sFilePath);
|
$sImportedFile = realpath($sFilePath);
|
||||||
if ($sImportedFile === false){
|
if ($sImportedFile === false){
|
||||||
// Handle shortcut syntax : @import "typo" ;
|
// Handle shortcut syntax : @import "typo" ;
|
||||||
@@ -763,7 +763,7 @@ CSS;
|
|||||||
$sFilePath2 = "$sFilePath.scss";
|
$sFilePath2 = "$sFilePath.scss";
|
||||||
$sImportedFile = realpath($sFilePath2);
|
$sImportedFile = realpath($sFilePath2);
|
||||||
if ($sImportedFile){
|
if ($sImportedFile){
|
||||||
self::FindStylesheetFile("$sFile.scss", [ $sPath ], $oFindStylesheetObject, $bImports);
|
self::FindStylesheetFile("$sFileURI.scss", [ $sPath ], $oFindStylesheetObject, $bImports);
|
||||||
$sImportedFile = false;
|
$sImportedFile = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -773,22 +773,22 @@ CSS;
|
|||||||
// file matched: _typo.scss
|
// file matched: _typo.scss
|
||||||
$sShortCut = substr($sFilePath, strrpos($sFilePath, '/') + 1);
|
$sShortCut = substr($sFilePath, strrpos($sFilePath, '/') + 1);
|
||||||
$sFilePath = str_replace($sShortCut, "_$sShortCut.scss", $sFilePath);
|
$sFilePath = str_replace($sShortCut, "_$sShortCut.scss", $sFilePath);
|
||||||
$sFile = str_replace($sShortCut, "_$sShortCut.scss", $sFile);
|
$sFileURI = str_replace($sShortCut, "_$sShortCut.scss", $sFileURI);
|
||||||
$sImportedFile = realpath($sFilePath);
|
$sImportedFile = realpath($sFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((file_exists($sImportedFile))
|
if ((file_exists($sImportedFile))
|
||||||
&& (!$oFindStylesheetObject->AlreadyFetched($sFile)))
|
&& (!$oFindStylesheetObject->AlreadyFetched($sImportedFile)))
|
||||||
{
|
{
|
||||||
if ($bImports){
|
if ($bImports){
|
||||||
$oFindStylesheetObject->AddImport($sFile, $sImportedFile);
|
$oFindStylesheetObject->AddImport($sFileURI, $sImportedFile);
|
||||||
}else{
|
}else{
|
||||||
$oFindStylesheetObject->AddStylesheet($sFile, $sImportedFile);
|
$oFindStylesheetObject->AddStylesheet($sFileURI, $sImportedFile);
|
||||||
}
|
}
|
||||||
$oFindStylesheetObject->UpdateLastModified($sImportedFile);
|
$oFindStylesheetObject->UpdateLastModified($sImportedFile);
|
||||||
|
|
||||||
//Regexp matching on all included scss files : @import 'XXX.scss';
|
//Regexp matching on all included scss files : @import 'XXX.scss';
|
||||||
$sDirUri = dirname($sFile);
|
$sDirUri = dirname($sFileURI);
|
||||||
preg_match_all('/@import \s*[\"\']([^\"\']*)\s*[\"\']\s*;/', file_get_contents($sImportedFile), $aMatches);
|
preg_match_all('/@import \s*[\"\']([^\"\']*)\s*[\"\']\s*;/', file_get_contents($sImportedFile), $aMatches);
|
||||||
if ( (is_array($aMatches)) && (count($aMatches)!==0) ){
|
if ( (is_array($aMatches)) && (count($aMatches)!==0) ){
|
||||||
foreach ($aMatches[1] as $sImportedFile){
|
foreach ($aMatches[1] as $sImportedFile){
|
||||||
@@ -833,121 +833,3 @@ CSS;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @since 3.0.0 N°2982
|
|
||||||
* Class FindStylesheetObject: dedicated class to store computations made in method FindStylesheetFile.
|
|
||||||
*/
|
|
||||||
class FindStylesheetObject{
|
|
||||||
|
|
||||||
//file URIs
|
|
||||||
private $aStylesheetImportURIs;
|
|
||||||
private $aStylesheetFileURIs;
|
|
||||||
|
|
||||||
//fill paths
|
|
||||||
private $aAllStylesheetFilePaths;
|
|
||||||
private $sLastStyleSheetPath;
|
|
||||||
|
|
||||||
private $iLastModified;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* FindStylesheetObject constructor.
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$this->aStylesheetFileURIs = [];
|
|
||||||
$this->aStylesheetImportURIs = [];
|
|
||||||
$this->sLastStyleSheetPath = "";
|
|
||||||
$this->iLastModified = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function GetLastStylesheetFile(): string
|
|
||||||
{
|
|
||||||
return $this->sLastStyleSheetPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function GetImports(): array
|
|
||||||
{
|
|
||||||
return $this->aStylesheetImportURIs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array : main stylesheets URIs
|
|
||||||
*/
|
|
||||||
public function GetStylesheetFileURIs(): array
|
|
||||||
{
|
|
||||||
return $this->aStylesheetFileURIs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function GetLastModified() : int
|
|
||||||
{
|
|
||||||
return $this->iLastModified;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array : included files URIs
|
|
||||||
*/
|
|
||||||
public function GetStylesheetImportURIs(): array
|
|
||||||
{
|
|
||||||
return $this->aStylesheetImportURIs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array : main stylesheets paths + included files paths
|
|
||||||
*/
|
|
||||||
public function GetAllStylesheetPaths(): array
|
|
||||||
{
|
|
||||||
return $this->aAllStylesheetFilePaths;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string : last found stylesheet URI
|
|
||||||
*/
|
|
||||||
public function GetLastStyleSheetPath(): string
|
|
||||||
{
|
|
||||||
return $this->sLastStyleSheetPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function AddStylesheet(string $sStylesheetFile, string $sStylesheetFilePath): void
|
|
||||||
{
|
|
||||||
$this->aStylesheetFileURIs[] = $sStylesheetFile;
|
|
||||||
$this->aAllStylesheetFilePaths[] = $sStylesheetFilePath;
|
|
||||||
$this->sLastStyleSheetPath = $sStylesheetFilePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function AlreadyFetched(string $sStylesheetFile) : bool {
|
|
||||||
return in_array($sStylesheetFile, $this->aStylesheetFileURIs)
|
|
||||||
|| in_array($sStylesheetFile, $this->aStylesheetImportURIs);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function AddImport(string $sStylesheetFile, string $sStylesheetFilePath): void
|
|
||||||
{
|
|
||||||
$this->aStylesheetImportURIs[] = $sStylesheetFile;
|
|
||||||
$this->aAllStylesheetFilePaths[] = $sStylesheetFilePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function UpdateLastModified(string $sStylesheetFile): void
|
|
||||||
{
|
|
||||||
$this->iLastModified = max($this->iLastModified, @filemtime($sStylesheetFile));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function ResetLastStyleSheet(): void
|
|
||||||
{
|
|
||||||
$this->sLastStyleSheetPath = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class CompileCSSService
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* CompileCSSService constructor.
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function CompileCSSFromSASS($sSassContent, $aImportPaths = [], $aVariables = []){
|
|
||||||
return utils::CompileCSSFromSASS($sSassContent, $aImportPaths, $aVariables);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -2896,6 +2896,8 @@ EOF;
|
|||||||
{
|
{
|
||||||
SetupLog::Info("Replacing theme '$sThemeId' precompiled file in file $sPostCompilationLatestPrecompiledFile for next setup.");
|
SetupLog::Info("Replacing theme '$sThemeId' precompiled file in file $sPostCompilationLatestPrecompiledFile for next setup.");
|
||||||
copy($sThemeDir.'/main.css', $sPostCompilationLatestPrecompiledFile);
|
copy($sThemeDir.'/main.css', $sPostCompilationLatestPrecompiledFile);
|
||||||
|
}else {
|
||||||
|
SetupLog::Info("No theme '$sThemeId' compilation was required during setup.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->Log(sprintf('Themes compilation took: %.3f ms for %d themes.', (microtime(true) - $fStart)*1000.0, count($aThemes)));
|
$this->Log(sprintf('Themes compilation took: %.3f ms for %d themes.', (microtime(true) - $fStart)*1000.0, count($aThemes)));
|
||||||
|
|||||||
@@ -20,9 +20,6 @@ class ThemeHandlerTest extends ItopTestCase
|
|||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
@include_once '/home/combodo/workspace/iTop/approot.inc.php';
|
|
||||||
|
|
||||||
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
require_once(APPROOT.'application/themehandler.class.inc.php');
|
require_once(APPROOT.'application/themehandler.class.inc.php');
|
||||||
require_once(APPROOT.'setup/modelfactory.class.inc.php');
|
require_once(APPROOT.'setup/modelfactory.class.inc.php');
|
||||||
@@ -179,7 +176,7 @@ class ThemeHandlerTest extends ItopTestCase
|
|||||||
{
|
{
|
||||||
$sStylesheetId = $oStylesheet->getAttribute('id');
|
$sStylesheetId = $oStylesheet->getAttribute('id');
|
||||||
$aThemeParameters['stylesheets'][$sStylesheetId] = $oStylesheet->GetText();
|
$aThemeParameters['stylesheets'][$sStylesheetId] = $oStylesheet->GetText();
|
||||||
ThemeHandler::FindStylesheetFile($oImport->GetText(), $aImportsPaths, $oFindStylesheetObject);
|
ThemeHandler::FindStylesheetFile($oStylesheet->GetText(), $aImportsPaths, $oFindStylesheetObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
$aIncludedImages = ThemeHandler::GetIncludedImages($aThemeParameters['variables'], $oFindStylesheetObject->GetStylesheetFileURIs(), $sThemeId);
|
$aIncludedImages = ThemeHandler::GetIncludedImages($aThemeParameters['variables'], $oFindStylesheetObject->GetStylesheetFileURIs(), $sThemeId);
|
||||||
@@ -611,12 +608,19 @@ SCSS;
|
|||||||
*/
|
*/
|
||||||
public function testFindStylesheetFile(string $sFileToFind, array $aAllImports){
|
public function testFindStylesheetFile(string $sFileToFind, array $aAllImports){
|
||||||
$aImportsPath = $this->sTmpDir.'/branding/';
|
$aImportsPath = $this->sTmpDir.'/branding/';
|
||||||
|
$aExpectedAllImports =[];
|
||||||
|
if (count($aAllImports)!==0){
|
||||||
|
foreach ($aAllImports as $sFileURI){
|
||||||
|
$aExpectedAllImports [$sFileURI] = $aImportsPath.$sFileURI;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$oFindStylesheetObject = new FindStylesheetObject();
|
$oFindStylesheetObject = new FindStylesheetObject();
|
||||||
ThemeHandler::FindStylesheetFile($sFileToFind, [$aImportsPath], $oFindStylesheetObject);
|
ThemeHandler::FindStylesheetFile($sFileToFind, [$aImportsPath], $oFindStylesheetObject);
|
||||||
|
|
||||||
$this->assertEquals([$sFileToFind], $oFindStylesheetObject->GetStylesheetFileURIs());
|
$this->assertEquals([$sFileToFind], $oFindStylesheetObject->GetStylesheetFileURIs());
|
||||||
$this->assertEquals($aAllImports, str_replace($aImportsPath, "", $oFindStylesheetObject->GetImports()));
|
$this->assertEquals($aExpectedAllImports, $oFindStylesheetObject->GetImportPaths());
|
||||||
$this->assertEquals($aImportsPath.$sFileToFind, $oFindStylesheetObject->GetLastStyleSheetPath());
|
$this->assertEquals($aImportsPath.$sFileToFind, $oFindStylesheetObject->GetLastStyleSheetPath());
|
||||||
|
|
||||||
$aExpectedAllStylesheetPaths = [];
|
$aExpectedAllStylesheetPaths = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user