<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Construct a tar or zip archive from an iterator</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phardata.buildfromdirectory.html">PharData::buildFromDirectory</a></div> <div class="next" style="text-align: right; float: right;"><a href="phardata.compress.html">PharData::compress</a></div> <div class="up"><a href="class.phardata.html">PharData</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="phardata.buildfromiterator" class="refentry"> <div class="refnamediv"> <h1 class="refname">PharData::buildFromIterator</h1> <p class="verinfo">(PHP 5 >= 5.3.0, PHP 7, PECL phar >= 2.0.0)</p><p class="refpurpose"><span class="refname">PharData::buildFromIterator</span> — <span class="dc-title">Construct a tar or zip archive from an iterator</span></p> </div> <div class="refsect1 description" id="refsect1-phardata.buildfromiterator-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>PharData::buildFromIterator</strong></span> ( <span class="methodparam"><span class="type"><a href="class.iterator.html" class="type Iterator">Iterator</a></span> <code class="parameter">$iter</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$base_directory</code></span> ] )</div> <p class="para rdfs-comment"> Populate a tar or zip archive from an iterator. Two styles of iterators are supported, iterators that map the filename within the tar/zip to the name of a file on disk, and iterators like DirectoryIterator that return SplFileInfo objects. For iterators that return SplFileInfo objects, the second parameter is required. </p> </div> <div class="refsect1 examples" id="refsect1-phardata.buildfromiterator-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-793"> <p><strong>Example #1 A <span class="function"><strong>PharData::buildFromIterator()</strong></span> with SplFileInfo</strong></p> <div class="example-contents"><p> For most tar/zip archives, the archive will reflect an actual directory layout, and the second style is the most useful. For instance, to create a tar/zip archive containing the files in this sample directory layout: </p></div> <div class="example-contents"><p> <div class="example-contents screen"> <div class="examplescode"><pre class="examplescode">/path/to/project/ config/ dist.xml debug.xml lib/ file1.php file2.php src/ processthing.php www/ index.php cli/ index.php</pre> </div> </div> </p></div> <div class="example-contents"><p> This code could be used to add these files to the "project.tar" tar archive: </p></div> <div class="example-contents"><p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">PharData</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.tar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-></span><span style="color: #0000BB">buildFromIterator</span><span style="color: #007700">(<br /> new </span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(<br /> new </span><span style="color: #0000BB">RecursiveDirectoryIterator</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/project'</span><span style="color: #007700">)),<br /> </span><span style="color: #DD0000">'/path/to/project'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p></div> <div class="example-contents"><p> The file <em>project.tar</em> can then be used immediately. <span class="function"><strong>PharData::buildFromIterator()</strong></span> does not set values such as compression, metadata, and this can be done after creating the tar/zip archive. </p></div> <div class="example-contents"><p> As an interesting note, <span class="function"><strong>PharData::buildFromIterator()</strong></span> can also be used to copy the contents of an existing phar, tar or zip archive, as the PharData object descends from <a href="class.directoryiterator.html" class="classname">DirectoryIterator</a>: </p></div> <div class="example-contents"><p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">PharData</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.tar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-></span><span style="color: #0000BB">buildFromIterator</span><span style="color: #007700">(<br /> new </span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(<br /> new </span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/anotherphar.phar'</span><span style="color: #007700">)),<br /> </span><span style="color: #DD0000">'phar:///path/to/anotherphar.phar/path/to/project'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-></span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-></span><span style="color: #0000BB">createDefaultStub</span><span style="color: #007700">(</span><span style="color: #DD0000">'cli/index.php'</span><span style="color: #007700">, </span><span style="color: #DD0000">'www/index.php'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p></div> </div> <div class="example" id="example-794"> <p><strong>Example #2 A <span class="function"><strong>PharData::buildFromIterator()</strong></span> with other iterators</strong></p> <div class="example-contents"><p> The second form of the iterator can be used with any iterator that returns a key => value mapping, such as an <a href="class.arrayiterator.html" class="classname">ArrayIterator</a>: </p></div> <div class="example-contents"><p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">PharData</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.tar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-></span><span style="color: #0000BB">buildFromIterator</span><span style="color: #007700">(<br /> new </span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(<br /> array(<br /> </span><span style="color: #DD0000">'internal/file.php' </span><span style="color: #007700">=> </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">) . </span><span style="color: #DD0000">'/somefile.php'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'another/file.jpg' </span><span style="color: #007700">=> </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/bigfile.jpg'</span><span style="color: #007700">, </span><span style="color: #DD0000">'rb'</span><span style="color: #007700">),<br /> )));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p></div> </div> </div> <div class="refsect1 parameters" id="refsect1-phardata.buildfromiterator-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">iter</code></dt> <dd> <p class="para"> Any iterator that either associatively maps tar/zip file to location or returns SplFileInfo objects </p> </dd> <dt> <code class="parameter">base_directory</code></dt> <dd> <p class="para"> For iterators that return SplFileInfo objects, the portion of each file's full path to remove when adding to the tar/zip archive </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-phardata.buildfromiterator-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>PharData::buildFromIterator()</strong></span> returns an associative array mapping internal path of file to the full path of the file on the filesystem. </p> </div> <div class="refsect1 errors" id="refsect1-phardata.buildfromiterator-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> This method returns <a href="class.unexpectedvalueexception.html" class="classname">UnexpectedValueException</a> when the iterator returns incorrect values, such as an integer key instead of a string, a <a href="class.badmethodcallexception.html" class="classname">BadMethodCallException</a> when an SplFileInfo-based iterator is passed without a <code class="parameter">base_directory</code> parameter, or a <a href="class.pharexception.html" class="classname">PharException</a> if there were errors saving the phar archive. </p> </div> <div class="refsect1 seealso" id="refsect1-phardata.buildfromiterator-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="phar.buildfromiterator.html" class="function" rel="rdfs-seeAlso">Phar::buildFromIterator()</a> - Construct a phar archive from an iterator</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="phardata.buildfromdirectory.html">PharData::buildFromDirectory</a></div> <div class="next" style="text-align: right; float: right;"><a href="phardata.compress.html">PharData::compress</a></div> <div class="up"><a href="class.phardata.html">PharData</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>