Skip to content

Commit 1423933

Browse files
committed
Allow to explicitly render file or content
1 parent 9246919 commit 1423933

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

src/JsPhpize/JsPhpize.php

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public function getOption($key, $default = null)
4141
/**
4242
* Compile file or code (detect if $input is an exisisting file, else use it as content).
4343
*
44-
* @param string $input file or content
45-
* @param string $filename if specified, input is used as content and filename as its name
44+
* @param string $input file or content
45+
* @param string $filename if specified, input is used as content and filename as its name
46+
* @param bool $catchDependencies if true, dependencies are not compiled and can be grouped and get separatly
4647
*
4748
* @return string
4849
*/
@@ -65,7 +66,8 @@ public function compile($input, $filename = null, $catchDependencies = false)
6566
/**
6667
* Compile a file.
6768
*
68-
* @param string $file input file
69+
* @param string $file input file
70+
* @param bool $catchDependencies if true, dependencies are not compiled and can be grouped and get separatly
6971
*
7072
* @return string
7173
*/
@@ -77,7 +79,8 @@ public function compileFile($file, $catchDependencies = false)
7779
/**
7880
* Compile raw code.
7981
*
80-
* @param string $code input code
82+
* @param string $code input code
83+
* @param bool $catchDependencies if true, dependencies are not compiled and can be grouped and get separatly
8184
*
8285
* @return string
8386
*/
@@ -87,7 +90,10 @@ public function compileCode($code, $catchDependencies = false)
8790
}
8891

8992
/**
90-
* @param string $input file or content
93+
* Compile without the dependencies.
94+
*
95+
* @param string $input file or content
96+
* @param string $filename if specified, input is used as content and filename as its name
9197
*
9298
* @return string
9399
*/
@@ -97,7 +103,9 @@ public function compileWithoutDependencies($input, $filename = null)
97103
}
98104

99105
/**
100-
* @return mixed
106+
* Return compiled dependencies caught during previous compilations.
107+
*
108+
* @return string
101109
*/
102110
public function compileDependencies()
103111
{
@@ -110,11 +118,14 @@ public function compileDependencies()
110118
}
111119

112120
/**
113-
* @param string $input file or content
121+
* Compile and return the code execution result.
122+
*
123+
* @param string $input file or content
124+
* @param string $filename if specified, input is used as content and filename as its name
114125
*
115126
* @return mixed
116127
*/
117-
public function render($input)
128+
public function render($input, $filename = null)
118129
{
119130
if (!in_array($this->stream, $this->streamsRegistered)) {
120131
$this->streamsRegistered[] = $this->stream;
@@ -126,7 +137,7 @@ public function render($input)
126137
}
127138

128139
try {
129-
return include $this->stream . '://data;<?php ' . $this->compile($input);
140+
return include $this->stream . '://data;<?php ' . $this->compile($input, $filename);
130141
} catch (\JsPhpize\Compiler\Exception $e) {
131142
throw $e;
132143
} catch (\JsPhpize\Lexer\Exception $e) {
@@ -141,4 +152,28 @@ public function render($input)
141152
throw new Exception("An error occur in [$summary]:\n" . $e->getMessage(), 2, E_ERROR, __FILE__, __LINE__, $e);
142153
}
143154
}
155+
156+
/**
157+
* Render a file.
158+
*
159+
* @param string $file input file
160+
*
161+
* @return string
162+
*/
163+
public function renderFile($file)
164+
{
165+
return $this->render(file_get_contents($file), $file);
166+
}
167+
168+
/**
169+
* Render raw code.
170+
*
171+
* @param string $code input code
172+
*
173+
* @return string
174+
*/
175+
public function renderCode($code)
176+
{
177+
return $this->compile($code, 'source.js');
178+
}
144179
}

0 commit comments

Comments
 (0)