Putting Phing to Work for You

41
Putting Phing to Work for You Hans Lellelid International PHP Conference 2007-11-05

description

Slides from Phing workshop from 2007 International PHP Conference in Frankfurt.

Transcript of Putting Phing to Work for You

Page 1: Putting Phing to Work for You

Putting Phing to Work for You

Hans LellelidInternational PHP Conference

2007-11-05

Page 2: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 2

Introduction• My name is Hans Lellelid• Developer & Manager at Applied Security,

Inc. (near Washington DC).• PHP developer and OO evangelist.• I ported Phing to PHP5 in 2004.• I now manage the Phing project with

Michiel Rook.

Page 3: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 3

What is it?• PHing Is Not Gnumake• It is a project build tool.• Original PHP4 version by Andreas Aderhold• Written for PHP5• Based on Apache Ant• Cross-platform (i.e. Windows too)• http://phing.info/

Page 4: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 4

Workshop Topics: Fundamentals• Phing basics• Creating a first build file• Transforming directories and files• Performing PDO tasks• Gathering user input• Building phpdoc• Running unit tests• Integrating with CI tools

Page 5: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 5

Workshop Topics: Extension• Using PHP code in a build file• Building a simple Task• Writing more complex Task• Creating a shared Type• Custom filters• Custom selectors• Custom logger/listener• Custom input handlers

Page 6: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 6

Getting Started

Page 7: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 7

Install Phing• Install via PEAR

– pear channel-discover pear.phing.info– pear install phing/phing

• Install “traditional” package– Available on Download page

• Install from SVN– svn co http://svn.phing.info/trunk phing

Page 8: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 8

Following Along• Install via PEAR

– pear install phing/phing– Install “traditional” package

• Available on Download page– Install from SVN

• svn co http://svn.phing.info/trunk phing

Page 9: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 9

The phing script• A wrapper shell script for phing.php script.• Sets default logger to use (dependent on

system)• Typical usage: phing [target]• Other useful options:

– Help (-h)– Specify properties (-Dpropname=value)– List targets (-l)– Get more output (-verbose or -debug)

Page 10: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 10

Buildfiles• Build files are XML• Build files are composed of:

– Tasks: a “build-in” piece of code that a specific function. E.g. <mkdir>

– Types: data structures that are used commonly by tasks. E.g. <path>

– Targets: grouping of Tasks that perform a more general function. E.g. Copy files to a new directory.

– A Project: the root node for the build file.

Page 11: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 11

A simple build file<project name="sample" default="main">

<property name="ver" value="1.0.1"/><property file="build.properties"/><target name="main">

<mkdir dir="./build/${ver}"><copy todir="./build/${ver}">

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

</copy></target>

</project>

Page 12: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 12

Javaisms• Properties

– Properties are variables for build scripts.– Like php.ini, but more flexible:

• tgz = ${pkg}-${ver}.tgz

– Can be set in build script or imported from files.

• Dot-path notation for class names:– path.to.Class = path/to/Class.php– Represents directory and class name in a

single string.

Page 13: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 13

Control Structures• depends, if, and unless attributes provide

control over target execution condition and sequence.

• <if> task provides a more familiar (to developers) control structure that can be used within a target.

Page 14: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 14

Reorganize, Transform

Page 15: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 15

Match a bunch of files• The <fileset> type represents an

extremely powerful way to select a group of files for processing

• Many built-in tasks support <fileset>

Page 16: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 16

Fileset Examples<fileset dir="./webapp"includes="**/*.html"excludes="**/test-*"/>

<fileset dir="./webapp"><include name="img/${theme}/*.jpg"/><include name="tpl/${lang}/*.phtml"/><exclude name="**/*.bak"/><exclude name="**/test/**"/>

</fileset>

Page 17: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 17

Fine-tuned Selection• Selectors provide entirely new dimensions

for <fileset> file matching criteria.• Some examples of selectors:

– Created before/after certain date– Greater/less than specified size– Type ('file' or 'dir')– At specific depth in dir structure– Having corresponding file in another dir.

Page 18: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 18

Selector examples<fileset dir="${htdocs.dir}">

<includes name=”**/*.html”/><containsregexp

expression="/prod\d+\.php"/></fileset><fileset dir="${dist}" includes="**"> <or> <present targetdir="${htdocs}"/> <date datetime="01/01/2007"

when="before"/> </or></fileset>

Page 19: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 19

Filesystem Transformations• The <mapper> element adds filesystem

transformation capabilities to supporting tasks (e.g. <copy>, <move>).

• For example:– Change all “.php” files to “.html”– Remove dirs from filename– Change all files to the same filename

• Custom mappers can be defined.

Page 20: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 20

Mapper examples<copy todir="/tmp">

<mapper type="glob" from="*.php" to="*.php.bak" />

<fileset dir="./app" includes="**/*.php" />

</copy><copy todir="${deploy.dir}">

<mapper type="regexp"from="^(.*)-(.*)\.conf\.xml" to="\1.\2.php"/>

<fileset dir="${config.src.dir}" includes="**/*.conf.xml" />

</copy>

Page 21: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 21

Data Transformation• The <filterchain> type adds data

filtering/transforming capabilities to supporting tasks.

• Tasks that support <filterchain> include <copy>, <move>, <append> + more

• For example:– Strip comments from files– Replace values in files (+ regexp)– Perform XSLT transformation

• Easily add your own.

Page 22: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 22

Filtering examples<copy todir="${build}/htdocs">

<fileset includes="*.html"/><filterchain>

<replaceregexp><regexp pattern="\r\n"

replace="\n"/></replaceregexp><tidyfilter encoding="utf8">

<config name="indent" value="true"/>

</tidyfilter></filterchain>

</copy>

Page 23: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 23

Other Tasks

Page 24: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 24

PDO Task• <pdo> task allows you to execute SQL

statements from– The buildfile itself– One or more files

• Transactions can be explicitly demarcated.• Output can be formatted with a provided or

custom formatter.

Page 25: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 25

User Input• The <propertyprompt> task provides an

basic, easy way to set a value from user input.

• The <input> task provides a more feature rich version.– Special handling of yes/no, multiple choice– Support for input validation

Page 26: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 26

Build API Docs• The <phpdoc> task provides a wrapper for

the PhpDocumentor application.• A few advantages over standalone phpdoc:

– Use properties from build script– Use Phing features like filesets

Page 27: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 27

Unit Testing• Phing has extensive support for PHPUnit

and SimpleTest unit testing frameworks• PHPUnit tasks provide support for

– Batch testing using <fileset> to select all the tests you wish to run.

– Output in XML (Junit-compatible) and Plain text.

– Report generator creates XHTML reports using XSLT

– Code coverage reports (requires Xdebug)

Page 28: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 28

Continuous Integration• Code -> Commit -> Build -> Test ->

Report• CI tools watch the repository and provide

automated building, testing, reporting.• CruiseControl is probably the most well-

known. • Xinc provides CI tool functionality written in

PHP.• Xinc is built to use Phing• CruiseControl also supports Phing

Page 29: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 29

Extending Phing

Page 30: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 30

Paths for Extension• Embedding PHP in build file.• Write your own class to provide any of the

functionality we have seen:– Task– Type– Selector– Filter– Mapper– Listener (logger)– Input Handler

Page 31: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 31

Embedding PHP• The <php> task allows you to evaluate a

PHP expression, function call, etc. and store result in a property.

• The <adhoc> task allows you to embed PHP directly. Useful for including setup files.

<adhoc><![CDATA[require_once 'propel/Propel.php';Propel::init('bookstore-conf.php');

]]></adhoc>

Page 32: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 32

Writing a Task• Extend Task• Add setter methods for any params your

task accepts.• Implement public function main()• Abstract subclasses exist to make life

easier (e.g. MatchingTask)

Page 33: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 33

Sample Taskclass SampleTask extends Task {

private $var;public function setVar($v) {

$this->var = $v;}public function main() {

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

}

Page 34: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 34

More Complex Task• Adding support for CDATA text.• Adding support for Fileset, Filelist,

Filterchain child elements.• Supporting nested arbitrary classes ...

Page 35: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 35

Data Types• Classes that can be shared by different

tasks.• Extend DataType• Add setter methods for any params your

data type accepts/expects.

Page 36: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 36

Filters• Extend BaseFilterReader or

BaseParamFilterReader and implement ChainableReader.

• Implement read() and chain() methods.– Read stream and return -1 when stream is

exhausted.

• If using params, you must initialize them locally.

• Use <filterreader classname=”YourClass”> in your build file.

Page 37: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 37

Selectors• Extend BaseExtendSelector• Implement isSelected()• Use <custom> tag in your build file.

Page 38: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 38

Mappers• Implement FileNameMapper• Implement main(filename), setFrom(str),

setTo(str)• Use <mapper classname=”...”> in your

build file.

Page 39: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 39

Build Listeners• Implement BuildListener (or

BuildLogger which expects streams)• Register on the commandline using

-listener (or -logger) option.

Page 40: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 40

Input Handlers• Implement InputHandler interface

– Implement the handleInput(InputRequest) method.

• Register on the commandline using -listener (or -inputhandler) option.

Page 41: Putting Phing to Work for You

Hans Lellelid: Putting Phing to Work for You 41

Where next?• Visit http://phing.info for downloads,

documentation, and issue tracking.• Ask questions on the mailing lists.

[email protected][email protected]