Presented at Singapore PHP Meetup on 25 March 2014.
BOOTSTRAPPING YOUR OWNPHP APP WITH COMPOSER25 March 2014
View Slide
WHY?• Other frameworks seems too bloated for yourproject. • Need more visibility/transparency on how the appis put together. • Don’t like the default libraries provided by theother frameworks & no easy way to circumvent them.
COMPOSER• Pick and choose libraries you want to put togetheron Packagist (https://packagist.org/). • Compose your own composer.json. • Choose a package namespace for your ownproject. • Reference: https://getcomposer.org/
SAMPLE PROJECT• Requirement: • REST Server • MySQL backend • Logging. !!• Consideration: • No need for anyviews. • Simple CRUD app.
SAMPLE PROJECT• Libraries • Respect/rest - (https://packagist.org/packages/respect/rest) • Monolog - (https://packagist.org/packages/monolog/monolog) • PHP-ActiveRecord - (https://packagist.org/packages/php-activerecord/php-activerecord)
COMPOSER.JSON{!"require":{!"respect/rest": "0.5.0",!"monolog/monolog": "1.7.0",!"php-activerecord/php-activerecord": "v1.1.2"!},!"require-dev": {!"phpunit/phpunit": "4.0.*",!"guzzle/guzzle": "v3.8.1"!}!}
INSTALL LIBRARIES$ composer install!!Loading composer repositories with package information!Installing dependencies (including require-dev)!- Installing respect/rest (0.5.0)!Downloading: 100%!!- Installing psr/log (1.0.0)!Downloading: 100%!!- Installing monolog/monolog (1.7.0)!Downloading: 100%!!- Installing php-activerecord/php-activerecord (v1.1.2)!Downloading: 100%
UNIT TESTS• Tests folder • bootstrap.php - Setup and server side includes. • phpunit.xml - Config for running PHPUnit. • *Test.php - The tests files.
TEST SPEC• API Consumer should be able to: • Get list of blog posts. • Create a new blog post. • Delete the blog post he created.
THE IMPLEMENTATION• TDD: Red, Green, Refactor. • Use your own namespace to take advantage ofComposer’s autoload mechanism. • Don’t get ahead of yourself.
WALK-THRUhttps://github.com/sgphpug/sampleblog