1+ <?php
2+
3+ namespace PhpTemplates ;
4+
5+ use PhpTemplates \Template ;
6+
7+ class Bootstrap
8+ {
9+ private $ components ;
10+
11+ public function mount (Template $ template , $ options = [])
12+ {
13+ $ prefix = isset ($ options ['prefix ' ]) ? $ options ['prefix ' ] : 'b- ' ;
14+ $ cfg = $ template ->subconfig ('bootstrap ' , __DIR__ . '/bootstrap/ ' );
15+ foreach ($ this ->getCached () as $ alias => $ rfilepath ) {
16+ // echo $prefix.$alias . ' => ' . $rfilepath . PHP_EOL;
17+
18+ $ template ->setAlias ($ prefix .$ alias , $ rfilepath );
19+ }
20+ }
21+
22+ private function getCached ()
23+ {
24+ $ cache = __DIR__ . '/cache.json ' ;
25+ if (!file_exists ($ file ) || !json_decode (file_get_contents ($ file )) || 1 ) {
26+ $ json = [];
27+ // components
28+ $ files = $ this ->recursiveScanDir (__DIR__ . '/bootstrap/components/ ' );
29+ foreach ($ files as $ file ) {
30+ $ rfilepath = explode (DIRECTORY_SEPARATOR . 'bootstrap ' . DIRECTORY_SEPARATOR , str_replace ('.t.php ' , '' , $ file ))[1 ];
31+ $ alias = explode (DIRECTORY_SEPARATOR , $ rfilepath ); $ alias = end ($ alias );
32+ $ json [$ alias ] = 'bootstrap: ' . $ rfilepath ;
33+ }
34+ // forms
35+ $ files = $ this ->recursiveScanDir (__DIR__ . '/bootstrap/forms/ ' );
36+ foreach ($ files as $ file ) {
37+ $ rfilepath = explode (DIRECTORY_SEPARATOR . 'bootstrap ' . DIRECTORY_SEPARATOR , str_replace ('.t.php ' , '' , $ file ))[1 ];
38+ $ alias = explode (DIRECTORY_SEPARATOR , $ rfilepath ); $ alias = end ($ alias );
39+ $ json [$ alias ] = 'bootstrap: ' . $ rfilepath ;
40+ }
41+
42+ file_put_contents ($ cache , json_encode ($ json ));
43+ }
44+
45+ return json_decode (file_get_contents ($ cache ), true );
46+ }
47+
48+ private function recursiveScanDir (string $ path , array &$ payload = [])
49+ {
50+ $ files = array_diff (scandir ($ path ), array ('. ' , '.. ' ));
51+ foreach ($ files as $ file ){
52+ $ file = rtrim ($ path , DIRECTORY_SEPARATOR ) . '/ ' . $ file ;
53+ if (is_file ($ file )) {
54+ if (strpos ($ file , '.t.php ' )) {
55+ $ payload [] = $ file ;
56+ }
57+ } else {
58+ $ this ->recursiveScanDir ($ file , $ payload );
59+ }
60+ }
61+
62+ return $ payload ;
63+ }
64+ }
0 commit comments