Versioned Libraries

Derek Jones edited this page Jul 5, 2012 · 15 revisions
Clone this wiki locally

Category:Core::Libraries

Introduction

As of Code Igniter v1.4.0, any library can be overridden by a custom one stored within the application/libraries folder. CI uses two different methods for loading libraries.

System Libraries

While you can provide your own version of the system-dependent libraries, CI loads the class file directly. This means that versioning (as discussed here) isn't supported. This is in contrast to how it loads all other libraries.

On-demand Libraries

Code Igniter comes with various libraries that you can use on-demand. You can also supply your own libraries on a per-application basis. When you request a library, by calling:

$this->load->library('samplelib');

CI first checks to see if a method is defined for loading that library. In this case, it will look for a method called _init_samplelib() within your application controller (although this isn't documented in the User Guide yet).

If it doesn't find that, it will search for a file within application/init called "init_samplelib" (with EXT as the extension).

The advantage of both of these techniques is that you can then load a library with any name that you desire. Why is this cool? Because it simplifies version management.

With a growing community and access to new tools for distributing add-ons for Code Igniter, versioning is going to become really important.

Adding a Version Number

Code Igniter has a version number. Why not libraries (and plugins, etc.). They evolve as bugs are fixed and new features are added, and it can be difficult to know which version is currently installed.

One option is to attach the version number to the library file. Here we have two versions of the same library installed, and we can choose to load one or the other.

application/libraries/SampleLib-1.0.php
application/libraries/SampleLib-1.3.php

Since you need to use an init method or file to load a library in the first place, this is where you'll specify the version number you want.

Hard-coding the version number in the init method or file:``` include_once(APPPATH.'libraries/SampleLib-1.0'.EXT);

... or loading from a configuration parameter:```
$vers = $this->config->item('samplelib_version');
include_once(APPPATH."libraries/SampleLib-$vers".EXT);