Standardize Generator documentation

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@330333 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Jakub Vrana 2013-05-21 18:44:54 +00:00
parent efaca17617
commit 8f10aa0448
12 changed files with 593 additions and 100 deletions

View file

@ -200,8 +200,7 @@ foreach ($generator as $value) {
<para>
This syntax may be used in conjunction with the
<link linkend="generator.send">send()</link> method on
Generator objects.
<methodname>Generator::send</methodname> method.
</para>
</caution>
@ -363,108 +362,17 @@ foreach (gen_reference() as &$number) {
</example>
</sect3>
</sect2>
</sect1>
<sect1 xml:id="class.generator">
<title><classname>Generator</classname> objects</title>
<para>
When a generator function is called for the first time, an object of the
internal <classname>Generator</classname> class is returned. This object
implements the <classname>Iterator</classname> interface in much the same
way as a forward-only iterator object would.
</para>
<para>
Most methods in the <classname>Generator</classname> class have the same
semantics as the methods in the <classname>Iterator</classname> interface,
but generator objects also have one additional method:
<command>send()</command>.
</para>
<caution>
<sect2 xml:id="language.generators.object">
<title><classname>Generator</classname> objects</title>
<para>
<classname>Generator</classname> objects cannot be instantiated via
<link linkend="language.oop5.basic.new">new</link>.
When a generator function is called for the first time, an object of the
internal <classname>Generator</classname> class is returned. This object
implements the <classname>Iterator</classname> interface in much the same
way as a forward-only iterator object would.
</para>
</caution>
<!-- TODO: I've nicked this from the exception documentation, but I don't
actually like it very much. At some point, let's try to break
this and Exception out into proper class/method documentation.
-->
<example>
<title>The <classname>Generator</classname> class</title>
<programlisting role="php">
<![CDATA[
<?php
class Generator implements Iterator {
public function rewind(); // Rewinds the iterator. If
// iteration has already begun,
// this will throw an exception.
public function valid(); // Returns false if the
// iterator has been closed.
// Otherwise returns true.
public function current(); // Returns the yielded value.
public function key(); // Returns the yielded key.
public function next(); // Resumes execution of the
// generator.
public function send($value); // Sends the given value to the
// generator as the result of
// the yield expression and
// resumes execution of the
// generator.
public function throw(Exception $ex);
// Throws an exception into the
// generator.
}
?>
]]>
</programlisting>
</example>
<sect2 xml:id="generator.send">
<title><methodname>Generator::send</methodname></title>
<para>
<methodname>Generator::send</methodname> allows values to be injected into
generator functions while iterating over them. The injected value will be
returned from the &yield; statement and can then be used like any other
variable within the generator function.
</para>
<example>
<title>Using <methodname>Generator::send</methodname> to inject values</title>
<programlisting role="php">
<![CDATA[
<?php
function printer() {
while (true) {
$string = yield;
echo $string;
}
}
$printer = printer();
$printer->send('Hello world!');
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Hello world!
]]>
</screen>
</example>
</sect2>
</sect1>
<sect1 xml:id="language.generators.comparison">

View file

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<phpdoc:classref xml:id="class.generator" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The Generator class</title>
<titleabbrev>Generator</titleabbrev>
<partintro>
<!-- {{{ Generator intro -->
<section xml:id="generator.intro">
&reftitle.intro;
<para>
<classname>Generator</classname> objects are returned from <link
linkend="language.generators">generators</link>.
</para>
<caution>
<para>
<classname>Generator</classname> objects cannot be instantiated via
<link linkend="language.oop5.basic.new">new</link>.
</para>
</caution>
</section>
<!-- }}} -->
<section xml:id="generator.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>Generator</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>Generator</classname>
</ooclass>
<oointerface>
<interfacename>Iterator</interfacename>
</oointerface>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.generator')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])" />
</classsynopsis>
<!-- }}} -->
</section>
</partintro>
&language.predefined.generator.current;
&language.predefined.generator.key;
&language.predefined.generator.next;
&language.predefined.generator.rewind;
&language.predefined.generator.send;
&language.predefined.generator.throw;
&language.predefined.generator.valid;
&language.predefined.generator.wakeup;
</phpdoc:classref>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.current" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::current</refname>
<refpurpose>Get the yielded value</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>mixed</type><methodname>Generator::current</methodname>
<void />
</methodsynopsis>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the yielded value.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.key" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::key</refname>
<refpurpose>Get the yielded key</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>mixed</type><methodname>Generator::key</methodname>
<void />
</methodsynopsis>
<para>
</para>
&warn.undocumented.func;
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the yielded key.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.next" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::next</refname>
<refpurpose>Resume execution of the generator</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>void</type><methodname>Generator::next</methodname>
<void />
</methodsynopsis>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.rewind" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::rewind</refname>
<refpurpose>Rewind the iterator</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>void</type><methodname>Generator::rewind</methodname>
<void />
</methodsynopsis>
<para>
If iteration has already begun, this will throw an exception.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.send" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::send</refname>
<refpurpose>Send a value to the generator</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>mixed</type><methodname>Generator::send</methodname>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
</methodsynopsis>
<para>
Sends the given value to the generator as the result of the yield expression
and resumes execution of the generator.
</para>
<para>
<methodname>Generator::send</methodname> allows values to be injected into
generator functions while iterating over them. The injected value will be
returned from the &yield; statement and can then be used like any other
variable within the generator function.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Using <methodname>Generator::send</methodname> to inject values</title>
<programlisting role="php">
<![CDATA[
<?php
function printer() {
while (true) {
$string = yield;
echo $string;
}
}
$printer = printer();
$printer->send('Hello world!');
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Hello world!
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the yielded value.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.throw" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::throw</refname>
<refpurpose>Throw an exception into the generator</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>void</type><methodname>Generator::throw</methodname>
<methodparam><type>Exception</type><parameter>exception</parameter></methodparam>
</methodsynopsis>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>exception</parameter></term>
<listitem>
<para>
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the yielded value.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.valid" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::valid</refname>
<refpurpose>Check if the iterator has been closed</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>bool</type><methodname>Generator::valid</methodname>
<void />
</methodsynopsis>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns &false; if the iterator has been closed. Otherwise returns &true;.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="generator.wakeup" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Generator::__wakeup</refname>
<refpurpose>Serialize callback</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>void</type><methodname>Generator::__wakeup</methodname>
<void />
</methodsynopsis>
<para>
Throws an exception as generators can't be serialized.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

View file

@ -17,6 +17,7 @@
&language.predefined.arrayaccess;
&language.predefined.serializable;
&language.predefined.closure;
&language.predefined.generator;
</part>

View file

@ -49,6 +49,16 @@
<function name='namespaces' from='PHP 5 &gt;= 5.3.0'/>
<function name='generators' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::current' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::key' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::next' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::rewind' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::send' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::throw' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::valid' from='PHP 5 &gt;= 5.5.0'/>
<function name='generator::__wakeup' from='PHP 5 &gt;= 5.5.0'/>
<function name='_COOKIE' from='PHP 4 &gt;= 4.1.0, PHP 5' />
<function name='_ENV' from='PHP 4 &gt;= 4.1.0, PHP 5' />
<function name='_FILES' from='PHP 4 &gt;= 4.1.0, PHP 5' />