4. Composer is a tool for dependency
management in PHP.
It allows you to declare the dependent libraries
your project needs and it will install them
in your project for you
11. How does it Works?
Icons by: Ryan Beck, Pieter Smits, Kirill Ulitin from the Noun Project
composer.json
internal dependencies
external dependencies
git
svn
mercurial
etc
Packagist.org
github.com
etc
12. How it Work?
$ cd /var/www/myProject
$ tree vendor -L 2
vendor
├── autoload.php
├── composer
│ ├── autoload_classmap.php
│ ├── autoload_namespaces.php
│ ├── autoload_psr4.php
│ ├── autoload_real.php
│ ├── ClassLoader.php
│ └── installed.json
├── symfony
│ ├── confy
│ └── yaml
└── psr
└── log
17. Basic commands
$ composer list
Display all commands
config Set config options
create-project Create new project from a package
global Allows running commands in the global composer dir
init Creates a basic composer.json file in current directory
install Installs the project dependencies
update Updates your dependencies to the latest version
self-update Updates composer.phar to the latest version
and more...
21. composer.json
$ composer validate
./composer.json is valid, but with a few warnings
See http://getcomposer.org/doc/04-schema.md for details on the schema
License "MIT" is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if
you use an open license.
If the software is closed-source, you may use "proprietary" as license.
The version field is present, it is recommended to leave it out if the package is published
on Packagist.
Name "Mostofreddy/MyProject" does not match the best practice (e.g. lower-cased/with-dashes).
Validate it!!
22. composer.lock
✓ Locks versions of dependencies
✓ Run the same version everywhere
! Is good practice to upload the file to the repository
27. type
type: “libray”
The type of the package. It defaults to library
! Only use a custom type if you need custom
logic during installation. It is recommended
to omit this field and have it just default to
library
29. author
authors: [{...}]
The authors of the package. This is an array of objects.
✓ name: The author's name. Usually
his real name.
✓ email: The author's email address.
✓ homepage: An URL to the author's
website.
✓ role: The authors' role in the
project (e.g. developer or
translator)
{
"authors": [
{
"name": "Federico Mosto",
"email": "mosto.federico@gmail.com",
"homepage": "http://mostofreddy.com.ar",
"role": "Developer"
},
{
…
}
]
}
30. others
homepage: “https://www.github.com/...”
An URL to the website of the project
“keywords”: [“key1”, “key2”]
An array of keywords that the package is related to
and more
https://getcomposer.org/doc/04-schema.md
45. Scripts
A script, in Composer's terms, can either be a PHP
callback (defined as a static method) or any command-
line executable command. Scripts are useful for
executing a package's custom code or package-specific
commands during the Composer execution process
48. throw script in events
namespace MyVendor;
use ComposerScriptEvent;
class MyClass
{
public static function postUpdate(Event $event) {
// do stuff
}
public static function postPackageInstall(Event $event) {
// do stuff
}
public static function warmCache(Event $event) {
// make cache toasty
}
}