1. Lean PHP Practices
Alan Pinstein & Jason Ardell
Atlanta PHP Users Group
April 1, 2009
2. What is Leverage?
More work the first time, usually involves:
Learning a new library or tool
Setting up infrastructure
Easier to do it 2..∞
Easier to maintain
4. Project Leverage
It hurts when I...
Set up my project on a new machine
Deploy new versions of my project
Stop doing that!
5. Project Leverage - Tools
mp - Migrations for PHP (presentation)
rake/cap => pantr is php
gitflow
user_management
Dependency management
runit - daemons / process supervision
config-magic
6. runit
Process Supervisor
Don’t write daemons - you’ll do it wrong
Write scripts, and runit keeps them alive in the
background
Gracefully handles errors, failures, logging, upgrades
7. runit in action
// graceful upgrade
2010-02-10_03:53:55 Stop requested for worker process on queue: inbox
2010-02-10_03:53:55 Stopping worker process on queue: inbox
2010-02-10_03:53:56 Starting worker process on queue: inbox
// graceful error handling
2010-04-01_17:55:08 [Job: 383820 RUNNING] Process InboxImage 60629 (Carol 011_fi.jpg)
2010-04-01_17:55:08 [Job: 383820 COMPLETED]
2010-04-01_17:55:08 JQWorker doesn't have enough memory for next job (4194304).
2010-04-01_17:55:08 Starting worker process on queue: inbox
2010-04-01_17:55:09 [Job: 383821 RUNNING] Process InboxImage 60622 (Carol 015_fi.jpg)
2010-04-01_17:55:09 [Job: 383821 COMPLETED]
8. config-magic
N config files * M environments = Too Many Config Files
Config-Magic applies
“profiles”: dev-alan, dev-jason, staging, production
to
“templates”: framework, orm, rake, cap, shell scripts,
webserver
9. # config.ini
[templates]
httpd.configFileTemplate = ##TEMPLATES_DIR##/##CONFIG##.conf
httpd.configFile = ##OUTPUT_DIR##/##CONFIG##.conf
propel-conf.configFileTemplate = ##TEMPLATES_DIR##/virtualtour-conf.php
propel-conf.configFile = ##OUTPUT_DIR##/virtualtour-conf.php
propel-build-properties.configFileTemplate = ##TEMPLATES_DIR##/build.properties
propel-build-properties.configFile = ##OUTPUT_DIR##/../propel-build/
build.properties
propel-conf-xml.configFileTemplate = ##TEMPLATES_DIR##/runtime-conf.xml
propel-conf-xml.configFile = ##OUTPUT_DIR##/../propel-build/runtime-conf.xml
webapp.configFileTemplate = ##TEMPLATES_DIR##/##CONFIG##.conf
webapp.configFile = ##OUTPUT_DIR##/##CONFIG##.conf
sh.configFileTemplate = ##TEMPLATES_DIR##/##CONFIG##.conf
sh.configFile = ##OUTPUT_DIR##/##CONFIG##.conf
[data]
; your default data here. any settings here will be overridden by values in the profile's
ini file on a setting-by-setting basis
isProduction = false
path.psql = /usr/bin/psql
path.convert = /usr/bin/convert
path.propel-gen = externals/pear/propel-gen
path.phocoa = ##path.project.appDir##/externals/phocoa/phocoa
path.php.include_path = ##path.phocoa##:##path.project.appDir##:##path.project.appDir##/
classes:##path.project.appDir##/externals:##path.project.appDir##/externals/pear/php
path.php = "/usr/bin/php -d include_path=##path.php.include_path##"
app.group = showcase_web
app.user = tourbuzz
httpd.user = _www
13. Testing Leverage
It hurts when I...
Refactor
Introduce regression bugs
Don’t have confidence that my code works
Have to test my web pages in N browsers
14. Testing Leverage -Tools
Write Testable Code - Miško Hevery
This is the hard part - there are good tools for the rest
Writing Tests (PHPUnit)
Mocking Collaborators - $this->getMock(‘MyClass’)
Scenario-based Tests - @dataProvider
Bootstrapping Data (Fixturenator)
Selenium RC - SauceLabs Hosted Browsers
15. fixturenator
Test Data doesn’t always scale
Makes it easy to decouple tests from test data
Prototypal Test Data Generation
Inspired by factory_girl
18. Community Leverage
It hurts when I....
Try to share my PHP libraries with other people
Re-write code that I know already exists
Try to fix bugs in projects that aren’t my own
19. Community Leverage - Tools
Pearfarm (Gemcutter for PHP)
A Community PEAR Server
Trivially Easy to Make PEAR Packages
“App Store” Effect
GitHub
Social Coding Infrastructure
Easy to contribute / accept contributions
“App Store” Effect
21. PHP really needs...
CLI Framework (for building good tools)
Project Management (rake/cap)
PEAR/Dependency Management
Efficient PEAR install/upgrades
Non-PEAR code: php/lib/exec
Better Community
More Quality Contributions
More Cooperation
More Use of Modern Language Features