leongrdic

LeBuilder

Aug 23rd, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0);
  4.  
  5. $options = getopt("l");
  6.  
  7. $config = [
  8.     'input_path' => 'source', 'output_path' => 'build',
  9.     'exclude_files' => [], 'exclude_extentions' => [],
  10.     'ignore_files' => [], 'ignore_extensions' => [],
  11.     'sign' => '{%}', 'vars' => []
  12. ];
  13.  
  14. echo PHP_EOL;
  15.  
  16. if(file_exists('config.json')){
  17.     $custom_config = json_decode(file_get_contents('config.json'), true);
  18.     if($error = json_last_error()) die('There is an error in your config: ' . $error);
  19.     $config = array_replace_recursive($config, $custom_config);
  20. }
  21.  
  22. if(!file_exists($config['input_path'])) die('Input path invalid');
  23.  
  24. if(file_exists($config['output_path'])){
  25.     echo 'Cleaning previous build files...' . PHP_EOL;
  26.     if(is_dir($config['output_path'])){
  27.         delTree($config['output_path']) || die('Could not clean target directory');
  28.     }else{
  29.         unlink($config['output_path']) || die('Please remove the file named as the target directory');
  30.     }
  31. }
  32.  
  33. mkdir($config['output_path']);
  34.  
  35. $filemtime = [];
  36. $n = 0;
  37. echo 'Processing files...' . PHP_EOL;
  38. do{
  39.     $files = processDir($config['input_path']);
  40.     if(isset($options['l']) && !($n++)) echo 'Done. Checking for changes...' . PHP_EOL;
  41. }while(isset($options['l']));
  42.  
  43. echo 'Done. File list:' . PHP_EOL;
  44. print_r($files);
  45.  
  46. // Functions
  47.  
  48. function delTree($dir){
  49.     if(!$dir || !is_dir($dir)) return false;
  50.    
  51.     foreach(array_diff(scandir($dir), ['.', '..']) as $value){
  52.         $path = $dir . DIRECTORY_SEPARATOR . $value;
  53.         if(is_dir($path)) delTree($path);
  54.         else unlink($path);
  55.     }
  56.    
  57.     return rmdir($dir);
  58. }
  59.  
  60. function processDir($dir, $ignore = false){
  61.     global $config, $filemtime;
  62.    
  63.     clearstatcache();
  64.    
  65.     $result = [];
  66.     foreach(array_diff(scandir($dir), ['.', '..']) as $value){
  67.         $path = $dir . DIRECTORY_SEPARATOR . $value;
  68.         $relpath = substr($path, strlen($config['input_path'])+1);
  69.         $outpath = $config['output_path'] . DIRECTORY_SEPARATOR . $relpath;
  70.        
  71.         $ignorepath = $ignore || in_array($relpath, $config['ignore_files']);
  72.         if(in_array($relpath, $config['exclude_files'])) continue;
  73.         if(is_dir($path)){
  74.             if(!file_exists($outpath)) mkdir($outpath);
  75.             $result[$value] = processDir($path, $ignorepath);
  76.         }else{
  77.             if(isset($filemtime[$relpath]) && filemtime($path) == $filemtime[$relpath]) continue;
  78.             if(isset($filemtime[$relpath])) echo date("Y-m-d H:i:s") . ' | Change detected: ' . $relpath . PHP_EOL;
  79.             $filemtime[$relpath] = filemtime($path);
  80.            
  81.             $ext = pathinfo($path, PATHINFO_EXTENSION);
  82.             if(in_array($ext, $config['exclude_extentions'])) continue;
  83.            
  84.             if($ignorepath || in_array($ext, $config['ignore_extensions'])){
  85.                 copy($path, $outpath);
  86.             }else{
  87.                 file_put_contents($outpath, processFile($path, $config['sign'], $config['vars'])['content']);
  88.             }
  89.             $result[] = $value;
  90.         }
  91.     }
  92.    
  93.     return $result;
  94. }
  95.  
  96. function processFile($path, $sign, $vars = []){
  97.     if(!file_exists($path)) throw new Exception('Included file not found: ' . $path);
  98.    
  99.     $finalcontent = '';
  100.     $dir = dirname($path) . DIRECTORY_SEPARATOR;
  101.     $content = file_get_contents($path);
  102.     $parts = explode($sign, $content);
  103.     for($i=0; $i<count($parts); $i++){
  104.         if(($i+1)%2){
  105.             $finalcontent .= $parts[$i];
  106.         }else{
  107.             $cmd = json_decode($parts[$i], true);
  108.             if(json_last_error()) throw new Exception('Syntax error: ' . $path);
  109.             if(isset($cmd['vars']) && is_array($cmd['vars'])) $vars = array_merge($vars, $cmd['vars']);
  110.             if(isset($cmd['var']) && array_key_exists($cmd['var'], $vars)) $finalcontent .= $vars[$cmd['var']];
  111.             if(isset($cmd['include'])){
  112.                 $result = processFile($dir . $cmd['include'], $sign, $vars);
  113.                 $finalcontent .= $result['content'];
  114.                 $vars = array_merge($vars, $result['vars']);
  115.             }
  116.         }
  117.     }
  118.    
  119.     return ['content' => $finalcontent, 'vars' => $vars];
  120. }
Advertisement
Add Comment
Please, Sign In to add comment