Question

I can't get my library to work. The directory structure is lib-name/src and inside theres a main dir and a tests dir, how do I tell composer to load from the /lib-name/src/main folder?

Link to my library github https://github.com/gerardorn/catalogo

Was it helpful?

Solution

There are 3 ways to map your classes for autloading with composer.

PSR-0

The recommended way is PSR-0 compatible. This protocol describes the directory structure of your library. Each namespace need to be a directory. Classes with underscores are separated as well (PEAR style).

In your case, the class Catalogable has a namespace gerardorn\catalogo. To be PSR-0 compatible the directory structure is:

- src
   - main
      - gerardorn
         - catalogo
            - Catalogable.php

In your composer.json you should put the following:

"autoload" : {
    "psr-0" : {"gerardorn" : "src/main"}
}

Classmap

Alternatively you could use classmap. Classes are searched for in the directory regardless of the namespace.

"autoload": {
    "classmap": ["src/main"]
}

Files

The 3th method files doesn't apply in your case.


PHPUnit

Note that PHPUnit is needed to test your library, but not to run your library. Therefor you shouldn't put that up as a required library.

It's good that you're writing unit tests. You should sign up for Travis CI. It will run your PHPUnit tests each time that you push to GitHub and warn you (by e-mail) if something got broken.

OTHER TIPS

Your directory structure should be PSR-0 compatible.

i.e. your file Catalogable.php has a namespace gerardorn\catalogo, so your directory structure for this has to match the following.

- src
    - main
        - gerardorn
            - catalogo
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top