]> granicus.if.org Git - php/commitdiff
add support for creating new files via array access
authorGreg Beaver <cellog@php.net>
Fri, 5 Jan 2007 04:27:49 +0000 (04:27 +0000)
committerGreg Beaver <cellog@php.net>
Fri, 5 Jan 2007 04:27:49 +0000 (04:27 +0000)
ext/phar/phar.c
ext/phar/tests/phar_oo_011.phpt [new file with mode: 0644]

index 63d8b7371f7814f85f1bde1f0c1f17a13735922c..fc1617f7c2fc62615076ef8930c930ca2d7294ed 100644 (file)
@@ -2366,7 +2366,29 @@ PHP_METHOD(Phar, offsetGet)
  */
 PHP_METHOD(Phar, offsetSet)
 {
-       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Operation currently not supported");
+       char *fname;
+       int fname_len;
+       char *contents;
+       int contents_len;
+       PHAR_ARCHIVE_OBJECT();
+       phar_entry_data *data;
+       php_stream *fp;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &fname, &fname_len, &contents, &contents_len) == FAILURE) {
+               return;
+       }
+
+       if (!(data = phar_get_or_create_entry_data(phar_obj->arc.archive->fname, phar_obj->arc.archive->fname_len, fname, fname_len TSRMLS_CC))) {
+               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be created", fname);
+       } else {
+               fname_len = spprintf(&fname, 0, "phar://%s/%s", phar_obj->arc.archive->fname, fname);
+               fp = php_stream_open_wrapper(fname, "wb", STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
+               if (contents_len != php_stream_write(fp, contents, contents_len)) {
+                       php_stream_close(fp);
+                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s could not be written to", fname);
+               }
+               php_stream_close(fp);
+       }
 }
 /* }}} */
 
diff --git a/ext/phar/tests/phar_oo_011.phpt b/ext/phar/tests/phar_oo_011.phpt
new file mode 100644 (file)
index 0000000..bdd3485
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Phar object: add file
+--SKIPIF--
+<?php if (!extension_loaded('phar')) die('skip'); ?>
+--FILE--
+<?php
+
+$pharconfig = 0;
+
+require_once 'phar_oo_test.inc';
+
+$phar = new Phar($fname);
+
+$phar['f.php'] = 'hi';
+var_dump(isset($phar['f.php']));
+echo $phar['f.php'];
+echo "\n";
+
+?>
+===DONE===
+--CLEAN--
+<?php 
+unlink(dirname(__FILE__) . '/phar_oo_test.phar.php');
+__halt_compiler();
+?>
+--EXPECT--
+bool(true)
+hi
+===DONE===