Today I will elaborate the intricacy of installing a Magento module with composer in Magento 1.x.

To start with, you might already know if you are an advanced PHP developer, we need to know what “Composer” is and how it has effect on PHP projects.

Composer is a dependency management tool in PHP. It manages packages and libraries for PHP. An simple example would be- if we say apt or yum are the packages in linux,  Composer would be the there to manage those. And it manages packages or libraries per project basis not globally. Even though, if we wanted, we can define composer to manage packages or libraries globally. We will only talk about the aspect of composer in this post per project basis.

To know about composer, few keywords and their perspective meanings are worth mentioning. In composer:

Project is the directory where composer.son file exists.

Repository is the remote repository where the packages exist.

Package can be mentioned as files, folders which will be installed.

composer.json file is the master of composer project. It’s like .git folder in any git version control project.

There are 5 basic steps from installing composer to our project and to install it to our Magento.

  1. Downlod and initialise composer.
  2. Installing composer.
  3. Change update composer repository.
  4. Add package to composer project.
  5. Install the package to Magento.

 

1. Download and initialise composer

Head up to https://getcomposer.org/download/ and follow the instruction to download composer in a folder. In my case, I have downloaded the composer in /Library/WebServer/Documents/php_composer/. Once you download and run the installer, you would see the following files inside the directory.

Composer After Downloaded

See the composer.phar fie, it’s a PHP Archive. The phar is the short form of PHP ARchive. You would run this archive as you would run a php script i.e. php composer.phar.

Now, we need to initialize our composer project. This is something we need to do regardless of whether we’re starting a new development project with composer, or just using it to install packages from repository. And this is just running composer’s init command

Composer Init

The -n means “no interactive”. Without this flag init asks you a bunch of questions about starting a new composer package. We don’t want to create a new composer package, we’re just using composer to manage other packages. That means the interactive questions don’t apply to us. If you now see files inside your composer folder, you would see a composer.json file.

composer after init

This confirms we have a composer project. So, the folder, in my case /Library/WebServer/Documents/php_composer/, is now a composer project. Yes, it’s as simple as that.

Let’s take a look inside of this composer.json file and you will see a skeleton of composer.json:

composer_json_skeleton

2. Installing composer

We now need to install composer inside our composer project. The init command just stes up a composer project while the install command installs the default PSR fundamental codes required by PHP for our composer project. Okay, if it’s too long, the install command installs the fundamental codes for our composer project. It will also install any packages configured in the require section in composer.json file.

To install composer, run the command inside your composer project folder in terminal

composer_install

The --no-dev flag makes sure a bunch of extra stuff we don’t need is skipped. A composer project has two sets of fundamental codes and packages.

  1. The packages that you need to use.
  2. The packages you need to work on i.e. tests, build tools, etc.

The --no-dev tells composer that we only want to use these packages. And we don’t want to download the developer dependencies pacakges, or we don’t want to work on someone else’s project.

Now take a look at your composer project folder after running install, you’ll see composer added a number of new files and folders to the vendor folder.

composer_install_after

This is the fundamental codes I was talking about earlier — the standard composer autoloader files. These files give composer projects a module like system for running code and using composer packages. If everything’s working correctly you won’t need to worry about these files — composer needs them, but they’re invisible glue. Additionally, since we’re using composer to manage Magento packages we won’t be using these autoloader files. However, composer needs them to run, so don’t remove them.

 

3. Change update composer repository

As we now installed composer in our composer project, the next thing is to change, update composer repository.

A composer repository is the repository where the packages exist. Mostly these repositories exist on cloud.

When we ask composer to install a specific package, the composer.phar program asks the repository if that package exists. Composer was built to be decentralised. It means anyone can create a repository, as mentioned most of these repositories are available on cloud. However, composer has support for a generic repository named packagist.org. Packagist is meant to be the central location for PHP packages. So the next question you might ask, how would we be able to get a composer repository for Magento packages? Well, cool folks at Firegento, via their hackathon, have created their own custom composer repository at packages.firegento.com.

To add, change repository, we need to manually edit composer.json file. As we will only manage Magento packages and don’t really need to manage Packagist packages, we need to edit composer.json file like below:

//File: ./composer.json
{
    "require": {
 
    },    
    "repositories":[     
        {
            "packagist": false
        },         
        {
            "type":"composer",
            "url":"http://packages.firegento.com"
        }
   ]    
}

The top-level key require is empty as of now, we normally add package names here. Another top-level repositories key is an array and has two objects. The first object is

{
  "packagist": false
}

which tells composer project to disable packagist repository. And the second object is

{
  "type":"composer",
  "url":"https://packages.firegento.com"
}

This tells composer to add the repository from https://packages.firegento.com. Remember, to pull repository from https location. Otherwise composer will show error.

Whenever you make an update to composer.json, you’ll need to tell composer to update your project with the update command. The update command is similar to the install command. The diference is, the install command installs the composer.json file, the fundamental codes and packages. However, the update command install the fundamental codes and packages based on your composer.json file. We can only run update command after  the initial installation.

Enter the followimg command in your composer project directoy

composer_update

Again --no-dev flag tell composer not to update the development related packages. At this point, composer informed us “Nothing to install or update”. To see, if composer added the repository from https://packages.firegento.com, run the following:

composer_update_vvv

If you take a look at the verbosed output, you would see composer added packages.json in it’s repository.

If you needed to add a repository available on github, you need to add it like below:

//File: ./composer.json
{
    "require": {
 
    },    
    "repositories":[     
        {
            "packagist": false
        },         
        {
            "type":"composer",
            "url":"http://packages.firegento.com"
        },
        {
            "type": "vcs",
            "url": "your/github/or/git/or/svn/etc/repository/uri"
        }
    ] 
}

4. Add package to composer project

We have come now to a point where we are able to add packages to our composer project folder. The require command adds packges to require section in composer.json and then it updates the composer project. Let’s say,  we want to install ajbonner/magento-composer-autoload  package from http://packages.firegento.com/. Here, ajbonner/magento-composer-autoload is the package name. The package name consists of a vendor name and the project’s name. The vendor name just exists to prevent naming clashes. It allows two different people to create a library named json, which would then just be named vendor1/json and vendor2/json.

Run the following command

composer_require

composer_require_2

The require command first adds the package, in this case ajbonner/magento-composer-autoload, to the composer.json file. Next the --update-no-dev flag updates the composer project folder with the packages defined in the require section. And it also confirms not to update the development related packages.

If we take a look into composer.json, you would see

composer_json_after_require

The require key has now the package ajbonner/magento-composer-autoload and it’s version next to it. In most cases, composer will get the latest stable version of a package.

When we run require, composer might ask you for a version constraint. This is the version of the package you want to install. If so, enter the string 1.* for this value, which will tell composer to always update the package to the latest version of the 1.x release branch. Covering composer’s versioning is beyond the scope of this article, but the composer book has a good section that covers versioning, and also tells you why you might not want to use a raw wildcard .

At this point, we have just installed ajbonner/magento-composer-autoload package in composer project forlder. To confirm that the package has been isntalled, you can check it inside the vendor folder:

composer_project_vendor_folder

This brings up the next step -- install this package to Magento.

 

5. Install the package to Magento

We have successfully installed ajbonner/magento-composer-autoload package in composer project forlder. However, to get this package available to Magento, we need to isntall a composer plugin.

This is where the magento-hackathon/magento-composer-installer project comes into the picture. Composer allows users to create and install custom plugins that create alternate installation routines. The magento-hackathon/magento-composer-installer plugin creates a composer installer for Magento. Put another way — the magento-hackathon/magento-composer-installer plugin will let you install packages via composer into vendor, and will automatically install the same packages into Magento for you.

To install magento-hackathon/magento-composer-installer plugin, we can manually edit the composer.json file to look like:

composer_json_require_magento_composer_installer

The first pacakge we already added in the previous step. The second package magento-hackathon/magento-composer-installer, we just added manaully. We just now need to update the composer project folder as we did in step 3.

composer_update

At this point, composer showed error as the package coud not identify the Magento root directory in your system:

composer_update_error

Let’s edit the composer.json file again like below:

//File: composer.json
{
    "require": {
        "ajbonner/magento-composer-autoload": "^0.1.1",
        "magento-hackathon/magento-composer-installer": "*"
    },
    "repositories":[
        {
            "packagist": false
        },
        {
            "type":"composer",
            "url":"https://packages.firegento.com"
        }
   ],
   "extra":{
        "magento-root-dir":"/path/to/magento",
        "magento-deploystrategy":"copy"
    } 
}

We have added a new top level key extra. The extra section contains extra configuration values. Then, within this extra key, two configuration keys are added.

The first key magento-root-dir has a value of /path/to/magento/. You need to replace this with the location of your local Magento documnet root.

The second key magento-deploystrategy has a value copy. This tells the Magento installer plugin that we’d like to copy the files out of the vendor folder and into our Magento system. This is an optional parameter — without it the installer will create symlinks to our vendor folder instead of copying files.

Once the keys are updated, just run the update command

Asrars-MacBook-Pro:php_composer asraralam$ composer.phar update --no-dev
Loading composer repositories with package information
Updating dependencies
  - Installing magento-hackathon/magento-composer-installer (1.3.2)
    Loading from cache

Writing lock file
Generating autoload files

This time the update will run without any errors.

That’s all the bits required to setup composer project and now you can install any Magento packages from packages.firegento.com.

Categorized in: