Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building and Deploying PHP Apps Using phing

2,247 views

Published on

The focus of the presentation is on organizing your PHP app build process, employing continuous testing, JS testing, automatic documentation, software metrics and other tools. The end result is expected to be a more stable, reliable, documented and healthy code base.

Published in: Software
  • Be the first to comment

Building and Deploying PHP Apps Using phing

  1. 1. HOW TO DEPLOY PHP APPLICATIONS SAFELY, EFFICIENTLY, AND FREQUENTLY WITHOUT LOOSING YOUR SANITY (COMPLETELY) Created by Mihail Irintchev Head of Software Development SiteGround.com Web Hosting Co. /m.irintchev@siteground.com @irintchev
  2. 2. ABOUT ME Name: Mihail Irintchev (Mike) From Sofia, Bulgaria Web developer since 2003 Work @ SiteGround Organizing @bgphp UG Love beer
  3. 3. MY USUAL ACTIVITIES
  4. 4. MY CAR
  5. 5. MY OFFICE
  6. 6. WHAT IS YOUR DEPLOYMENT PROCESS LIKE?
  7. 7. OVERVIEW OF BUILD ESSENTIALS: Revision control system Continuous testing Continuous integration Staging environment Automatic documentation Code health metrics Deployment mechanism ... and you want all that plus more wrapped in ... AN AUTOMATED BUILD PROCEDURE!
  8. 8. REVISION CONTROL SYSTEMS
  9. 9. C'MON, IT'S 2014 !!!
  10. 10. SOME LESS COMMON ONES
  11. 11. BASIC DISTINCTION BETWEEN VERSIONING SYSTEMS Local data model Distributed model SVN GIT CVS Bazaar darcs
  12. 12. BASIC DISTINCTION BETWEEN MODELS
  13. 13. WHAT CAN THE VERSION CONTROL DO FOR YOU? (APART FROM STORING AND VERSIONING YOUR CODE) pre-commit and post-commit hooks
  14. 14. WHAT IS PRE-COMMIT GOOD FOR? PREVENT PEOPLE FROM COMMITTING BAD CODE Run php lint (php -l) on the newly committed code for i in `$SVNLOOK changed -t "$TXN" "$REPOS" | $AWK '{print $2}'` do if [[ $i =~ ".php$" ]]; then # Run php lint to make sure no code with parse errors is committed CHECK=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHP -d html_errors=off -l || echo $i` RETURN=`echo $CHECK | $GREP "^No syntax" > /dev/null && echo TRUE || echo FALSE` if [ $RETURN = 'FALSE' ]; then echo $CHECK 1>&2; exit 1 fi fi done
  15. 15. ENFORCE CODE STANDARDS Run PHP_CodeSniffer (phpcs) Catch any serious violations (use levels) <?php echo "Hello world!"; #phpcs hello.php FILE: /home/madasha/tmp/hello.php -------------------------------------------------------------------------------- FOUND 1 ERROR(S) AFFECTING 1 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment -------------------------------------------------------------------------------- Time: 16 ms, Memory: 2.50Mb
  16. 16. PREVENT SOME FOOLISH MISTAKES ... like dumping DB user and pass on your home page ... or embarass yourself by leaving some other var_dump()/ print_r()/ var_export()behind
  17. 17. THE "GOLDEN" RULE
  18. 18. SO JUST ADD ANOTHER SECTION TO THE PRE-COMMIT HOOK if [[ $i =~ ".php$" ]]; then # ... # Grep for var_dump/var_export/print_r (the last two without second param passed) # to prevent code committed with debugging instructions to go into the repo CHECK=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $GREP -e "(var_dump[ t]*(|(var_export|print_r)[ t]*([^,)]+))" -m1 -c` RETURN=`echo $CHECK | $GREP "1" > /dev/null && echo TRUE || echo FALSE` if [ $RETURN = 'TRUE' ]; then echo "var_dump/print_r/var_export found in $i" 1>&2; exit 2 fi # ... fi
  19. 19. ANYTHING ELSE? YOUR IMAGINATION IS PRETTY MUCH THE BOUNDARY
  20. 20. WHAT IS POST-COMMIT GOOD FOR? Automated logging Notifications
  21. 21. THERE ARE OTHER HOOKS AS WELL SVN GIT pre-lock pre-unlock post-lock post-unlock start-commit pre-revprop-change post-revprop-change prepare-commit-msg post-merge pre-receive post-receive post-checkout pre-applypatch post-applypatch
  22. 22. THE BUILD SOFTWARE
  23. 23. WHAT DOES PHING STAND FOR? PHing Is Not GNU make “It's a PHP project build system or build tool based on . You can do anything with it that you could do with a traditional build system like GNU make...” Apache Ant
  24. 24. BUT WHY PHING?
  25. 25. SERIOUSLY, WHY PHING? Very flexible and robust build tool Simple XML build file Variety of built-in and optional tasks Very easy to write your own custom task Good and community Platform independent No required external dependencies docs
  26. 26. VERY EASY INSTALLATION Through PEAR: $ pear channel-discover pear.phing.info $ pear install [--alldeps] phing/phing Through Composer: { "require-dev": { "phing/phing": "2.*" } }
  27. 27. A SAMPLE XML BUILD FILE <?xml version="1.0" encoding="UTF-8"?> <project name="FooBar" default="dist"> <!-- ============================================ --> <!-- Target: prepare --> <!-- ============================================ --> <target name="prepare"> <echo msg="Making directory ./build" /> <mkdir dir="./build" /> </target> <!-- ============================================ --> <!-- Target: build --> <!-- ============================================ --> <target name="build" depends="prepare"> <echo msg="Copying files to build directory..." /> <echo msg="Copying ./about.php to ./build directory..." /> <copy file="./about.php" tofile="./build/about.php" /> <echo msg="Copying ./browsers.php to ./build directory..." /> <copy file="./browsers.php" tofile="./build/browsers.php" /> <echo msg="Copying ./contact.php to ./build directory..." /> <copy file="./contact.php" tofile="./build/contact.php" /> </target> <!-- ============================================ --> <!-- (DEFAULT) Target: dist -->
  28. 28. TASK #1: UPDATE FROM SVN <!-- ============================================================ --> <!-- Target: svn_update - updates from svn, prints last revision --> <!-- ============================================================ --> <target name="svn_update"> <svnupdate svnpath="/usr/bin/svn" nocache="true" username="${svn_user}" password="${svn_pass}" todir="${srcdir}" /> <svnlastrevision svnpath="/usr/bin/svn" workingcopy="${srcdir}" propertyname="svn.lastrevision" /> <echo msg="Updated ${srcdir} to revision ${svn.lastrevision}" /> </target>
  29. 29. TASK #2: UNIT-TESTS WHAT IS YOUR PHP UNIT-TESTING PREFERRED FRAMEWORK? By Sebastian Bergmann
  30. 30. TASK #2: RUN UNIT-TESTS IN PHING <!-- ===================================================================== --> <!-- Target: phpunit - a subtask that runs PHPUnit on phpunit tests/suits --> <!-- ===================================================================== --> <target name="phpunit"> <echo msg="Running PHPUnit tests after SVN update:" /> <phpunit haltonfailure="true" haltonerror="true" bootstrap="bootstrap.php"> <formatter type="plain" usefile="false" /> <batchtest> <fileset dir="tests"> <include name="*Test.php"/> </fileset> </batchtest> </phpunit> </target>
  31. 31. TASK #3: JS UNIT-TESTS Your client-side code can be just as crucial as the server-side one, so why not test it continuously as well? THE TOOLS?
  32. 32. TASK #3: RUN QUNIT TESTS IN PHING <path id="project.class.path"> <pathelement dir="./lib/" /> </path> <taskdef name="qunit" classname="QunitTask"> <classpath refid="project.class.path" /> </taskdef> <target name="qunit" description="JavaScript Unit Test"> <qunit executable="/usr/bin/phantomjs" haltonfailure="true" runner="lib/run-qunit.js"> <fileset dir="."> <include name="jstests/runner.htm" /> </fileset> </qunit> </target> The example above is using by Martin Jonsson Qunit Phing Task
  33. 33. TASK #4: AUTOMATIC DOCUMENTATION The tool: phpDocumentor 2 <phpdoc2 title="Gallery Documentation" destdir="docs" template="responsive-twig"> <fileset dir="./classes"> <include name="**/*.php" /> </fileset> </phpdoc2>
  34. 34. CODE METRICS TASKS THE TOOLS? phpmd phpcpd pdepend phpcs Guess what? There exist ready-made phing tasks for all of them!
  35. 35. TASKS #5: PHPMD Person behind: Manuel Pichler <!-- ===================================================================== --> <!-- Target: phpmd - a subtask that runs PHPMD on predefined dirs/files --> <!-- and generates reports in the desired format --> <!-- ===================================================================== --> <target name="phpmd"> <phpmd rulesets="cleancode,design,unusedcode"> <fileset dir="${srcdir}"> <include name="classes/**/*.php" /> </fileset> <formatter type="xml" outfile="${srcdir}/reports/pmd.xml"/> </phpmd> </target>
  36. 36. TASK #6: PHPCPD Person behind: Sebastian Bergmann <!-- ===================================================================== --> <!-- Target: phpcpd - a subtask that runs PHPCPD on predefined dirs/files --> <!-- and generates reports in the desired format --> <!-- ===================================================================== --> <target name="phpcpd"> <phpcpd> <fileset dir="${srcdir}"> <include name="classes/**/*.php" /> </fileset> <formatter type="pmd" outfile="${srcdir}/reports/pmd-cpd.xml"/> </phpcpd> </target>
  37. 37. TASK #7: PDEPEND Person behind: Manuel Pichler <!-- ===================================================================== --> <!-- Target: pdepend - a subtask that runs pdepend on predefined dirs/files--> <!-- and generates reports in the desired format --> <!-- ===================================================================== --> <target name="pdepend"> <phpdepend> <fileset dir="${srcdir}"> <include name="classes/**/*.php" /> </fileset> <logger type="jdepend-xml" outfile="${srcdir}/reports/pdepend.xml"/> <logger type="jdepend-chart" outfile="${srcdir}/reports/pchart.svg"/> <logger type="overview-pyramid" outfile="${srcdir}/reports/pyramid.svg"/> <analyzer type="coderank-mode" value="method"/> </phpdepend> </target>
  38. 38. WHAT ELSE CAN YOU USE PHINGFOR? Encrypting/obfuscating code before deployment Generating static assets Generate and deploy static sites with tools like sculpin Execute post-deploy scripts Pretty much anything useful around the build/deploy
  39. 39. TAKEAWAYS There are a lot of things you can automate in a build (strong emphasis on continuous testing & integration) There are a lot of tools out there that can help you do that phing is a very neat tool to organize your build process
  40. 40. CREDITS & REFERENCES "Quality Assurance for PHP Projects" by Michelangelo van Dam by Jordan Kasper"Browser Eyeballing != JavaScript Testing" The PHP Quality Assurance Toolchain
  41. 41. QUESTIONS?
  42. 42. THANK YOU! m.irintchev@siteground.com @irintchev

×