Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bootstrapping Your Own PHP App with Composer

Bootstrapping Your Own PHP App with Composer

Presented at Singapore PHP Meetup on 25 March 2014.

Michael Cheng

March 25, 2014
Tweet

More Decks by Michael Cheng

Other Decks in Programming

Transcript

  1. BOOTSTRAPPING YOUR OWN
    PHP APP WITH COMPOSER
    25 March 2014

    View Slide

  2. WHY?
    • Other frameworks seems too bloated for your
    project.

    • Need more visibility/transparency on how the app
    is put together.

    • Don’t like the default libraries provided by the
    other frameworks & no easy way to

    circumvent them.

    View Slide

  3. COMPOSER
    • Pick and choose libraries you want to put together
    on Packagist (https://packagist.org/).

    • Compose your own composer.json.

    • Choose a package namespace for your own
    project.

    • Reference: https://getcomposer.org/

    View Slide

  4. SAMPLE PROJECT
    • Requirement:

    • REST Server

    • MySQL backend

    • Logging.

    !
    !
    • Consideration:

    • No need for any
    views.

    • Simple CRUD app.

    View Slide

  5. 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)

    View Slide

  6. 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"!
    }!
    }

    View Slide

  7. 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%

    View Slide

  8. UNIT TESTS
    • Tests folder

    • bootstrap.php - Setup and server side includes.

    • phpunit.xml - Config for running PHPUnit.

    • *Test.php - The tests files.

    View Slide

  9. TEST SPEC
    • API Consumer should be able to:

    • Get list of blog posts.

    • Create a new blog post.

    • Delete the blog post he created.

    View Slide

  10. THE IMPLEMENTATION
    • TDD: Red, Green, Refactor.

    • Use your own namespace to take advantage of
    Composer’s autoload mechanism.

    • Don’t get ahead of yourself.

    View Slide

  11. WALK-THRU
    https://github.com/
    sgphpug/sampleblog

    View Slide