Use phing to make our application "installable"

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

To use this "how-to", you need to install 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 ...:

'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:

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

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

<?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:

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.

7 0
10 followers
Viewed: 14 667 times
Version: Unknown (update)
Category: How-tos
Tags: phing
Written by: sensorario
Last updated by: Maurizio Domba Cerin
Created on: May 22, 2012
Last updated: 11 years ago
Update Article

Revisions

View all history