Skip to content

Commit 0640fba

Browse files
committed
ext/standard/dir.c: Directory class should behave like other resource objects
1 parent 76f6592 commit 0640fba

7 files changed

+25
-15
lines changed

ext/standard/dir.c

+13
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@ php_dir_globals dir_globals;
4848
#endif
4949

5050
static zend_class_entry *dir_class_entry_ptr;
51+
static zend_object_handlers dir_class_object_handlers;
5152

5253
#define Z_DIRECTORY_PATH_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 0)
5354
#define Z_DIRECTORY_HANDLE_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 1)
5455

56+
static zend_function *dir_class_get_constructor(zend_object *object)
57+
{
58+
zend_throw_error(NULL, "Cannot directly construct Directory, use dir() instead");
59+
return NULL;
60+
}
61+
5562
#define FETCH_DIRP() \
5663
myself = getThis(); \
5764
if (!myself) { \
@@ -115,6 +122,12 @@ PHP_MINIT_FUNCTION(dir)
115122
register_dir_symbols(module_number);
116123

117124
dir_class_entry_ptr = register_class_Directory();
125+
dir_class_entry_ptr->default_object_handlers = &dir_class_object_handlers;
126+
127+
memcpy(&dir_class_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
128+
dir_class_object_handlers.get_constructor = dir_class_get_constructor;
129+
dir_class_object_handlers.clone_obj = NULL;
130+
dir_class_object_handlers.compare = zend_objects_not_comparable;
118131

119132
#ifdef ZTS
120133
ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);

ext/standard/dir.stub.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@
8787
*/
8888
const SCANDIR_SORT_NONE = UNKNOWN;
8989

90-
class Directory
90+
/**
91+
* @strict-properties
92+
* @not-serializable
93+
*/
94+
final class Directory
9195
{
9296
public readonly string $path;
9397

ext/standard/dir_arginfo.h

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/tests/directory/DirectoryClass_cannot_clone.phpt

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ try {
2323

2424
?>
2525
--EXPECT--
26-
int(17)
27-
Using original object:
28-
int(0)
26+
Error: Trying to clone an uncloneable object of class Directory

ext/standard/tests/directory/DirectoryClass_cannot_construct.phpt

+1-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,4 @@ try {
1212

1313
?>
1414
--EXPECT--
15-
object(Directory)#1 (0) {
16-
["path"]=>
17-
uninitialized(string)
18-
["handle"]=>
19-
uninitialized(mixed)
20-
}
15+
Error: Cannot directly construct Directory, use dir() instead

ext/standard/tests/directory/DirectoryClass_cannot_serialize.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ try {
1212
}
1313

1414
?>
15-
--EXPECTF--
16-
string(%d) "O:9:"Directory":2:{s:4:"path";s:%d:"%s";s:6:"handle";i:%d;}"
15+
--EXPECT--
16+
Exception: Serialization of 'Directory' is not allowed

ext/standard/tests/directory/DirectoryClass_reflection_structure.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ echo $rc;
1414
?>
1515
--EXPECTF--
1616
Structure of Directory class:
17-
Class [ <internal%s> class Directory ] {
17+
Class [ <internal%s> final class Directory ] {
1818

1919
- Constants [0] {
2020
}

0 commit comments

Comments
 (0)