]> granicus.if.org Git - php/commitdiff
- Add Phar::getStub(), step 1
authorMarcus Boerger <helly@php.net>
Mon, 22 Jan 2007 00:17:14 +0000 (00:17 +0000)
committerMarcus Boerger <helly@php.net>
Mon, 22 Jan 2007 00:17:14 +0000 (00:17 +0000)
ext/phar/TODO
ext/phar/phar_object.c
ext/phar/tests/phar_stub_write.phpt [new file with mode: 0755]

index a35e195a366a6e3d9a100cf5828897e67cd70445..e24ce132bbcaf873598e96647f56bda0dabefeb4 100644 (file)
@@ -15,6 +15,7 @@ Version 1.0.0
  X stream context for specifying compression of a file [Marcus]
  * stream context for specifying meta-data
  X Phar->setStub() for specifying a new stub to the phar [Greg]
+ X Phar->getStub() for retrieving the stub of the phar [Marcus]
  X add setUncompressed(), setCompressedGZ() and setCompressedBZ2() to 
    PharFileInfo class [Greg]
  * add uncompressAllFiles(), compressAllFilesGZ() and compressAllFilesBZ2() 
index bfde05a3db544558bb64efe661fae7474a9ddf95..3750ed8663d8d9366d65354b7f1c4222a295747d 100755 (executable)
@@ -373,7 +373,7 @@ PHP_METHOD(Phar, offsetUnset)
 /* }}} */
 
 /* {{{ proto int Phar::setStub(string|stream stub [, int len])
- * set the pre-phar stub for the current writeable phar
+ * Set the pre-phar stub for the current writeable phar
  */
 PHP_METHOD(Phar, setStub)
 {
@@ -382,6 +382,7 @@ PHP_METHOD(Phar, setStub)
        long len = -1;
        php_stream *stream;
        PHAR_ARCHIVE_OBJECT();
+
        if (PHAR_G(readonly)) {
                zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
                        "Cannot set stub, phar is read-only");
@@ -413,6 +414,37 @@ PHP_METHOD(Phar, setStub)
 }
 /* }}} */
 
+/* {{{ proto string Phar::getStub()
+ * Get the pre-phar stub
+ */
+PHP_METHOD(Phar, getStub)
+{
+       char *buf;
+       int len;
+       PHAR_ARCHIVE_OBJECT();
+
+       len = phar_obj->arc.archive->halt_offset;
+
+       if (!phar_obj->arc.archive->fp)  {
+               zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
+                       "Unable to read stub");
+               return;
+       }
+
+       buf = emalloc(len+1);
+       php_stream_rewind(phar_obj->arc.archive->fp);
+       if (len != php_stream_read(phar_obj->arc.archive->fp, buf, len)) {
+               zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
+                       "Unable to read stub");
+               efree(buf);
+               return;
+       }
+       buf[len] = '\0';
+       
+       RETURN_STRINGL(buf, len, 0);
+}
+/* }}}*/
+
 /* {{{ proto void PharFileInfo::__construct(string entry)
  * Construct a Phar entry object
  */
@@ -817,23 +849,24 @@ zend_function_entry php_archive_methods[] = {
 #if !HAVE_SPL
        PHP_ME(Phar, __construct,   arginfo_phar___construct,  ZEND_ACC_PRIVATE)
 #else
-       PHP_ME(Phar, __construct,   arginfo_phar___construct,  0)
-       PHP_ME(Phar, count,         NULL,                      0)
-       PHP_ME(Phar, getVersion,    NULL,                      0)
-       PHP_ME(Phar, getSignature,  NULL,                      0)
-       PHP_ME(Phar, getModified,   NULL,                      0)
-       PHP_ME(Phar, setStub,       arginfo_phar_setStub,      ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, __construct,   arginfo_phar___construct,  ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, count,         NULL,                      ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, getModified,   NULL,                      ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, getSignature,  NULL,                      ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, getStub,       NULL,                      ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, getVersion,    NULL,                      ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, offsetExists,  arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
        PHP_ME(Phar, offsetGet,     arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
        PHP_ME(Phar, offsetSet,     arginfo_phar_offsetSet,    ZEND_ACC_PUBLIC)
        PHP_ME(Phar, offsetUnset,   arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
-       PHP_ME(Phar, offsetExists,  arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, setStub,       arginfo_phar_setStub,      ZEND_ACC_PUBLIC)
 #endif
        /* static member functions */
        PHP_ME(Phar, apiVersion,    NULL,                      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
        PHP_ME(Phar, canCompress,   NULL,                      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
        PHP_ME(Phar, canWrite,      NULL,                      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
-       PHP_ME(Phar, mapPhar,       arginfo_phar_mapPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
        PHP_ME(Phar, loadPhar,      arginfo_phar_loadPhar,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
+       PHP_ME(Phar, mapPhar,       arginfo_phar_mapPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
        {NULL, NULL, NULL}
 };
 
diff --git a/ext/phar/tests/phar_stub_write.phpt b/ext/phar/tests/phar_stub_write.phpt
new file mode 100755 (executable)
index 0000000..031ec68
--- /dev/null
@@ -0,0 +1,47 @@
+--TEST--
+Phar stub read
+--SKIPIF--
+<?php if (!extension_loaded("phar")) print "skip"; ?>
+--INI--
+phar.require_hash=0
+--FILE--
+<?php
+$fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php';
+$pname = 'phar://' . $fname;
+$stub = '<?php echo "first stub\n"; __HALT_COMPILER(); ?>';
+$file = $stub;
+
+$files = array();
+$files['a'] = 'a';
+$files['b'] = 'b';
+$files['c'] = 'c';
+
+include 'phar_test.inc';
+
+$phar = new Phar($fname);
+var_dump($stub);
+var_dump($phar->getStub());
+var_dump($phar->getStub() == $stub);
+
+$stub = '<?php echo "second stub\n"; __HALT_COMPILER(); ?>';
+$phar->setStub($stub);
+var_dump($phar->getStub() == $stub);
+
+$phar = new Phar($fname);
+var_dump($phar->getStub() == $stub);
+
+?>
+===DONE===
+--CLEAN--
+<?php 
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phartmp.php');
+__HALT_COMPILER();
+?>
+--EXPECT--
+string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
+string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
+bool(true)
+bool(true)
+bool(true)
+===DONE===