“PHP Composer” compared with “Ruby Gems and Bundle

2019-03-19 07:33发布

问题:

(As first, this question is not "which is better" question. I just want to know how they are similar and differ in functionality perspective.)

I'm php developer and currently learning ruby. In these days, many php developers are encouraged to use Composer for dependency management. Before asking questions, I would like to validate my knowledge first.

According to my understanding,

  • Composer is a dependency management tool for php libraries (or packages).
  • Composer installed php library per project basis (so-called locally). I mean php library installed for project1 cannot be reused for project2 without install again for project2.

I've noticed that Ruby also has very good dependency management tool "RubyGems". According to my knowledge,

  • "RubyGems" is a package manager for "Gems".
  • "Gem" is a Ruby program or library packaged in a standard format for sharing. (Rails is also a gem.)
  • Gem can be installed by command like that gem install sinatra.
  • But, there is also so-called "Bundler" which is also a gem for bundling gems for an application.
  • When command bundle install runs (under specific ruby project directory), all the gems listed in the Gemfile are installed for this ruby project.

So, my questions are here.

  1. Composer is similar to whether RubyGems or Bundler ?
  2. When run gem install sinatra, is it installed on system-wide level ?
  3. When run bundle install, the Gems are installed whether locally (on this project only) or system-wide level ?
  4. If I need two versions of one gem (for e.g. sinatra) for different projects, how should I handle ?

(Sorry for my long question. If my understandings are something wrong, sorry again and please point out the right one.)

回答1:

1) Composer is more similar to bundler. Composer brings everything in your project, bundler brings everything to your system and "links" them in the context of your project. Bundler is working with gems in the back.

2) yes. gem install does things system-wide (or per user if you use something like rbenv or rvm)

3) see 1. system wide and are correctly picked according to the Gemfile when you run things through bundle exec

4) I recommend using a Gemfile, putting the version you're interested in there and letting bundler do the rest (it will in the back install multiple versions and pick the right one). Be sure to run "bundle exec" though. You also have the option of using an rvm gemset if you're into rvm but that's harder to handle and you will have a really bad time when attempting to deploy.