phar.buildfromiterator.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 201 行
HTML
201 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Construct a phar archive from an iterator.</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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">(No version information available, might be only in CVS)</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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><b><b>Phar::buildFromIterator</b></b></span> ( <span class="methodparam"><span class="type"><span class="type Iterator">Iterator</span></span> <tt class="parameter">$iter</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$base_directory</tt></span> ] )</div> <blockquote><p><b class="note">Note</b>: Thismethod requires the <var class="filename">php.ini</var> setting <i>phar.readonly</i> to beset to <i>0</i> 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.<br /></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 examples"> <h3 class="title">Examples</h3> <div class="example"> <p><b>Example #1 A <b>Phar::buildFromIterator()</b> with SplFileInfo</b></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"><pre> <div class="cdata"><pre>/path/to/project/ config/ dist.xml debug.xml lib/ file1.php file2.php src/ processthing.php www/ index.php cli/ index.php </pre></div> </pre></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">createDefaultWebStub</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. <b>buildFromIterator()</b> 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, <b>buildFromIterator()</b> 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">createDefaultWebStub</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"> <p><b>Example #2 A <b>Phar::buildFromIterator()</b> with other iterators</b></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">createDefaultWebStub</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 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">iter</tt></i></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"><i><tt class="parameter">base_directory</tt></i></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"> <h3 class="title">Return Values</h3> <p class="para"> <b>buildFromIterator()</b> 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"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> This method returns <b class="classname">UnexpectedValueException</b> when the iterator returns incorrect values, such as an integer key instead of a string, a <b class="classname">BadMethodCallException</b> when an SplFileInfo-based iterator is passed without a <i><tt class="parameter">base_directory</tt></i> 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"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="phar.buildfromdirectory.html" class="function" rel="rdfs-seeAlso">Phar::buildFromDirectory()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?