Skip to content

Commit e012bdf

Browse files
authored
Fix GH-225 (#226)
* Closes GH-225 - cannot reload saved configuration * Add test * Prevent serializing and deserializing non-serializable properties
1 parent c651993 commit e012bdf

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

phpdotnet/phd/Config.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ class Config
5757
public string $phpwebSourcesFilename = '';
5858
public string $phpwebHistoryFilename = '';
5959

60+
private const NON_SERIALIZABLE_PROPERTIES = [
61+
"copyright",
62+
"indexCache",
63+
"phpErrorOutput",
64+
"userErrorOutput",
65+
"phdInfoOutput",
66+
"phdWarningOutput",
67+
];
68+
6069
public function __construct() {
6170
$this->copyright = 'Copyright(c) 2007-' . \date('Y') . ' The PHP Documentation Group';
6271

@@ -76,19 +85,29 @@ public function init(array $configOptions): void {
7685
throw new \Exception("Invalid option supplied: $option");
7786
}
7887

88+
if (\in_array($option, self::NON_SERIALIZABLE_PROPERTIES, true)) {
89+
continue;
90+
}
91+
7992
$this->$option = $value;
8093
}
8194

8295
\error_reporting($GLOBALS['olderrrep'] | $this->verbose);
8396
}
8497

8598
/**
86-
* Returns all configuration options and their values
99+
* Returns all serializable configuration options and their values
87100
*
88101
* @return array<string, mixed>
89102
*/
90-
public function getAllFiltered(): array {
91-
return \get_object_vars($this);
103+
public function getAllSerializableProperties(): array {
104+
$object_vars = \get_object_vars($this);
105+
106+
foreach (self::NON_SERIALIZABLE_PROPERTIES as $property) {
107+
unset($object_vars[$property]);
108+
}
109+
110+
return $object_vars;
92111
}
93112

94113
/**

render.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
if ($config->saveConfig) {
7676
$outputHandler->v("Writing the config file", VERBOSE_MESSAGES);
77-
file_put_contents("phd.config.php", "<?php\nreturn " . var_export($config->getAllFiltered(), 1) . ";");
77+
file_put_contents("phd.config.php", "<?php\nreturn " . var_export($config->getAllSerializableProperties(), 1) . ";");
7878
}
7979

8080
if ($config->quit) {

tests/GH-225.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-225 - SaveConfig tries to overwrite readonly property
3+
--ARGS--
4+
--docbook tests/data/bug-GH-225.xml --quit
5+
--FILE--
6+
<?php
7+
namespace phpdotnet\phd;
8+
9+
if (!\file_exists(__DIR__ . "/../output/")) {
10+
\mkdir(__DIR__ . "/../output/", 0777, true);
11+
}
12+
13+
if (\file_exists(__DIR__ . "/../phd.config.php")) {
14+
\unlink(__DIR__ . "/../phd.config.php");
15+
}
16+
17+
\file_put_contents(__DIR__ . "/../phd.config.php",
18+
"<?php
19+
return array (
20+
'copyright' => 'Should not be imported',
21+
);");
22+
23+
require_once __DIR__ . "/../render.php";
24+
?>
25+
--CLEAN--
26+
<?php
27+
\unlink(__DIR__ . "/../phd.config.php");
28+
\rmdir(__DIR__ . "/../output/");
29+
?>
30+
--EXPECTF--
31+
%s[%d:%d:%d - Heads up ]%s Loaded config from existing file

tests/data/bug-GH-225.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<test>
2+
</test>

0 commit comments

Comments
 (0)