Deploying PHP applications with Phing

Post on 08-May-2015

15.671 views 1 download

description

Deploying an application can be tedious and error-prone. Using Phing’s rich set of tasks, easy extension points and simple XML build files to handle the packaging, deploying and testing of your application can help you save time and increase quality. After this talk you will know how to use Phing and how to tailor it to your specific situation. A number of demonstrations will help illustrate transformation, file synchronization, database migration and other real-world use cases.

Transcript of Deploying PHP applications with Phing

Deploying PHP applications with Phing – 1 / 37

Deploying PHP applications with Phing

Michiel Rook

PHPNW11 - October 8th, 2011

About me

Deploying PHP applications with Phing – 2 / 37

� Freelance PHP/Java consultant

� Phing project lead

� http://www.linkedin.com/in/michieltcs

� @michieltcs

About Phing

Deploying PHP applications with Phing – 3 / 37

� PHing Is Not GNU make; it’s a PHP project build system or build tool based on

Apache Ant.

� Originally developed by Binarycloud

� Ported to PHP5 by Hans Lellelid

� I joined in 2005

Features

Deploying PHP applications with Phing – 4 / 37

� Scripting using XML build files

� Mostly cross-platform

� Interface to various popular (PHP) tools

Features

Deploying PHP applications with Phing – 5 / 37

Installation

Deploying PHP applications with Phing – 6 / 37

� PEAR installation

$ pear channel-discover pear.phing.info$ pear install [--alldeps] phing/phing

� Optionally, install the documentation package

$ pear install phing/phingdocs

Why Use A Build Tool?

Deploying PHP applications with Phing – 7 / 37

Why Use A Build Tool

Deploying PHP applications with Phing – 8 / 37

� Repetitive tasks

� Version control

� (Unit) Testing

� Configuring

� Packaging

� Uploading

� DB changes

� ...

Why Use A Build Tool

Deploying PHP applications with Phing – 9 / 37

� For developers and administrators

� Automate!

� Easier handover to new team members

� Improves quality

� Reduces errors

� Saves time

Why Use Phing

Deploying PHP applications with Phing – 10 / 37

� Rich set of tasks

� Integration with PHP specific tools

� Allows you to stay in the PHP infrastructure

� Easy to extend

� Embed PHP code directly in the build file

Why Use Phing

Deploying PHP applications with Phing – 10 / 37

� Rich set of tasks

� Integration with PHP specific tools

� Allows you to stay in the PHP infrastructure

� Easy to extend

� Embed PHP code directly in the build file

� ... in the end, the choice is yours

The Basics

Deploying PHP applications with Phing – 11 / 37

Build Files

Deploying PHP applications with Phing – 12 / 37

� Phing uses XML build files

� Contain standard elements

� Task: code that performs a specific function (svn checkout, mkdir, etc.)

� Target: groups of tasks, can optionally depend on other targets

� Project: root node, contains multiple targets

Example Build File

Deploying PHP applications with Phing – 13 / 37

<project name="Example" default="world"><target name="hello">

<echo>Hello</echo></target>

<target name="world" depends="hello"><echo>World!</echo>

</target></project>

Properties

Deploying PHP applications with Phing – 14 / 37

� Simple key-value files (.ini)

## build.propertiesversion=1.0

� Can be expanded by using ${key} in the build file

$ phing -propertyfile build.properties ...

<project name="Example" default="default"><property file="build.properties" />

<target name="default"><echo>${version}</echo>

</target></project>

File Sets

Deploying PHP applications with Phing – 15 / 37

� Constructs a group of files to process

� Supported by most tasks

<fileset dir="./application" includes="**"/>

<fileset dir="./application"><include name="**/*.php" /><exclude name="**/*Test.php" />

</fileset>

� Supports references

<fileset dir="./application" includes="**" id="files"/>

<fileset refid="files"/>

File Sets

Deploying PHP applications with Phing – 16 / 37

� Selectors allow fine-grained matching on certain attributes

� contains, date, file name & size, ...

<fileset dir="${dist}"><and>

<filename name="**"/><date datetime="01/01/2011" when="before"/>

</and></fileset>

Mappers and Filters

Deploying PHP applications with Phing – 17 / 37

� Transform files during copy/move/...

� Mappers

� Change filename

� Filters

� Strip comments, white space

� Replace values

� Perform XSLT transformation

� Translation (i18n)

Mappers and Filters

Deploying PHP applications with Phing – 18 / 37

<copy todir="${build}"><fileset refid="files"/><mapper type="glob" from="*.txt" to="*.new.txt"/><filterchain>

<replaceregexp><regexp pattern="\r\n" replace="\n"/><expandproperties/>

</replaceregexp></filterchain>

</copy>

Practical Examples

Deploying PHP applications with Phing – 19 / 37

Testing

Deploying PHP applications with Phing – 20 / 37

� Built-in support for PHPUnit / SimpleTest

� Code coverage through XDebug

� Various output formats

PHPUnit

Deploying PHP applications with Phing – 21 / 37

<target name="test"><coverage-setup database="reports/coverage.db">

<fileset dir="src"><include name="**/*.php"/><exclude name="**/*Test.php"/>

</fileset></coverage-setup><phpunit codecoverage="true">

<formatter type="xml" todir="reports"/><batchtest>

<fileset dir="src"><include name="**/*Test.php"/>

</fileset></batchtest>

</phpunit><phpunitreport infile="reports/testsuites.xml"

format="frames" todir="reports/tests"/><coverage-report outfile="reports/coverage.xml">

<report todir="reports/coverage" title="Demo"/></coverage-report>

</target>

DocBlox

Deploying PHP applications with Phing – 22 / 37

<target name="docs"><docblox title="Phing API Documentation"

output="docs" quiet="true"><fileset dir="../../classes">

<include name="**/*.php"/></fileset>

</docblox></target>

Database Migration

Deploying PHP applications with Phing – 23 / 37

� DbDeploy

� Set of delta files (SQL)

� Tracks current version in changelog table

� Generates do & undo scripts

Database Migration

Deploying PHP applications with Phing – 24 / 37

� Numbered delta file (1-create-post.sql)

� Apply & undo statements

--//

CREATE TABLE ‘post‘ (‘title‘ VARCHAR(255),‘time_created‘ DATETIME,‘content‘ MEDIUMTEXT

);

--//@UNDO

DROP TABLE ‘post‘;

--//

Database Migration

Deploying PHP applications with Phing – 25 / 37

<target name="migrate"><dbdeploy

url="sqlite:test.db"dir="deltas"outputfile="deploy.sql"undooutputfile="undo.sql"/>

<pdosqlexecsrc="deploy.sql"url="sqlite:test.db"/>

</target>

Packaging

Deploying PHP applications with Phing – 26 / 37

� Create complete PEAR packages

<pearpkg name="demo" dir="."><fileset refid="files"/>

<option name="outputdirectory" value="./build"/><option name="description">Test package</option><option name="version" value="0.1.0"/><option name="state" value="beta"/>

<mapping name="maintainers"><element>

<element key="handle" value="test"/><element key="name" value="Test"/><element key="email" value="test@test.nl"/><element key="role" value="lead"/>

</element></mapping>

</pearpkg>

Packaging

Deploying PHP applications with Phing – 27 / 37

� Then build a TAR

<tar compression="gzip" destFile="package.tgz"basedir="build"/>

� ... or ZIP

<zip destfile="htmlfiles.zip"><fileset dir=".">

<include name="**/*.html"/></fileset>

</zip>

Deployment

Deploying PHP applications with Phing – 28 / 37

� SSH

<scp username="john" password="smith"host="webserver" todir="/www/htdocs/project/"><fileset dir="test">

<include name="*.html"/></fileset>

</scp>

� FTP

<ftpdeployhost="server01"username="john"password="smit"dir="/var/www"><fileset dir=".">

<include name="*.html"/></fileset>

</ftpdeploy>

Extending Phing

Deploying PHP applications with Phing – 29 / 37

Extending Phing

Deploying PHP applications with Phing – 30 / 37

� Numerous extension points

� Tasks

� Types

� Selectors

� Filters

� Mappers

� Loggers

� ...

Sample Task

Deploying PHP applications with Phing – 31 / 37

<?

class SampleTask extends Task{

private $var;

public function setVar($v){

$this->var = $v;}

public function main(){

$this->log("value: " . $this->var);}

}

Sample Task

Deploying PHP applications with Phing – 32 / 37

<project name="Example" default="default"><taskdef name="sample"

classpath="/dev/src"classname="tasks.my.SampleTask" />

<target name="default"><sample var="Hello World" />

</target></project>

Ad Hoc Extension

Deploying PHP applications with Phing – 33 / 37

� Define a task within your build file

<target name="main"><adhoc-task name="foo"><![CDATA[class FooTest extends Task {

private $bar;

function setBar($bar) {$this->bar = $bar;

}

function main() {$this->log("In FooTest: " . $this->bar);

}}]]></adhoc-task><foo bar="TEST"/>

</target>

Demo

Deploying PHP applications with Phing – 34 / 37

More Uses For Phing

Deploying PHP applications with Phing – 35 / 37

� Installations and upgrades

� Bootstrapping development environments

� Code analysis

� Version control (SVN / GIT)

� Code encryption / encoding

More Uses For Phing

Deploying PHP applications with Phing – 35 / 37

� Installations and upgrades

� Bootstrapping development environments

� Code analysis

� Version control (SVN / GIT)

� Code encryption / encoding

� Check the documentation!

The Future

Deploying PHP applications with Phing – 36 / 37

� Improvements

� Better performance

� Increased test coverage

� Cross-platform compatibility

� Pain-free installation of dependencies (PHAR?)

� More documentation

� IDE support

� Moving to GitHub

� We would love (more) contributions!

Questions?

Deploying PHP applications with Phing – 37 / 37

http://www.phing.info

http://joind.in/3590

#phing (freenode)

@phingofficial

Thank you!