Vivendi
Vivendi

Reputation: 21007

How to use composer to manage custom and core libraries in a framework

I have a small custom made mvc framework that I use to built sites with. I also use a lot of 3rd party libraries. Some of those are "core" libraries for my framework. I'd like to use Composer now to manage those libraries.

So for example my framework comes default with the Smarty template engine and with a Logger class called Monolog.

On the other hand, the users/developers that use my framework can also install 3rd party libraries them selves. Like if they want to use something like SwiftMailer. Or use the Twig templating engine instead of Smarty. They can easily do that.

Currently my application is structured like this:

/htdocs
  /System
    /Vendors
      /Smarty
      /Monolog
  /Controllers
  /Models
  /Views
  /Vendors
    /Swiftmailer
    /Twig

So all "core" libraries are placed in the System/Vendor directory. The framework comes default with these libs.

Users can install additional libraries and they will be placed in the Vendors directory which is in the root of the web folder.

If I use composer I can easily install libraries like SwiftMailer and Twig. Composer would automatically install these libraries into the htdocs/Vendors directory for me.

But what about the libraries that are in the htdocs/System/Vendors directory? Can I manage both Vendors directories with composer? Or should I do two installs of composers? So one in the root of my web directory (htdocs) and one in the System directory?

Or shouldn't I use composer at all for the libraries in the System/Vendors folder?

I'm not sure how to manage both Vendors directories. What is the best practice for this?

Upvotes: 0

Views: 700

Answers (2)

Sven
Sven

Reputation: 70863

Your code belongs in your version control system and should get a composer.json declaring the autoloading of your code - and should mention if you need third party code as well - and the required version.

You should not include third party code in your own code base. If you do, things get more complicated.

For example, if someone uses your framework, and decides to also use a different version of Smarty than you do internally, this version conflict shouldn't go undetected. So if you only define the autoloading for Smarty yourself, you do not prevent anyone from including the Smarty dependency in their own software. Now one of two things can happen: Either your own autoloading for Smarty gets executed first, or the other Smarty autoloading is first. Either way, things will break without leaving a trace of what went wrong. In the worst case, the class with the feature to ask for the version number gets loaded correctly, and anything else gets messed up...

So the correct way is to only embed these third party libraries with Composer, but not include them inside your own code base. This will also reduce the download size for your framework. Starting from this, the world will see which libraries you already use, and Composer can detect version conflicts and alert the other developer who tried to mix incompatible versions. Or can help grab only one version of that library instead of two (reduced file size, no ambiguities when autoloading).

Upvotes: 1

Wouter J
Wouter J

Reputation: 41934

I would suggest to use only one vendors folder, why do you want to seperate them?

I also want to suggest to decouple the framework itself from the project. I would create a package for the framework and require than within the project. Composer will take care of all dependencies and everything will be installed for the project in the project's vendor directory.

Upvotes: 1

Related Questions