How to make changes on your own repositories imported with Composer

composerIn a previous article, I talked about reusing and exporting code. This article is more practical, and it’s about implementing that with PHP.

In the PHP world, composer is the de-factor tool to put together, solve dependencies and autoload external libraries (the tons you can find on github) or your own repos (that means everything non-application-specific that you can export and it’s not already decently implemented by anyone else).

Export your libraries into a public github repo 

Say you locate some code that is non-application-specific and you want to export it into another public repository. To do that, create a new Github repo, using this skeleton for example, and add there your libraries, tests and composer.json with autoload information. Commit and push.

Use your new library on your main project

On your main project, add the require for your repo, and specify source as a preferred install. See the example below.
composer repo

You can now run

composer update your-github-username/your-repo

And have your repository autoloaded into your main one.

Make changes to your repo

You can make changes your your imported repository with your IDE directly inside the vendor/your-github-username/your-repo directory.

If you want to commit a change, bash into the vendor directory, commit and push the change.

Before you commit changes (that use the modified version of your imported repo) on your main project, remember to run

composer update

so that the composer.lock file will now point at the new version.

Point to a specific commit hash

If you want to point your main repo at a specific commit of your imported repo, get the commit hash (git log), then in your composer file use

dev-master#adbd92dcf17b4ca5ecf5de61964a01daf9f92ebd

instead of

dev-master

If you want to make changes to an existing github repository that is not yours

If you need to use your fork instead an official repo, please refer to another article on this blog about using your own forks with composer. Making changes will be the same as described above. If you need to pull changes from the original repository, just add the remote origin (git remote add upstream <originalRepoUrl)). Refer to git remote operations for that.

 

Firefox user preferences with selenium (behat+mink) to bypass a proxy or disable cache

At work we are testing an existing application with behat+mink. We normally use goutte driver, but in some cases (when javascript is needed) we need to use selenium server piloting firefox.

When using firefox, the page is loaded including external javascript (google analytics, network ads etc..) so it needs connetivity. An alternative is disabling external requests on the testing environment or use a fake DNS entry, but that wasn’t a viable solution for us.

A small problem I had was bypassing the office network proxy, that requires a proxy auto config file (PAC) when firefox was running. You can find many links online mentioning the various firefox preferences you can set, for example network.proxy.http and network.proxy.http_port to specify a proxy, or network.http.use-cache to enable/disable the browser cache.

In my configuration (ubuntu 12.04, with selenium 2.28, firefox 18.0.1), the preferences file read by firefox is located at /usr/lib/firefox/defaults/pref/proxy.js, where you can add the various calls to user_pref([settings], [value]) .

Here an example of how I changed the puppet manifest of the box to make things working for us

file { "/usr/lib/firefox/defaults/pref/proxy.js":
 content =>
'user_pref("network.proxy.autoconfig_url", "http://chante-with-your-value/default.pac");
user_pref("network.http.use-cache", false);
user_pref("network.dnsCacheExpiration", 1);
user_pref("network.proxy.type", 2);'
}

Composer: how to use your github forks

Scenario

Imagine you are using a github project in your composer file

"require": {
  "original-user/project": "dev-bugfix"
}

Now, this dependency does not do exactly what you want, so you want to change it and use your fork instead (and potentially submit a pull-request).

How to use your fork

Just add your repository into composer.json

"repositories": [
   {
     "type": "vcs",
     "url": "https://github.com/mygithub-username/project"
   }
  ]

Important: Do not change the “require” part, it must continue using original-user/project.
Run a “./composer.phar update” and composer will then download your fork (even though it echoes “installing  original-user/project” during the operation).

Go into “vendor/original-user/project” and run “git remote -v” for a double check, you’ll see the remote source “composer” that is your fork, and “origin” that is the original project. From there you can commit the changes to your fork and update it from origin (git pull origin master).

 

documentation

Composer

Loading a package from a VCS repository# There are a few use cases for this. The most common one is maintaining your own fork of a third party library. If you are using a certain library for your project and you decide to change something in the library, you will want your project to use the patched version. If the library is on GitHub (this is the case most of the time), you can simply fork it there and push your changes to your fork. After that you update the project’s composer.json. All you have to do is add your fork as a repository and update the version constraint to point to your custom branch. For version constraint naming conventions see Libraries for more information. Example assuming you patched monolog to fix a bug in the bugfix branch:

GIT: discard local changes and commit after a merge/pull

git – How can I discard remote changes and mark a file as “resolved”? – Stack Overflow

git checkout has the –ours option to check out the version of the file that you had locally (as opposed to –theirs, which is the version that you pulled in). You can pass . to git checkout to tell it to check out everything in the tree. Then you need to mark the conflicts as resolved, which you can do with git add, and commit your work once done:

Git pre-commit hook to check PHP syntax

Similarly to SVN, Git supports hook scripts. They are located under

<workcopy>/.git/hooks/

applypatch-msg.sample  post-update.sample     prepare-commit-msg.sample
commit-msg.sample      pre-applypatch.sample  pre-rebase.sample
post-commit.sample     pre-commit             update.sample
post-receive.sample    pre-commit.sample

To check the syntax before committing, I found an interesting script

https://github.com/ReekenX/git-php-syntax-checker/blob/master/pre-commit

Works great for me,

save into .git/hooks/pre-commit

GIT: how to set a private repository on your server

What you need

  • Server side: SSH access to the server + Git installed
  • Client side:Git shell ((Linux shell access, or Gui Bash for Windows)

Set up remote repository

#login to your server
ssh elvis@phpntips.com
# create repository
mkdir ~/myproject.git
# create empty repository
cd ~/myproject.git
git init --bare

Server: Set SSH keys

# only if you do not have an identity (~/.ssh/id_rsa.pub)
ssh-keygen -t rsa
# copy public key to the server
cat ~/.ssh/id_rsa.pub | ssh elvis@phpntips.com "cat >> ~/.ssh/authorized_keys"

in case youv’e specified a password for the RSA keys, and the password is asked all the time, use ssh-agent

eval `ssh-agent`
ssh-add
# If the keys are set properly, you can now login to the server without specifying any password
ssh elvis@phpntips.com

Local machine: Clone the repository

cd ~/myProject
git clone ssh://user@phpntips.com/~/myproject.git
# make changes and commit
git add
git commit -a && git push

Some GIT tutorials / reference
Official Git tutorial
Git tutorial if you already know SVN
Git reference

GIT: first impression, pros and cons for single user projects

I’ve recently moved a personal project of mine from SVN to GIT.
As I’m the only committer, I’m not using a lot the merge/branching and I do not need to navigate into the history so often.
However, my opinion is that GIT is better for those kind of projects too

Advantages

  • speed: GIT compresses the files before sending/receiving and speeds up the network operations by an order of magnitude faster than SVN. It also made unnecessary the use of ZF libs using svn:external, now only one fast repository.
  • only one .git directory at the root level, instead of .svn dirs at each level as SVN does. That made easy the deploy, and make useless the svn export operation.- local repository, fast history navigation

Disadvantages

  • IDE integration: currently there is a stable plugin for Netbeans 7.0, good to commit and show the modified files/lines, but some features are not yet implemented (like the integrated SVN annotations showing the author of each line, and the navigation of the history directly from the IDE) and I have to use the shell for that.
    [update 10/7-11 : the new git plugin (nb dev version, or manual update) implements history navigation and diff, and show annotations too]
  • The process to set up a GIT repository on your server is a bit more complicated than SVN, when done via SSH with keys (and a bit more tricky on Windows). I’ll write a quick guide in the next post.

I’m using it in the office too, and the branch/merge advantages, local history actually speeds up development (and deploy)

Eclipse hangs on SVN commit

I’m currently using Eclipse PDT at work (build 20100917-0705). I usually prefer Netbeans as more stable and with more or less the same features, but the application I’m working on contains a huge amount of classes and the eclipse quick search (open resource) is extremely useful to navigate and open files (currently better and faster than Netbeans file search).

The most annoying bug of Eclipse I’ve found happens when the IDE hangs when commiting with SVN. It needs to kill the process, restart, clean svn, reinsert SVN password and commit again (after having lost the SVN comment inserted), enjoyable isn’t it ?

After some googling (read here) it seems that the problem is due to a limit on the console output.

Untick the option Limit Console Output under Eclipse Preferences ->Team -> SVN -> Console and the problem should be solved.

Map svn inside another svn with svn:externals. Example with Zend Framework and Netbeans

SVN supports external definitions.

Basically, it allows to map a subdirectory of our SVN working copy to an URL of another SVN repository. See the link above for more details.

That’s can be useful for example to mount zend framework libraries in our repository, in order to have a smaller and faster svn as well as an automatic update of the libraries directly from the Zend repositories (instead of download them again and add to the working copy, that is a very long process with the ~3k ZF files).
Continue reading