Skip to content

Instantly share code, notes, and snippets.

@moalex
Forked from ProcessEight/Composer cheatsheet.md
Last active February 22, 2022 14:21
Show Gist options
  • Save moalex/b1bdce68d4674bb8806dec2684f79c79 to your computer and use it in GitHub Desktop.
Save moalex/b1bdce68d4674bb8806dec2684f79c79 to your computer and use it in GitHub Desktop.
Composer cheatsheet: Common Composer tasks

Composer cheatsheet

Table of Contents

Require new module

composer require <vendor_name>/<package_name> <desired_version>

Require developer-only module

composer require --dev <vendor_name>/<package_name> <desired_version>

Add a new repository

# composer config <flags> repositories.<repo_identifier> <type> <url>
composer config -g repositories.firegento composer https://packages.firegento.com

Add auth keys for repository

# composer config <flags> <auth_method>.<composer_repository_url> <username> <password>
composer config http-basic.dist.aheadworks.com ABC123HYGKBGIG DEF456F76FGYBUOHNJKL

Use a git repository as a dependency

The git repo must have a composer.json (like this one: https://github.com/ProjectEight/AvS_FastSimpleImport/blob/master/composer.json). Use the name field from that file as the vendor_name/package_name combination.

  • Then add the git repo as a new composer repository:
{
    "repositories": {
        "avs": {
            "type": "git",
            "url": "git@github.com:ProjectEight/AvS_FastSimpleImport.git"
        }
    }
}

Then require the dependency in the usual way, but with the branch name prefixed with dev- as the version constraint:

# composer require --dev <vendor_name>/<module_name>:dev-<branch_name>
composer require --dev avstudnitz/fast-simple-import:dev-master

Use a local folder as a repository

Use the path repository type:

composer config <flags> repositories.<repo_identifier> <type> <path>
composer config repositories.vendorname path "extensions/vendorname/*"

The path is relative to the composer.json file. The path can include glob() directives, e.g. * as a wildcard character. In the above example, any Magento extension in the path <magento_base_dir>/extensions/vendorname/* which has a composer.json can be installed using composer.

The include the package as normal:

composer require <vendor_name>/<module_name>:<version_string>
composer require projecteight/module:1.0

Full article: https://medium.com/@barryvdh/using-local-repositories-to-easily-install-private-magento-extensions-with-composer-7eb966dec23e

Package a module for Composer

Packaging a module involves creating composer.json and registration.php files, then choosing a deployment method.

  • Create the composer.json file in $MODULE_ROOT/composer.json:
{
  "name": "projecteight/magento2-tpp-training1-freegeoip",
  "description": "Magento 2 Trained Partner Program Unit One Task One",
  "require": {
    "php": "7.0.2|7.0.4|~7.0.6|~7.1.0"
  },
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Training1\\FreeGeoIp\\": ""
    }
  },
  "type": "magento2-module",
  "version": "0.1.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ]
}
  • Validate the module using the Validation Tool
  • Your $MODULE_ROOT should look something like this:
$ ll magento2-tpp-training1-freegeoip/
total 76
drwxrwxr-x  6 zone8 zone8  4096 Feb 24 09:49 ./
drwxr-xr-x 74 zone8 zone8  4096 Feb 24 09:49 ../
-rw-rw-r--  1 zone8 zone8   417 Feb 24 09:49 composer.json
drwxrwxr-x  3 zone8 zone8  4096 Feb 24 09:49 etc/
drwxrwxr-x  8 zone8 zone8  4096 Feb 24 09:49 .git/
-rw-rw-r--  1 zone8 zone8    53 Feb 24 09:49 .gitignore
-rw-rw-r--  1 zone8 zone8 35147 Feb 24 09:49 LICENSE
drwxrwxr-x  3 zone8 zone8  4096 Feb 24 09:49 Model/
drwxrwxr-x  2 zone8 zone8  4096 Feb 24 09:49 Observer/
-rw-rw-r--  1 zone8 zone8   301 Feb 24 09:49 README.md
-rw-rw-r--  1 zone8 zone8   167 Feb 24 09:49 registration.php

A packaged module must have, at a minimum, composer.json and registration.php files.

With Packagist

Git is just used as a convenient way to send the package files to Packagist. It is not required to package a module.

  • Create a git repo in $MODULE_ROOT. Both the composer.json and .git directory must be in $MODULE_ROOT. Do not wrap the module in the $VENDOR_NAME/$MODULE_NAME directories. Composer will create these when the package is required.
  • Login to https://packagist.org/. Specify your public GitHub/BitBucket/etc repo URL. Packagist will pull in your files automatically and create the package by reading the composer.json.
  • You can now install the extension in the normal way using composer require projecteight/magento2-tpp-training1-freegeoip
  • You don't need to add a new repository to the project composer.json because the package is listed on the public Packagist, which is where Composer will look first when searching for the package.

Without Packagist

You can package and install a module without using git.

You can either create an archive of your module and tell composer where to find it, or you can just give composer the path to your files.

To archive your module:

zip -r vendor-name_package-name-1.0.0.zip package-path/ -x 'package-path/.git/*'

Then you will need to add an artifact repository like so:

{
    "repositories": [
        {
            "type": "artifact",
            "url": "path/to/directory/with/zips/"
        }
    ],
    "require": {
        "vendor_name/package_name": "1.0.0"
    }
}
  • You can now install the extension in the normal way using composer require vendor_name/package_name

To tell Composer to require a package from your local filesystem, you will need to add a path repository like so:

{
    "repositories": [
        {
            "type": "path",
            "url": "../../packages/vendor_name-package_name"
        }
    ]
}
  • You can now install the extension in the normal way using composer require vendor_name/package_name

Notes

If the package is a local VCS repository, the version may be inferred by the branch or tag that is currently checked out. Otherwise, the version should be explicitly defined in the package's composer.json file. If the version cannot be resolved by these means, it is assumed to be dev-master.

The local package will be symlinked if possible, in which case the output in the console will read Symlinked from ../../packages/my-package. If symlinking is not possible the package will be copied. In that case, the console will output Mirrored from ../../packages/my-package.

Instead of default fallback strategy you can force to use symlink with "symlink": true or mirroring with "symlink": false option. Forcing mirroring can be useful when deploying or generating package from a monolithic repository.

Sources: Magento 2 Dev Docs: Package a component, Composer: Repositories

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment