What Is It? – Why Should You Use It? – Installing It



What Is It? – Why Should You Use It? – Installing It

0 0


presentation-composer


On Github underparnv / presentation-composer

Reno php

Introduction to Composer

David Mosher / @arenowebdev

What Is It?

"[A] tool for dependency management in PHP"
"NOT a package manager"

Cross-platform compatible requiring only > PHP version 5.3.2

Why Should You Use It?

  • Ruby (Bundler), NodeJS (npm)
  • Working with zip files sucks and increases risk
  • Opens you up to the most annoying type of security issue…

Ones that have already been patched!

  • Decreases development time
  • Easy access to libraries leads to more people re-using existing code
  • Automatic dependency management encourages developers to publish new packages while relying on old (DRY)

You specify what your project needs are in writing

Installing It

Locally (only within your project)
$ curl -sS http://getcomposer.org/installer | php
Globally (system wide - recommended)
$ sudo mv composer.phar /usr/local/bin/composer
  • I'm only going to cover installing it on non-windows machines.

Using It

The Basics

$ composer --version
$ composer
Warning: This development build of composer is over 30 days old. It is
recommended to update it by running "composer self-update" to get the
latest version.
$ composer self-update

composer.json file

{
    "name": "My Simple Laravel Project",
    "require": {
        "laravel/laravel": "v4.0.0"
    },
    "authors": [
        {
            "name": "David Mosher",
            "email": "dmosher@noip.com"
        }
    ]
}
$ composer install

Finding Packages

Packagist.org

You are able to utilize your own packages hosted in version control (both private and public!)
{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://dmosher@bitbucket.org/dmosher/presentation.git"
        }
    ]
}

Version Constraints

  • Exact Version
    1.0.3
  • Range
    >=1.0
    >=1.2,<2.0
  • Wildcard
    1.0.*
  • Next Significant Release
    ~1.2
    ~1.2.3

Platform (Virtual) Packages

  • PHP VersionAllows you to apply constraints on the PHP version
    "php": ">=5.3.2"
  • PHP ExtensionAllows requiring of PHP extensions
    "ext-gd": "*"
  • PHP LibraryAllows version constraints of PHP libraries
    "lib-curl": ">=7.3"

Fruits of Our Labor

Autoloading

"Besides downloading the libraries outlined in your composer.json file, Composer also prepares an autoload file that's capable of autoloading all of those classes in any of the libraries it downloaded."
<?php
require 'vendor/autoload.php';

You now have full access to any of the namespaced classes!

PSR-0

"autoload": {
    "psr-0": { "Mosher\\Presentation\\": "src/" }
}

Classmap

"autoload": {
    "classmap": [ "lib/", "ext/library.php" ]
}

Directories / Individual Files

"autoload": {
    "files": [ "ext/functions.php" ]
}

Thank you!

David Mosher / @arenowebdev