deploying php applications with phing

39
Deploying PHP applications with Phing – 1 / 37 Deploying PHP applications with Phing Michiel Rook PHPNW11 - October 8th, 2011

Upload: michiel-rook

Post on 08-May-2015

15.690 views

Category:

Technology


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

Page 1: Deploying PHP applications with Phing

Deploying PHP applications with Phing – 1 / 37

Deploying PHP applications with Phing

Michiel Rook

PHPNW11 - October 8th, 2011

Page 2: Deploying PHP applications with Phing

About me

Deploying PHP applications with Phing – 2 / 37

� Freelance PHP/Java consultant

� Phing project lead

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

� @michieltcs

Page 3: Deploying PHP applications with Phing

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

Page 4: Deploying PHP applications with Phing

Features

Deploying PHP applications with Phing – 4 / 37

� Scripting using XML build files

� Mostly cross-platform

� Interface to various popular (PHP) tools

Page 5: Deploying PHP applications with Phing

Features

Deploying PHP applications with Phing – 5 / 37

Page 6: Deploying PHP applications with Phing

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

Page 7: Deploying PHP applications with Phing

Why Use A Build Tool?

Deploying PHP applications with Phing – 7 / 37

Page 8: Deploying PHP applications with Phing

Why Use A Build Tool

Deploying PHP applications with Phing – 8 / 37

� Repetitive tasks

� Version control

� (Unit) Testing

� Configuring

� Packaging

� Uploading

� DB changes

� ...

Page 9: Deploying PHP applications with Phing

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

Page 10: Deploying PHP applications with Phing

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

Page 11: Deploying PHP applications with Phing

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

Page 12: Deploying PHP applications with Phing

The Basics

Deploying PHP applications with Phing – 11 / 37

Page 13: Deploying PHP applications with Phing

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

Page 14: Deploying PHP applications with Phing

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>

Page 15: Deploying PHP applications with Phing

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>

Page 16: Deploying PHP applications with Phing

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"/>

Page 17: Deploying PHP applications with Phing

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>

Page 18: Deploying PHP applications with Phing

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)

Page 19: Deploying PHP applications with Phing

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>

Page 20: Deploying PHP applications with Phing

Practical Examples

Deploying PHP applications with Phing – 19 / 37

Page 21: Deploying PHP applications with Phing

Testing

Deploying PHP applications with Phing – 20 / 37

� Built-in support for PHPUnit / SimpleTest

� Code coverage through XDebug

� Various output formats

Page 22: Deploying PHP applications with Phing

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>

Page 23: Deploying PHP applications with Phing

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>

Page 24: Deploying PHP applications with Phing

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

Page 25: Deploying PHP applications with Phing

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‘;

--//

Page 26: Deploying PHP applications with Phing

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>

Page 27: Deploying PHP applications with Phing

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="[email protected]"/><element key="role" value="lead"/>

</element></mapping>

</pearpkg>

Page 28: Deploying PHP applications with Phing

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>

Page 29: Deploying PHP applications with Phing

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>

Page 30: Deploying PHP applications with Phing

Extending Phing

Deploying PHP applications with Phing – 29 / 37

Page 31: Deploying PHP applications with Phing

Extending Phing

Deploying PHP applications with Phing – 30 / 37

� Numerous extension points

� Tasks

� Types

� Selectors

� Filters

� Mappers

� Loggers

� ...

Page 32: Deploying PHP applications with Phing

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);}

}

Page 33: Deploying PHP applications with Phing

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>

Page 34: Deploying PHP applications with Phing

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>

Page 35: Deploying PHP applications with Phing

Demo

Deploying PHP applications with Phing – 34 / 37

Page 36: Deploying PHP applications with Phing

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

Page 37: Deploying PHP applications with Phing

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!

Page 38: Deploying PHP applications with Phing

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!

Page 39: Deploying PHP applications with Phing

Questions?

Deploying PHP applications with Phing – 37 / 37

http://www.phing.info

http://joind.in/3590

#phing (freenode)

@phingofficial

Thank you!