<!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 phar 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="phar.buildfromdirectory.html">Phar::buildFromDirectory</a></div> <div class="next" style="text-align: right; float: right;"><a href="phar.cancompress.html">Phar::canCompress</a></div> <div class="up"><a href="class.phar.html">Phar</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="phar.buildfromiterator" class="refentry"> <div class="refnamediv"> <h1 class="refname">Phar::buildFromIterator</h1> <p class="verinfo">(PHP >= 5.3.0, PECL phar >= 2.0.0)</p><p class="refpurpose"><span class="refname">Phar::buildFromIterator</span> — <span class="dc-title">Construct a phar archive from an iterator.</span></p> </div> <div class="refsect1 description" id="refsect1-phar.buildfromiterator-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>Phar::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> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">This method requires the <var class="filename">php.ini</var> setting <em>phar.readonly</em> to be set to <em>0</em> in order to work for <a href="class.phar.html" class="classname">Phar</a> objects. Otherwise, a <a href="class.pharexception.html" class="classname">PharException</a> will be thrown.</p></p></blockquote> <p class="para"> Populate a phar archive from an iterator. Two styles of iterators are supported, iterators that map the filename within the phar 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 parameters" id="refsect1-phar.buildfromiterator-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">iter</code></em></span> <dd> <p class="para"> Any iterator that either associatively maps phar file to location or returns SplFileInfo objects </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">base_directory</code></em></span> <dd> <p class="para"> For iterators that return SplFileInfo objects, the portion of each file's full path to remove when adding to the phar archive </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-phar.buildfromiterator-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>Phar::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 examples" id="refsect1-phar.buildfromiterator-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-661"> <p><strong>Example #1 A <span class="function"><strong>Phar::buildFromIterator()</strong></span> with SplFileInfo</strong></p> <div class="example-contents"><p> For most phar archives, the archive will reflect an actual directory layout, and the second style is the most useful. For instance, to create a phar 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.phar" phar 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 /></span><span style="color: #FF8000">// create with alias "project.phar"<br /></span><span style="color: #0000BB">$phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">'project.phar'</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">$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 class="example-contents"><p> The file project.phar can then be used immediately. <span class="function"><strong>Phar::buildFromIterator()</strong></span> does not set values such as compression, metadata, and this can be done after creating the phar archive. </p></div> <div class="example-contents"><p> As an interesting note, <span class="function"><strong>Phar::buildFromIterator()</strong></span> can also be used to copy the contents of an existing phar archive, as the Phar 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 /></span><span style="color: #FF8000">// create with alias "project.phar"<br /></span><span style="color: #0000BB">$phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">'project.phar'</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-662"> <p><strong>Example #2 A <span class="function"><strong>Phar::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 /></span><span style="color: #FF8000">// create with alias "project.phar"<br /></span><span style="color: #0000BB">$phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">'project.phar'</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">$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> <div class="refsect1 errors" id="refsect1-phar.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 <em><code class="parameter">base_directory</code></em> 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-phar.buildfromiterator-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="phar.buildfromdirectory.html" class="function" rel="rdfs-seeAlso">Phar::buildFromDirectory()</a> - Construct a phar archive from the files within a directory.</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="phar.buildfromdirectory.html">Phar::buildFromDirectory</a></div> <div class="next" style="text-align: right; float: right;"><a href="phar.cancompress.html">Phar::canCompress</a></div> <div class="up"><a href="class.phar.html">Phar</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>