]> granicus.if.org Git - php/commitdiff
add PharFileInfo->getContents() to retrieve file contents
authorGreg Beaver <cellog@php.net>
Wed, 9 Apr 2008 18:12:41 +0000 (18:12 +0000)
committerGreg Beaver <cellog@php.net>
Wed, 9 Apr 2008 18:12:41 +0000 (18:12 +0000)
[DOC]

ext/phar/phar_object.c
ext/phar/tests/phar_oo_compressallbz2.phpt
ext/phar/tests/phar_oo_compressallgz.phpt
ext/phar/tests/phar_oo_getcontents.phpt [new file with mode: 0644]
ext/phar/tests/phar_oo_getcontentsgz.phpt [new file with mode: 0644]
ext/phar/tests/phar_oo_getmodified.phpt
ext/phar/tests/phar_oo_uncompressall.phpt

index ce2356b5e41a7efd8c73af0611e19ffbc5f26c8e..bc7f7ec065d8cca0492e98d0cbd7f3ffeed1f2f4 100755 (executable)
@@ -3466,6 +3466,37 @@ PHP_METHOD(PharFileInfo, delMetadata)
 }
 /* }}} */
 
+/* {{{ proto string PharFileInfo::getContents()
+ * return the complete file contents of the entry (like file_get_contents)
+ */
+PHP_METHOD(PharFileInfo, getContents)
+{
+       char *error;
+       php_stream *fp;
+       PHAR_ENTRY_OBJECT();
+
+       if (entry_obj->ent.entry->is_dir) {
+               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                       "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\" is a directory", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
+               return;
+       }
+       if (SUCCESS != phar_open_entry_fp(entry_obj->ent.entry, &error TSRMLS_CC)) {
+               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                       "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\": %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error);
+               efree(error);
+               return;
+       }
+       if (!(fp = phar_get_efp(entry_obj->ent.entry TSRMLS_CC))) {
+               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                       "Phar error: Cannot retrieve contents of \"%s\" in phar \"%s\"", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
+               return;
+       }
+       phar_seek_efp(entry_obj->ent.entry, 0, SEEK_SET, 0 TSRMLS_CC);
+       Z_TYPE_P(return_value) = IS_STRING;
+       Z_STRLEN_P(return_value) = php_stream_copy_to_mem(fp, &(Z_STRVAL_P(return_value)), entry_obj->ent.entry->uncompressed_filesize, 0);
+}
+/* }}} */
+
 /* {{{ proto int PharFileInfo::setCompressedGZ()
  * Instructs the Phar class to compress the current file using zlib
  */
@@ -3822,6 +3853,7 @@ zend_function_entry php_entry_methods[] = {
        PHP_ME(PharFileInfo, __destruct,         NULL,                       0)
        PHP_ME(PharFileInfo, chmod,              arginfo_entry_chmod,        0)
        PHP_ME(PharFileInfo, delMetadata,        NULL,                       0)
+       PHP_ME(PharFileInfo, getContents,        NULL,                       0)
        PHP_ME(PharFileInfo, getCompressedSize,  NULL,                       0)
        PHP_ME(PharFileInfo, getCRC32,           NULL,                       0)
        PHP_ME(PharFileInfo, getMetadata,        NULL,                       0)
index 7120f6411d90dd5076472a619d86035fd3644369..ef91272cdbc86c63a23d4d4ddbc7f84e4297f453 100644 (file)
@@ -45,6 +45,7 @@ var_dump($phar['b']->isCompressedBZIP2());
 --CLEAN--
 <?php 
 unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar');
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
 ?>
 --EXPECTF--
 string(1) "a"
index 71274491f1718b55134e443fee37b2a2c0b5c81a..3cce2b4363c8b23ddadc8edd73a361b5074bc722 100644 (file)
@@ -45,6 +45,7 @@ var_dump($phar['b']->isCompressedBZIP2());
 --CLEAN--
 <?php 
 unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar');
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
 ?>
 --EXPECTF--
 string(1) "a"
diff --git a/ext/phar/tests/phar_oo_getcontents.phpt b/ext/phar/tests/phar_oo_getcontents.phpt
new file mode 100644 (file)
index 0000000..3f093ad
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+Phar object: getContents()
+--SKIPIF--
+<?php if (!extension_loaded("phar")) die("skip"); ?>
+<?php if (!extension_loaded("spl")) die("skip SPL not available"); ?>
+--INI--
+phar.readonly=0
+--FILE--
+<?php
+$fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php';
+
+$phar = new Phar($fname);
+$phar['a'] = 'file contents
+this works';
+echo $phar['a']->getContents() . "\n";
+?>
+===DONE===
+--CLEAN--
+<?php 
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
+__halt_compiler();
+?>
+--EXPECT--
+file contents
+this works
+===DONE===
\ No newline at end of file
diff --git a/ext/phar/tests/phar_oo_getcontentsgz.phpt b/ext/phar/tests/phar_oo_getcontentsgz.phpt
new file mode 100644 (file)
index 0000000..c2a16f2
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Phar object: getContents() (verify it works with compression)
+--SKIPIF--
+<?php if (!extension_loaded("phar")) die("skip"); ?>
+<?php if (!extension_loaded("spl")) die("skip SPL not available"); ?>
+<?php if (!extension_loaded("zlib")) die("skip zlib not available"); ?>
+--INI--
+phar.readonly=0
+--FILE--
+<?php
+$fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php';
+$fname2 = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.2.phar.php';
+
+$phar = new Phar($fname);
+$phar['a'] = 'file contents
+this works';
+$phar['a']->setCompressedGZ();
+copy($fname, $fname2);
+$phar2 = new Phar($fname2);
+var_dump($phar2['a']->isCompressed());
+echo $phar2['a']->getContents() . "\n";
+?>
+===DONE===
+--CLEAN--
+<?php 
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.2.phar.php');
+__halt_compiler();
+?>
+--EXPECT--
+bool(true)
+file contents
+this works
+===DONE===
\ No newline at end of file
index c53729163176e1801992d5daefffca6eac716996..90eb056fa2a6bada8883cc23f85c8c99856664fc 100644 (file)
@@ -28,6 +28,7 @@ var_dump($phar->getModified());
 --CLEAN--
 <?php 
 unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar');
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
 ?>
 --EXPECTF--
 bool(false)
index b447e96d021f598d71f82b9bcbc17c3a69c171ae..4e3e73875e27efb679d17d7767789b3668ba54fb 100644 (file)
@@ -53,6 +53,7 @@ var_dump($phar['a']->isCompressed());
 --CLEAN--
 <?php 
 unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar');
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
 ?>
 --EXPECTF--
 string(1) "a"