Skip to content

Commit 7a161b0

Browse files
committed
Changes in file process methods. Passing variables is now allowed.
1 parent 059cba1 commit 7a161b0

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/HTMLServerComponentsCompiler.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function process($html, $options = [])
7171
if ($scheme === 'data') {
7272
$componentHTML = $this->processData($sourceParts[1], isset($componentOptions) ? $componentOptions : $options);
7373
} elseif ($scheme === 'file') {
74-
$componentHTML = $this->processFile(urldecode($sourceParts[1]), $attributes, $component->innerHTML, isset($componentOptions) ? $componentOptions : $options);
74+
$componentHTML = $this->processFile(urldecode($sourceParts[1]), $attributes, $component->innerHTML, [], isset($componentOptions) ? $componentOptions : $options);
7575
} else {
7676
throw new \Exception('URI scheme not valid!' . $domDocument->saveHTML($component));
7777
}
@@ -126,13 +126,14 @@ public function processData($data, $options = [])
126126
* @param string $file
127127
* @param array $attributes
128128
* @param string $innerHTML
129+
* @param array $variables
129130
* @param array $options
130131
* @return string
131132
*/
132-
public function processFile($file, $attributes = [], $innerHTML = '', $options = [])
133+
public function processFile($file, $attributes = [], $innerHTML = '', $variables = [], $options = [])
133134
{
134135
$component = $this->constructComponent($attributes, $innerHTML);
135-
return $this->process($this->getComponentFileContent($file, $component), $options);
136+
return $this->process($this->getComponentFileContent($file, array_merge($variables, ['component' => $component])), $options);
136137
}
137138

138139
/**
@@ -152,19 +153,24 @@ protected function constructComponent($attributes = [], $innerHTML = '')
152153
/**
153154
*
154155
* @param string $file
155-
* @param \IvoPetkov\HTMLServerComponent $component
156+
* @param array $variables
156157
* @throws \Exception
157158
* @return string
158159
*/
159-
protected function getComponentFileContent($file, $component)
160+
protected function getComponentFileContent($file, $variables)
160161
{
161162
if (is_file($file)) {
163+
$__componentFile = $file;
164+
unset($file);
165+
if (!empty($variables)) {
166+
extract($variables, EXTR_SKIP);
167+
}
162168
ob_start();
163-
include $file;
169+
include $__componentFile;
164170
$content = ob_get_clean();
165171
return $content;
166172
} else {
167-
throw new \Exception('Component file cannot be found');
173+
throw new \Exception('Component file cannot be found (' . $file . ')');
168174
}
169175
}
170176

tests/Test.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ public function testProccessHTML()
3939
$this->assertTrue($result === $expectedResult);
4040
}
4141

42+
/**
43+
*
44+
*/
45+
public function testVariables()
46+
{
47+
$fullFilename = $this->createFile('component1.php', '<html><body><?= $component->test1?><?= $test2?></body></html>');
48+
49+
$compiler = new \IvoPetkov\HTMLServerComponentsCompiler();
50+
$result = $compiler->processFile($fullFilename, ['test1' => '1'], '', ['test2' => 2]);
51+
$expectedResult = '<!DOCTYPE html><html><body>12</body></html>';
52+
$this->assertTrue($result === $expectedResult);
53+
}
54+
4255
/**
4356
*
4457
*/

0 commit comments

Comments
 (0)