Installing the PHP library with "Composer"

Installing the PHP library with "Composer"

A few weeks ago, a customer suggested that we modify our PHP library so that it would be easier to use with the Composer dependency manager. We decided to implement these changes and we published our PHP library on Composer's official repository: packagist. Therefore, we are going to show you how to use our library with Composer.




Composer is a dependency manager for PHP. It is inspired from the npm package system used by node.js. The Composer executable is a PHP script which can download and install the libraries required for a project.

It can solve recursive dependencies. That is, if the project needs library A which in turn requires library B, Composer installs library A and library B.

As any good dependency manager, Composer allows you to specify criteria to control which version of a library must be used.

Finally, Composer enables you to simplify the list of includes that must appear at the top of each PHP page.

Installing Composer


If Composer is not already available on your machine, you must start by downloading and installing it. The process is very easy and well documented. You can find instructions on the Composer web site: https://getcomposer.org/doc/00-intro.md.

The packagist.org repository


The main advantage of Composer is its packagist repository which references the compatible libraries. To use one of these libraries, you only need to add the library name in the dependency list, potentially with a version number and that's it. Composer takes care of everything else.

Since version 1.10.32417, our PHP library is available on packagist: https://packagist.org/packages/yoctopuce/yoctolib_php.

How Composer works


Composer uses a composer.json file containing the project description and the list of the libraries used by this project. Note: this same file allows you as well to publish the project more easily, but we are not going to dwell on this topic.

In case of a PHP script using libraries available on packagist, the JSON file has only one "require" key listing the name of these libraries as well as the criteria to select the library version.

For example, the minimum file to use the Yoctopuce PHP library is as follows:

{
    "require": {
        "yoctopuce/yoctolib_php": ">=1.10.32417"
    }
}



To install the library, you only need to run the following command:

    c:\> composer install



Composer automatically downloads the Yoctopuce library in the vendor sub-directory and creates two files: composer.lock and vendor/autoload.php.

composer.lock


This file is a kind of summary of the installation. When first installing, Composer creates this file with an exact reference (name, version number, URL) of all the libraries it has downloaded. When the "install" command is executed again, Composer always uses the same version of the library.

If we later want to use a more recent version of the installed libraries, for example to obtain the latest fixes, we must use the "update" command:

    c:\> composer update



vendor/autoload.php


For libraries which specify automatic loading information, Composer generates a vendor/autoload.php file regrouping all the necessary includes to use the libraries. Simply include this file to enable you to use all the classes provided by the libraries:

require __DIR__ . '/vendor/autoload.php';



A concrete example

Let's see how to implement the same example as in our post How to start in PHP with Yoctopuce modules but using Composer.

The first step is to write copy the post PHP script in an empty directory.


<!DOCTYPE html>
<html lang="en">
<body>
<?php
require('Sources/yocto_api.php');
require('Sources/yocto_temperature.php');

YAPI::RegisterHub("127.0.0.1");
$ytemp = YTemperature::FirstTemperature();
if(is_null($ytemp)) {
    die("No temperature sensor found");
}
$value = $ytemp->get_currentValue();
$unit = $ytemp->get_unit();
Print("<h2>Temperature: $value $unit</h2>");
YAPI::FreeAPI();
?>
</body>
</html>



If you test this script, PHP generates an error because it can't find the Sources/yocto_api.php and Sources/yocto_temperature.php files. Instead of copying these files by hand, we are going to create a composer.json file containing a dependency on our PHP library:

{
    "require": {
        "yoctopuce/yoctolib_php": ">=1.10.32417"
    }
}



We must then install the library with Composer:

    c:\> composer install



Composer downloads our PHP library in a 'vendor' sub-directory and creates the composer.lock and vendor/autoload.php files.

We only need to replace the lines which included Sources/yocto_api.php and Sources/yocto_temperature.php by a single line which includes the vendor/autoload.php file:

<!DOCTYPE html>
<html lang="en">
<body>
<?php
require __DIR__ . '/vendor/autoload.php';

YAPI::RegisterHub("127.0.0.1");
$ytemp = YTemperature::FirstTemperature();
if(is_null($ytemp)) {
    die("No temperature sensor found");
}
$value = $ytemp->get_currentValue();
$unit = $ytemp->get_unit();
Print("<h2>Temperature: $value $unit</h2>");
YAPI::FreeAPI();
?>
</body>
</html>



A short comment on safety


In this example, we placed the PHP script and the composer.json file in the same directory, but it is probably more prudent to use a directory that is not accessible from the web site. Otherwise, someone a little smart could guess that you're using Composer, and access the files of the libraries that you're using.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.