ext/standard/dir.c: Directory class should behave like other resource objects

This commit is contained in:
Gina Peter Banyard 2024-09-24 17:58:57 +01:00
parent 76f6592d12
commit 0640fbab60
7 changed files with 25 additions and 15 deletions

View file

@ -48,10 +48,17 @@ php_dir_globals dir_globals;
#endif
static zend_class_entry *dir_class_entry_ptr;
static zend_object_handlers dir_class_object_handlers;
#define Z_DIRECTORY_PATH_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 0)
#define Z_DIRECTORY_HANDLE_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 1)
static zend_function *dir_class_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct Directory, use dir() instead");
return NULL;
}
#define FETCH_DIRP() \
myself = getThis(); \
if (!myself) { \
@ -115,6 +122,12 @@ PHP_MINIT_FUNCTION(dir)
register_dir_symbols(module_number);
dir_class_entry_ptr = register_class_Directory();
dir_class_entry_ptr->default_object_handlers = &dir_class_object_handlers;
memcpy(&dir_class_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
dir_class_object_handlers.get_constructor = dir_class_get_constructor;
dir_class_object_handlers.clone_obj = NULL;
dir_class_object_handlers.compare = zend_objects_not_comparable;
#ifdef ZTS
ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);

View file

@ -87,7 +87,11 @@ const SCANDIR_SORT_DESCENDING = UNKNOWN;
*/
const SCANDIR_SORT_NONE = UNKNOWN;
class Directory
/**
* @strict-properties
* @not-serializable
*/
final class Directory
{
public readonly string $path;

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 4b0f093770ff9a6cad9db033e0b62b412408b937 */
* Stub hash: 3fdc106d96cf9e728886637eecdb43c2552b174f */
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Directory_close, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
@ -58,7 +58,7 @@ static zend_class_entry *register_class_Directory(void)
zend_class_entry ce, *class_entry;
INIT_CLASS_ENTRY(ce, "Directory", class_Directory_methods);
class_entry = zend_register_internal_class_with_flags(&ce, NULL, 0);
class_entry = zend_register_internal_class_with_flags(&ce, NULL, ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES|ZEND_ACC_NOT_SERIALIZABLE);
zval property_path_default_value;
ZVAL_UNDEF(&property_path_default_value);

View file

@ -23,6 +23,4 @@ try {
?>
--EXPECT--
int(17)
Using original object:
int(0)
Error: Trying to clone an uncloneable object of class Directory

View file

@ -12,9 +12,4 @@ try {
?>
--EXPECT--
object(Directory)#1 (0) {
["path"]=>
uninitialized(string)
["handle"]=>
uninitialized(mixed)
}
Error: Cannot directly construct Directory, use dir() instead

View file

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

View file

@ -14,7 +14,7 @@ echo $rc;
?>
--EXPECTF--
Structure of Directory class:
Class [ <internal%s> class Directory ] {
Class [ <internal%s> final class Directory ] {
- Constants [0] {
}