External libraries


1Presentation

When you're developing a website or an API, it's common to use function libraries to perform certain processes (to avoid having to develop everything yourself).

Temma is very flexible and adapts to the way you want to load these libraries. Generally speaking, libraries can be loaded either by copying them into the lib/ directory, or by using the Composer dependency manager.


2Copying the library

If the library is a set of .php files or a .phar file, you can copy the file(s) into the project's lib/ directory.


2.1.php files

If you place .php files in the lib/ directory, and they contain PSR-4 compliant objects, they will be handled automatically by Temma's autoloader.

For example, the \Skywalker\Luke object must be stored in the lib/Skywalker/Luke.php file, and can be instantiated directly:

$luke = new \Skywalker\Luke();

If, on the other hand, the files don't comply with the PSR-4 standard, you'll need to include them explicitly before you can use the objects and functions they contain.

For example, if the lib/skywalker/family.php file contains the \Skywalker\Luke object, you'll need to include it before instantiating the:

<?php

require_once('skywalker/family.php');

// some code

$luke = new \Skywalker\Luke();

2.2.phar file

If you place a .phar file directly in the lib/ directory, all your objects that use the contents of this file will have to include it:

<?php

require_once("a_library.phar");

If you have placed the .phar file in a subdirectory, you must include the entire path below lib/:

<?php

require_once("path/to/a_library.phar");

3Using Composer

Composer is the dependency manager commonly used in PHP projects. It makes it easy to load libraries declared on the Packagist service.

Assuming you already have Composer installed, you can create a composer.json file at the root of your project, and list the libraries that need to be installed there:

{
    "require": {
        "lib1": ">=1.0.0",
        "lib2": "2.*"
    }
}

Then, to have Composer download the listed libraries (and any dependencies), run the command: composer update

If you've installed Composer as a .phar file, you'll need to run php composer.phar update

Composer will place the downloaded files in the vendors/ directory.

If you've installed libraries with Composer, Temma will automatically load the Composer autoloader, making library objects directly instantiable.