RSS
 
30. Mar. 2014

Using Composer

30 Mar

There are quite a few good articles on this topic, already. Especially on how to leverage it for CakePHP projects.

See

So I won’t go into detail too much.
It is worth noting that composer really made a few things easier for us in the long run.
And I was really against composer and packagist at the beginning as I didn’t want to use more dependencies than necessary.
But that one sure is worth it if you use it right. And as every project pretty much uses it these days it is
fairly stable and future proof to rely upon.

Installation

Easiest way:
Download the phar and run php composer.phar [command]. You can just place this file into your app dir.
You can also install it more globally, of course to just have to run composer [command] directly. The above articles or the composer docs explain that in more detail.

Usage

In general you can either use the composer commands to add dependencies or you can directly modify the composer.json file via editor (note that some IDEs support that natively with auto-completion).
You also need to distinguish between "install" and "update" command. The first respects the current composer.lock file. The latter should only be used for development/staging, not for productive systems.
See the basic usage docs for more details.

Composer and CakePHP

Can I switch my 2.x git project from submodules to composer?

Most definitly, yes. And you probably should.
Using submodules really is a great annoyance, especially with the number of plugins increasing in a project.
Unfortunately, getting rid of them is also not always easy – but doable.

You need to

  • git submodule deinit <path/to/plugin>
  • git rm <path/to/plugin>

Which, under windows, for example can proof a little bit difficult sometimes.

Then re-add them to the composer file and don’t forget to exlude the folder in the .gitignore file.

Composer and CakePHP3.x

In 3.x composer will pretty much be the de facto standard for CakePHP. So it is worth getting used to it now.

A few tips and tricks

I can add least talk about a few things I came across working with composer now for a few months.

VCS

Version control system repositories can easily be included if they don’t support composer or if you need to run a modified version of a 3rd party plugin/dependency.
We then include the main source in the "require" section and add our specific (tweaked) fork as VCS in "repositories":

"require": {
	"josegonzalez/cakephp-wysiwyg" : "dev-master",
},
"repositories" : [{
		"type" : "vcs",
		"url" : "https://github.com/MyOwnNameSpace/cakephp-wysiwyg"	
}],

Our version then contains some bug-fixes or additional stuff that needed it to branch off here.

dev prefix/suffix?

It kind of confused me when to use the suffix and when to use the prefix for "dev".
I found quite a look explanation from someone I added to this PR:

Basically dev releases are now simply branch names with a dev suffix for numeric branches which are comparable (3.0.*-dev) – or a dev prefix for textual names that are not comparable, like feature branches and master (dev-master)

You should authenticate the server/locally to avoid hitting the API limit

This page describes on how to generate an OAuth token and use this to avoid hitting the limit of requests per minute for GitHub.

Use proper user management to run composer commands

If you log in as root or any user that is not affialited with the www-data user, the files that are updated/created will most likely not be readable/usable.
And using chmor -r 777 * sure is a really bad idea, as well. It is better to create a user that shares the same rights/group as www-data and use this one to deploy or do any changes on the code.

If something aint working

Try it again first – sometimes there are failures in the communication between GitHub, Packagist and your local connection.

Also try the self-update command to make sure you are running a recent version of composer. diagnose can also tell you of there is something wrong.
Maybe you forgot to enable some required PHP extensions, like "curl" (which is necessary for it). But the error message usually tells you what exactly what the issue is.

Last Tip

You can also take a look at my enhanced Upgrade shell. Maybe it can help getting your code to the latest code-base.

0.00 avg. rating (0% score) - 0 votes
 
 

Tags: , , ,

Leave a Reply

Tip:
If you need to post a piece of code use {code type=php}...{/code}.
Allowed types are "php", "mysql", "html", "js", "css".

Please do not escape your post (leave all ", <, > and & as they are!). If you have encoded characters and need to reverse ("decode") it, you can do that here!