-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·610 lines (539 loc) · 15.8 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
// -------------------------------------------------------------------------------------------
// Terrific Micro v1.0.4
// https://github.com/namics/terrific-micro/blob/master/README.md
//
// Do not change this file - use project/index.project.php for your customisations
// -------------------------------------------------------------------------------------------
define( 'BASE', __DIR__ . '/' );
$config = json_decode( file_get_contents( BASE . 'config.json' ) );
$nocache = false; // true -> disables .less/.scss caching
$cachedir = ( is_writeable( sys_get_temp_dir() ) ? sys_get_temp_dir() : BASE . 'app/cache' ); // use php temp or the local cache directory
// ---------------------
// Project & Check
// ---------------------
include_once( BASE . 'project/index.project.php' ); // use this file for all your customisations
if ( !function_exists( 'check_config' ) ) {
/**
* checks base configuration
*/
function check_config() {
global $config;
if ( false === is_object( $config ) ) {
die( BASE . 'config.json missing or broken' );
}
}
}
check_config();
// ------------------
// Base Functionality
// ------------------
if ( !function_exists( 'partial' ) ) {
/**
* Outputs a partial
*
* @param $file
* @param array $data
*/
function partial( $file, $data = array() ) {
global $config;
$partial_template = BASE . $config->micro->view_partials_directory . '/' . $file . '.' . $config->micro->view_file_extension;
if ( file_exists( $partial_template ) ) {
require $partial_template;
}
else {
echo '<p>Could not find the partial file: <code>' . $file . '.' . $config->micro->view_file_extension . '</code></p>';
}
}
}
if ( !function_exists( 'component' ) ) {
/**
* Outputs component markup.
*
* @param $name
* @param null $template
* @param array $skin
* @param array $attr
* @param null $tag
*/
function component( $name, $template = null, $skin = array(), $attr = array(), $tag = null ) {
global $config;
$flat = strtolower( $name );
$template = empty( $template ) || $template === true ? '' : '-' . strtolower( $template );
$component_wrapper = !empty( $skin ) || $skin === '0' || $skin === null || $skin === false || !empty( $attr ) || !empty( $tag );
$component_template = $name . '/' . $flat . $template . '.' . $config->micro->view_file_extension;
$component_file = false;
foreach ( $config->micro->components as $key => $component ) {
$directory = $component->path;
$component_prefix = property_exists( $component, 'component_prefix' ) ? $component->component_prefix : '';
if ( file_exists( BASE . $directory . '/' . $component_template ) ) {
$component_file = BASE . $directory . '/' . $component_template;
break 1;
}
else {
foreach ( glob( BASE . $directory . '/*/' . $component_template ) as $component_template_file ) {
if ( file_exists( $component_template_file ) ) {
$component_file = $component_template_file;
break 2;
}
}
}
}
echo PHP_EOL;
if ( $component_file === false ) {
echo '<p>Could not find the component template: <code>' . $flat . $template . '.' . $config->micro->view_file_extension . '</code></p>';
}
else {
if ( $component_wrapper ) {
$dashed = strtolower( preg_replace( array(
'/([A-Z]+)([A-Z][a-z])/',
'/([a-z\d])([A-Z])/'
), array( '\\1-\\2', '\\1-\\2' ), $name ) );
$skins = '';
if ( !empty( $skin ) || $skin === '0' ) {
$skin_prefix = property_exists( $component, 'skin_prefix' ) ? $component->skin_prefix : '';
if ( is_array( $skin ) ) {
foreach ( $skin as $key => $value ) {
$skins .= ' ' . $skin_prefix . '-' . $dashed . '-' . $value;
}
}
else {
$skins = !is_string( $skin ) ? '' : ' ' . $skin_prefix . '-' . $dashed . '-' . $skin;
}
}
$attributes = ' ';
$additional_classes = '';
if ( !empty( $attr ) && is_array( $attr ) ) {
foreach ( $attr as $key => $value ) {
if ( $key === 'class' && !empty( $value ) ) {
$additional_classes .= ' ' . $value;
}
else {
$attributes .= $key . '="' . $value . '" ';
}
}
}
if ( empty( $tag ) ) {
$tag = 'div';
}
echo '<' . $tag . ' class="' . $component_prefix . ' ' . $component_prefix . '-' . $dashed . $skins . $additional_classes . '"' . chop( $attributes ) . '>' . PHP_EOL;
}
require $component_file;
if ( $component_wrapper ) {
echo '</' . $tag . '>';
}
}
echo PHP_EOL;
}
}
if ( !function_exists( 'module' ) ) {
/**
* Wrapper function for component().
*
* This function has been deprecated in favour of component() and will be removed in a future release.
*
* @param name
* @param null $template
* @param array $skin
* @param array $attr
* @param null $tag
*
* @deprecated
*/
function module( $name, $template = null, $skin = array(), $attr = array(), $tag = null ) {
component( $name, $template, $skin, $attr, $tag );
}
}
// ------------------
// Helpers
// ------------------
if ( !function_exists( 'compile' ) ) {
/**
* Compiles a CSS/LESS/SCSS/JS file.
*
* @param $filename
* @param $extension
* @param array $dependencies
*
* @return string
*/
function compile( $filename, $extension, $dependencies = array() ) {
global $nocache, $cachedir;
switch ( $extension ) {
case 'less':
$modified = filemtime( $filename );
foreach ( $dependencies as $dep ) {
if ( substr( strrchr( $dep, '.' ), 1 ) == $extension && filemtime( BASE . $dep ) > $modified ) {
$modified = filemtime( BASE . $dep );
}
}
$cachefile = $cachedir . '/terrific-' . md5( BASE . implode( '', $dependencies ) . $filename ) . '.css';
if ( $nocache || !is_file( $cachefile ) || ( filemtime( $cachefile ) != $modified ) ) {
$filecontents = '';
foreach ( $dependencies as $dep ) {
if ( substr( strrchr( $dep, '.' ), 1 ) == $extension ) {
$filecontents .= file_get_contents( BASE . $dep );
}
}
$filecontents .= file_get_contents( $filename );
$less = get_less_parser();
try {
$content = $less->compile( $filecontents );
file_put_contents( $cachefile, $content );
touch( $cachefile, $modified );
}
catch ( Exception $e ) {
$content = get_compile_error_css( $e, $filename, 'lessphp' );
}
}
else {
$content = file_get_contents( $cachefile );
}
break;
case 'scss':
$modified = filemtime( $filename );
foreach ( $dependencies as $dep ) {
if ( substr( strrchr( $dep, '.' ), 1 ) == $extension && filemtime( BASE . $dep ) > $modified ) {
$modified = filemtime( BASE . $dep );
}
}
$cachefile = $cachedir . '/terrific-' . md5( BASE . implode( '', $dependencies ) . $filename ) . '.css';
if ( $nocache || !is_file( $cachefile ) || ( filemtime( $cachefile ) != $modified ) ) {
$filecontents = '';
foreach ( $dependencies as $dep ) {
if ( substr( strrchr( $dep, '.' ), 1 ) == $extension ) {
$filecontents .= file_get_contents( BASE . $dep );
}
}
$filecontents .= file_get_contents( $filename );
$scss = get_scss_parser();
try {
$content = $scss->compile( $filecontents );
file_put_contents( $cachefile, $content );
touch( $cachefile, $modified );
}
catch ( Exception $e ) {
$content = get_compile_error_css( $e, $filename, 'scssphp' );
}
}
else {
$content = file_get_contents( $cachefile );
}
break;
default:
$content = file_get_contents( $filename );
break;
}
return $content . PHP_EOL;
}
}
if ( !function_exists( 'dump' ) ) {
/**
* Dumps a CSS/JS file
*
* @param $name
* @param $mimetype
*/
function dump( $name, $mimetype ) {
global $config;
$starttime = microtime( true );
$excludes = array();
$dependencies = array();
$patterns = array();
$filetype = substr( strrchr( $name, '.' ), 1 );
$output = '';
$minify = isset( $_REQUEST['min'] );
$debugjavascript = $filetype === 'js' && isset( $_REQUEST['debug'] );
if ( $debugjavascript ) {
$output .= '// load js files in a synchronous way' . PHP_EOL;
}
// collect excluded pattern & (less/scss) dependencies & patterns
foreach ( $config->assets->$name as $pattern ) {
$firstchar = substr( $pattern, 0, 1 );
if ( $firstchar === '!' ) {
$excludes[] = substr( $pattern, 1 );
}
else if ( $firstchar === '+' ) {
$dependencies[] = substr( $pattern, 1 );
}
else {
$patterns[] = $pattern;
}
}
$dependencies = get_files( $dependencies );
$excludes = array_merge( $dependencies, $excludes );
$files = get_files( $patterns, $excludes );
foreach ( $files as $entry ) {
if ( !$debugjavascript ) {
$format = substr( strrchr( $entry, '.' ), 1 );
$output .= compile( BASE . $entry, $format, $dependencies );
}
else {
$output .= "document.write('<script type=\"text/javascript\" src=\"$entry\"><\/script>');" . PHP_EOL;
}
}
if ( $minify ) {
switch ( $filetype ) {
case 'css':
require BASE . 'app/library/cssmin/cssmin.php';
$output = CssMin::minify( $output );
break;
case 'js':
require BASE . 'app/library/jshrink/Minifier.php';
$output = \JShrink\Minifier::minify( $output );
break;
}
}
$time_taken = microtime( true ) - $starttime;
$output = get_asset_banner( $name, $filetype, $minify, $time_taken ) . $output;
header( 'Content-Type: ' . $mimetype );
echo $output;
}
}
if ( !function_exists( 'get_files' ) ) {
/**
* Gets an array of files with given glob patterns
*
* @param $patterns
* @param array $excludes
*
* @return array
*/
function get_files( $patterns, $excludes = array() ) {
$files = array();
foreach ( $patterns as $pattern ) {
foreach ( glob( BASE . $pattern ) as $uri ) {
$file = str_replace( BASE, '', $uri );
if ( is_file( $uri ) && !is_excluded_file( $file, $excludes ) ) {
$files[] = $file;
}
}
}
return array_unique( $files );
}
}
if ( !function_exists( 'is_excluded_file' ) ) {
/**
* Checks if a file matches an exclude pattern
*
* @param $filename
* @param array $excludes
*
* @return bool
*/
function is_excluded_file( $filename, $excludes = array() ) {
foreach ( $excludes as $exclude ) {
if ( fnmatch( $exclude, $filename ) ) {
return true;
break;
}
}
return false;
}
}
if ( !function_exists( 'get_asset_banner' ) ) {
/**
* Gets a header string for a processed asset
*
* @param string $filename
* @param string $filetype
* @param bool $minified
* @param $duration
*
* @return string
*/
function get_asset_banner( $filename = '', $filetype = '', $minified = false, $duration ) {
$ret = '';
if ( isset( $duration ) ) {
$time_taken = round( $duration * 1000 );
$ret .= '/* time taken: ' . $time_taken . ' ms';
$ret .= $minified ? ' (minified)' : '';
$ret .= ' */' . PHP_EOL;
}
return $ret;
}
}
if ( !function_exists( 'get_compile_error_css' ) ) {
/**
* Gets a css rule for an exception message to visualize it
*
* @param Exception $e
* @param string $filename
* @param string $tool
*
* @return string
*/
function get_compile_error_css( $e, $filename = '', $tool = '' ) {
$pattern = array(
'/ in file.*$/',
'/ in anonymous-file.*$/',
'/line: [0-9]+$/',
'/[^A-Za-z0-9\.:;@$() -]/'
);
$error_msg = preg_replace( $pattern, '', $e->getMessage() );
$msg = 'body:before, body:after {
display: block; box-sizing: border-box; position: fixed; top: 3vw; right: 3vw; bottom: 3vw; left: 3vw; z-index: 100000;
overflow: auto;
color: red; font: 20px/1.5 monospace; text-shadow: 1px 1px 1px rgba(255,255,255,.2); text-align: center;
border: 1px solid #ededff;
padding: 1.9em 2em;
background-color:#fff;
background-image: linear-gradient(0deg, #ededff 1px, transparent 1px), linear-gradient(90deg, #ededff 1px, transparent 1px);
background-size: 15px 15px, 15px 15px;
box-shadow: 0 25px 20px -20px rgba(0,0,0,.4);
white-space: pre-wrap;
content: "Ups, an error occured.\A ' . strtoupper( $tool ) . ' compile error - ' . $error_msg . ' \A in ' . addslashes( $filename ) . '";
}';
return $msg;
}
}
if ( !function_exists( 'get_less_parser' ) ) {
/**
* Returns the less parser
*
* @return lessc
*/
function get_less_parser() {
require_once BASE . 'app/library/lessphp/lessc.inc.php';
$less = new lessc;
//$less->setImportDir( array( '' ) ); // default
//$less->addImportDir( 'assets/bootstrap' );
return $less;
}
}
if ( !function_exists( 'get_scss_parser' ) ) {
/**
* Returns the scss parser
*
* @return scssc
*/
function get_scss_parser() {
require_once BASE . 'app/library/scssphp/scss.inc.php';
$scss = new scssc;
//$scss->setImportPaths( array( '' ) ); // default
//$scss->addImportPath( 'assets/bootstrap' );
return $scss;
}
}
// ------------------
// View templates
// ------------------
if ( !function_exists( 'render_view_template' ) ) {
/**
* Gets / compiles the view template
*
* @param $view
*
* @throws Exception
*/
function render_view_template( $view ) {
if ( file_exists( $view ) ) {
require $view;
}
else {
throw new Exception();
}
}
}
if ( !function_exists( 'render_view' ) ) {
/**
* Renders the view
*
* @param $view
*/
function render_view( $view ) {
$rendered = false;
try {
render_view_template( $view );
$rendered = true;
}
catch ( Exception $e ) {
}
if ( !$rendered ) {
header( 'HTTP/1.0 404 Not Found' );
echo '<h1>404 - not found</h1>';
}
}
}
// ------------------
// Main Processing
// ------------------
if ( !function_exists( 'process_asset' ) ) {
/**
* Processes a requested asset (from config.json)
*/
function process_asset() {
global $config;
foreach ( $config->assets as $asset => $value ) {
if ( preg_match( '/\/' . $asset . '/', $_SERVER['REQUEST_URI'] ) ) {
$filetype = substr( strrchr( $asset, '.' ), 1 );
switch ( $filetype ) {
case 'css':
$mimetype = 'text/css';
break;
case 'js':
$mimetype = 'text/javascript';
break;
default:
$mimetype = '';
break;
}
dump( $asset, $mimetype );
exit();
}
}
}
}
if ( !function_exists( 'process_view' ) ) {
/**
* Processes a requested view
*/
function process_view() {
global $config;
$url = str_replace( '?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI'] ); // remove query string
$url = preg_replace( '/\.[^.\s]{2,4}$/', '', $url ); // remove file extension
$route = explode( '/', $url );
$action = end( $route );
if ( $action == '' ) {
$action = 'index';
}
$view = BASE . $config->micro->view_directory . '/' . $action . '.' . $config->micro->view_file_extension;
if ( !is_file( $view ) ) {
// first part could be a subfolder
$view = BASE . $config->micro->view_directory . '/' . preg_replace( '/-/', '/', $action, 1 ) . '.' . $config->micro->view_file_extension;
if ( !is_file( $view ) ) {
// check for all subfolder combinations
$_positions = array();
$_start = 0;
while ( ( $_pos = strpos( $action, '-', $_start ) ) !== false ) {
$_start = $_pos + 1;
$_positions[] = $_pos;
}
$_len = count( $_positions );
$_combinations = array();
for ( $_i = 1; $_i < ( 1 << $_len ); $_i ++ ) {
$_c = array();
for ( $_j = 0; $_j < $_len; $_j ++ ) {
if ( $_i & ( 1 << $_j ) ) {
$_c[] = $_positions[ $_j ];
}
}
$_combinations[] = $_c;
}
foreach ( $_combinations as $_combination ) {
$_action = $action;
foreach ( $_combination as $_pos ) {
$_action = substr_replace( $_action, '/', $_pos, 1 );
}
$view = BASE . $config->micro->view_directory . '/' . $_action . '.' . $config->micro->view_file_extension;
if ( is_file( $view ) ) {
break;
}
}
}
}
render_view( $view );
}
}
process_asset();
process_view();