0
votes

I'll ask the question with an example. Say I have a project directory like so.

project

  vendor

      samplevendor

  src

  composer.json

First i want to know if i have placed the composer.json file in the project directory correctly in order to install libraries in the vendor directory?second, say i have the following lines the composer.json

 "autoload":{
      "psr-0": {
              "samplevendor\\package": "lib/"
                }
            }

then where will composer configure autoloading to look for the class samplevendor\package\sampleclass in regards to the project root? I ask this after having seen the following lines in the symfony2 composer.locked file:

 "autoload": {
            "psr-0": {
                "Doctrine\\Common\\Annotations\\": "lib/"
            }
        }

update:I found the corresponding namespace map in autoload_namespaces.php which just doesn't seem to be in accordance with psr-0:

'Doctrine\Common\Annotations\' => array($vendorDir . '/doctrine/annotations/lib')

1

1 Answers

2
votes

You probably can place the composer.json anywhere in your project depending on requirements, but for a pure PHP (and HTML/CSS/JS) project, it is the best idea to place the file in the topmost directory. That way Composer would be able to find it if you create a library or want some other nice side effects. (There are plenty of them that do not directly affect how Composer or your software works).

It is a good idea to create an initial composer.json by running composer init. Composer will ask you about a vendor name and software name. I recommend you give a reasonable answer here. Also, you can add yourself as a developer, add some dependencies right now (not necessarily needed), and finally create the initial file.

PSR-0 autoloading means that the given prefix of namespace or class name (remember those Underscore_Classname_Namespace_Emulation) is to be searched in the directory, i.e. a class named \Namespace\Class that is configured as "psr-0": {"Namespace\\": "src"} will be located at src/Namespace/Class.php (if the file is not there, maybe another component is able to load that exact class, so autoloading will not immediately fail - Composer will register that failure and see if a later autoload definition can load it).

The fact you see different definitions in the autoload_namespaces.php file for required libraries is correct. Composer has to load these from different directories located inside vendor, and your own classes will be autoloaded from the location given in your autoload section in the composer.json file.