Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a library to be used by composer autoloading?

I want to make this package to be autoloaded by Composer.

This package is available on Packagist

I realized I need to add something to composer.json and I need to have a autoload.php somewhere.

The only class that should be autoloaded is the Webbot.php.

Can someone give me the step by step breakdown to accomplish this?

Google search results returned are instructions to autoload libraries.

I need instructions on how to write autoloadable libraries.

like image 284
aikchun Avatar asked Jan 15 '14 12:01

aikchun


People also ask

How do I create a composer json file?

To configure Composer for your PHP app json file specifies required packages. Verify that a composer. json file is present in the root of your git repository. Run composer install (on your local machine) to install the required packages and generate a composer.


1 Answers

First, you need to have your package structured in either PSR-0 or PSR-4. I haven't started using PSR-4 yet as it has only just been accepted as a standard. Composer will still support PSR-0 for a long time to come.

This means that you MUST follow these rules:

  • A fully-qualified namespace and class must have the following structure <Vendor Name>\(<Namespace>\)*<Class Name>
  • Each namespace must have a top-level namespace ("Vendor Name").
  • Each namespace can have as many sub-namespaces as it wishes.
  • Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.
  • Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.
  • The fully-qualified namespace and class is suffixed with .php when loading from the file system.
  • Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.

Full FIG guidelines here

This would mean that your package should be laid out in your github repository as follows:

-src
    -Simkimsia
        -Webbot
            -Webbot.php
-composer.json
-license.md
-{any other base level files}

Webbot.php would be in the namespace : Simkimsia\Webbot as dicated by the directory structure.

Then... As this is a github package, you can add it to your projects composer.json using the repositories property.

{
    "name" : 'test',
    "description" : 'Test',
    "keywords" : ['test'],
    "repositories" : [
    {
        "type": "vcs",
        "url": "https://github.com/simkimsia/webbot.git"
    }
    ],
    "require" : {
        "simkimsia/webbot" : "dev-master"
    }
}

The package will be available from Composers autoload and can be instantiated as :

$webbot = new Simkimsia\Webbot\Webbot();

Note: Composers autoload.php will be available in once you have run composer install:

/vendor/composer/autoload.php

Just include this file at the start of your PHP script and your classes will be available.

like image 143
David Barker Avatar answered Nov 14 '22 21:11

David Barker