Skip to content

Commit 059cba1

Browse files
committed
Added recursive option that enables processing only level of components. Default is true.
1 parent c7cb62c commit 059cba1

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/HTMLServerComponentsCompiler.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ function addAlias($alias, $original)
3939
/**
4040
* Process (merge) components
4141
* @param string $html
42+
* @param array $options
4243
* @return string
4344
*/
44-
public function process($html)
45+
public function process($html, $options = [])
4546
{
47+
if (isset($options['_internal_process_components']) && $options['_internal_process_components'] === false) {
48+
return $html;
49+
}
4650
$domDocument = new \IvoPetkov\HTML5DOMDocument();
4751
$domDocument->loadHTML($html);
4852
$componentElements = $domDocument->getElementsByTagName('component');
@@ -60,10 +64,14 @@ public function process($html)
6064
}
6165
if (sizeof($sourceParts) === 2) {
6266
$scheme = $sourceParts[0];
67+
if (isset($options['recursive']) && $options['recursive'] === false && ($scheme === 'data' || $scheme === 'file')) {
68+
$componentOptions = array_values($options);
69+
$componentOptions['_internal_process_components'] = false;
70+
}
6371
if ($scheme === 'data') {
64-
$componentHTML = $this->processData($sourceParts[1]);
72+
$componentHTML = $this->processData($sourceParts[1], isset($componentOptions) ? $componentOptions : $options);
6573
} elseif ($scheme === 'file') {
66-
$componentHTML = $this->processFile(urldecode($sourceParts[1]), $attributes, $component->innerHTML);
74+
$componentHTML = $this->processFile(urldecode($sourceParts[1]), $attributes, $component->innerHTML, isset($componentOptions) ? $componentOptions : $options);
6775
} else {
6876
throw new \Exception('URI scheme not valid!' . $domDocument->saveHTML($component));
6977
}
@@ -101,28 +109,30 @@ public function process($html)
101109
/**
102110
*
103111
* @param string $data
112+
* @param array $options
104113
* @return string
105114
*/
106-
public function processData($data)
115+
public function processData($data, $options = [])
107116
{
108117
$html = $data;
109118
if (substr($data, 0, 7) === 'base64,') {
110119
$html = base64_decode(substr($data, 7));
111120
}
112-
return $this->process($html);
121+
return $this->process($html, $options);
113122
}
114123

115124
/**
116125
*
117126
* @param string $file
118127
* @param array $attributes
119128
* @param string $innerHTML
129+
* @param array $options
120130
* @return string
121131
*/
122-
public function processFile($file, $attributes = [], $innerHTML = '')
132+
public function processFile($file, $attributes = [], $innerHTML = '', $options = [])
123133
{
124134
$component = $this->constructComponent($attributes, $innerHTML);
125-
return $this->process($this->getComponentFileContent($file, $component));
135+
return $this->process($this->getComponentFileContent($file, $component), $options);
126136
}
127137

128138
/**

tests/Test.php

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

42+
/**
43+
*
44+
*/
45+
public function testProccessRecursion()
46+
{
47+
$fullFilename1 = $this->createFile('component1.php', '<html><head><meta custom="value1"></head><body>text1</body></html>');
48+
$fullFilename2 = $this->createFile('component1.php', '<html><head><meta custom="value2"></head><body><component src="file:' . urlencode($fullFilename1) . '"></component>text2</body></html>');
49+
$fullFilename3 = $this->createFile('component1.php', '<html><head><meta custom="value3"></head><body><component src="file:' . urlencode($fullFilename2) . '"></component>text3</body></html>');
50+
51+
$compiler = new \IvoPetkov\HTMLServerComponentsCompiler();
52+
$result = $compiler->process('<component src="file:' . $fullFilename3 . '"/>');
53+
$expectedResult = '<!DOCTYPE html><html><head><meta custom="value3"><meta custom="value2"><meta custom="value1"></head><body>text1text2text3</body></html>';
54+
$this->assertTrue($result === $expectedResult);
55+
56+
$result = $compiler->process('<component src="file:' . $fullFilename3 . '"/>', ['recursive' => false]);
57+
$expectedResult = '<!DOCTYPE html><html><head><meta custom="value3"></head><body><component src="file:' . urlencode($fullFilename2) . '"></component>text3</body></html>';
58+
$this->assertTrue($result === $expectedResult);
59+
}
60+
4261
}

0 commit comments

Comments
 (0)