Difference between #2 and #1 of Use phing to make Yii application "installable"

unchanged
Title
Use phing to make our application "installable"
unchanged
Category
How-tos
unchanged
Tags
phing
changed
Content
To use this "how-to", you need to install
phing[phing](http://www.phing.info/trac/
"phing") in your machine. [Phing](PHing Is Not GNU make;
it's a PHP project build system or build tool based on  Apache Ant.
"Phing").

I want to show you a little example that allow me to configure gii password and
database informations, with phing.

First of all, when I start a new yii application, I copy
protected/config/main.php in protected/config/main.php.dist, updating some field
like ...:

~~~
[php]
        'db' => array(
            'connectionString' =>
'mysql:host=[[hostname]];dbname=[[database]]',
            'emulatePrepare' => true,
            'username' => '[[username]]',
            'password' => '[[password]]',
            'charset' => 'utf8',
        ),
~~~

I like also set gii configuration in this way:

~~~
[php]
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => '[[giipassword]]',
            'ipFilters' => array('127.0.0.1', '::1'),
        ),
~~~

Now, my config(.dist) file looks like this:

~~~
[php]
<?php

return array(
    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
    'name' => 'YiiApp',
    'preload' => array(
        'log'
    ),
    'import' => array(
        'application.models.*',
        'application.components.*',
    ),
    'modules' => array(
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => '[[giipassword]]',
            'ipFilters' => array('127.0.0.1', '::1'),
        ),
    ),
    'components' => array(
        'user' => array(
            'allowAutoLogin' => true,
        ),
        'db' => array(
            'connectionString' =>
'mysql:host=[[hostname]];dbname=[[database]]',
            'emulatePrepare' => true,
            'username' => '[[username]]',
            'password' => '[[password]]',
            'charset' => 'utf8',
        ),
        'errorHandler' => array(
            'errorAction' => 'site/error',
        ),
        'log' => array(
            'class' => 'CLogRouter',
            'routes' => array(
                array(
                    'class' => 'CFileLogRoute',
                    'levels' => 'error, warning',
                ),
            ),
        ),
    ),
    'params' => array(
        'adminEmail' => 'webmaster@example.com',
    ),
);
~~~

Now I can create build.xml and put inside this content:

~~~
[xml]
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyYiiApp" default="database">

    <target name="database" description="Configure
database">
        <input propertyname="hostname"
defaultValue="127.0.0.1"
                promptChar="?">Enter the hostname of
db.</input>
        <input propertyname="database"
defaultValue="database"
                promptChar="?">Enter the name of db.</input>
        <input propertyname="username"
defaultValue="root"
                promptChar="?">Enter username to access to the
db.</input>
        <input propertyname="password"
defaultValue="root"
                promptChar="?">Enter password to access to the
db.</input>
        <input propertyname="giipassword"
defaultValue="giipassword"
                promptChar="?">Enter password to access to
gii.</input>
        <copy file="protected/config/main.php.dist"
                tofile="protected/config/main.php"
overwrite="true">
            <filterchain>
                <replacetokens begintoken="[["
endtoken="]]">
                    <token key="hostname"
value="${hostname}" />
                    <token key="database"
value="${database}" />
                    <token key="username"
value="${username}" />
                    <token key="password"
value="${password}" />
                    <token key="giipassword"
value="${giipassword}" />
                </replacetokens>
            </filterchain>
        </copy>
    </target>

</project>
~~~

And now, I can run

$ phing

The output will be similar to this:

~~~
[php]
Buildfile: /var/www/yiiapp/build.xml:

Enter the hostname of db. [127.0.0.1]? 
Enter the name of db. [database]? yiiapp
Enter username to access to the db. [root]? 
Enter password to access to the db. [root]? 
Enter password to access to gii. [password]? 
     [copy] Copying 1 file to /var/www/yiiapp/protected/config
     [copy] Copying 1 file to /var/www/yiiapp/protected/config

BUILD FINISHED
~~~

As you can see, some questions will be asket to you. Your answers will be
override main.php.dist file copying it into main.php. And you can install your
application in this easy way. This is just a simple example.

You can also, for example, create your own console command and call them with
phing.
Write new article