composer namespacing

20
The wonderful world of Composer

Upload: deepak-chandani

Post on 11-Apr-2017

76 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Composer namespacing

The wonderful world of Composer

Page 2: Composer namespacing

Transforming from Older PHP to Modern PHP

PHP community: Huge but extremely isolated libraries, we do not reuse code ,Do we ?.

Issues faced while using shared code: How do I autoload the PHP classes in the code , without using require or include

statements? Which version of file to include according to my installed PHP version ?

We need to know if this library depends on any other libraries, if YES then that’s yet another library I need to download & configure

How should I store the library in my project? Should I use Git submodules? Just commit the entire lib code into my project?

Composer sets out to solve this situation by positioning itself as "the glue between all projects" - meaning that packages can be written, developed and shared in a format that other developers can plug into other applications with ease.

Page 3: Composer namespacing

ContentsIntroduction & Basic Usage1. Composer Introduction

2. Installing

3. Basic Usage & commands

4. Packagist Repository

5. Specifying package versions

Namespacing6. Namespacing Introduction

7. Using the namespaced class

Autoloading you own code 8. Using classmap

9. Using PSR-0 standard

10. Using PSR-4 standard

11. PSR-0 vs PSR-4

Final Summary : Basic Commands & composer.json schema

Page 4: Composer namespacing

Introduction

Composer is a dependency manager tool for PHP.Like npm in Node Or bundler in Ruby.

Suppose:• You have a project that depends on a number of libraries.

• Some of those libraries depend on other libraries.

Composer:• Enables you to declare the libraries you depend on (inside config file).

• Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).

Page 5: Composer namespacing

A Dependency Manager lets you:

1. Define dependencies in a version controlled config file.

2. Download & Install them all in one command

3. Ensures that identical versions are installed in all project environments .

4. Automate this part of your build process.

Action time

Installation

Page 6: Composer namespacing

Basic Usage: Require dependencies

Action time

Example1: require command

Generated files

 

1.2.

$ composer require monolog/monolog

Page 7: Composer namespacing

Basic Usage: Install dependencies from composer.jsona composer.json 

$ composer install 

Installing 

Generated files

 

1.

2.3.

Documentation time

Packagist

Page 8: Composer namespacing

Two important files

Demo time

Example2: composer install

Warning: lock & json file not synced

This file describes the dependencies of your project  and  may contain other metadata as well.

◎ composer.json – (config file)

After installing the dependencies, Composer writes the list of the exact versions it installed into a composer.lock file. This locks the project to those specific versions.

This is important because the install command checks if a lock file is present, and if it is, it downloads the versions specified there (regardless of what composer.json says).

◎ composer.lock – (lock file)

Page 9: Composer namespacing

Specifying versions

{ "require": { "monolog/monolog": "1.0.2", "symfony/http-foundation": “1.0.*", "vendor/package": ">=1.0 <2.0", "vendor/package": "~1.2" }}

Wildcard (*): 1.0.* is the equivalent of >=1.0 <2.0

Range: is the equivalent of >=1.0 <2.0

Next significant release (tilde): ~1.2 is equivalent to >=1.2 < 2.0.0

~1.2.3 is equivalent to >=1.2.3 < 1.3.0

Demo time

semver.mwl.be

Page 10: Composer namespacing

Namespacing

Technique for organizing your code &

prevent naming conflicts between

classes.

Page 11: Composer namespacing

Namespace example

Demo time

Example3: Namespacing

// PSI/Libraries/Calculator.php namespace PSI\Libraries;

class Calculator {

public function add($a, $b){

if ( !is_numeric($a) || !is_numeric($b) ) { throw new \InvalidArgumentException; } return $a + $b; } }

use PSI\Libraries\Calculator;

$cal = new Calculator();echo $cal->add(4 ,5);

to instantiate an object of this class , we need to use backslash notation

Page 12: Composer namespacing

Autoloading

Autoloading allows us to use PHP classes without the need to require() or

include() them and is considered a hallmark of modern-day programming.

Page 13: Composer namespacing

Different ways to autoload classes

◎ Classmap◎ Files◎ PSR-0◎ PSR-4

deprecated: recommended to use PSR-4 instead

Questions

Q: PSR ?

Q: PHP-FIG

Page 14: Composer namespacing

Autoloading using classmapa composer.json 

$ composer dump-autoload 

Generate autoload files 

Generated files

 

1.

2.3.

Action time

example4: autoload classmap

{ "autoload": { "classmap": ["src/", "lib/", "Something.php"] }}

Page 15: Composer namespacing

Autoloading using PSR-0a composer.json 

$ composer dump-autoload 

Generate autoload files 

Generated files

 

1.

2.3.

Action time

example5: autoload via PSR-0

{ "autoload": { "psr-0": {

"PSI\\": "src/", } }}

Page 16: Composer namespacing

Autoloading using PSR-4a composer.json 

$ composer dump-autoload 

Generate autoload files 

Generated files

 

1.

2.3.

Action time

example6: autoload via PSR-4

{ "autoload": { "psr-4": {

"PSI\\": "src/PSI", } }}

Page 17: Composer namespacing

Optimize performance using classmap

$ composer dump-autoload --optimize 

In production, you can generate a class map for all the classes, get a faster autoloader and optimize performance, you can get 20% boost.

Page 18: Composer namespacing

Basic Commands & composer.json schemaName Usage Description

Require composer require vendor-name/package-name

Adds required packages to your composer.json and installs them.

Install composer install Parses the composer.json file and

downloads the needed dependencies.

Update composer updateUpdates your dependencies to the

latest version, and updates the composer.lock file.

Dump-autoload composer dump-autoload --optimize

If you need to update the autoloader because of new classes in a classmap

package for example, you can use "dump-autoload" to do that without

having to go through an install or update.

Use --optimize to convert PSR-0 autoloading to classmap to get a faster autoloader. This is strongly

recommended for production (you can get a 20% boost), but can take a

bit of time to run so it is currently not done by default. Documentation time

Developer Cheat Sheet

Page 19: Composer namespacing

References

Special thanks to all the people who made and released these awesome resources for free:◎ https://getcomposer.org/◎ http://www.php-fig.org/◎ Optimizing composer autoloader performance

Page 20: Composer namespacing

Thanks!Any questions?

You can find me at:[email protected]