1
// **********************************************************************
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
5
// This copy of Ice is licensed to you under the terms described in the
6
// ICE_LICENSE file included in this distribution.
8
// **********************************************************************
10
#include <Properties.h>
14
using namespace IcePHP;
16
ZEND_EXTERN_MODULE_GLOBALS(ice)
19
// Class entries represent the PHP class implementations we have registered.
23
zend_class_entry* propertiesClassEntry = 0;
27
// Properties support.
29
static zend_object_handlers _handlers;
33
static zend_object_value handleAlloc(zend_class_entry* TSRMLS_DC);
34
static void handleFreeStorage(void* TSRMLS_DC);
35
static zend_object_value handleClone(zval* TSRMLS_DC);
38
ZEND_METHOD(Ice_Properties, __construct)
40
runtimeError("properties objects cannot be instantiated, use createProperties()" TSRMLS_CC);
43
ZEND_METHOD(Ice_Properties, __toString)
45
if(ZEND_NUM_ARGS() > 0)
50
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
55
Ice::PropertyDict val = _this->getPropertiesForPrefix("");
57
for(Ice::PropertyDict::const_iterator p = val.begin(); p != val.end(); ++p)
63
str.append(p->first + "=" + p->second);
65
RETURN_STRINGL(STRCAST(str.c_str()), str.length(), 1);
67
catch(const IceUtil::Exception& ex)
69
throwException(ex TSRMLS_CC);
74
ZEND_METHOD(Ice_Properties, getProperty)
79
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s"), &name, &nameLen) == FAILURE)
84
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
87
string propName(name, nameLen);
90
string val = _this->getProperty(propName);
91
RETURN_STRINGL(STRCAST(val.c_str()), val.length(), 1);
93
catch(const IceUtil::Exception& ex)
95
throwException(ex TSRMLS_CC);
100
ZEND_METHOD(Ice_Properties, getPropertyWithDefault)
107
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("ss!"), &name, &nameLen, &def, &defLen) ==
113
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
116
string propName(name, nameLen);
120
defaultValue = string(def, defLen);
125
string val = _this->getPropertyWithDefault(propName, defaultValue);
126
RETURN_STRINGL(STRCAST(val.c_str()), val.length(), 1);
128
catch(const IceUtil::Exception& ex)
130
throwException(ex TSRMLS_CC);
135
ZEND_METHOD(Ice_Properties, getPropertyAsInt)
140
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s"), &name, &nameLen) == FAILURE)
145
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
148
string propName(name, nameLen);
151
Ice::Int val = _this->getPropertyAsInt(propName);
152
RETURN_LONG(static_cast<long>(val));
154
catch(const IceUtil::Exception& ex)
156
throwException(ex TSRMLS_CC);
161
ZEND_METHOD(Ice_Properties, getPropertyAsIntWithDefault)
167
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("sl"), &name, &nameLen, &def) == FAILURE)
172
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
175
string propName(name, nameLen);
178
Ice::Int val = _this->getPropertyAsIntWithDefault(propName, def);
179
RETURN_LONG(static_cast<long>(val));
181
catch(const IceUtil::Exception& ex)
183
throwException(ex TSRMLS_CC);
188
ZEND_METHOD(Ice_Properties, getPropertyAsList)
193
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s"), &name, &nameLen) == FAILURE)
198
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
201
string propName(name, nameLen);
204
Ice::StringSeq val = _this->getPropertyAsList(propName);
205
if(!createStringArray(return_value, val TSRMLS_CC))
210
catch(const IceUtil::Exception& ex)
212
throwException(ex TSRMLS_CC);
217
ZEND_METHOD(Ice_Properties, getPropertyAsListWithDefault)
223
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("sa!"), &name, &nameLen, &def) == FAILURE)
228
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
231
string propName(name, nameLen);
232
Ice::StringSeq defaultValue;
233
if(def && !extractStringArray(def, defaultValue TSRMLS_CC))
240
Ice::StringSeq val = _this->getPropertyAsListWithDefault(propName, defaultValue);
241
if(!createStringArray(return_value, val TSRMLS_CC))
246
catch(const IceUtil::Exception& ex)
248
throwException(ex TSRMLS_CC);
253
ZEND_METHOD(Ice_Properties, getPropertiesForPrefix)
258
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s!"), &p, &pLen) == FAILURE)
263
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
269
prefix = string(p, pLen);
274
Ice::PropertyDict val = _this->getPropertiesForPrefix(prefix);
275
if(!createStringMap(return_value, val TSRMLS_CC))
280
catch(const IceUtil::Exception& ex)
282
throwException(ex TSRMLS_CC);
287
ZEND_METHOD(Ice_Properties, setProperty)
294
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("ss!"), &name, &nameLen, &val, &valLen) ==
300
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
303
string propName(name, nameLen);
307
propValue = string(val, valLen);
312
_this->setProperty(propName, propValue);
314
catch(const IceUtil::Exception& ex)
316
throwException(ex TSRMLS_CC);
321
ZEND_METHOD(Ice_Properties, getCommandLineOptions)
323
if(ZEND_NUM_ARGS() != 0)
328
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
333
Ice::StringSeq val = _this->getCommandLineOptions();
334
if(!createStringArray(return_value, val TSRMLS_CC))
339
catch(const IceUtil::Exception& ex)
341
throwException(ex TSRMLS_CC);
346
ZEND_METHOD(Ice_Properties, parseCommandLineOptions)
352
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s!a!"), &p, &pLen, &opts) == FAILURE)
357
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
363
prefix = string(p, pLen);
365
Ice::StringSeq options;
366
if(opts && !extractStringArray(opts, options TSRMLS_CC))
373
Ice::StringSeq val = _this->parseCommandLineOptions(prefix, options);
374
if(!createStringArray(return_value, val TSRMLS_CC))
379
catch(const IceUtil::Exception& ex)
381
throwException(ex TSRMLS_CC);
386
ZEND_METHOD(Ice_Properties, parseIceCommandLineOptions)
390
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("a!"), &opts) == FAILURE)
395
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
398
Ice::StringSeq options;
399
if(opts && !extractStringArray(opts, options TSRMLS_CC))
406
Ice::StringSeq val = _this->parseIceCommandLineOptions(options);
407
if(!createStringArray(return_value, val TSRMLS_CC))
412
catch(const IceUtil::Exception& ex)
414
throwException(ex TSRMLS_CC);
419
ZEND_METHOD(Ice_Properties, load)
424
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s"), &f, &fLen) == FAILURE)
429
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
432
string file(f, fLen);
438
catch(const IceUtil::Exception& ex)
440
throwException(ex TSRMLS_CC);
445
ZEND_METHOD(Ice_Properties, clone)
447
if(ZEND_NUM_ARGS() > 0)
452
Ice::PropertiesPtr _this = Wrapper<Ice::PropertiesPtr>::value(getThis() TSRMLS_CC);
457
Ice::PropertiesPtr pclone = _this->clone();
459
if(!createProperties(return_value, pclone TSRMLS_CC))
464
catch(const IceUtil::Exception& ex)
466
throwException(ex TSRMLS_CC);
474
static zend_object_value
475
handleAlloc(zend_class_entry* ce TSRMLS_DC)
477
zend_object_value result;
479
Wrapper<Ice::PropertiesPtr>* obj = Wrapper<Ice::PropertiesPtr>::create(ce TSRMLS_CC);
482
result.handle = zend_objects_store_put(obj, 0, (zend_objects_free_object_storage_t)handleFreeStorage, 0 TSRMLS_CC);
483
result.handlers = &_handlers;
492
handleFreeStorage(void* p TSRMLS_DC)
494
Wrapper<Ice::PropertiesPtr>* obj = static_cast<Wrapper<Ice::PropertiesPtr>*>(p);
496
zend_objects_free_object_storage(static_cast<zend_object*>(p) TSRMLS_CC);
502
static zend_object_value
503
handleClone(zval* zv TSRMLS_DC)
505
zend_object_value result;
506
memset(&result, 0, sizeof(zend_object_value));
508
Ice::PropertiesPtr p = Wrapper<Ice::PropertiesPtr>::value(zv TSRMLS_CC);
511
Ice::PropertiesPtr pclone = p->clone();
514
MAKE_STD_ZVAL(clone);
515
if(!createProperties(clone, pclone TSRMLS_CC))
521
// We only need to return the new object's handle, so we must destroy the zval containing
522
// a reference to the new object. We increment the object's reference count to ensure it
523
// does not get destroyed.
525
result = clone->value.obj;
526
Z_OBJ_HT_P(clone)->add_ref(clone TSRMLS_CC);
533
ZEND_FUNCTION(Ice_createProperties)
536
zval* defaultsObj = 0;
538
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("|a!O!"), &arglist, &defaultsObj,
539
propertiesClassEntry) == FAILURE)
545
if(arglist && !extractStringArray(arglist, seq TSRMLS_CC))
550
Ice::PropertiesPtr defaults;
551
if(defaultsObj && !fetchProperties(defaultsObj, defaults TSRMLS_CC))
558
Ice::PropertiesPtr props;
559
if(arglist || defaults)
561
props = Ice::createProperties(seq, defaults);
565
props = Ice::createProperties();
568
if(!createProperties(return_value, props TSRMLS_CC))
573
if(arglist && PZVAL_IS_REF(arglist))
576
if(!createStringArray(arglist, seq TSRMLS_CC))
582
catch(const IceUtil::Exception& ex)
584
throwException(ex TSRMLS_CC);
590
// Predefined methods for Properties.
592
static function_entry _interfaceMethods[] =
596
static function_entry _classMethods[] =
598
ZEND_ME(Ice_Properties, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
599
ZEND_ME(Ice_Properties, __toString, NULL, ZEND_ACC_PUBLIC)
600
ZEND_ME(Ice_Properties, getProperty, NULL, ZEND_ACC_PUBLIC)
601
ZEND_ME(Ice_Properties, getPropertyWithDefault, NULL, ZEND_ACC_PUBLIC)
602
ZEND_ME(Ice_Properties, getPropertyAsInt, NULL, ZEND_ACC_PUBLIC)
603
ZEND_ME(Ice_Properties, getPropertyAsIntWithDefault, NULL, ZEND_ACC_PUBLIC)
604
ZEND_ME(Ice_Properties, getPropertyAsList, NULL, ZEND_ACC_PUBLIC)
605
ZEND_ME(Ice_Properties, getPropertyAsListWithDefault, NULL, ZEND_ACC_PUBLIC)
606
ZEND_ME(Ice_Properties, getPropertiesForPrefix, NULL, ZEND_ACC_PUBLIC)
607
ZEND_ME(Ice_Properties, setProperty, NULL, ZEND_ACC_PUBLIC)
608
ZEND_ME(Ice_Properties, getCommandLineOptions, NULL, ZEND_ACC_PUBLIC)
609
ZEND_ME(Ice_Properties, parseCommandLineOptions, NULL, ZEND_ACC_PUBLIC)
610
ZEND_ME(Ice_Properties, parseIceCommandLineOptions, NULL, ZEND_ACC_PUBLIC)
611
ZEND_ME(Ice_Properties, load, NULL, ZEND_ACC_PUBLIC)
612
ZEND_ME(Ice_Properties, clone, NULL, ZEND_ACC_PUBLIC)
617
IcePHP::propertiesInit(TSRMLS_D)
620
// We register an interface and a class that implements the interface. This allows
621
// applications to safely include the Slice-generated code for the type.
625
// Register the Properties interface.
628
#ifdef ICEPHP_USE_NAMESPACES
629
INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("Properties"), _interfaceMethods);
631
INIT_CLASS_ENTRY(ce, "Ice_Properties", _interfaceMethods);
633
zend_class_entry* interface = zend_register_internal_interface(&ce TSRMLS_CC);
636
// Register the Properties class.
638
INIT_CLASS_ENTRY(ce, "IcePHP_Properties", _classMethods);
639
ce.create_object = handleAlloc;
640
propertiesClassEntry = zend_register_internal_class(&ce TSRMLS_CC);
641
memcpy(&_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
642
_handlers.clone_obj = handleClone;
643
zend_class_implements(propertiesClassEntry TSRMLS_CC, 1, interface);
649
IcePHP::createProperties(zval* zv, const Ice::PropertiesPtr& p TSRMLS_DC)
651
if(object_init_ex(zv, propertiesClassEntry) != SUCCESS)
653
runtimeError("unable to initialize properties object" TSRMLS_CC);
657
Wrapper<Ice::PropertiesPtr>* obj = Wrapper<Ice::PropertiesPtr>::extract(zv TSRMLS_CC);
659
obj->ptr = new Ice::PropertiesPtr(p);
665
IcePHP::fetchProperties(zval* zv, Ice::PropertiesPtr& p TSRMLS_DC)
667
if(!ZVAL_IS_NULL(zv))
669
if(Z_TYPE_P(zv) != IS_OBJECT || Z_OBJCE_P(zv) != propertiesClassEntry)
671
invalidArgument("value is not a properties object" TSRMLS_CC);
674
p = Wrapper<Ice::PropertiesPtr>::value(zv TSRMLS_CC);
677
runtimeError("unable to retrieve properties object from object store" TSRMLS_CC);