@@ -39,31 +39,61 @@ public function getOption($key, $default = null)
39
39
}
40
40
41
41
/**
42
- * @param string $input file or content
42
+ * Compile file or code (detect if $input is an exisisting file, else use it as content).
43
43
*
44
- * @return mixed
44
+ * @param string $input file or content
45
+ * @param string $filename if specified, input is used as content and filename as its name
46
+ *
47
+ * @return string
45
48
*/
46
- public function compile ($ input )
49
+ public function compile ($ input, $ filename = null , $ catchDependencies = false )
47
50
{
48
- $ parser = new Parser ($ this , $ input );
51
+ if ($ filename === null ) {
52
+ $ filename = file_exists ($ input ) ? $ input : null ;
53
+ $ input = $ filename === null ? $ input : file_get_contents ($ filename );
54
+ }
55
+ $ parser = new Parser ($ this , $ input , $ filename );
49
56
$ compiler = new Compiler ($ this );
57
+ $ block = $ parser ->parse ();
58
+ if ($ catchDependencies ) {
59
+ $ this ->dependencies = array_merge ($ this ->dependencies , $ block ->popDependencies ());
60
+ }
50
61
51
- return $ compiler ->compile ($ parser -> parse () );
62
+ return $ compiler ->compile ($ block );
52
63
}
53
64
54
65
/**
55
- * @param string $input file or content
66
+ * Compile a file.
56
67
*
57
- * @return mixed
68
+ * @param string $file input file
69
+ *
70
+ * @return string
58
71
*/
59
- public function compileWithoutDependencies ( $ input )
72
+ public function compileFile ( $ file , $ catchDependencies = false )
60
73
{
61
- $ parser = new Parser ($ this , $ input );
62
- $ compiler = new Compiler ($ this );
63
- $ block = $ parser ->parse ();
64
- $ this ->dependencies = array_merge ($ this ->dependencies , $ block ->popDependencies ());
74
+ return $ this ->compile (file_get_contents ($ file ), $ file , $ catchDependencies );
75
+ }
65
76
66
- return $ compiler ->compile ($ block );
77
+ /**
78
+ * Compile raw code.
79
+ *
80
+ * @param string $code input code
81
+ *
82
+ * @return string
83
+ */
84
+ public function compileCode ($ code , $ catchDependencies = false )
85
+ {
86
+ return $ this ->compile ($ code , 'source.js ' , $ catchDependencies );
87
+ }
88
+
89
+ /**
90
+ * @param string $input file or content
91
+ *
92
+ * @return string
93
+ */
94
+ public function compileWithoutDependencies ($ input , $ filename = null )
95
+ {
96
+ return $ this ->compile ($ input , $ filename , true );
67
97
}
68
98
69
99
/**
0 commit comments