1
+ #!/usr/bin/env php
2
+ <?php
3
+
4
+ // Send all errors to stderr
5
+ ini_set ('display_errors ' , 'stderr ' );
6
+
7
+ // setup composer autoloading
8
+ $ composerAutoload = [
9
+ __DIR__ . '/../vendor/autoload.php ' , // standalone with "composer install" run
10
+ __DIR__ . '/../../../autoload.php ' , // script is installed as a composer binary
11
+ ];
12
+ foreach ($ composerAutoload as $ autoload ) {
13
+ if (file_exists ($ autoload )) {
14
+ require ($ autoload );
15
+ break ;
16
+ }
17
+ }
18
+
19
+ if (!class_exists ('cebe\jssearch\Indexer ' )) {
20
+ error ('Autoloading does not seem to work. Looks like you should run `composer install` first. ' );
21
+ }
22
+
23
+ // check arguments
24
+ $ src = [];
25
+ foreach ($ argv as $ k => $ arg ) {
26
+ if ($ k == 0 ) {
27
+ continue ;
28
+ }
29
+ if ($ arg [0 ] == '- ' ) {
30
+ $ arg = explode ('= ' , $ arg );
31
+ switch ($ arg [0 ]) {
32
+ // TODO allow baseUrl to be set via arg
33
+ case '-h ' :
34
+ case '--help ' :
35
+ echo "jssearch index builder \n" ;
36
+ echo "---------------------- \n\n" ;
37
+ echo "by Carsten Brandt <[email protected] > \n\n" ;
38
+ usage ();
39
+ break ;
40
+ default :
41
+ error ("Unknown argument " . $ arg [0 ], "usage " );
42
+ }
43
+ } else {
44
+ $ src [] = $ arg ;
45
+ }
46
+ }
47
+
48
+ if (empty ($ src )) {
49
+ error ("You have to give an input directory. " , "usage " );
50
+ }
51
+
52
+ $ indexer = new \cebe \jssearch \Indexer ();
53
+
54
+ $ files = [];
55
+ foreach ($ src as $ dir ) {
56
+ $ files = array_merge ($ files , findFiles ($ dir ));
57
+
58
+ if (empty ($ files )) {
59
+ error ("No files where found in $ dir. " );
60
+ }
61
+
62
+ $ indexer ->indexFiles ($ files , $ dir );
63
+ }
64
+
65
+ $ js = $ indexer ->exportJs ();
66
+ file_put_contents ('jssearch.index.js ' , $ js );
67
+
68
+
69
+ // functions
70
+
71
+ /**
72
+ * Display usage information
73
+ */
74
+ function usage () {
75
+ global $ argv ;
76
+ $ cmd = $ argv [0 ];
77
+ echo <<<EOF
78
+ Usage:
79
+ $ cmd [src-directory]
80
+
81
+ --help shows this usage information.
82
+
83
+ creates and jssearch.index.js file in the current directory.
84
+
85
+ EOF ;
86
+ exit (1 );
87
+ }
88
+
89
+ /**
90
+ * Send custom error message to stderr
91
+ * @param $message string
92
+ * @param $callback mixed called before script exit
93
+ * @return void
94
+ */
95
+ function error ($ message , $ callback = null ) {
96
+ $ fe = fopen ("php://stderr " , "w " );
97
+ fwrite ($ fe , "Error: " . $ message . "\n" );
98
+
99
+ if (is_callable ($ callback )) {
100
+ call_user_func ($ callback );
101
+ }
102
+
103
+ exit (1 );
104
+ }
105
+
106
+ function findFiles ($ dir , $ ext = '.html ' )
107
+ {
108
+ if (!is_dir ($ dir )) {
109
+ error ("$ dir is not a directory. " );
110
+ }
111
+ $ dir = rtrim ($ dir , DIRECTORY_SEPARATOR );
112
+ $ list = [];
113
+ $ handle = opendir ($ dir );
114
+ if ($ handle === false ) {
115
+ error ('Unable to open directory: ' . $ dir );
116
+ }
117
+ while (($ file = readdir ($ handle )) !== false ) {
118
+ if ($ file === '. ' || $ file === '.. ' ) {
119
+ continue ;
120
+ }
121
+ $ path = $ dir . DIRECTORY_SEPARATOR . $ file ;
122
+ if (substr ($ file , -($ l = strlen ($ ext )), $ l ) === $ ext ) {
123
+ if (is_file ($ path )) {
124
+ $ list [] = $ path ;
125
+ } else {
126
+ $ list = array_merge ($ list , findFiles ($ path , $ ext ));
127
+ }
128
+ }
129
+ }
130
+ closedir ($ handle );
131
+
132
+ return $ list ;
133
+ }
0 commit comments