<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.phar.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'phar.buildfromiterator.php',
    1 => 'Phar::buildFromIterator',
    2 => 'Construct a phar archive from an iterator',
  ),
  'up' => 
  array (
    0 => 'class.phar.php',
    1 => 'Phar',
  ),
  'prev' => 
  array (
    0 => 'phar.buildfromdirectory.php',
    1 => 'Phar::buildFromDirectory',
  ),
  'next' => 
  array (
    0 => 'phar.cancompress.php',
    1 => 'Phar::canCompress',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/phar/Phar/buildFromIterator.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="phar.buildfromiterator" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Phar::buildFromIterator</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8, PECL phar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">Phar::buildFromIterator</span> &mdash; <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">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Phar::buildFromIterator</strong></span>(<span class="methodparam"><span class="type"><a href="class.traversable.php" class="type Traversable">Traversable</a></span> <code class="parameter">$iterator</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$baseDirectory</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <blockquote class="note"><p><strong class="note">Nota</strong>: <p class="para">This
method requires the <var class="filename">php.ini</var> setting <code class="literal">phar.readonly</code> to be
set to <code class="literal">0</code> in order to work for <span class="classname"><a href="class.phar.php" class="classname">Phar</a></span>
objects.  Otherwise, a <span class="classname"><a href="class.pharexception.php" class="classname">PharException</a></span> 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">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">iterator</code></dt>
     <dd>
      <p class="para">
       Any iterator that either associatively maps phar file to location or
       returns SplFileInfo objects
      </p>
     </dd>
    
    
     <dt><code class="parameter">baseDirectory</code></dt>
     <dd>
      <p class="para">
       For iterators that return SplFileInfo objects, the portion of each
       file&#039;s full path to remove when adding to the phar archive
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-phar.buildfromiterator-returnvalues">
  <h3 class="title">Valori restituiti</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 errors" id="refsect1-phar.buildfromiterator-errors">
  <h3 class="title">Errori/Eccezioni</h3>
  <p class="para">
   This method returns <span class="classname"><a href="class.unexpectedvalueexception.php" class="classname">UnexpectedValueException</a></span> when the
   iterator returns incorrect values, such as an integer key instead of a
   string, a <span class="classname"><a href="class.badmethodcallexception.php" class="classname">BadMethodCallException</a></span> when an
   SplFileInfo-based iterator is passed without a <code class="parameter">baseDirectory</code>
   parameter, or a <span class="classname"><a href="class.pharexception.php" class="classname">PharException</a></span> if there were errors
   saving the phar archive.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-phar.buildfromiterator-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versione</th>
      <th>Descrizione</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.1.0</td>
      <td>
       <span class="methodname"><strong>Phar::buildFromIterator()</strong></span> no longer returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">baseDirectory</code> is now nullable.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-phar.buildfromiterator-examples">
  <h3 class="title">Esempi</h3>
   <div class="example" id="example-1">
    <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 &quot;project.phar&quot; 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">&lt;?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">-&gt;</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">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</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">?&gt;</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 <span class="classname"><a href="class.directoryiterator.php" class="classname">DirectoryIterator</a></span>:
  </p></div>
  <div class="example-contents"><p>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">-&gt;</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">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</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">?&gt;</span></span></code></div>
   </div>

  </p></div>
  </div>
  <div class="example" id="example-2">
    <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 =&gt; value mapping, such as an <span class="classname"><a href="class.arrayiterator.php" class="classname">ArrayIterator</a></span>:
  </p></div>
  <div class="example-contents"><p>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">-&gt;</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">=&gt; </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">=&gt; </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">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</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">?&gt;</span></span></code></div>
   </div>

  </p></div>
  </div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-phar.buildfromiterator-seealso">
  <h3 class="title">Vedere anche:</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="phar.buildfromdirectory.php" class="function" rel="rdfs-seeAlso">Phar::buildFromDirectory()</a> - Construct a phar archive from the files within a directory</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>