Search code examples
phpgitphpunitproject-organization

Using git and submodule, what's a good folder structure?


I use git with submodules, and I've hard time to figure out how to organize my files. I develop in PHP, and use unit testing. So right now, I use this organization for each module:

  • src/
  • tests/

That seems like a brilliant idea, but the problem is that when I do a "git submodule add" to a project, I'll have that path: project/modules/News/src/index.php

The src/ folder is really problematic.

Should I simply put all my file in the module root, and have a tests/ folder mixed in the source ? To me that sound bad. What are you doing ?

Edit: The src/ Folder is problematic because of the autoload. I should not have to put "src" in all my class name...


Solution

  • Your folder layout is mostly irrelevant as long as the autoloader can find your files somehow. If you are using PEAR convention for mapping class names to their source files, you can add the src directory to the include path or stack a second autoloader. Then you dont have to add src to the class names.

    The alternative to PEAR convention would be to use a static mapping between files and classes. There is a tool that can automatically generate such an autoloader for you at GitHub.

    The static autoloader approach is also used in PHP Project Wizard. That tool will create src and tests folders, including your phpunit config and the build file to connect your project with Jenkins CI. It's a convenient package.

    As for including submodules, consider putting them into a lib or ext folder. An example of how that looks can be found in the phpdox project at GitHub. Make sure you also look at the main bootstrap file to see how to include the various autoloaders then.