@@ -39,31 +39,61 @@ public function getOption($key, $default = null)
3939 }
4040
4141 /**
42- * @param string $input file or content
42+ * Compile file or code (detect if $input is an exisisting file, else use it as content).
4343 *
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
4548 */
46- public function compile ($ input )
49+ public function compile ($ input, $ filename = null , $ catchDependencies = false )
4750 {
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 );
4956 $ compiler = new Compiler ($ this );
57+ $ block = $ parser ->parse ();
58+ if ($ catchDependencies ) {
59+ $ this ->dependencies = array_merge ($ this ->dependencies , $ block ->popDependencies ());
60+ }
5061
51- return $ compiler ->compile ($ parser -> parse () );
62+ return $ compiler ->compile ($ block );
5263 }
5364
5465 /**
55- * @param string $input file or content
66+ * Compile a file.
5667 *
57- * @return mixed
68+ * @param string $file input file
69+ *
70+ * @return string
5871 */
59- public function compileWithoutDependencies ( $ input )
72+ public function compileFile ( $ file , $ catchDependencies = false )
6073 {
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+ }
6576
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 );
6797 }
6898
6999 /**
0 commit comments