PluginProbe
UpdraftCentral Dashboard / trunk
UpdraftCentral Dashboard vtrunk
0.8.33 0.7.2 0.7.3 0.7.4 0.8.0 0.8.1 0.8.10 0.8.11 0.8.12 0.8.13 0.8.14 0.8.15 0.8.16 0.8.17 0.8.18 0.8.19 0.8.2 0.8.20 0.8.21 0.8.22 0.8.23 0.8.24 0.8.25 0.8.26 0.8.27 All 51 releases
updraftcentral / templates / handlebars-compiler.php

handlebars-compiler.php in UpdraftCentral Dashboard trunk, at templates/handlebars-compiler.php

107 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // @codingStandardsIgnoreFile
4
5 if (!empty($_SERVER['HTTP_HOST']) || !empty($_SERVER['SERVER_NAME'])) die('This script is for running from the command-line, not via a browser');
6
7 // The handlebars compiler - http://handlebarsjs.com/precompilation.html
8 $path_to_handlebars = !empty($_ENV['HANDLEBARS']) ? $_ENV['HANDLEBARS'] : '/usr/bin/handlebars';
9
10 if (!file_exists($path_to_handlebars)) die('Handlebars not found: '.$path_to_handlebars);
11
12 $templates_dir = wp_normalize_path(dirname(__FILE__));
13
14 // Copy over (temporarily) templates from modules
15 $modules_dir = dirname($templates_dir).'/modules';
16 $added_modules = array();
17 if (is_dir($modules_dir) && $dir_handle = opendir($modules_dir)) {
18 while (false !== ($e = readdir($dir_handle))) {
19 if ('.' == $e || '..' == $e) continue;
20 if (is_dir($modules_dir.'/'.$e) && file_exists($modules_dir.'/'.$e.'/templates') && is_dir($modules_dir.'/'.$e.'/templates')) {
21 if (is_dir($e)) {
22 die("ABORT: Directory '$e' in the templates directory already exists (i.e. clashing namespaces)");
23 }
24 exec('cp -r '.escapeshellarg($modules_dir.'/'.$e.'/templates').' '.escapeshellarg($e));
25 $added_modules[] = $e;
26 }
27 }
28 @closedir($dir_handle);
29 }
30
31 $directory = new RecursiveDirectoryIterator($templates_dir);
32 $iterator = new RecursiveIteratorIterator($directory);
33 $regex = new RegexIterator($iterator, '/^.+\.handlebars\.html$/i', RecursiveRegexIterator::GET_MATCH);
34
35 $output_files = array();
36
37 foreach ($regex as $files) {
38
39 foreach ($files as $file) {
40
41 $file = wp_normalize_path($file);
42 if (strpos($file, $templates_dir) === 0) {
43 $output_files[] = handlebars_compile(substr($file, 1 + strlen($templates_dir)));
44 } else {
45 echo "ERROR: Could not parse filename: $file\n";
46 }
47 }
48
49 }
50
51 $complete_output = "UpdraftCentral_Handlebars = (typeof UpdraftCentral_Handlebars === 'undefined') ? {} : UpdraftCentral_Handlebars;\n";
52
53 foreach ($output_files as $of) {
54 $complete_output .= file_get_contents($of)."\n";
55 unlink($of);
56 }
57
58 file_put_contents('handlebars-compiled.js', $complete_output);
59
60 foreach ($added_modules as $module) {
61 echo "Cleanup: remove: templates/$module\n";
62 exec('rm -rf '.escapeshellarg($module));
63 }
64
65 function handlebars_compile($file) {
66 global $path_to_handlebars, $templates_dir;
67 // $output_file = substr($file, 0, -5).'.js';
68 $output_file = substr($file, 0, -5).'.js';
69
70 $template_name = str_replace('/', '-', substr($file, 0, -16));
71
72 $output_file = dirname($output_file).'/'.$template_name.'.js';
73
74 // We have to do this to get Handlebars to create the right variable name
75 $copy_file = dirname($output_file).'/'.$template_name.'.handlebars.html';
76 copy($file, $copy_file);
77
78 // http://handlebarsjs.com/precompilation.html
79
80 // echo ($path_to_handlebars.' '.escapeshellarg($templates_dir.'/'.$file)." --name $template_name --namespace UpdraftCentral_Handlebars --min --extension handlebars.html --output ".escapeshellarg($templates_dir.'/'.$output_file)."\n");
81
82 exec($path_to_handlebars.' '.escapeshellarg($templates_dir.'/'.$copy_file).' --namespace UpdraftCentral_Handlebars --min --extension handlebars.html --output '.escapeshellarg($templates_dir.'/'.$output_file), $output, $return_code);
83 if ($return_code) {
84 echo "Return code ($file): $return_code\n";
85 } else {
86 echo "OK: $output_file\n";
87 }
88 if (is_array($output)) {
89 foreach ($output as $line) {
90 echo $line."\n";
91 }
92 }
93 @unlink($copy_file);
94
95 return $templates_dir.'/'.$output_file;
96 }
97
98 function wp_normalize_path($path) {
99 $path = str_replace('\\', '/', $path);
100 $path = preg_replace('|/+|', '/', $path);
101 if (':' === substr($path, 1, 1)) {
102 $path = ucfirst($path);
103 }
104
105 return $path;
106 }
107