Slide 1

Slide 1 text

Building and deploying PHP applications with Phing Michiel Rook PHP Benelux Meeting June 2012

Slide 2

Slide 2 text

About me Building and deploying PHP applications with Phing • Freelance PHP & Java contractor / consultant • PHP since ’99 • Phing project lead • http://www.linkedin.com/in/michieltcs • @michieltcs

Slide 3

Slide 3 text

This Talk Building and deploying PHP applications with Phing • Why use a build tool • What is Phing • Usage • Various examples • Extending Phing

Slide 4

Slide 4 text

Why Use A Build Tool?

Slide 5

Slide 5 text

Why Use A Build Tool Building and deploying PHP applications with Phing Repetition http://www.flickr.com/photos/andrewmalone/5162632817/

Slide 6

Slide 6 text

Repetition Building and deploying PHP applications with Phing • We are human • We get bored • We forget things • We make mistakes

Slide 7

Slide 7 text

Repetition Building and deploying PHP applications with Phing • Version control • (Unit) Testing • Configuring • Packaging • Uploading • DB changes • ...

Slide 8

Slide 8 text

Repetition Building and deploying PHP applications with Phing • Version control • (Unit) Testing • Configuring • Packaging • Uploading • DB changes • ... • Boring!

Slide 9

Slide 9 text

Why Use A Build Tool Building and deploying PHP applications with Phing Automate! http://www.flickr.com/photos/patrick_h/6209981673/

Slide 10

Slide 10 text

Automate! Building and deploying PHP applications with Phing • Developers, testers, administrators... • Easier handover to new team members • Improves quality • Reduces errors • Saves time • Consolidate scripts, reduce technical debt

Slide 11

Slide 11 text

What Is Phing Building and deploying PHP applications with Phing http://www.flickr.com/photos/canucksfan604/5471322484/

Slide 12

Slide 12 text

What Is Phing Building and deploying PHP applications with Phing PHING IS AWESOME

Slide 13

Slide 13 text

What Is Phing Building and deploying PHP applications with Phing • 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 • 2004: my first commit • 2009: lead

Slide 14

Slide 14 text

What Can Phing Do Building and deploying PHP applications with Phing • Scripting using XML build files • Human readable • Mostly cross-platform • Minimal dependencies • Interface to various popular (PHP) tools

Slide 15

Slide 15 text

What Can Phing Do Building and deploying PHP applications with Phing • Scripting using XML build files • Human readable • Mostly cross-platform • Minimal dependencies • Interface to various popular (PHP) tools • ”Good glue”

Slide 16

Slide 16 text

What Can Phing Do Building and deploying PHP applications with Phing

Slide 17

Slide 17 text

Why Use Phing Building and deploying PHP applications with Phing • Ant? • 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

Slide 18

Slide 18 text

The Basics

Slide 19

Slide 19 text

Installing Phing Building and deploying PHP applications with Phing • PEAR installation $ pear channel-discover pear.phing.info $ pear install [--alldeps] phing/phing • Optionally, install the documentation package $ pear install phing/phingdocs • Or... • http://packagist.org/packages/phing/phing • http://www.phing.info/get/phing-latest.phar

Slide 20

Slide 20 text

Build Files Building and deploying PHP applications with Phing • 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

Slide 21

Slide 21 text

Example Build File Building and deploying PHP applications with Phing Hello World! Buildfile: /home/michiel/phing/simple.xml Example > hello: [echo] Hello Example > world: [echo] World! BUILD FINISHED

Slide 22

Slide 22 text

Properties Building and deploying PHP applications with Phing • Simple key-value files (.ini) ## build.properties version=1.0 • Can be expanded by using ${key} in the build file $ phing -propertyfile build.properties ... ${version}

Slide 23

Slide 23 text

Filesets Building and deploying PHP applications with Phing • Constructs a group of files to process • Supported by most tasks • References: define once, use many

Slide 24

Slide 24 text

Filesets Building and deploying PHP applications with Phing • Selectors allow fine-grained matching on certain attributes • contains, date, file name & size, ...

Slide 25

Slide 25 text

Mappers & Filters Building and deploying PHP applications with Phing • Transform files during copy/move/... • Mappers • Change filename • Flatten directories • Filters • Strip comments, white space • Replace values • Perform XSLT transformation • Translation (i18n)

Slide 26

Slide 26 text

Mappers & Filters Building and deploying PHP applications with Phing

Slide 27

Slide 27 text

Examples

Slide 28

Slide 28 text

Examples Building and deploying PHP applications with Phing • Version control • Unit testing • Packaging • Deployment • Database migration • Continuous integration

Slide 29

Slide 29 text

Version Control Building and deploying PHP applications with Phing • (CVS), SVN, Git Last revision: ${lastrev}

Slide 30

Slide 30 text

PHPUnit Building and deploying PHP applications with Phing • Built-in support for most configuration options • Gathers code coverage information • Various output formats (JUnit / Clover) • Reporting (JUnit style)

Slide 31

Slide 31 text

PHPUnit Example Building and deploying PHP applications with Phing • Stop the build when a test fails Buildfile: /home/michiel/phpunit/build.xml Demo > test: [phpunit] Total tests run: 1, Failures: 1, Errors: 0, Incomplete: 0, Skipped: 0, Time elapsed: 0.00591 s Execution of target "test" failed for the following reason: /home/michiel/phpunit/build.xml:3:44: Test FAILURE (testSayHello in class HelloWorldTest): Failed asserting that two strings are equal.

Slide 32

Slide 32 text

PHPUnit Example Building and deploying PHP applications with Phing • Determine which files to include in the coverage report • Gather code coverage and other data during the test run

Slide 33

Slide 33 text

PHPUnit Example Building and deploying PHP applications with Phing • Generate some reports

Slide 34

Slide 34 text

Documentation Building and deploying PHP applications with Phing • Phing currently integrates with popular documentation tools • PhpDocumentor 2 (previously DocBlox) • PhpDocumentor 1 • ApiGen • Also supports r(e)ST (reStructuredText)

Slide 35

Slide 35 text

Packaging Building and deploying PHP applications with Phing • Create bundles or packages • Phing supports most popular formats: tar (pear), zip, phar Test package

Slide 36

Slide 36 text

Packaging - TAR / ZIP Building and deploying PHP applications with Phing

Slide 37

Slide 37 text

Packaging - PHAR Building and deploying PHP applications with Phing

Slide 38

Slide 38 text

Putting it all together - deployments

Slide 39

Slide 39 text

Copying to a server Building and deploying PHP applications with Phing • SSH • FTP

Slide 40

Slide 40 text

Symbolic links Building and deploying PHP applications with Phing • All releases stored in ”backup” directory • Symlink application directory to latest release (similar to Capistrano) • Allows for easy (code) rollbacks • Also on a remote server

Slide 41

Slide 41 text

Multiple servers / targets Building and deploying PHP applications with Phing • Several deployment targets: testing, staging, production, ... • Keep one property file per target • Select property file based on input Enter environment name

Slide 42

Slide 42 text

Database Migration Building and deploying PHP applications with Phing • Set of delta SQL files (1-create-post.sql) • Tracks current version of your db in changelog table • Generates do and undo SQL files CREATE TABLE changelog ( change_number BIGINT NOT NULL, delta_set VARCHAR(10) NOT NULL, start_dt TIMESTAMP NOT NULL, complete_dt TIMESTAMP NULL, applied_by VARCHAR(100) NOT NULL, description VARCHAR(500) NOT NULL )

Slide 43

Slide 43 text

Database Migration Building and deploying PHP applications with Phing • Delta scripts with do (up) & undo (down) parts --// CREATE TABLE ‘post‘ ( ‘title‘ VARCHAR(255), ‘time_created‘ DATETIME, ‘content‘ MEDIUMTEXT ); --//@UNDO DROP TABLE ‘post‘; --//

Slide 44

Slide 44 text

Database Migration Building and deploying PHP applications with Phing Buildfile: /home/michiel/dbdeploy/build.xml Demo > migrate: [dbdeploy] Getting applied changed numbers from DB: mysql:host=localhost;dbname=demo [dbdeploy] Current db revision: 0 [dbdeploy] Checkall: [pdosqlexec] Executing file: /home/michiel/dbdeploy/deploy.sql [pdosqlexec] 3 of 3 SQL statements executed successfully BUILD FINISHED

Slide 45

Slide 45 text

Database Migration Building and deploying PHP applications with Phing -- Fragment begins: 1 -- INSERT INTO changelog (change_number, delta_set, start_dt, applied_by, description) VALUES (1, ’Main’, NOW(), ’dbdeploy’, ’1-create_initial_schema.sql’); --// CREATE TABLE ‘post‘ ( ‘title‘ VARCHAR(255), ‘time_created‘ DATETIME, ‘content‘ MEDIUMTEXT ); UPDATE changelog SET complete_dt = NOW() WHERE change_number = 1 AND delta_set = ’Main’; -- Fragment ends: 1 --

Slide 46

Slide 46 text

Database Migration Building and deploying PHP applications with Phing -- Fragment begins: 1 -- DROP TABLE ‘post‘; --// DELETE FROM changelog WHERE change_number = 1 AND delta_set = ’Main’; -- Fragment ends: 1 --

Slide 47

Slide 47 text

Phing & Jenkins Building and deploying PHP applications with Phing • Continuous integration • Phing plugin • Build periodically or after each commit • Verify and test the build • Deploy results

Slide 48

Slide 48 text

Phing & Jenkins Building and deploying PHP applications with Phing

Slide 49

Slide 49 text

Phing & Jenkins Building and deploying PHP applications with Phing

Slide 50

Slide 50 text

Phing & Jenkins Building and deploying PHP applications with Phing

Slide 51

Slide 51 text

Demonstration

Slide 52

Slide 52 text

Extending Phing

Slide 53

Slide 53 text

Extending Phing Building and deploying PHP applications with Phing • Numerous extension points • Tasks • Types • Selectors • Filters • Mappers • Loggers • ...

Slide 54

Slide 54 text

Sample Task Building and deploying PHP applications with Phing • Extends from Task • Contains main() method and optionally init() • Setter method for each attribute in the build file class SampleTask extends Task { private $var; public function setVar($v) { $this->var = $v; } public function main() { $this->log("value: " . $this->var); } }

Slide 55

Slide 55 text

Sample Task Building and deploying PHP applications with Phing • Use taskdef to make Phing aware of your new task

Slide 56

Slide 56 text

Ad Hoc Extension Building and deploying PHP applications with Phing • Define a task within your build file bar = $bar; } function main() { $this->log("In FooTest: " . $this->bar); } } ]]>

Slide 57

Slide 57 text

Future Improvements Building and deploying PHP applications with Phing • More tasks & support • Better performance • Improved PHAR • More documentation • Increased test coverage • IDE support

Slide 58

Slide 58 text

Future Improvements Building and deploying PHP applications with Phing • More tasks & support • Better performance • Improved PHAR • More documentation • Increased test coverage • IDE support • Pull requests! :-)

Slide 59

Slide 59 text

Helpful Links Building and deploying PHP applications with Phing • http://pear.php.net/ • http://www.phpdoc.org/ • http://www.dbdeploy.com/ • http://www.jenkins-ci.org/ • http://www.phing.info/docs/guide/stable/ • http://github.com/phingofficial/phing

Slide 60

Slide 60 text

Questions? Building and deploying PHP applications with Phing https://joind.in/6695 http://www.phing.info #phing (freenode) @phingofficial Thank you!