Composer is an application-level package manager. Composer auto-loads the dependencies on demand and can also auto-load custom classes .

This article will cover the needed steps to add composer support to your DotKernel project or even “composify” it.

Assuming that you know how to use composer (if not you should consider reading this article) we wil move on to your DotKernel project “composification”.

 

First things first

The DotKernel project must have a composer.json file so that composer can work.

Our composer.json file should look like this:

{
  "require" : {
    "zendframework/zendframework1" : "1.12.*",
    "mobiledetect/mobiledetectlib" : "2.8.*" 
  },
  "require-dev" : {
    "php" : ">=5.4.0"
  },
}

Note that zend/zendframework1 may not be necessarily if you already have ZendServer running or the Zend Framework folder within /usr/share/.

The file above makes sure:

  • Zend Framework is present and at version > 1.12.*
  • MobileDetect is present and at version > 2.8
  • The PHP executable is at least at version > 5.4.0 (only for development, because it is not present in the main require)

 

The dependencies provided in the require section are also loaded for development purpose if not provided in require-dev.

 

In order to have these components instaled you must run the following command in your DotKernel root path

composer update

If the vendor folder is present composer will check for updates, and update the packages as needed.

If the vendor folder does not exist composer will create a vendor folder. This folder will contain all the requested packages.

Composer will create an autoload file, which will be use to load our dependencies/packages.

 

Adding Composer Support to DotKernel

The autoload file created by composer will be used to load our packages.

$composerAutoLoaderPath = realpath(APPLICATION_PATH.'/vendor/autoload.php');
require_once($composerAutoLoaderPath);

So far so good, but what if the file does not exist or composer is not present?

First we must make sure the composer autoload path exists and only load the dependencies if composer autoload file was found.

$composerAutoLoaderPath = realpath('./vendor/autoload.php');

$composerEnabled = file_exists($composerAutoLoaderPath);

if($composerEnabled == true) 
{
    require_once($composerAutoLoaderPath);
}
else
{
    // handle the error gracefully
   // or load fallbacks - if exist
}

The variable $composerEnabled will be true only if the composer path exists so the application behavior can be controlled if composer is not present.

Later on, the packages can be used like this:

use VendorName\PackageName\ClassName as MyDependency;

$myDependency = new MyDependency($neededArguments);
$myDependency->doSomething();

Learn more about how to use dependencies in DotKernel 1.x by reading this article.

This article works for any DotKernel 1.x version if your server is running PHP >5.4.0.

One Comment

  1. Using DotKernel with Composer Dependencies | DotKernel PHP Application Framework

    […] There is also an article explaining how composer can be added to DotKernel learn more. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>