1

Given the following directories at the root of a project:

  • app/, A combination of static resources and class-mapped project code
  • src/, Configured to load namespaced PHP code
  • vendor/, Packages managed by composer.json

Regardless of what framework I happen to be using, would it be correct to make use of the src directory for portable code intended to be usable outside of the project, or is that the role of the vendor directory?

Are there any issues that might arise as a result of using the src directory for code intended to be shared?

Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112

2 Answers2

1

This seems like a reasonable design approach. Composer lets you do this by putting your own classes to be autoloaded into a directory and then referring to it in your composer.json . See this question for how: Using Composer's Autoload

However, this won't handle versioning of your code under src/ . If you are going to be separately publishing this, it might be worth learning how to make your library installable through Composer and once you have checked it in to a Git repo somewhere, reference it like this:

"require": {
    "me/testlib": "1.0.*"
}
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/username/hello-world"
    }
]

Sure, then you'll be maintanining multiple source trees, one for your project and one for each library, but that's a good thing right? ("Separation of concerns".)

Community
  • 1
  • 1
Alastair Irvine
  • 1,166
  • 12
  • 16
  • So, while the directory structure itself is good, you wouldn't suggest putting code intended to be portable outside of `vendors`? – Alexander Trauzzi Mar 12 '14 at 08:03
  • `vendors/` is where composer installs stuff. Don't put your own stuff there, unless you are getting it from a repo via composer as per my suggestion. And I'm not certain I understand your question, but I think an appropriate answer is "I recommend against putting your own libraries in the same source tree with your program". – Alastair Irvine Mar 12 '14 at 08:13
  • Yeah, what I meant by "putting code in vendors" would be synonymous with "author a package". And you're suggesting the `src/` directory is still project level and specific to the entire solution: A developer wouldn't plan to be manually copying that entire directory between projects for code reuse, instead they should just be making a package for `vendors/`? – Alexander Trauzzi Mar 12 '14 at 08:21
0

Your approach looks like a valid one in the general case.

It will however become invalid if the amount of data app or anywhere else outweighs the amount of usable code in src. In other words: If you only host about one shared class in src, and one gigabyte of assets in app that isn't needed for the class, it would be stupid to make this a library.

But in such situation, it would be easy to simply move that class into a composer library on it's own and again include it. Because of autoloading the class will still be there (although the file changed it's location) and be usable in other code. Any any other software requiring the whole bunch of assets will continue to work even if that class is moved.

When moving code into libraries, it might be worth considering that smaller usually is better, but too small is worthless. You don't want to require every single class on it's own - but a reasonable collection of classes with the same topic make an excellent candidate.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • And the src/ directory is obviously not the correct place to put code intended t be used across pages, correct? – Alexander Trauzzi Mar 12 '14 at 21:48
  • You can put code wherever you like, and in fact `src` is a common choice for many libraries, together with `tests` right next to it. I can't see why this should be bad - I am using it myself. – Sven Mar 13 '14 at 08:08
  • That seems like a misuse as composer designates vendor/ as the directory to manage external dependencies. Any other directories should be project specific. – Alexander Trauzzi Mar 13 '14 at 11:26
  • 1
    `vendor` is the place where Composer puts *other* code, not yours. `src` is the place where you can put *your* code and make it available for others by defining a `composer.json` file to allow autoloading and including that code. So if the project you ask about gets used somewhere else, *only then* would the code be inside that `vendor` folder together with your projects dependencies (that will be in your own `vendor` folder now). – Sven Mar 13 '14 at 13:39
  • That's exactly what I'm saying. So if I have code I want reused between projects, I should create a package and have it loaded into the `vendor` directory. – Alexander Trauzzi Mar 13 '14 at 13:41
  • 1
    And what I am saying is that if you have a project that has some code you want to share, and the amount of useless data in there is low, you already *have* that project you want to include somewhere else - just make it includeable via Composer. – Sven Mar 13 '14 at 14:34