<commit_before><?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.tool.framework.writing-providers">
    <title>Creating Providers to use with Zend_Tool_Framework</title>

    <para>
        In general, a provider, on its own, is nothing more than the shell for a
        developer to bundle up some capabilities they wish to dispatch with the
        command line (or other) clients. It is an analogue to what a
        "controller" is inside of your <acronym>MVC</acronym> application.
    </para>

    <sect2 id="zend.tool.framework.writing-providers.manifest">
        <title>Exposing Your Providers with a Manifest</title>

        <para>
            Before writing your own providers you need to create a manifest, which
            returns instances of your providers to allow Zend Tool to detect
            them. A Provider Manifest is an implementation of the
            <interface>Zend_Tool_Framework_Manifest_ProviderManifestable</interface>
            and requires the <code>getProviders()</code> method to return
            an array of instantiated providers. In anticipation of our first
            own provider <classname>My_Component_HelloProvider</classname>
            we will create the following manifest:
        </para>

        <programlisting language="php"><![CDATA[
class My_Component_Manifest implements Zend_Tool_Framework_Manifest_ProviderManifestable
{
    public function getProviders()
    {
        return array(
            new My_Component_HelloProvider()
        );
    }
}
]]>
        </programlisting>

        <para>
            A provider manifest class always has to end with the Term "Manifest" otherwise
            Zend_Tool cannot autodetect it while searching through your include path.
        </para>
    </sect2>

    <sect2 id="zend.tool.framework.writing-providers.basic">
        <title>Basic Instructions for Creating Providers</title>

        <para>
            As an example, if a developer wants to add the capability of showing
            the version of a datafile that his 3rd party component is working
            from, there is only one class the developer would need to implement.
            Assuming the component is called <classname>My_Component</classname>, he would
            create a class named <classname>My_Component_HelloProvider</classname> in a
            file named <filename>HelloProvider.php</filename> somewhere on the
            <property>include_path</property>. This class would implement
            <classname>Zend_Tool_Framework_Provider_Interface</classname>, and the body of
            this file would only have to look like the following:
        </para>

        <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    implements Zend_Tool_Framework_Provider_Interface
{
    public function say()
    {
        echo 'Hello from my provider!';
    }
}
]]></programlisting>

        <para>
            Given that code above, and assuming the developer wishes to access
            this functionality through the console client, the call would look
            like this:
        </para>

        <programlisting language="sh"><![CDATA[
% zf say hello
Hello from my provider!
]]></programlisting>
    </sect2>

    <sect2 id="zend.tool.framework.writing-providers.response">
        <title>The response object</title>

        <para>
            As discussed in the architecture section Zend Tool allows to hook different clients for
            using your Zend Tool providers. To keep compliant with different clients you should
            use the response object to return messages from your providers instead of using <code>echo</code>
            or a similiar output mechanism. Rewritting our hello provider with this knowledge it looks like:
        </para>

        <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    extends Zend_Tool_Framework_Provider_Abstract
{
    public function say()
    {
        $this->_registry->getResponse()->appendContent("Hello from my provider!");
    }
}
]]>
        </programlisting>

        <para>
            As you can see one has to extend the <classname>Zend_Tool_Framework_Provider_Abstract</classname>
            to gain access to the Registry which holds the <classname>Zend_Tool_Framework_Client_Response</classname>
            instance.
        </para>
    </sect2>

    <sect2 id="zend.tool.framework.writing-providers.advanced">
        <title>Advanced Development Information</title>

        <sect3 id="zend.tool.framework.writing-providers.advanced.variables">
            <title>Passing Variables to a Provider</title>
            
            <para>
                The above "Hello World" example is great for simple commands, but
                what about something more advanced? As your scripting and tooling
                needs grow, you might find that you need the ability to accept
                variables. Much like function signatures have parameters, your
                tooling requests can also accept parameters.
            </para>

            <para>
                Just as each tooling request can be isolated to a method within a
                class, the parameters of a tooling request can also be isolated in a
                very well known place. Parameters of the action methods of a
                provider can include the same parameters you want your client to
                utilize when calling that provider and action combination. For
                example, if you wanted to accept a name in the above example, you
                would probably do this in OO code:
            </para>

        <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    implements Zend_Tool_Framework_Provider_Interface
{
    public function say($name = 'Ralph')
    {
        echo 'Hello' . $name . ', from my provider!';
    }
}
]]></programlisting>

            <para>
                The above example can then be called via the command line
                <command>zf say hello Joe</command>. "Joe" will be supplied to the provider as
                a parameter of the method call. Also note, as you see that the
                parameter is optional, that means it is also optional on the command
                line, so that <command>zf say hello</command> will still work, and default
                to the name "Ralph".
            </para>

        </sect3>

        <sect3 id="zend.tool.framework.writing-providers.advanced.pretendable">
            <title>Pretending to execute a Provider Action</title>

            <para>
                Another interesting feature you might wish to implement is
                <emphasis>pretendability</emphasis>. Pretendabilty is the ability
                for your provider to "pretend" as if it is doing the requested
                action and provider combination and give the user as much
                information about what it <emphasis>would</emphasis> do without
                actually doing it. This might be an important notion when doing
                heavy database or filesystem modifications that the user might not
                otherwise want to do.
            </para>

            <para>
                Pretendability is easy to implement. There are two parts to this
                feature: 1) marking the provider as having the ability to "pretend",
                and 2) checking the request to ensure the current request was indeed
                asked to be "pretended". This feature is demonstrated in the code
                sample below.
            </para>

            <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    extends    Zend_Tool_Framework_Provider_Abstract
    implements Zend_Tool_Framework_Provider_Pretendable<commit_msg>ZF-7797 - Add example how to handle interactive input scenarios in your providers.<commit_after><?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.tool.framework.writing-providers">
    <title>Creating Providers to use with Zend_Tool_Framework</title>

    <para>
        In general, a provider, on its own, is nothing more than the shell for a
        developer to bundle up some capabilities they wish to dispatch with the
        command line (or other) clients. It is an analogue to what a
        "controller" is inside of your <acronym>MVC</acronym> application.
    </para>

    <sect2 id="zend.tool.framework.writing-providers.manifest">
        <title>Exposing Your Providers with a Manifest</title>

        <para>
            Before writing your own providers you need to create a manifest, which
            returns instances of your providers to allow Zend Tool to detect
            them. A Provider Manifest is an implementation of the
            <interface>Zend_Tool_Framework_Manifest_ProviderManifestable</interface>
            and requires the <code>getProviders()</code> method to return
            an array of instantiated providers. In anticipation of our first
            own provider <classname>My_Component_HelloProvider</classname>
            we will create the following manifest:
        </para>

        <programlisting language="php"><![CDATA[
class My_Component_Manifest implements Zend_Tool_Framework_Manifest_ProviderManifestable
{
    public function getProviders()
    {
        return array(
            new My_Component_HelloProvider()
        );
    }
}
]]>
        </programlisting>

        <para>
            A provider manifest class always has to end with the Term "Manifest" otherwise
            Zend_Tool cannot autodetect it while searching through your include path.
        </para>
    </sect2>

    <sect2 id="zend.tool.framework.writing-providers.basic">
        <title>Basic Instructions for Creating Providers</title>

        <para>
            As an example, if a developer wants to add the capability of showing
            the version of a datafile that his 3rd party component is working
            from, there is only one class the developer would need to implement.
            Assuming the component is called <classname>My_Component</classname>, he would
            create a class named <classname>My_Component_HelloProvider</classname> in a
            file named <filename>HelloProvider.php</filename> somewhere on the
            <property>include_path</property>. This class would implement
            <classname>Zend_Tool_Framework_Provider_Interface</classname>, and the body of
            this file would only have to look like the following:
        </para>

        <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    implements Zend_Tool_Framework_Provider_Interface
{
    public function say()
    {
        echo 'Hello from my provider!';
    }
}
]]></programlisting>

        <para>
            Given that code above, and assuming the developer wishes to access
            this functionality through the console client, the call would look
            like this:
        </para>

        <programlisting language="sh"><![CDATA[
% zf say hello
Hello from my provider!
]]></programlisting>
    </sect2>

    <sect2 id="zend.tool.framework.writing-providers.response">
        <title>The response object</title>

        <para>
            As discussed in the architecture section Zend Tool allows to hook different clients for
            using your Zend Tool providers. To keep compliant with different clients you should
            use the response object to return messages from your providers instead of using <code>echo</code>
            or a similiar output mechanism. Rewritting our hello provider with this knowledge it looks like:
        </para>

        <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    extends Zend_Tool_Framework_Provider_Abstract
{
    public function say()
    {
        $this->_registry->getResponse()->appendContent("Hello from my provider!");
    }
}
]]>
        </programlisting>

        <para>
            As you can see one has to extend the <classname>Zend_Tool_Framework_Provider_Abstract</classname>
            to gain access to the Registry which holds the <classname>Zend_Tool_Framework_Client_Response</classname>
            instance.
        </para>
    </sect2>

    <sect2 id="zend.tool.framework.writing-providers.advanced">
        <title>Advanced Development Information</title>

        <sect3 id="zend.tool.framework.writing-providers.advanced.variables">
            <title>Passing Variables to a Provider</title>
            
            <para>
                The above "Hello World" example is great for simple commands, but
                what about something more advanced? As your scripting and tooling
                needs grow, you might find that you need the ability to accept
                variables. Much like function signatures have parameters, your
                tooling requests can also accept parameters.
            </para>

            <para>
                Just as each tooling request can be isolated to a method within a
                class, the parameters of a tooling request can also be isolated in a
                very well known place. Parameters of the action methods of a
                provider can include the same parameters you want your client to
                utilize when calling that provider and action combination. For
                example, if you wanted to accept a name in the above example, you
                would probably do this in OO code:
            </para>

        <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    implements Zend_Tool_Framework_Provider_Interface
{
    public function say($name = 'Ralph')
    {
        echo 'Hello' . $name . ', from my provider!';
    }
}
]]></programlisting>

            <para>
                The above example can then be called via the command line
                <command>zf say hello Joe</command>. "Joe" will be supplied to the provider as
                a parameter of the method call. Also note, as you see that the
                parameter is optional, that means it is also optional on the command
                line, so that <command>zf say hello</command> will still work, and default
                to the name "Ralph".
            </para>

        </sect3>

        <sect3 id="zend.tool.framework.writing-providers.advanced.prompt">
            <title>Prompt the User for Input</title>

            <para>
                There are cases when the workflow of your provider requires
                to prompt the user for input. This can be done by requesting
                the client to ask for more the required input by calling:
            </para>

            <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    extends Zend_Tool_Framework_Provider_Abstract
{
    public function say($name = 'Ralph')
    {
        $nameResponse = $this->_registry->getClient()->promptInteractiveInput("Whats your name?");
        $name = $name->getContent();

        echo 'Hello' . $name . ', from my provider!';
    }
}
]]>
            </programlisting>

            <para>
                This command throws an exception if the current client is not
                able to handle interactive requests. In case of the default Console Client
                however you will be asked to enter the name.
            </para>
        </sect3>

        <sect3 id="zend.tool.framework.writing-providers.advanced.pretendable">
            <title>Pretending to execute a Provider Action</title>

            <para>
                Another interesting feature you might wish to implement is
                <emphasis>pretendability</emphasis>. Pretendabilty is the ability
                for your provider to "pretend" as if it is doing the requested
                action and provider combination and give the user as much
                information about what it <emphasis>would</emphasis> do without
                actually doing it. This might be an important notion when doing
                heavy database or filesystem modifications that the user might not
                otherwise want to do.
            </para>

            <para>
                Pretendability is easy to implement. There are two parts to this
                feature: 1) marking the provider as having the ability to "pretend",
                and 2) checking the request to ensure the current request was indeed
                asked to be "pretended". This feature is demonstrated in the code
                sample below.
            </para>

            <programlisting language="php"><![CDATA[
class My_Component_HelloProvider
    extends    Zend_Tool_Framework_Provider_Abstract
    implements Zend_Tool_Framework_Provider_Pretendable
{'fraction_non_alphanumeric': 0.06537343536809634, 'fraction_numerical': 0.002372901465266655, 'mean_word_length': 2.31719795356159, 'pattern_counts': {'":': 0, '<': 214, '<?xml version=': 2, '>': 222, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112434', 'n_tokens_mistral': 4132, 'n_tokens_neox': 3889, 'n_words': 1646}