diff --git a/README b/README deleted file mode 100644 index 4b4466a..0000000 --- a/README +++ /dev/null @@ -1,45 +0,0 @@ -INSTALL -========================================================================== - -Install instructions (you’ll need php5 dev package): - -$ pecl install augeas - -or from the git repo: - -$ git clone git://github.com/ppadron/php-augeas.git -$ cd php-augeas -$ phpize -$ ./configure -$ make -$ make install - -Create a file called augeas.ini in your PHP conf.d directory containing: - -extension=augeas.so - - -API REFERENCE -========================================================================== - -void Augeas::__construct([string $root[, string $loadpath[, int $flags]]]) -string Augeas::get(string $path) -array Augeas::match(string $path); -boolean Augeas::set(string $path, string $value); -boolean Augeas::rm($augeas, string $path); -boolean Augeas::insert(string $path, string $label, int $order); -boolean Augeas::mv(string $source, string $destination); -boolean Augeas::save(); - -Constants used as $flags in Augeas::__construct() - -Augeas::AUGEAS_NONE = 0 -Augeas::AUGEAS_SAVE_BACKUP = 1 -Augeas::AUGEAS_SAVE_NEWFILE = 2 -Augeas::AUGEAS_TYPE_CHECK = 4 -Augeas::AUGEAS_NO_STDINC = 8 - -Constants used as $order in Augeas::insert() - -Augeas::AUGEAS_INSERT_BEFORE = 0 -Augeas::AUGEAS_INSERT_AFTER = 1 diff --git a/README.md b/README.md new file mode 100644 index 0000000..6530ab5 --- /dev/null +++ b/README.md @@ -0,0 +1,97 @@ +### new addition in this realese + +- add dump_to_xml to retreave augeas tree in xml string from [aug_to_xml](http://augeas.net/docs/references/c_api/files/augeas-h.html#aug_to_xml) + +### INSTALL + +Install instructions (you’ll need php5 dev package): + +``` +$ pecl install augeas +``` + +or from the git repo: + +``` +$ git clone git://github.com/ppadron/php-augeas.git +$ cd php-augeas +$ phpize +$ ./configure +$ make +$ make install +``` + +Create a file called augeas.ini in your PHP conf.d directory containing: + +``` +extension=augeas.so +``` + +### API REFERENCE + +``` +void Augeas::__construct([string $root[, string $loadpath[, int $flags]]]) +string Augeas::get(string $path) +string Augeas::dump_to_xml(string $path) +array Augeas::match(string $path); +boolean Augeas::set(string $path, string $value); +boolean Augeas::rm($augeas, string $path); +boolean Augeas::insert(string $path, string $label, int $order); +boolean Augeas::mv(string $source, string $destination); +boolean Augeas::save(); +``` + +Constants used as $flags in Augeas::__construct() + +``` +Augeas::AUGEAS_NONE = 0 +Augeas::AUGEAS_SAVE_BACKUP = 1 +Augeas::AUGEAS_SAVE_NEWFILE = 2 +Augeas::AUGEAS_TYPE_CHECK = 4 +Augeas::AUGEAS_NO_STDINC = 8 +``` + +Constants used as $order in Augeas::insert() + +``` +Augeas::AUGEAS_INSERT_BEFORE = 0 +Augeas::AUGEAS_INSERT_AFTER = 1 +``` + +### EXAMPLES + +```php +get("/files/etc/hosts/1/ipaddr")."\n"; +echo $augeas->get("/files/etc/hosts/1/canonical")."\n"; +echo $augeas->get("/files/etc/hosts/1/alias")."\n"; + +// dump_to_xml +echo $augeas->dump_to_xml("/files/etc/hosts/1/*")."\n"; + +// match +$matches = $augeas->match("/files/etc/hosts/1/*"); +var_dump($matches); + +// mv +echo $augeas->get("/files/etc/hosts/1/canonical")."\n"; +$augeas->mv("/files/etc/hosts/1/canonical", "/files/etc/hosts/1/alias"); +echo $this->augeas->get("/files/etc/hosts/1/alias")."\n"; + +// rm and save +$augeas->rm('/files/etc/hosts/1/ipaddr'); +$augeas->save(); + +// inserting before the first comment +$beforeFirstComment = "comment inserted before the first comment"; +$firstComment = "first comment"; +$augeas->insert("/files/etc/hosts/#comment", "#comment", Augeas::AUGEAS_INSERT_BEFORE); +$augeas->set("/files/etc/hosts/#comment[1]", $beforeFirstComment); +echo $augeas->get("/files/etc/hosts/#comment[1]")."\n"; +echo $augeas->get("/files/etc/hosts/#comment[2]")."\n"; + +?> +``` diff --git a/augeas.c b/augeas.c index 997ff84..d72ee12 100644 --- a/augeas.c +++ b/augeas.c @@ -43,6 +43,10 @@ ZEND_BEGIN_ARG_INFO(arginfo_Augeas_get, 0) ZEND_ARG_INFO(0, path) ZEND_END_ARG_INFO(); +ZEND_BEGIN_ARG_INFO(arginfo_Augeas_dump_to_xml, 0) + ZEND_ARG_INFO(0, path) +ZEND_END_ARG_INFO(); + ZEND_BEGIN_ARG_INFO(arginfo_Augeas_match, 0) ZEND_ARG_INFO(0, path) ZEND_END_ARG_INFO(); @@ -75,6 +79,7 @@ ZEND_END_ARG_INFO(); static zend_function_entry augeas_methods[] = { PHP_ME(Augeas, __construct, arginfo_Augeas__construct, ZEND_ACC_PUBLIC) PHP_ME(Augeas, get, arginfo_Augeas_get, ZEND_ACC_PUBLIC) + PHP_ME(Augeas, dump_to_xml, arginfo_Augeas_dump_to_xml, ZEND_ACC_PUBLIC) PHP_ME(Augeas, set, arginfo_Augeas_set, ZEND_ACC_PUBLIC) PHP_ME(Augeas, match, arginfo_Augeas_match, ZEND_ACC_PUBLIC) PHP_ME(Augeas, rm, arginfo_Augeas_rm, ZEND_ACC_PUBLIC) @@ -148,7 +153,11 @@ static zend_object_value augeas_object_new(zend_class_entry *class_type TSRMLS_D ALLOC_HASHTABLE(intern->zo.properties); zend_hash_init(intern->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0); +#if PHP_VERSION_ID < 50399 zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); +#else + object_properties_init((zend_object*) &(intern->zo), class_type); +#endif retval.handle = zend_objects_store_put(intern, augeas_object_dtor, NULL, NULL TSRMLS_CC); retval.handlers = zend_get_std_object_handlers(); @@ -163,10 +172,16 @@ static zend_object_value augeas_object_new(zend_class_entry *class_type TSRMLS_D PHP_MINIT_FUNCTION(augeas) { zend_class_entry ce, ce_exception; + zend_class_entry *default_exception=NULL; /* Register AugeasException class (inherits Exception) */ INIT_CLASS_ENTRY(ce_exception, "AugeasException", NULL); - augeas_ce_AugeasException = zend_register_internal_class_ex(&ce_exception, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_DC); + #if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2) + default_exception= zend_exception_get_default(); + #else + default_exception= zend_exception_get_default(TSRMLS_C); + #endif + augeas_ce_AugeasException = zend_register_internal_class_ex(&ce_exception, default_exception, NULL TSRMLS_CC); /* Register Augeas class */ INIT_CLASS_ENTRY(ce, "Augeas", augeas_methods); @@ -223,11 +238,11 @@ PHP_METHOD(Augeas, __construct) RETURN_FALSE; } - if (php_check_open_basedir(root TSRMLS_DC) != 0) { + if (php_check_open_basedir(root TSRMLS_CC) != 0) { RETURN_FALSE; } - obj = (php_augeas_object *) zend_object_store_get_object(getThis() TSRMLS_DC); + obj = (php_augeas_object *) zend_object_store_get_object(getThis() TSRMLS_CC); obj->augeas = aug_init(root, loadpath, flags); if (!obj->augeas) zend_throw_exception(augeas_ce_AugeasException, "could not initialize augeas resource", 0 TSRMLS_CC); @@ -280,6 +295,62 @@ PHP_METHOD(Augeas, get) } /* }}} */ +/* {{{ proto string Augeas::dump_to_xml(string $path); + dump to xml string. */ +PHP_METHOD(Augeas, dump_to_xml) +{ + char *path, *value; + char **matches; + int path_len, retval; + php_augeas_object *obj; + augeas *aug_intern; + xmlNodePtr xmldoc=NULL; + xmlBufferPtr buffer=NULL; + int size = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) { + RETURN_FALSE; + } + + if (path_len < 1) { + RETURN_FALSE; + } + + AUGEAS_FROM_OBJECT(aug_intern, getThis()); + + retval = aug_to_xml(aug_intern, path, &xmldoc, 0); + switch (retval) { + + /* export success */ + case 0: + // dump xmldoc to xmlbuffer + buffer = xmlBufferCreate(); + size = xmlNodeDump(buffer, xmldoc->doc, xmldoc, 0, 1); + if (size==-1) { + zend_throw_exception(augeas_ce_AugeasException, "xmlNodeDump failed", 0 TSRMLS_CC); + break; + } + + // duplicate xml string emallocated memory + value=emalloc(size); + memcpy(value,buffer->content,size); + value[size]='\0'; + + xmlFreeNode(xmldoc); + xmlBufferFree(buffer); + + RETURN_STRING(value, 0); + break; + + default: + zend_throw_exception(augeas_ce_AugeasException, "XML export failed", 0 TSRMLS_CC); + break; + } + + +} +/* }}} */ + /* {{{ proto boolean Augeas::set(string $path, string $value); Sets the value of $path to $value */ PHP_METHOD(Augeas, set) diff --git a/config.m4 b/config.m4 index 85ccf78..62d1789 100644 --- a/config.m4 +++ b/config.m4 @@ -1,3 +1,19 @@ +dnl Configuring the LibXML external Library +if test -z "$PHP_LIBXML_DIR"; then + PHP_ARG_WITH(libxml-dir, libxml2 install dir, + [ --with-libxml-dir=[DIR] php-augeas : libxml2 install prefix], no, no) +fi + +if test "$PHP_LIBXML" = "no"; then + AC_MSG_ERROR([php-augeas extension requires LIBXML extension, add --with-libxml-dir]) +fi + +PHP_SETUP_LIBXML(XML_SHARED_LIBADD, [ + PHP_ADD_EXTENSION_DEP(xml, libxml) +], [ + AC_MSG_ERROR([xml2-config not found. Use --with-libxml-dir=]) +]) + PHP_ARG_WITH(augeas,for AUGEAS support, [ --with-augeas[=DIR] Include AUGEAS support]) @@ -20,7 +36,7 @@ if test "$PHP_AUGEAS" != "no"; then done if test -z "$AUGEAS_DIR"; then - AC_MSG_ERROR(Cannot find libaugeas) + AC_MSG_ERROR(Cannot find libaugeas use --with-augeas=) fi AUGEAS_LIBDIR=$AUGEAS_DIR/$PHP_LIBDIR @@ -31,5 +47,6 @@ if test "$PHP_AUGEAS" != "no"; then PHP_NEW_EXTENSION(augeas, augeas.c, $ext_shared) PHP_SUBST(AUGEAS_SHARED_LIBADD) + PHP_SUBST(XML_SHARED_LIBADD) AC_DEFINE(HAVE_AUGEAS,1,[ ]) fi diff --git a/package.xml b/package.xml index 7610c10..1873d97 100644 --- a/package.xml +++ b/package.xml @@ -10,9 +10,9 @@ ppadron@php.net yes - 2010-02-12 + 2016-04-25 - 0.6.1 + 0.6.2 0.6.0 diff --git a/php_augeas.h b/php_augeas.h index c8c3243..50c8470 100644 --- a/php_augeas.h +++ b/php_augeas.h @@ -32,7 +32,7 @@ extern zend_module_entry augeas_module_entry; #include "TSRM.h" #endif -#define PHP_AUGEAS_VERSION "0.6.1" +#define PHP_AUGEAS_VERSION "0.6.2" typedef struct _php_augeas_object { zend_object zo; @@ -50,6 +50,7 @@ PHP_MINFO_FUNCTION(augeas); PHP_METHOD(Augeas, __construct); PHP_METHOD(Augeas, get); +PHP_METHOD(Augeas, dump_to_xml); PHP_METHOD(Augeas, set); PHP_METHOD(Augeas, match); PHP_METHOD(Augeas, save);