1+ @license {
2+ Copyright (c) 2013, NWO-I Centrum Wiskunde & Informatica (CWI), Mark Hills, Appalachian State University
3+ All rights reserved.
4+
5+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6+
7+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
9+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+
11+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12+ }
13+ module lang ::php ::config ::Config
14+
15+ import lang ::php ::util ::Option ;
16+
17+ import IO ;
18+ import Exception ;
19+ import lang ::yaml ::Model ;
20+
21+ public data Exception
22+ = configMissing (str key , str msg )
23+ ;
24+
25+ public data Config
26+ = config (
27+ Option [bool ] usePhpParserJar ,
28+ Option [loc ] phpLoc ,
29+ Option [loc ] parserLoc ,
30+ Option [loc ] analysisLoc ,
31+ Option [str ] parserMemLimit ,
32+ Option [str ] astToRascal ,
33+ Option [loc ] parserWorkingDir ,
34+ Option [loc ] baseLoc ,
35+ Option [loc ] parsedDir ,
36+ Option [loc ] statsDir ,
37+ Option [loc ] corpusRoot ,
38+ Option [loc ] countsDir ,
39+ Option [bool ] useBinaries ,
40+ Option [int ] logLevel ,
41+ Option [loc ] clocLoc )
42+ | emptyConfig ()
43+ ;
44+
45+ private Config c = emptyConfig ();
46+
47+ public Config getConfig () {
48+ if (c is emptyConfig ) {
49+ c = loadConfig ();
50+ }
51+ return c ;
52+ }
53+
54+ private Config loadConfig () {
55+ bool usePhpParserJar = false ;
56+ loc phpLoc = |file:///opt/homebrew/bin/php| ;
57+ loc parserLoc = |file:///Users/hillsma/Projects/php-analysis/PHP-Parser| ;
58+ loc analysisLoc = |file:///Users/hillsma/Projects/php-analysis/php-analysis/| ;
59+ str parserMemLimit = "1024M" ;
60+ str astToRascal = "AST2Rascal.php" ;
61+ loc parserWorkingDir = (parserLoc + astToRascal ).parent ;
62+ loc baseLoc = |home:///PHPAnalysis| ;
63+ loc parsedDir = baseLoc + "serialized/parsed" ;
64+ loc statsDir = baseLoc + "serialized/stats" ;
65+ loc corpusRoot = baseLoc + "systems" ;
66+ loc countsDir = baseLoc + "serialized/counts" ;
67+ bool useBinaries = false ;
68+ int logLevel = 2 ;
69+ loc clocLoc = |file:///opt/homebrew/bin/cloc| ;
70+
71+ Config c = config (
72+ some (usePhpParserJar ),
73+ some (phpLoc ),
74+ some (parserLoc ),
75+ some (analysisLoc ),
76+ some (parserMemLimit ),
77+ some (astToRascal ),
78+ some (parserWorkingDir ),
79+ some (baseLoc ),
80+ some (parsedDir ),
81+ some (statsDir ),
82+ some (corpusRoot ),
83+ some (countsDir ),
84+ some (useBinaries ),
85+ some (logLevel ),
86+ some (clocLoc )
87+ );
88+
89+ return c ;
90+ }
91+
92+ @doc {Indicates whether to use the parser contained in a distributed jar file or from the directory given below}
93+ public bool usePhpParserJar () {
94+ c = getConfig ();
95+ return (c has usePhpParserJar && some (bool b ) := c .usePhpParserJar ) ? b : false ;
96+ }
97+
98+ @doc {The location of the PHP executable}
99+ public loc phpLoc () {
100+ c = getConfig ();
101+ if (c has phpLoc && some (loc l ) := c .phpLoc ) {
102+ return l ;
103+ }
104+ throw configMissing ("phpLoc" , "Make sure to set phpLoc to a valid location in your configuration file" );
105+ }
106+
107+ @doc {The base install location for the PHP-Parser project}
108+ public loc parserLoc () {
109+ c = getConfig ();
110+ if (c has parserLoc && some (loc l ) := c .parserLoc ) {
111+ return l ;
112+ }
113+ throw configMissing ("parserLoc" , "Make sure to set parserLoc to a valid location in your configuration file" );
114+ }
115+
116+ @doc {The base install location for the php-analysis project}
117+ public loc analysisLoc () {
118+ c = getConfig ();
119+ if (c has analysisLoc && some (loc l ) := c .analysisLoc ) {
120+ return l ;
121+ }
122+ throw configMissing ("analysisLoc" , "Make sure to set analysisLoc to a valid location in your configuration file" );
123+ }
124+
125+ @doc {The memory limit for PHP when the parser is run}
126+ public str parserMemLimit () {
127+ c = getConfig ();
128+ return (c has parserMemLimit && some (str s ) := c .parserMemLimit ) ? s : "1024M" ;
129+ }
130+
131+ @doc {The location of the AST2Rascal.php file, inside the PHP-Parser directories}
132+ public str astToRascal () {
133+ c = getConfig ();
134+ return (c has astToRascal && some (str s ) := c .astToRascal ) ? s : "AST2Rascal.php" ;
135+ }
136+
137+ @doc {The working directory for when the parser runs}
138+ public loc parserWorkingDir () {
139+ c = getConfig ();
140+ if (c has parserWorkingDir && some (loc l ) := c .parserWorkingDir ) {
141+ return l ;
142+ }
143+ throw configMissing ("parserWorkingDir" , "Make sure to set parserWorkingDir to a valid location in your configuration file" );
144+ }
145+
146+ @doc {The base location for the corpus and any serialized files}
147+ public loc baseLoc () {
148+ c = getConfig ();
149+ if (c has baseLoc && some (loc l ) := c .baseLoc ) {
150+ return l ;
151+ }
152+ throw configMissing ("baseLoc" , "Make sure to set baseLoc to a valid location in your configuration file" );
153+ }
154+
155+ @doc {Where to put the binary representations of parsed systems}
156+ public loc parsedDir () {
157+ c = getConfig ();
158+ if (c has parsedDir && some (loc l ) := c .parsedDir ) {
159+ return l ;
160+ }
161+ throw configMissing ("parsedDir" , "Make sure to set parsedDir to a valid location in your configuration file" );
162+ }
163+
164+ @doc {Where to put the binary representations of extracted statistics}
165+ public loc statsDir () {
166+ c = getConfig ();
167+ if (c has statsDir && some (loc l ) := c .statsDir ) {
168+ return l ;
169+ }
170+ throw configMissing ("statsDir" , "Make sure to set statsDir to a valid location in your configuration file" );
171+ }
172+
173+ @doc {Where the PHP sources for the corpus reside}
174+ public loc corpusRoot () {
175+ c = getConfig ();
176+ if (c has corpusRoot && some (loc l ) := c .corpusRoot ) {
177+ return l ;
178+ }
179+ throw configMissing ("corpusRoot" , "Make sure to set corpusRoot to a valid location in your configuration file" );
180+ }
181+
182+ @doc {Where to put extracted counts (e.g., SLOC)}
183+ public loc countsDir () {
184+ c = getConfig ();
185+ if (c has countsDir && some (loc l ) := c .countsDir ) {
186+ return l ;
187+ }
188+ throw configMissing ("countsDir" , "Make sure to set countsDir to a valid location in your configuration file" );
189+ }
190+
191+ @doc {This should only ever be true if we don't have source, we only have the extracted binaries for parsed systems}
192+ public bool useBinaries () {
193+ c = getConfig ();
194+ return (c has useBinaries && some (bool b ) := c .useBinaries ) ? b : false ;
195+ }
196+
197+ @doc {Debugging options
198+ @logLevel {
199+ Log level 0 => no logging;
200+ Log level 1 => main logging;
201+ Log level 2 => debug logging;
202+ }
203+ }
204+ public int logLevel () {
205+ c = getConfig ();
206+ return (c has logLevel && some (int n ) := c .logLevel ) ? n : 2 ;
207+ }
208+
209+ @doc {The location of the cloc tool}
210+ public loc clocLoc () {
211+ c = getConfig ();
212+ if (c has clocLoc && some (loc l ) := c .clocLoc ) {
213+ return l ;
214+ }
215+ throw configMissing ("clocLoc" , "Make sure to set clocLoc to a valid location in your configuration file" );
216+ }
0 commit comments