Inside the composer.json-file we specify the general information (see: composer-properties) of the project, it's dependencies and the autoloader configuration.
The following is mainly based on:
There are a lot of possible keys (see: composer-schema), but we stick to just the few to keep things simple.
/
. (see: composer-name)For a example look at the composer.json of our example-packages.
{
"name" : "graphit/examples",
"description": "Some examples,..",
"type": "project",
"authors": [
{
"name": "Graph-IT",
"email": "info@graph-it.com"
}
],
"license": "proprietary",
"require": {
"graphit/giwt": "dev-master"
},
"autoload": {
"psr-4": { "Graphit\\Examples\\": "src/" }
},
"config": {
"preferred-install": {
"*": "source"
}
}
}
The name of a package is always lower-case and consists of two parts the companyname and the package-/projectname separated by a single /
.
A short summary of what the project is about. the description usally is only one line long.
The type of the project. Out of the box composer supports four types: (see: composer-type)
Basic information about the author(s) of the package. (see: composer-authors) We usually only set name and email, but the following are possible.
The license the package is published under. (see: composer-license) The recommended notation for the most common licenses are (alphabetical):
More identifiers are listed at the SPDX Open Source License Registry. For closed-source software, one uses proprietary as the license identifier. Which is the default we use for our company projects unless specifically noted otherwise.
The require-key is the only manadotry key of any composer.json, without it the file isn't valid. It lists the dependencies of the package. The package will not be installed unless those requirements can be met. Dependencies are listed by a key<>value-pair of package name and version restriction.
The name of a package is always lower-case and consists of two parts the companyname and the package-/projectname separated by a single /
.
The version restriction uses [semantic versioning][http://semver.org/] with suffixes together with constrains. (see: (composer-version-restriction](https://getcomposer.org/doc/articles/versions.md) and graphit-composer-german)
one should always try to use the latest stable release using the tile (~) operator.
~1.2
is equivalent to >=1.2 <2.0.0
, and ~1.2.3
is equivalent to >=1.2.3 <1.3.0
.
The autoload-key specifies the 'autoload'-mapping for the PHP autoloader (see: PHP-FIG). PSR-0 / PSR-4 sets the specification the autoloader follows. The specification descripes how Namespaces and files/folders map together. Since PSR-0 is deprecated since fall of 2014, we always use PSR-4 and recommend to do so likewise. For example:
"autoload": {
"psr-4": {
"Graphit\\Examples\\": "src/Examples",
"Graphit\\Core\\": "src/Core"
}
},
matches:
Graphit\Example
namespace.Graphit\Core
namespace.
so:Graphit\Example\HelloWorldController
-class.Graphit\Example\Secret\SecretController
-class.Graphit\Core\WorldDomination
-class.Graphit\Core\Domination\Country\Germany
-class.Under the config-key an array of key<>value-pairs allow to specify some addition characteristics of the package. (see: composer-config) Since we usally don't use these at all or only use the "preferred-install" I only write about the 'preferred-install'-key and recommend to read about it under the previously mentioned link.
This option allows you to set the install method Composer will prefer to use based on patterns. The entries always consist of a pattern and a installation method that should be used. Supported installation methods are:
Note:
{
"config": {
"preferred-install": {
"my-organization/stable-package": "dist",
"my-organization/*": "source",
"partner-organization/*": "auto",
"*": "dist"
}
}
}