Skip to content

Commit 9246919

Browse files
committed
Allow to explicitly compile file or content with or without dependencies
1 parent 3d4b46f commit 9246919

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

src/JsPhpize/JsPhpize.php

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/JsPhpize/Parser/Parser.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ class Parser extends Visitor
2828
*/
2929
protected $dependencies;
3030

31-
public function __construct(JsPhpize $engine, $input)
31+
public function __construct(JsPhpize $engine, $input, $filename)
3232
{
33-
$filename = file_exists($input) ? $input : null;
34-
$input = $filename === null ? $input : file_get_contents($filename);
3533
$input = str_replace(array("\r\n", "\r"), array("\n", ''), $input);
36-
3734
$this->tokens = array();
3835
$this->dependencies = array();
3936
$this->engine = $engine;

0 commit comments

Comments
 (0)