©  Viktor Khliupko 2017

Viktor Khliupko, Magento 2 DIY , 10.1007/978-1-4842-2460-1_6

6. Composer

Viktor Khliupko

(1)Duesseldorf, Germany

In this chapter, we will explain how to use the application-level package manager with the most popular e-commerce platform. Being inspired by Node.js npm, Composer provides a unified format that simplifies dependency management of PHP software. The usage of Composer in various Magento 2 projects is inevitable, because it is a part of the platform. You can run the package manager through the command line, installing dependencies for your modules. Besides, Composer provides the ability to install Magento 2 extensions available on Packagist (the repo is described below). Another important feature introduced in Composer is a set of autoload capabilities for libraries that essentially simplifies the usage of third-party software solutions.

Magento 2 Composer Integration

You must install Composer if you use the Magento software. The package manager will be necessary to update Magento 2 and its components, even if you’ve installed the e-commerce solution from an archive.

The Magento 2 Composer combination offers the following features :

  • provides the ability to use third-party libraries while you don’t have to bundle them with source code of Magento 2

  • offers a component-based architecture as well as reliable dependency management

  • reduces extension conflicts as well as various compatibility issues

  • streamlines your work with versioned dependencies

  • introduces semantic versioning

  • supports some useful standards, such as PHP Framework Interoperability

In its turn, Magento 2 relies on Composer while packaging different components as well as product editions. Since some third party components used by Magento are not presented in the code base, they exist in a form of dependencies in composer.json files. Below, we will shed light on them.

Magento 2 and composer.json

While talking about composer.json files , it is necessary to explain what is a package. A package is a directory which contains composer.json as well as other component files. It’s an inevitable component of the Magento 2 Composer tandem. These are the types of composer.json packages :

  • Root composer.json package includes the main composer.json file utilized for declaring dependencies on third-party components and used as a template for other root composer.json files. It is located under composer.json, its naming convention is magento/magento2ce, its type is project.

  • CE project composer.json package represents Magento CE project and declares dependencies on the class autoloader and the magento product. It is located under composer.json, its naming convention is magento/project-community-edition, its type is project.

  • CE product composer.json package represents Magento CE product and declares dependencies on such Magento components as themes and modules as well as third-party components. The package is located under composer.json, its naming convention is magento/project-community-edition, its type is metapackage.

  • base composer.json package represents Magento CE base package which includes files/directories (excluding components) as well as introduces the “require” section to list the latter. The package is located under composer.json, its naming convention is magento/magento2-base, its type is magento2-component.

  • module shows a fully qualified name of a module, which is broken down into vendor, suffixes that represent the rest of the words, and the mandatory prefix. The package is located under app/code/Magento/<Module>/composer.json, its naming conventions are magento/module-catalog-inventory and magento/module-checkout, its type is magento2-module.

  • theme belongs to areas, that’s why you should use the area name as the first suffix. The package is located under app/design/<area>/Magento/<theme>/composer.json, its naming conventions are magento/theme-frontend-blank and magento/theme-adminhtml-backend, its type is magento2-theme.

  • language packs package require the lowercase language identifier. The package is located under app/i18n/magento/<language>/composer.json, its naming conventions are magento/language-en_us, magento/language-de_de, etc., its type is magento2-language.

  • Magento framework/libraries contains only one package—magento/framework. Particular libraries are not distinguished as separate components. The package is located under lib/internal/Magento/Framework/composer.json, its naming convention is magento/framework, its type is magento2-library.

The Magento 2 Composer cooperation provides you with the ability to submit the following package types to Magento Connect:

  • magento2-module usually includes source files and the top composer.json.

  • metapackage is a placeholder that gathers a collection of packages.

  • magento2-theme package type is utilized with theme packages.

  • magento2-language usually contains a .csv file used for translating certain content.

Naming conventions in Magento 2 and Composer

The required format for Composer packages in Magento 2 is:

1   <vendor_name>/<package_name>

Please note that there is a requirement for all letters in the name in the Magento 2 Composer duo—they must be lowercase.

Hence, Magento packages are named according to the following format:

1   magento/*

If a package name consists of several words, these word should be separated with dash according to the Composer specification. The Magento package name convention looks as follows:

1   magento/<type-prefix>-<suffix>[-<suffix>]...
  • type-prefix is a component type in a Magento-specific domain.

  • suffix is something that identifies the component within its type.

Package Types

Each component of Magento 2 can be categorized into one of eight types listed above. In case you have a component that doesn’t fit into any specific category, you can generalize it to magento-component.

With the aid of identifier types related to each component, the Magento 2 system arranges both directories and files of each component and places them in correct locations according to the available directory structure of Magento 2.

How to Install Composer

The following section of our Magento 2 Composer guide describes how to install Composer. First of all, you should check whether the software solution is already installed or not. Open a command prompt and enter this command:

1   composer --help

If the command displays the appropriate information, you’ve already installed Composer. If you see an error, perform the following actions:

  • Go to your Magento server and create a new empty there.

  • Run the commands listed below:

1   curl -sS https://getcomposer.org/installer | php
2   mv composer.phar /usr/local/bin/composer

Congratulations! You’ve just installed Composer on Magento 2. If you have any questions, see the official Composer installation guide.

How to Deploy Magento 2 CE with Composer

A435230_1_En_6_Figa_HTML.jpg

To deploy Magento 2 CE with Composer, use the following command:

1   git clone https://github.com/magento/magento2 ./magento2
2   composer install

If you want to update your Magento 2 project to the latest available version, utilize this command:

1   git pull composer install

If you are going to deploy Magento 2 CE through Composer to the web site root, which is /var/www/example.com/htdocs, there can be two different conditions: you can be inside or outside the deploying directory performing the appropriate Magento 2 Composer command. In the first case, run this one:

1   cd /var/www/example.com/htdocs
2   composer create-project "magento/project-community-edition"

In the second case, utilize this one:

1   composer create-project "magento/project-community-edition" /var/www/example.com/htdocs

Source

How to clone the Magento 2 repository via Composer

This Magento 2 Composer procedure is necessary if you are going to contribute to the code base of the platform. You can choose between the master or develop branches. The first one is more stable and you can clone it with the help of the optional [-b master] argument, while the second one is the latest and and it will be cloned by default. You can find the full description of the procedure here: (Contributor) Clone the Magento repository.

How to package a module in Magento 2

In Magento 2, Composer packages are used for distributing, installing, and upgrading modules. To package your module, it is necessary to:

  • create a composer.json file;

  • package and then publish a module on the Magento Connect.

Magento 2 Composer file

composer.json defines such basic information as name, version, and requirements. Use only a root directory of a module to place its composer.json file and pay attention to the Composer generic schema and the following restrictions:

  • name—requires using the <vendor-name>/module-<module-name> format as well as lowercase letters only. Dashes must be used to separate words in the <module-name>.

  • type—set this value to magento2-module for all your modules.

  • autoload—specifies important data to be loaded. For further information, check Composer Autoloading.

Metapackages

While talking about the Magento 2 Composer integration, it is necessary to pay special attention to metapackages, since they provide the ability to create an extension that consists of several packages, combining it into a cohesive system. A metapackage is a .zip file that contains the metapackage composer.json file only. If your extension uses more than one package, use a metapackage as its root package. Note that it is the only use case for this package type. For further information, check the official composer.json documentation.

An ordinary composer.json metapackage file looks as follows:

 1   {
 2   "name": "magento/sample-data",
 3   "version": "1.0.0-beta",
 4   "type": "metapackage",
 5   "require": {
 6   "magento/module-sample-data": "self.version",
 7   "magento/sample-data-media": "∼0.42.0-beta2",
 8   },
 9   "autoload": {
10   "files": [ "registration.php" ],
11   "psr-4": {
12   "Magento\sample-data": ""
13   }
14   }
15   }

As for a composer.json file for a module, it has the following structure:

 1   {
 2   "name": "magento/sample-module-newpage",
 3   "description": "A Magento 2 module that creates a new page",
 4   "type": "magento2-module",
 5   "version": "1.0.0",
 6   "license": [
 7   "OSL-3.0",
 8   "AFL-3.0"
 9   ],
10   "require": {
11   "php": "∼5.5.0|∼5.6.0|∼7.0.0",
12   "magento/framework": "∼1.0.0"
13   },
14   "autoload": {
15   "files": [ "registration.php" ],
16   "psr-4": {
17   "Magento\SampleNewPage\": ""
18   }
19   }
20   }

Packaging and publishing extensions

The Magento 2 Composer combination requires the following steps to be performed in order to package and publish your extensions:

  1. First of all, create a package of your module via a zip operation. Unnecessary directories should be excluded. Use dashes to separate words and alphanumeric characters to set the package file name. Please note that it is important not to use whitespaces.

  2. It is also worth mentioning that the Magento 2 system can retrieve your package from any valid URL of GitHub or other third-party repo.

GitHub and Packagist

If you are going to work with GitHub and Packagist, do the following things:

  1. If you are planning to host your extension package on GitHub and Packagist, set up git on your machine.

  2. Then, navigate to your module directory, it should have composer.json in the root, and turn it into a new git repository. For further information, check this GitHub documentation.

  3. Now, when your module is pushed on GitHub, you can refer to it directly via Composer or do the same action through Packagist:

    • Create an account on Packagist.

    • Find the Submit Package button, click it, and paste your GitHub repository link there.

Private repositories

As for private repositories, they can be used for private code or development. At the same time, it is necessary to perform installation of such modules via a CLI. The Magento 2 Composer tandem sets the following requirements:

  1. Use Satis or Toran to make your packaging repository.

  2. Create a package and submit or register it on your repository.

  3. Add the following code to your composer.json:

    1   {
    2   "repositories": [
    3   {
    4   "type": "composer",
    5   "url": [repository url here]
    6   }
    7   ]
    8   }

Now, you can use the require field to reference all your packages from the private repository.

For more information on creating Magento 2 Composer packages, follow this link: CREATING A MAGENTO 2 COMPOSER MODULE by Alan Kent.

Source

Useful Links

  • Core Magento 2 Composer packages

  • Magento 2 extensions on Packagist—the main Composer repository

  • Composer

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.30.232