Skip to content

Commit 687714e

Browse files
committed
Introduced PropertyPtrPtr interface to return pointer to Zval of property object.
1 parent 26cbf1e commit 687714e

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ SET(PHPCPP_HEADERS_INCLUDE
190190
include/argument.h
191191
include/array.h
192192
include/arrayaccess.h
193+
include/propertyptrptr.h
193194
include/base.h
194195
include/byref.h
195196
include/byval.h

include/propertyptrptr.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* PropertyPtrPtr.h
3+
*
4+
* "Interface" that can be "implemented" by your class. If you do, you
5+
* create your class like this:
6+
*
7+
* class MyClass : public Php::Base, public Php::PropertyPtrPtr { ... }
8+
*
9+
* @author Kirill Morozov <[email protected]>
10+
* @copyright 2020 Morozov
11+
*/
12+
13+
/**
14+
* Set up namespace
15+
*/
16+
namespace Php {
17+
18+
/**
19+
* Class definition
20+
*/
21+
class PHPCPP_EXPORT PropertyPtrPtr
22+
{
23+
public:
24+
25+
/**
26+
* Retrieve a member
27+
* @param key
28+
* @return value
29+
*/
30+
virtual Php::Value getPropertyPtrPtr(const Php::Value &member, int type) = 0;
31+
32+
/**
33+
* Destructor
34+
*/
35+
virtual ~PropertyPtrPtr() = default;
36+
};
37+
38+
/**
39+
* End namespace
40+
*/
41+
}

phpcpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <phpcpp/base.h>
5959
#include <phpcpp/countable.h>
6060
#include <phpcpp/arrayaccess.h>
61+
#include <phpcpp/propertyptrptr.h>
6162
#include <phpcpp/iterator.h>
6263
#include <phpcpp/traversable.h>
6364
#include <phpcpp/serializable.h>

zend/classimpl.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ zend_object_handlers *ClassImpl::objectHandlers()
360360
_handlers.has_property = &ClassImpl::hasProperty;
361361
_handlers.unset_property = &ClassImpl::unsetProperty;
362362

363+
_handlers.get_property_ptr_ptr = &ClassImpl::getPropertyPtrPtr;
364+
363365
// when a method is called (__call and __invoke)
364366
_handlers.get_method = &ClassImpl::getMethod;
365367
_handlers.get_closure = &ClassImpl::getClosure;
@@ -651,6 +653,46 @@ zval *ClassImpl::readDimension(zval *object, zval *offset, int type, zval *rv)
651653
return std_object_handlers.read_dimension(object, offset, type, rv);
652654
}
653655
}
656+
/**
657+
* Function that is called when the object property is used for write operations in PHP
658+
*
659+
* This is the object->property[x]=y operation in PHP, and mapped to the getPropertyPtrPtr() method
660+
* of the ArrayAccess PropertyPtrPtr
661+
*
662+
* @param object The object on which it is called
663+
* @param member The name of the property
664+
* @param type The type of operation 0 - read, 1 - write
665+
* @return zval*
666+
*/
667+
zval *ClassImpl::getPropertyPtrPtr(zval *object, zval *member, int type, void **cache_slot)
668+
{
669+
PropertyPtrPtr *p_ptr_ptr = dynamic_cast<PropertyPtrPtr*>(ObjectImpl::find(object)->object());
670+
if (p_ptr_ptr)
671+
{
672+
try
673+
{
674+
Php::Value res = p_ptr_ptr->getPropertyPtrPtr(member, type);
675+
return res.detach(true);
676+
}
677+
catch (Throwable &throwable)
678+
{
679+
// object was not caught by the extension, let it end up in user space
680+
throwable.rethrow();
681+
682+
// unreachable
683+
return Value(nullptr).detach(false);
684+
}
685+
686+
}
687+
else
688+
{
689+
// ArrayAccess not implemented, check if there is a default handler
690+
if (!std_object_handlers.get_property_ptr_ptr) return nullptr;
691+
692+
// call default
693+
return std_object_handlers.get_property_ptr_ptr(object, member, type, cache_slot);
694+
}
695+
}
654696

655697
/**
656698
* Function that is called when the object is used as an array in PHP

zend/classimpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ class ClassImpl
235235
*/
236236
static zend_object_handlers *objectHandlers(zend_class_entry *entry);
237237

238+
static zval *getPropertyPtrPtr(zval *object, zval *member, int type, void **cache_slot);
239+
238240
/**
239241
* Function to create a new iterator to iterate over an object
240242
* @param entry The class entry

zend/includes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#include "../include/base.h"
9191
#include "../include/countable.h"
9292
#include "../include/arrayaccess.h"
93+
#include "../include/propertyptrptr.h"
9394
#include "../include/serializable.h"
9495
#include "../include/iterator.h"
9596
#include "../include/traversable.h"

0 commit comments

Comments
 (0)