Created
February 10, 2019 09:03
-
-
Save AlekVolsk/bc183d577c162df6eed5eac0c98762a6 to your computer and use it in GitHub Desktop.
Get UIkit 3 variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Collector of all UIkit3 variables into one file | |
* PHP 7.1+ !!! | |
* version 1.1.3 | |
*/ | |
error_reporting( E_ALL ); | |
ini_set( 'display_errors', 1 ); | |
// the file that lists all the components | |
$fileList = __DIR__ . '/uikit/src/less/components/_import.less'; | |
// folder with component files | |
$lessFiles = __DIR__ . '/uikit/src/less/components'; | |
// result file | |
$fileResult = __DIR__ . '/less/uikit-variables.less'; | |
// pattern | |
$pattern = "/@import \"(.+)\";/"; | |
// substitute the path for the images | |
$isImage = true; | |
// the desired path of the images | |
$imageSearh = '../../images'; | |
// the new path of the images | |
$imageReplace = '@{images-base}'; | |
//////////////////////////////////////////////////////////////////////////////// | |
function microtime_float() | |
{ | |
[ $usec, $sec ] = explode( ' ', microtime() ); | |
return ( (float)$usec + (float)$sec ); | |
} | |
$time_start = microtime_float(); | |
$template = '<!DOCTYPE html><html lang="en"><meta charset="utf-8"><style>pre{font:16px/1.2 Consolas,monaco,monospace;padding:20px;background-color:#f7f7f7;border:1px solid #ddd;unicode-bidi:isolate;}</style></head><body><pre>%s</pre></body></html>'; | |
$fileList = str_replace( '\\', '/', $fileList ); | |
$lessFiles = str_replace( '\\', '/', $lessFiles ); | |
$fileResult = str_replace( '\\', '/', $fileResult ); | |
if ( !file_exists($fileList ) ) | |
{ | |
echo sprintf( $template, '<strong>Error:</strong> File with list of less files not found (code 1): <em>' . $fileList . '</em>' ); | |
exit; | |
} | |
if ( !is_dir( $lessFiles ) ) | |
{ | |
echo sprintf( $template, '<strong>Error:</strong> Directory with less files not found (code 2): <em>' . $lessFiles . '</em>' ); | |
exit; | |
} | |
$strs = file( $fileList ); | |
if ( empty( $strs ) ) | |
{ | |
echo sprintf( $template, '<strong>Error:</strong> File with list of less files is empty (code 3)' ); | |
exit; | |
} | |
$files_pre = []; | |
$files = []; | |
foreach ( $strs as $str ) | |
{ | |
preg_match( $pattern, $str, $matches ); | |
if ( count( $matches ) > 1 ) | |
{ | |
if ( in_array( $matches[1], ['variables.less', 'inverse.less', 'base.less'] ) ) | |
{ | |
$files_pre[] = $matches[1]; | |
} else { | |
$files[] = $matches[1]; | |
} | |
} | |
} | |
if ( empty( $files ) && empty( $files_pre ) ) | |
{ | |
echo sprintf( $template, '<strong>Error:</strong> File with list of less files does not contain a list of files (code 4)' ); | |
exit; | |
} | |
sort( $files ); | |
if ( in_array( 'base.less', $files_pre ) ) { array_unshift( $files, 'base.less' ); } | |
if ( in_array( 'inverse.less', $files_pre ) ) { array_unshift( $files, 'inverse.less' ); } | |
if ( in_array( 'variables.less', $files_pre ) ) { array_unshift( $files, 'variables.less' ); } | |
unset( $files_pre ); | |
$variables = []; | |
$variables[] = '/* UIkit variables'; | |
$variables[] = '============================================================================ */'; | |
$variables[] = ''; | |
$variables[] = ''; | |
$var_count = 0; | |
$err = ''; | |
foreach ( $files as $file ) | |
{ | |
$sectionName = explode( '.', $file )[0]; | |
$sectionName[0] = strtoupper( $sectionName[0] ); | |
$tmp = []; | |
$lessFile = $lessFiles . DIRECTORY_SEPARATOR . $file; | |
if ( !file_exists( $lessFile ) ) | |
{ | |
if ( $err == '' ) | |
{ | |
$err = '<br><br><strong>Errors:</strong>'; | |
} | |
$err .= '<br> ' . $file . ' — <strong>file not found</strong>'; | |
continue; | |
} | |
$strs = file( $lessFile ); | |
foreach ( $strs as $str ) | |
{ | |
if ( $str[0] === '@' && strpos( $str, '{' ) === false ) | |
{ | |
[ $name, $value ] = explode( ':', trim( $str ) ); | |
$value = trim( $value ); | |
if ( $isImage ) | |
{ | |
$value = str_replace( $imageSearh, $imageReplace, $value ); | |
} | |
$name = str_pad( trim( $name ) . ':', 50, ' ', STR_PAD_RIGHT ); | |
$tmp[] = $name . $value; | |
} | |
if ( trim( $str ) === '' && count( $tmp ) > 0 && $tmp[ count( $tmp ) - 1 ] !== '' ) | |
{ | |
$tmp[] = ''; | |
} | |
} | |
if ( count( $tmp ) ) | |
{ | |
$var_count += count( $tmp ); | |
$variables[] = '// ' . $sectionName; | |
$variables[] = '// ========================================================================'; | |
$variables[] = ''; | |
$variables = array_merge( $variables, $tmp ); | |
$variables[] = ''; | |
} | |
} | |
$pathResult = pathinfo( $fileResult, PATHINFO_DIRNAME ); | |
if ( !is_dir( $pathResult ) && !mkdir( $pathResult, 0755, true ) ) | |
{ | |
echo sprintf( $template, '<strong>Error:</strong> It is impossible to create result directory (code 5): <em>' . $pathResult . '</em>' ); | |
exit; | |
} | |
if ( file_put_contents( $fileResult, implode( "\n", $variables ) ) === false ) | |
{ | |
echo sprintf( $template, '<strong>Error:</strong> It is impossible to create a result file (code 6): <em>' . $fileResult . '</em>' ); | |
exit; | |
} | |
$time_end = microtime_float(); | |
$time = round( ( $time_end - $time_start ) * 1000, 1 ); | |
$result = sprintf( | |
'%s Saved <strong>%s variables</strong> in %s <em>%sms</em>', | |
date( 'Y-m-d H:i:s' ), | |
$var_count, | |
$fileResult, | |
$time | |
); | |
echo sprintf( $template, $result . $err ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment