<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jon Cairns &#187; Tech</title>
	<atom:link href="http://joncairns.com/c/tech/feed/" rel="self" type="application/rss+xml" />
	<link>http://joncairns.com</link>
	<description>Web dev, musical experimenter, design enthusiast and committed Christian.</description>
	<lastBuildDate>Tue, 15 May 2012 14:21:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Vim as a PHP IDE</title>
		<link>http://joncairns.com/2012/05/using-vim-as-a-php-ide/</link>
		<comments>http://joncairns.com/2012/05/using-vim-as-a-php-ide/#comments</comments>
		<pubDate>Wed, 09 May 2012 13:34:49 +0000</pubDate>
		<dc:creator>joonty</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpqa]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://joncairns.com/?p=448</guid>
		<description><![CDATA[This isn&#8217;t a blog about why you should use Vim. That&#8217;s obvious. If it isn&#8217;t, read Why do those nutheads use Vi? Instead, this a tutorial about using Vim as a fully-fledged IDE, and specifically for PHP projects. Many Vim users &#8230; <a href="http://joncairns.com/2012/05/using-vim-as-a-php-ide/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This isn&#8217;t a blog about why you should use Vim. That&#8217;s obvious. If it isn&#8217;t, read <a title="Why you should use Vi" href="http://www.viemu.com/a-why-vi-vim.html" target="_blank">Why do those nutheads use Vi?</a> Instead, this a tutorial about using Vim as a fully-fledged IDE, and specifically for PHP projects. Many Vim users would only use it for dipping in and out of the odd file on their computer, or maybe just on a remote server during an SSH session. I&#8217;m here to tell you that it can be configured to be more efficient and productive than your favourite IDE.</p>
<ol>
<li><a href="#why">Why bother?</a></li>
<li><a href="#starting">Starting out with Vim</a></li>
<li><a href="#highlighting">PHP syntax highlighting</a></li>
<li><a href="#syntax">PHP syntax checking/linting</a></li>
<li><a href="#tags">Tags and auto-completion</a></li>
<li><a href="#csmd">Codesniffer and mess detector support</a></li>
<li><a href="#phpunit">PHPUnit support</a></li>
<li><a href="#xdebug">Xdebug integration</a></li>
<li><a href="#myvim">Everything in a neat package</a></li>
</ol>
<p>If you&#8217;re impatient and want to start tinkering straight away, jump to <a href="#myvim">section 9</a> to install my Vim configuration.</p>
<h2><a name="why" href="#"></a>1. Why bother?</h2>
<p>I&#8217;ve used a number of IDEs for PHP projects over the past few years: Eclipse, Aptana, Komodo Edit/IDE, Netbeans and PHPStorm. The problem with many of them is speed and resource usage: they eat away at your memory and hog your CPU. Another problem is keyboard shortcuts and commands: it is impossible to do everything using the keyboard, and many things are quicker with the mouse, requiring you to constantly swap between keyboard and mouse. This may not sound like a big deal, but spend a few months learning Vim and it will be a big source of frustration.</p>
<p>So, the answer to that question is this: in the long run, you will be a lot more efficient. Jump to exactly the right place in a file in just a couple of key strokes; open a horizontal split window comparing two files with a command as simple as <code>&lt;Ctrl-w&gt; + s</code>; swap between open files by number (the order of opening); and my favourite &#8211; view and navigate a directory tree with all the usual Vim functionality.</p>
<h2><a name="starting" href="#"></a>2. Starting out with Vim</h2>
<p>If you&#8217;ve never used Vim before, try<a href="http://www.viemu.com/a-why-vi-vim.html" target="_blank"> the tutorial</a> I mentioned at the top of this post. Also, take a look at the <a title="Vim cheat sheet" href="http://www.tuxfiles.org/linuxhelp/vimcheat.html" target="_blank">Vim cheat sheet</a>.</p>
<p>The rest of this tutorial assumes that you have a working knowledge of Vim and know how to configure it on at least a basic level (i.e. you know where your <em>vimrc</em> file is, and you know how to install vim scripts). It also assumes that you&#8217;re running at least version 7.0 of Vim.</p>
<p>If you don&#8217;t know how to install Vim scripts I&#8217;d recommend using <a title="Vundle package manager for Vim" href="https://github.com/gmarik/vundle" target="_blank">Vundle</a>, which will manage all your plugins for you and make installing/removing a breeze.</p>
<h2><a name="highlighting" href="#"></a>3. PHP syntax highlighting</h2>
<p>Vim understands PHP by itself, but you can get improved highlighting and basic syntax checking with the <a title="php.vim plugin for improved PHP syntax highlighting" href="http://www.vim.org/scripts/script.php?script_id=1571" target="_blank">php.vim</a> plugin. This will add better support for certain PHP keywords like <strong>define</strong> and <strong>static</strong>, etc., and will allow you to apply custom colours to these keywords. Also, it puts some basic checks in place for invalid syntax, and will highlight the line if you don&#8217;t close your brackets, for instance.</p>
<h2><a name="syntax" href="#"></a>4. PHP syntax checking/linting</h2>
<p>Most IDEs will run the PHP parser on the file periodically, and warn you of any errors such as invalid syntax and multiple declarations of classes/functions within the same file. One of the beautiful things about Vim is that you can run files through command line programs with total ease, so to check your PHP file you could just run:</p>
<pre class="brush: vimscript; title: ; notranslate">
:!php -l %
</pre>
<p>This prints out any error message to the screen. However, we can do better. Vim has a feature called <em>signs</em>, which are used to mark and highlight lines in a file. Wouldn&#8217;t it be nice if we could take the PHP parser&#8217;s error message and highlight the right line in the file?</p>
<p>It would be nice. And it <em>is</em> nice, because it&#8217;s been done. <strong>&lt;shameless plug&gt;</strong> I have a plugin that does it, called <a title="PHPQA tools for Vim" href="https://github.com/joonty/vim-phpqa" target="_blank">PHP QA tools</a> <strong>&lt;/shameless plug&gt;</strong>. Install this, and every time you write a file it will check it for syntax errors and highlight any offending lines in the file. It will also open a quickfix window that displays the error. This window is specially made for listing errors, and you can read about how to use it in <a title="Vim quickfix documentation" href="http://vimdoc.sourceforge.net/htmldoc/quickfix.html" target="_blank">the documentation</a>.</p>
<div id="attachment_459" class="wp-caption aligncenter" style="width: 283px"><a href="http://joncairns.com/wp-content/uploads/2012/05/vim-php-lint.png"><img class="size-medium wp-image-459" title="PHP syntax errors are highlighted and shown in the quickfix window" src="http://joncairns.com/wp-content/uploads/2012/05/vim-php-lint-273x300.png" alt="PHP lint error in Vim window" width="273" height="300" /></a><p class="wp-caption-text">PHP syntax errors are highlighted and shown in the quickfix window</p></div>
<p>The same plugin will be useful for some things that appear later, so read on&#8230;</p>
<h2><a name="tags" href="#"></a>5. Tags and auto-completion</h2>
<p>Code-completion is a feature that most large IDEs provide, along with the ability to jump to class and function definitions within multiple files. All IDEs do this by creating a database of the definitions, and updating this database as you change the code. Many people think that this is a reason not to change to using Vim, as the support isn&#8217;t there; this isn&#8217;t true.</p>
<p>Omnicompletion is a feature of Vim 7+, and it works by reading a tag file generated by a command-line program called <em>ctags</em>. This file contains definitions for all the classes, functions and variables in your project. You then use <code>&lt;c-x&gt;&lt;c-o&gt;</code> (read that as: <code>Ctrl+x</code> followed by <code>Ctrl+o</code> &#8211; that&#8217;s Vim&#8217;s way of showing a Ctrl modifier shortcut) to bring up a list of completions when you are in insert mode. Here&#8217;s a screenshot showing it at work:</p>
<div id="attachment_467" class="wp-caption aligncenter" style="width: 775px"><img class="size-full wp-image-467" title="Omnicompletion in Vim" src="http://joncairns.com/wp-content/uploads/2012/05/omnicompletion.png" alt="Omnicompletion in Vim" width="765" height="400" /><p class="wp-caption-text">Omnicompletion at work</p></div>
<h3>A quick note</h3>
<p>Before we go on, I&#8217;d just like to say that I don&#8217;t use omnicompletion. Instead, I use Vim&#8217;s local keyword completion. There are several reasons for this:</p>
<ol>
<li>There&#8217;s a delay as the context for omnicompletion is determined, and the tags are collected together. This is the case in every IDE I&#8217;ve used &#8211; it makes logical sense that it as a project gets bigger, the impact on performance of autocompletion gets greater.</li>
<li>Local keyword completion is lightning fast, and in many cases saves me valuable seconds (or at least milliseconds!) in typing longer words. This is even more the case with the SuperTab plugin, which I&#8217;ll mention soon.</li>
<li>It only works for words defined in the current file, but it encourages me to be a better programmer by using consistent naming methods for functions and variables, for instance.</li>
</ol>
<div>Even so, the tags files are extremely useful even if not using omnicompletion, as they are used for jumping to definitions quickly. I&#8217;ll explain how to set-up the environment and start generating tag files.</div>
<div></div>
<h3>How to generate Ctags</h3>
<div>You&#8217;ll want to use exuberant-ctags (<a href="http://ctags.sourceforge.net/">http://ctags.sourceforge.net/</a>), which has support for over 40 programming languages, including PHP, of course. Installation is a doddle on Ubuntu (<code>sudo apt-get install exuberant-ctags</code>), otherwise follow the installation instructions on the website for your OS. After installation you should be able to run ctags-exuberant from the command line. Go to the top-level directory of a project containing PHP files and run:</div>
<pre class="brush: bash; title: ; notranslate">
ctags-exuberant -f php.tags --languages=PHP -R
</pre>
<p>This will create a file named php.tags, which contains a summary of all the definitions of PHP constructs in your project. You may need to tweak the parameters in this command: you may want to exclude certain files (e.g. build artefacts, documentation, etc.), or exclude whole languages like JavaScript from being parsed (<code>--languages=+PHP,-JavaScript</code>). When you&#8217;re happy with your tags file, you need to tell Vim to use it:</p>
<pre class="brush: vimscript; title: ; notranslate">
:set tags=~/path/to/php.tags
</pre>
<p>You can use more than one tag file at a time: I generate a tag file for my entire PHP include library (i.e. PEAR stuff), a tag file for the framework I&#8217;m using and then a tag file for each project, and set all three to be used with a comma separated list.</p>
<p>Now that you&#8217;ve set up your tags, try it out. Open up a new PHP file, set the tag file and start typing the name of a class that you know exists. After a few characters type <code>&lt;c-x&gt;&lt;c-o&gt;</code> (insert mode) and the omnicompletion will kick in. Then, on the completed class name in command mode, type <code>&lt;c-]&gt;</code>. You will be taken to the class definition, or given a list of definitions if it matches more than one.</p>
<p>Another neat feature is jumping to a tag from the Vim command line. For example, if you know you need to get to the file containing the class <code>MyLovelyClass</code>, just type:</p>
<pre class="brush: vimscript; title: ; notranslate">
:tag MyLovelyClass
</pre>
<p>And hit return. You will be taken straight to it. This is almost always quicker than trying to navigate a file tree to find the file you want. Also, this supports tab completion, so you could just type <code>:tag My&lt;tab&gt;</code> and you would get a list of options.</p>
<p>If you&#8217;re going to use omnicompletion, definitely install the <a title="phpcomplete Vim plugin" href="http://www.vim.org/scripts/script.php?script_id=3171">phpcomplete plugin</a>, as this adds better context awareness for omnicompletion in PHP. I&#8217;d also recommend installing <a title="SuperTab Vim plugin" href="http://www.vim.org/scripts/script.php?script_id=1643" target="_blank">SuperTab</a>, and adding this to your <em>vimrc</em>:</p>
<pre class="brush: vimscript; title: ; notranslate">
let g:SuperTabDefaultCompletionType = &quot;&quot;
</pre>
<p>Now you just press <code>Tab</code> to trigger omnicompletion.</p>
<h2><a name="csmd" href="#"></a>6. Codesniffer and mess detector support</h2>
<p>If you&#8217;re not familiar with these tools then you may want to skip this section, as you will only want this support if you already use them. Support for these comes bundled in the <a title="PHP QA tools Vim plugin on vim.org" href="http://www.vim.org/scripts/script.php?script_id=3980" target="_blank">PHP QA tools</a> plugin (view on <a title="PHP QA tools for Vim" href="https://github.com/joonty/vim-phpqa" target="_blank">github</a>), which you will already have installed if you&#8217;re following each step of this tutorial. The plugin parses their output, puts the violations into a quickfix window and puts line markers in the file (uses the marker <strong>S&gt; </strong>for codesniffer and <strong>M&gt;</strong> for mess detector violations). Nice and simple.</p>
<div id="attachment_461" class="wp-caption aligncenter" style="width: 310px"><a href="http://joncairns.com/wp-content/uploads/2012/05/vim-qatools.png"><img class="size-medium wp-image-461" title="Codesniffer and mess detector signs are shown in the margin, and messages in the quickfix window" src="http://joncairns.com/wp-content/uploads/2012/05/vim-qatools-300x259.png" alt="PHP mess detector and codesniffer support in Vim" width="300" height="259" /></a><p class="wp-caption-text">Codesniffer and mess detector signs are shown in the margin, and messages in the quickfix window</p></div>
<p>Everyone who uses these programs has their own set of configuration rules, and happily this plugin allows you to change the configuration to suit. You can set various options in your <em>vimrc</em> file:</p>
<h3>CODESNIFFER</h3>
<pre class="brush: vimscript; title: ; notranslate">
&quot; Pass arguments to phpcs binary
let g:phpqa_codesniffer_args = &quot;--standard=Zend&quot;
&quot; Another example
let g:phpqa_codesniffer_args = &quot;--standard=/path/to/xml/file.xml --tab-width=4&quot;

&quot; PHP codesniffer binary (default = phpcs)
let g:phpqa_codesniffer_cmd='/path/to/phpcs'

&quot; Run codesniffer on save (default = 1)
let g:phpqa_codesniffer_autorun = 0
</pre>
<h3>Mess detector</h3>
<pre class="brush: vimscript; title: ; notranslate">
let g:phpqa_messdetector_ruleset = &quot;/path/to/phpmd.xml&quot;

&quot; PHP mess detector binary (default = phpmd)
let g:phpqa_messdetector_cmd='/path/to/phpmd'

&quot; Run mess detector on save (default = 1)
let g:phpqa_messdetector_autorun = 0
</pre>
<h2><a name="phpunit" href="#"></a>7. PHPUnit support</h2>
<p>If you use PHPUnit then you can run tests from the Vim command window with another one of my plugins: <a title="PHPUnit QF plugin on vim.org" href="http://www.vim.org/scripts/script.php?script_id=4054" target="_blank">PHPUnitQF</a> (view on <a title="PHPUnit QF plugin for Vim" href="https://github.com/joonty/vim-phpunitqf" target="_blank">github</a>). This parses the output from PHPUnit tests, and any failures or errors are put into a quickfix window, allowing you to easily jump to the tests that weren&#8217;t successful.</p>
<p>To run the tests, just execute:</p>
<pre class="brush: vimscript; title: ; notranslate">
:Test
</pre>
<p>Where <em>&lt;arg&gt;</em> is passed straight to the <strong>phpunit</strong> command. For example, if you would normally run <code>phpunit MyClassTest</code> from a command line, you run <code>:Test MyClassTest</code> from Vim. If you have a standard set of arguments that you pass to phpunit (e.g. a configuration file), you can set a variable in your <em>vimrc</em> file. Here are the available configuration options:</p>
<pre class="brush: vimscript; title: ; notranslate">
&quot; Arguments to pass to the phpunit binary
let g:phpunit_args = &quot;--configuration /path/to/config&quot;

&quot; Location of phpunit
let g:phpunit_cmd = &quot;/usr/bin/mytest&quot;

&quot; Temporary file used by the plugin
&quot; (shouldn't need to change unless runing on Windows)
let g:phpunit_tmpfile = &quot;/my/new/tmp/file&quot;
</pre>
<p>You can use any test wrapper whose output is the same format as PHPUnit. For instance, I sometimes use the CakePHP framework which has a wrapper around the PHPUnit command, so I just change <code>g:phpunit_cmd</code> as shown above.</p>
<p>You can also view the full output of the last test run with:</p>
<pre class="brush: vimscript; title: ; notranslate">
:TestOutput
</pre>
<p>This opens the output in a split window.</p>
<h2><a name="xdebug" href="#"></a>8. Xdebug integration</h2>
<p>That&#8217;s right. You can use Vim as a debugger client with Xdebug, so it throws the same punches as the big boy IDEs. In fact, I think that the Xdebug plugin for Vim allows more flexibility and functionality than any other implementation I&#8217;ve found. It&#8217;s certainly easier to tweak and modify it. It&#8217;s also great out of the box, and you can get it up and running within a few minutes.</p>
<p>This plugin has a bit of a legacy: it started <a title="The original Vim Xdebug script" href="http://www.vim.org/scripts/script.php?script_id=1152" target="_blank">here</a>, a script by Seung Woo Shin, and has been modified and forked like mad. It goes without saying that I have my own version of it, which you can get from the <a href="https://github.com/joonty/vim-xdebug" target="_blank">github repository</a>.</p>
<p><em>Note: by all means use another version if you wish, but I have added quite a few features that I use all the time, so they might be useful to you too. Look at the README in the repository for a list of changes.</em></p>
<p>After installing the plugin you will need to set up Xdebug on your machine. To do this, follow step 1 and (optionally) 4 and 5 on my <a title="Set up Xdebug with Netbeans and LAMP (and CakePHP)" href="http://joncairns.com/2012/01/set-up-xdebug-with-netbeans-and-lamp-and-cakephp/" target="_blank">Xdebug, LAMP and Netbeans tutorial</a>.</p>
<p>Now you can set breakpoints in your code with <code>F1</code> or:</p>
<pre class="brush: vimscript; title: ; notranslate">
:Bp
</pre>
<p>Press <code>F5</code> to make Vim try and connect to the debugger (on port 9000 by default), where it waits for 30 seconds before timing out. During the 30 seconds, if you load the webpage or run the script then the session will start. One of my changes is to open the session in a new tab, to not disrupt the window configuration in your current tab, so you will see a new tab and a few windows open up. It will look something like this:</p>
<div id="attachment_475" class="wp-caption aligncenter" style="width: 310px"><a href="http://joncairns.com/wp-content/uploads/2012/05/vim-xdebug.png"><img class="size-medium wp-image-475" title="The Xdebug Vim plugin at work" src="http://joncairns.com/wp-content/uploads/2012/05/vim-xdebug-300x144.png" alt="Snapshot of a Xdebug session in Vim" width="300" height="144" /></a><p class="wp-caption-text">Windows from left, going clockwise: source code pane, watch window, current stack trace, command window</p></div>
<p>The left window shows the source code and typically the current execution point. The watch window is one of the key features, as it shows the result of getting the context (i.e. all the variables and their values at the current execution point) and <code>eval</code> results.</p>
<p>To start stepping through the program line-by-line, press <code>F3</code>. The <strong>-&gt;</strong> marker will move as the program moves on. Execution will pause at any breakpoints that you&#8217;ve set up. To step into a function on the current execution line press <code>F2</code>, and <code>F4</code> to step back out.</p>
<p>To get the current context, press <code>F11</code> &#8211; the watch window will show all variables and their values. You can also evaluate expressions, by typing:</p>
<pre class="brush: vimscript; title: ; notranslate">
e
</pre>
<p>(The <code>Leader</code> key is usually \ or <code>,</code>) You can then type a PHP expression (e.g. a variable, or any valid expression like <code>sqrt(100)</code>), press enter, and the result will be printed in the watch window.</p>
<p>When you&#8217;ve finished, press <code>F6</code> to end the session and close all the windows.</p>
<p>That&#8217;s enough to get you started, but you can read the README on the Github repository for more information.</p>
<h2><a href="#" name="myvim"></a>9. Everything in a neat package</h2>
<p>Maintaining a Vim configuration is a tricky business. Sometimes you make changes that you think will be helpful, find it annoying after a couple of weeks, and you want to revert back to an old version. That&#8217;s exactly why I keep mine under version control &#8211; you can view it <a title="My Vim configuration on Github" href="https://github.com/joonty/myvim" target="_blank">here</a>.</p>
<p>If you want to use this configuration, there are a couple of things you&#8217;ll need to install. You&#8217;ll need <em>exuberant-ctags</em>, as talked about in section 5. I&#8217;d also recommend installing the <a title="Anonymous Pro font" href="http://www.fontsquirrel.com/fonts/Anonymous-Pro" target="_blank">Anonymous Pro</a> font if you&#8217;re using GVim, as it&#8217;s the best code font I&#8217;ve come across.</p>
<p>To install, clone the repository to <em>~/.vim</em>, and initialize the submodule. Then, install the Vundle bundles:</p>
<pre class="brush: bash; title: ; notranslate">
git clone git://github.com/joonty/myvim.git ~/.vim --recurse-submodules
vim +BundleInstall +qall
</pre>
<p>What&#8217;s in the package? Along with everything mentioned in this tutorial, it has the following plugins:</p>
<ul>
<li><a title="NERDTree Vim plugin" href="http://www.vim.org/scripts/script.php?script_id=1658" target="_blank">NERDTree</a>: shows the directory tree, and allows for navigation and modification. Completely essential in my eyes.</li>
<li><a title="MiniBufExpl Vim plugin" href="http://www.vim.org/scripts/script.php?script_id=159" target="_blank">MiniBufExpl</a>: (Mini buffer explorer) shows the open buffers in a kind of tab format, allowing you to easily manage multiple files.</li>
<li><a title="Tag List Vim plugin" href="http://www.vim.org/scripts/script.php?script_id=273" target="_blank">Tag List</a>:  show definition summaries of classes, functions, variables, etc. for all open files.</li>
<li><a title="Fugitive plugin for Vim" href="http://www.vim.org/scripts/script.php?script_id=2975" target="_blank">Fugitive</a>: the definitive Git plugin.</li>
<li><a title="Command-T Vim plugin" href="http://www.vim.org/scripts/script.php?script_id=3025" target="_blank">Command-T</a>: super-fast file finding and opening.</li>
<li><a title="EasyMotion Vim plugin" href="http://www.vim.org/scripts/script.php?script_id=3526" target="_blank">EasyMotion</a>: quickly jump to any word or character in a file.</li>
</ul>
<p>To use Command-T you need to install a C library &#8211; follow the instructions on the plugin&#8217;s webpage to install on your system. Trust me, it&#8217;s worth it.</p>
<p>Here&#8217;s a word of warning: this is <em>my</em> configuration, and it&#8217;s likely to change on a fairly regular basis. I don&#8217;t guarantee that it will work on your system, and I certainly don&#8217;t guarantee that you&#8217;ll like it. Feel free to chop and change it as you please.</p>
<h2>The Final Word</h2>
<p>Hopefully this has been helpful. If you have any suggestions for things I should add to this list then just add a comment.</p>
<p>May the Vim be with you.</p>
]]></content:encoded>
			<wfw:commentRss>http://joncairns.com/2012/05/using-vim-as-a-php-ide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vim with PHP code sniffer, mess detector and code coverage</title>
		<link>http://joncairns.com/2012/03/vim-with-php-code-sniffer-mess-detector-and-code-coverage/</link>
		<comments>http://joncairns.com/2012/03/vim-with-php-code-sniffer-mess-detector-and-code-coverage/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 13:29:29 +0000</pubDate>
		<dc:creator>joonty</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpqa]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://joncairns.com/?p=443</guid>
		<description><![CDATA[Vim is a lot more powerful than people think. Many people who use it would do so just for quickly editing a single file, but would use an IDE for their main development. But I&#8217;ve discovered recently that Vim is &#8230; <a href="http://joncairns.com/2012/03/vim-with-php-code-sniffer-mess-detector-and-code-coverage/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Vim is a lot more powerful than people think. Many people who use it would do so just for quickly editing a single file, but would use an IDE for their main development. But I&#8217;ve discovered recently that Vim is so extensible that it can do all the things that you thought it couldn&#8217;t: code completion, syntax checking, project management, debugger integration and more.</p>
<p>Check out <a href="http://vim.wikia.com/wiki/Use_Vim_like_an_IDE">http://vim.wikia.com/wiki/Use_Vim_like_an_IDE</a> and <a href="http://arstechnica.com/open-source/guides/2009/05/vim-made-easy-how-to-get-your-favorite-ide-features-in-vim.ars">http://arstechnica.com/open-source/guides/2009/05/vim-made-easy-how-to-get-your-favorite-ide-features-in-vim.ars</a> for more on that.</p>
<p>I now use Vim as my main PHP editor, and one thing that I&#8217;ve managed to get working better than any other IDE I&#8217;ve used is integration with quality assurance tools like <a title="PHP Code Sniffer" href="http://pear.php.net/package/PHP_CodeSniffer/redirected" target="_blank">PHP code sniffer</a>, <a title="PHP Mess Detector" href="http://phpmd.org/" target="_blank">PHP mess detector</a> and clover code coverage (produced from <a title="PHPUnit" href="https://github.com/sebastianbergmann/phpunit/" target="_blank">PHPUnit</a> tests). I&#8217;ve written <a title="PHPQA Vim plugin" href="https://github.com/joonty/vim-phpqa" target="_blank">phpqa</a> (vim script <a title="PHPQA vim plugin on vim.org" href="http://www.vim.org/scripts/script.php?script_id=3980" target="_blank">#3980</a>), a plugin for Vim that integrates these three tools with your code by highlighting and marking lines with violations and code coverage output. Here are some screenshots showing it at work:</p>
<div id="attachment_444" class="wp-caption aligncenter" style="width: 594px"><a href="http://joncairns.com/wp-content/uploads/2012/03/vim-phpqa2.png"><img class="size-large wp-image-444" title="vim-phpqa2" src="http://joncairns.com/wp-content/uploads/2012/03/vim-phpqa2-1024x568.png" alt="PHP syntax error marker" width="584" height="323" /></a><p class="wp-caption-text">Showing PHP syntax error caused by a missing semicolon</p></div>
<div id="attachment_446" class="wp-caption aligncenter" style="width: 594px"><a href="http://joncairns.com/wp-content/uploads/2012/03/Vim-phpqa1.png"><img class="size-large wp-image-446" title="vim-phpqa" src="http://joncairns.com/wp-content/uploads/2012/03/Vim-phpqa1-1024x654.png" alt="Showing code coverage and code sniffer violations" width="584" height="372" /></a><p class="wp-caption-text">Showing code coverage and code sniffer violations. The lines covered by tests have a yellow &quot;C&gt;&quot; marker, and lines not covered have a red marker.</p></div>
<p>For more information, including installation, configuration and usage, visit the <a title="PHPQA Github repository" href="https://github.com/joonty/vim-phpqa" target="_blank">github page</a>.</p>
<p>Hopefully this plugin will be useful to others, and maybe even convert some people to using it as their IDE replacement! (I know, I&#8217;m a dreamer)</p>
]]></content:encoded>
			<wfw:commentRss>http://joncairns.com/2012/03/vim-with-php-code-sniffer-mess-detector-and-code-coverage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Meme Generator plugin for Jenkins</title>
		<link>http://joncairns.com/2012/03/meme-generator-plugin-for-jenkins/</link>
		<comments>http://joncairns.com/2012/03/meme-generator-plugin-for-jenkins/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 12:38:33 +0000</pubDate>
		<dc:creator>joonty</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jenkins]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://joncairns.com/?p=438</guid>
		<description><![CDATA[Over the past few months I&#8217;ve been secretly working on a Jenkins plugin, partly because of my love for the chipper butler and all his efforts to improve the quality of your code, and partly because I wanted to take &#8230; <a href="http://joncairns.com/2012/03/meme-generator-plugin-for-jenkins/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Over the past few months I&#8217;ve been secretly working on a <a title="Jenkins Continuous Integration" href="http://jenkins-ci.org/" target="_blank">Jenkins</a> plugin, partly because of my love for the chipper butler and all his efforts to improve the quality of your code, and partly because I wanted to take a look at Java.</p>
<p>The plugin is a meme generator. Don&#8217;t know what a meme is? Take a look at <a title="Visit the meme generator" href="http://memegenerator.net" target="_blank">memegenerator.net</a>, and any one of those pictures is a meme. The plugin creates a new meme if your project&#8217;s build fails, using the memegenerator.net API, and posts it on the project page. Who broke the build? No more hiding &#8211; they&#8217;re immortalised in an image. Just a bit of fun.</p>
<p>If you fancy giving it a go, sign up for a free account at memegenerator.net and install the plugin from the update center in your Jenkins instance.</p>
<ul>
<li><strong>Wiki page: </strong><a href="https://wiki.jenkins-ci.org/display/JENKINS/Meme+Generator+Plugin">https://wiki.jenkins-ci.org/display/JENKINS/Meme+Generator+Plugin</a></li>
<li><strong>Source code: </strong><a title="Github repository for meme generator Jenkins plugin" href="https://github.com/jenkinsci/memegen-plugin/" target="_blank">https://github.com/jenkinsci/memegen-plugin</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joncairns.com/2012/03/meme-generator-plugin-for-jenkins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Make your Netbeans amazing with jVi</title>
		<link>http://joncairns.com/2012/03/make-your-netbeans-amazing-with-jvi/</link>
		<comments>http://joncairns.com/2012/03/make-your-netbeans-amazing-with-jvi/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 13:52:19 +0000</pubDate>
		<dc:creator>joonty</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://joncairns.com/?p=435</guid>
		<description><![CDATA[I&#8217;ve been a Vim user for a couple of years, and love it. After a steep learning curve, your coding speed increases dramatically. As good as IDEs like Netbeans and Eclipse can be, the normal operations &#8211; navigating round the &#8230; <a href="http://joncairns.com/2012/03/make-your-netbeans-amazing-with-jvi/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been a Vim user for a couple of years, and love it. After a steep learning curve, your coding speed increases dramatically. As good as IDEs like Netbeans and Eclipse can be, the normal operations &#8211; navigating round the file, editing, copying, pasting, deleting &#8211; are never as efficient as they are when using Vim.</p>
<p>If you don&#8217;t agree with me about vim, or you don&#8217;t know what I&#8217;m talking about, read this article: <a title="Why oh why do those nutheads use vi?" href="http://www.viemu.com/a-why-vi-vim.html">Why, oh WHY, do those #?@! nutheads use vi?</a></p>
<p>Two worlds are about to collide: there&#8217;s a fantastic plugin for Netbeans called jVi (<a href="http://jvi.sourceforge.net/">http://jvi.sourceforge.net/</a>), which allows you to work with all the features of Netbeans and edit code as if you were using Vim. You can install it from <em>Tools -&gt; Plugin</em> in your Netbeans. It works beautifully, and any Vim user will immediately feel at home. In fact, I&#8217;m forgetting what it&#8217;s like to live without it.</p>
<p>If you give it the time it deserves, I promise you that you won&#8217;t want to go back to the way things used to be!</p>
]]></content:encoded>
			<wfw:commentRss>http://joncairns.com/2012/03/make-your-netbeans-amazing-with-jvi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set up Xdebug with Netbeans and LAMP (and CakePHP)</title>
		<link>http://joncairns.com/2012/01/set-up-xdebug-with-netbeans-and-lamp-and-cakephp/</link>
		<comments>http://joncairns.com/2012/01/set-up-xdebug-with-netbeans-and-lamp-and-cakephp/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 13:42:04 +0000</pubDate>
		<dc:creator>joonty</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[LAMP]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xdebug]]></category>

		<guid isPermaLink="false">http://joncairns.com/?p=416</guid>
		<description><![CDATA[If you&#8217;re a regular user of Netbeans IDE then, like me, you might sometimes get the feeling that you&#8217;re not using it to its full potential. Take debugging for instance: did you know that you can use Netbeans to debug a &#8230; <a href="http://joncairns.com/2012/01/set-up-xdebug-with-netbeans-and-lamp-and-cakephp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re a regular user of <a title="Netbeans IDE website" href="http://netbeans.org/" target="_blank">Netbeans IDE</a> then, like me, you might sometimes get the feeling that you&#8217;re not using it to its full potential. Take debugging for instance: did you know that you can use Netbeans to debug a web application by pausing at particular lines of code, and get a list of all the variables in the current scope? It takes a bit of setting up, but you can do it &#8211; here&#8217;s how.</p>
<p><em>I&#8217;ve written this tutorial using Ubuntu 11.10 with Netbeans IDE version 7. It&#8217;s recommended that you have this version of Netbeans, as certain things change pretty drastically from version to version. This tutorial mentions CakePHP specific set-up from time to time, but will work for non-Cake projects. I assume that you have a working LAMP environment (Linux, Apache, MySQL and PHP) with PHP 5+. If not, Google it ^_^</em></p>
<h2>1. Install Xdebug</h2>
<p>If you&#8217;re using Ubuntu, this is as gloriously easy as typing this in a terminal:</p>
<pre class="brush: bash; title: ; notranslate">$ sudo apt-get install php5-xdebug</pre>
<p>If you&#8217;re not, don&#8217;t worry &#8211; you can install it using <code>pecl</code>. (Having said that, I&#8217;ve frequently found that <code>pecl</code> doesn&#8217;t work the way it&#8217;s supposed to, and have had to compile extensions manually. Drop a comment if you have problems.)</p>
<pre class="brush: bash; title: ; notranslate">$ sudo pecl install xdebug</pre>
<p>Providing that one of the two previous commands worked, you need to add some configuration settings so that PHP can use Xdebug. Edit or create the file <code><strong>/etc/php5/conf.d/xdebug.ini</strong></code>, adding the following lines:</p>
<pre class="brush: bash; title: ; notranslate">
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
</pre>
<p>Xdebug should now be working &#8211; running <code>php -m</code> on its own should list xdebug as one of the loaded extensions.</p>
<h2>2. Configure Netbeans IDE</h2>
<p>Go to <strong>Tools -&gt; Options</strong>, then click the <strong>PHP</strong> tab. Under &#8220;Debugging&#8221;, the following options should be set:</p>
<ul>
<li>Debugger port: 9000</li>
<li>Session ID: netbeans-xdebug</li>
<li>Stop at first line: (ticked)</li>
</ul>
<p>This is all that&#8217;s needed to set up Netbeans in general, but it&#8217;s slightly more complex configuring each project.</p>
<h2>3. Configure Netbeans project</h2>
<p>There&#8217;s a huge variety of ways that you can set up a web project on your LAMP configuration, so I&#8217;ll try to make it clear what each option means. To edit the settings for an individual project, right click on the project in the Projects side bar, and choose <em>Properties</em>.</p>
<p>This panel has several categories on the right, and should have <em>Sources</em> selected. On this panel, set the <em>Web Root</em> to the directory containing your <strong>index.php</strong> file (for CakePHP projects this should be <strong>app/webroot </strong>- this is essential for making it work).</p>
<p>Next, select the <em>Run Configuration</em> panel. This is where you set up profiles for running your web application. You can have multiple profiles for different configurations, which can come in handy if you are using a framework that also supports command line execution. Choose &#8220;Local Web SIte (running on local web server)&#8221; as the <em>Run As</em> option, and then enter the local URL of your project (this could be <em>http://localhost</em>, or whatever host name you&#8217;ve given). The panel should now look like this:</p>
<div id="attachment_420" class="wp-caption aligncenter" style="width: 310px"><a href="http://joncairns.com/wp-content/uploads/2012/01/Screenshot-Project-Properties.png"><img class="size-medium wp-image-420" title="Project properties panel showing Run Configuration" src="http://joncairns.com/wp-content/uploads/2012/01/Screenshot-Project-Properties-300x212.png" alt="Project properties panel showing Run Configuration" width="300" height="212" /></a><p class="wp-caption-text">Run Configuration panel in project properties</p></div>
<p><em>Note: many people have one local website running at a time (i.e. http://localhost), and swap the Apache document root as and when they need. I prefer to set my sites up as virtual hosts, and give them each internal host names (e.g. http://joncairns.local).</em></p>
<p>Now, on the same panel, click &#8220;Advanced&#8221;, which will open up a new window. The most important thing you should do here is add &#8220;localhost&#8221; as the debugger proxy at the bottom of the window. Another thing that you would probably like to do is choose &#8220;Ask Every Time&#8221;  for the debug URL, which will ask you to specify the URL when running the debugger, otherwise it will always go to the root URL.</p>
<p>You should now be ready to run the debugger. Click on a line number somewhere in your code (a line that you know will be executed) to mark it as a breakpoint; this means that the execution will pause when it gets to this line. This is what a breakpoint looks like:</p>
<div id="attachment_421" class="wp-caption aligncenter" style="width: 297px"><img class="size-full wp-image-421" title="Click on the line number to activate a breakpoint" src="http://joncairns.com/wp-content/uploads/2012/01/Breakpoint.png" alt="Shows a breakpoint in Netbeans IDE" width="287" height="90" /><p class="wp-caption-text">Click on the line number to activate a breakpoint</p></div>
<p>One of the buttons at the top of the Netbeans window, below the menu, is the button to start the debugger (tooltip &#8220;Debug Main Project&#8221;) &#8211; click it. Your browser should open a tab with your project URL, and hang. You will then see that the breakpoint that you set has changed colour, meaning that the debugger has reached that point. You can use the debug windows to browse any variables in the current scope, which is totally awesome! I&#8217;ll leave it to you to use its full potential. When you&#8217;re ready to move on, click the icon that looks like a white arrow in a green circle, at the top of the Netbeans window. This will tell the debugger to move to the next breakpoint, or complete the execution if there aren&#8217;t any more set. You can stop the debugger at any point by clicking the big red square icon.</p>
<h2>4. Integrating with Chrome/Firefox (optional)</h2>
<p>Currently we can kick off a debug session from Netbeans, which opens a browser window. But what about if we want to go the other way, starting with our browser, and wake the debugger by reloading our website? This is possible for Google Chrome or Firefox, using plugins.</p>
<h3>Google CHrome</h3>
<p>This is my personal choice, and there&#8217;s a neat plugin called <a title="Xdebug helper" href="https://chrome.google.com/webstore/detail/eadndfjplgieldjbigjakmdgkmoaaaoc" target="_blank">Xdebug helper</a> that does a good job of linking your sites with Netbeans and Xdebug. Download and install, then go to <strong>Tools -&gt; Extensions </strong>and open the options for Xdebug helper. Under <strong>Domains</strong>, enter the domain of your site (e.g. <em>localhost </em>or <em>joncairns.local</em>). Then open up a tab with your site and click the little bug icon in the far right of the URL bar until he turns green. Start up the debugger in Netbeans, and leave it running. Now, every time you load your site it will use Xdebug.</p>
<h3>Firefox</h3>
<p>To be perfectly honest I haven&#8217;t tried it in Firefox, but I know that an extension exists &#8211; try <a title="Easy Xdebug for Firefox" href="https://addons.mozilla.org/en-US/firefox/addon/easy-xdebug/" target="_blank">Easy Xdebug</a>. If anyone fancies giving a step-by-step guide, then that would be welcome.</p>
<h2>5. Command-line debugging</h2>
<p>To start a debugging session from a command line script you need to set an environment variable before the PHP executable starts. I find that the easiest way to do this is to create a bash script that starts PHP with this variable set, and put it in <em>/usr/bin/php-xdebug</em>:</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash
export XDEBUG_CONFIG=&quot;idekey=netbeans-xdebug&quot;
/usr/bin/php &quot;$@&quot;
</pre>
<p>Run <code>chmod +x</code> on the new file and run the PHP script with:</p>
<pre class="brush: bash; title: ; notranslate">
php-xdebug myscript.php
</pre>
<p>Happy debugging!</p>
]]></content:encoded>
			<wfw:commentRss>http://joncairns.com/2012/01/set-up-xdebug-with-netbeans-and-lamp-and-cakephp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to use Git submodules</title>
		<link>http://joncairns.com/2011/10/how-to-use-git-submodules/</link>
		<comments>http://joncairns.com/2011/10/how-to-use-git-submodules/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 13:27:11 +0000</pubDate>
		<dc:creator>joonty</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[Github]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://joncairns.com/?p=299</guid>
		<description><![CDATA[Git submodules are a way of linking to a repository inside another. For example, if your project needs to use someone else&#8217;s project that&#8217;s hosted on Github then you can use it as a submodule rather than including all the &#8230; <a href="http://joncairns.com/2011/10/how-to-use-git-submodules/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>Git submodules are a way of linking to a repository inside another</strong>. For example, if your project needs to use someone else&#8217;s project that&#8217;s hosted on Github then you can use it as a submodule rather than including all the code from that project. This has the added benefit of being able to track and include changes from the submodule repo, rather than having to update project files yourself.</p>
<p><em>This tutorial will show you:</em></p>
<ul>
<li><em><a href="#adding">How to add a submodule to your project</a></em></li>
<li><em><a href="#cloning">How to clone repositories with submodules</a></em></li>
<li><em><a href="#updating">How to update submodules</a></em></li>
<li><em><a href="#recursive">How to manage submodules within submodules (recursive submodules)</a></em></li>
<li><em>What to do if it all goes wrong</em></li>
</ul>
<p>I know that last point is slightly ominous, but we&#8217;ll get to that.</p>
<h2><a name="settingup"></a>Setting Up</h2>
<p>For this tutorial you&#8217;ll need a Github account. You probably do already, otherwise you wouldn&#8217;t be reading this far in. You can either create a repository, or you can just fork my example project, which you can do at  <a title="My example repository" href="https://github.com/joonty/example" target="_blank">https://github.com/joonty/example</a>. Skip the rest of this section if you are comfortable with forking a repository and cloning into a local copy.</p>
<p>Otherwise, click on the &#8220;Fork&#8221; button at the top right of the screen, which looks like this:</p>
<p><img class="aligncenter size-full wp-image-303" title="Github fork repository" src="http://joncairns.com/wp-content/uploads/2011/10/github-fork.jpg" alt="Github fork repository" width="155" height="55" />This will create a copy of the example repo in your account. Then, clone the repo, replacing <em>(username)</em> with your actual username:</p>
<pre class="brush: bash; light: true; title: ; notranslate">jcairns$ git clone git@github.com:(username)/example.git submodule-example</pre>
<p>This will clone your version of the project into the directory &#8216;submodule-example&#8217;. Move into this directory and you are ready to go.</p>
<h2><a name="adding" href="#"></a>Adding a submodule</h2>
<p>My example project is a high-powered web application. It&#8217;s range of features include:</p>
<ol>
<li>Printing out &#8220;Hello, World&#8221; as a header</li>
</ol>
<div>Hmm. Seems to be all it does. Anyway, this high-powered web application is going to need a good ORM to help it function efficiently, so we&#8217;ll use PHP Doctrine. Doctrine2 is <a title="Doctrine2 on Github" href="https://github.com/doctrine/doctrine2" target="_blank">available on github</a>, so it&#8217;s the perfect example. To add a submodule you use the command <code>git submodule add</code>:</div>
<pre class="brush: bash; light: true; title: ; notranslate">jcairns$ git submodule add (repository) (directory)</pre>
<p>This would register the repository as a submodule under the directory specified. Therefore, in the context of this example, we want to add the Doctrine repository as a submodule in the directory &#8216;doctrine&#8217;, using <code>git submodule add</code>:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git submodule add git://github.com/doctrine/doctrine2.git doctrine
</pre>
<p>This will register doctrine as a submodule within your project and will clone the data into the &#8216;doctrine&#8217; directory. The command <code>git submodule status</code> will tell you that the submodule has been registered, and which commit it is pointing to in the Doctrine repository:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git submodule status
-15877e14430a316a7576918bc7c996e52d91105d doctrine
</pre>
<p>Also, to see what changes have been registered in the parent repository, use the <code>git status</code> command:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git status
# On branch master
# Changes to be committed:
#   (use &quot;git reset HEAD ...&quot; to unstage)
#
#	new file:   .gitmodules
#	new file:   doctrine
#
</pre>
<p>This shows us that two &#8220;files&#8221; have been modified: &#8216;.gitmodules&#8217;, which holds information about the repository&#8217;s submodules; and &#8216;doctrine&#8217;, which is the submodule itself. Git doesn&#8217;t track files inside the submodule when you&#8217;re in the parent repository, it just sees it as a single file. We&#8217;ll touch on that a bit more later. First, we want to save our changes and push it to the remote repository:</p>
<h3>Commit Changes</h3>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git commit -m 'Added Doctrine submodule'
[master 37f40bb] Added Doctrine submodule
 2 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 doctrine
</pre>
<h3>Push to Remote</h3>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 433 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:joonty/example.git
   223e6ba..37f40bb  master -&gt; master
</pre>
<p>If you look at your repository on Github you will be able to see a link to the Doctrine repository in your parent repository, which will look like this:</p>
<div id="attachment_314" class="wp-caption aligncenter" style="width: 434px"><img class="size-full wp-image-314" title="Github submodule link" src="http://joncairns.com/wp-content/uploads/2011/10/github-submodule.jpg" alt="Github submodule link" width="424" height="168" /><p class="wp-caption-text">Doctrine submodule link in Github</p></div>
<p>Congratulations! You have added a submodule. Now let&#8217;s look at cloning repositories that contain submodules.</p>
<h2><a name="cloning" href="#"></a>Cloning a repository with submodules</h2>
<p>Let&#8217;s try cloning the repository you&#8217;ve been working on into a different directory. Move to a directory above the one you&#8217;re currently in, and run:</p>
<pre class="brush: bash; light: true; title: ; notranslate">jcairns$ git clone git@github.com:(username)/example.git submodule-clone-example</pre>
<p>Again, make sure you replace <em>(username)</em> with your actual username. This will clone the example project to the directory &#8216;submodule-clone-example&#8217; &#8211; move into this directory. If you take a look inside the &#8216;doctrine&#8217; directory, which is meant to contain our submodule, you&#8217;ll see that it&#8217;s empty. What&#8217;s happened? Has git given up?</p>
<p>No. By default, a <strong>git clone</strong> won&#8217;t pull the submodules along with the parent repository. To pull the Doctrine submodule, you need to run <code>git submodule init</code> and then <code>git submodule update</code>:</p>
<h3>Submodule Init</h3>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git submodule init
Submodule 'doctrine' (git://github.com/doctrine/doctrine2.git) registered for path 'doctrine'
</pre>
<p>This registers the submodule in the correct path, &#8216;doctrine&#8217;, but doesn&#8217;t clone the code.</p>
<h3>Submodule Update</h3>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git submodule update
Submodule 'doctrine' (git://github.com/doctrine/doctrine2.git) registered for path 'doctrine'
Jonathan-Cairns-iMac:submodule-example jcairns$ git submodule update
Cloning into doctrine...
remote: Counting objects: 63339, done.
remote: Compressing objects: 100% (16009/16009), done.
remote: Total 63339 (delta 44444), reused 62836 (delta 43982)
Receiving objects: 100% (63339/63339), 16.16 MiB | 712 KiB/s, done.
Resolving deltas: 100% (44444/44444), done.
Submodule path 'doctrine': checked out '15877e14430a316a7576918bc7c996e52d91105d'
</pre>
<p>This actually pulls the code for the registered submodules. You have now got a working copy of your repository, submodules intact.</p>
<h2><a name="updating" href="#"></a>Updating a submodule</h2>
<p>As I&#8217;ve said before, submodules are simply Git repositories inside another: there&#8217;s nothing magical about them. Therefore, as long as you are in the submodule directory, all the normal Git actions like <code>push</code>, <code>pull</code>, <code>reset</code>, <code>log</code>, <code>status</code>, etc. work as normal. If you want to make sure that your submodule is bang up to date with the remote repository, simply do a <code>git pull</code> (don&#8217;t use <code>submodule</code>!) when you&#8217;re in the submodule directory:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ cd doctrine
jcairns$ git pull
Already up-to-date.
</pre>
<p>If it looks like you&#8217;re getting an error message saying that you aren&#8217;t on any branch, do a <code>git checkout master</code> to fix it.</p>
<p>If your submodule did have some updates when you did a pull then your parent repository will be telling you that there are some changes to commit. The submodule is pointing to a particular commit, and if the commit changes then the parent repository registers the change. If your submodule needs to be at a particular commit to work with your repository you can set it using <code>git reset</code>:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git reset --hard (commit hash)
</pre>
<p>For example, if we want to fix our version of Doctrine to the most recent tag (currently 2.1.2), we would just need to do:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ cd doctrine
jcairns$ git reset --hard 144d0de
HEAD is now at 144d0de Release 2.1.2
</pre>
<p>We can then go back to the parent directory, and commit it:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ cd ..
jcairns$ git commit -am 'Set doctrine version to 2.1.2'
</pre>
<p>When this is pushed to the remote repository the submodule will be associated with that particular commit. It&#8217;s worth mentioning that, if you have others working on the same project, someone else could pull on the submodule and commit the change, thereby changing the submodule commit. But that&#8217;s easily fixed &#8211; just do the reset again.</p>
<p>It&#8217;s also worth noting that, if your submodule is a repository that you have push access to, you can edit, commit and push submodule content completely independently from your parent repository. The only thing that affects the parent is when the submodule is pointing to a different commit, as mentioned previously.</p>
<h2><a name="recursive" href="#"></a>Submodules within submodules</h2>
<p>One great feature about submodules is that they can contain submodules themselves. For instance, the Doctrine repository has several submodules, including <a title="Doctrine common repository" href="https://github.com/doctrine/common" target="_blank">doctrine-common</a> and <a title="Dbal repository" href="https://github.com/doctrine/dbal" target="_blank">dbal</a>.</p>
<p>Let&#8217;s go back to the example repository. Currently, Doctrine is registered as a submodule, but none of its submodules have been pulled. In the <em>doctrine</em> directory, type <code>git submodule status</code>:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git submodule status
-3762cec59aaecf1e55ed92b2b0b3e7f2d602d09a lib/vendor/Symfony/Component/Console
-c3e1d03effe339de6940f69a4d0278ea34665702 lib/vendor/Symfony/Component/Yaml
-ef431a14852d7e8f2d0ea789487509ab266e5ce2 lib/vendor/doctrine-common
-f91395b6f469b5076f52fefd64574c443b076485 lib/vendor/doctrine-dbal
</pre>
<p>This shows that there are four submodules in the doctrine repository. The &#8220;-&#8221; sign at the beginning of each line shows that the submodules haven&#8217;t been downloaded yet. We can run <code>git init</code> and then <code>git update</code>, which would clone all the repositories and put them into the right place. But what about when we&#8217;re cloning our repository to another machine? It would get pretty tedious to have to go through all the submodules, performing an <code>init</code> and <code>update</code> as deep as we need.</p>
<p>Fortunately, we can get round this at the point of cloning. We can just use the <code>--recursive</code> option when doing a <code>git clone</code>:</p>
<pre class="brush: bash; title: ; notranslate">
jcairns$ git clone --recursive (repository)
</pre>
<p>This initialises and pulls all submodules, recursively. Simple!</p>
]]></content:encoded>
			<wfw:commentRss>http://joncairns.com/2011/10/how-to-use-git-submodules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jTools Google Chrome plugin</title>
		<link>http://joncairns.com/2011/09/jtools-google-chrome-plugin/</link>
		<comments>http://joncairns.com/2011/09/jtools-google-chrome-plugin/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 13:14:19 +0000</pubDate>
		<dc:creator>joonty</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Github]]></category>
		<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jTools]]></category>

		<guid isPermaLink="false">http://joncairns.com/?p=47</guid>
		<description><![CDATA[One of the tools I use on a daily basis when browsing the Internet is the developer tools that come with webkit based browsers (i.e. Google Chrome and Safari). The &#8220;Elements&#8221; tab gives you a slick breakdown of the entire &#8230; <a href="http://joncairns.com/2011/09/jtools-google-chrome-plugin/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the tools I use on a daily basis when browsing the Internet is the developer tools that come with webkit based browsers (i.e. Google Chrome and Safari). The &#8220;Elements&#8221; tab gives you a slick breakdown of the entire page DOM, and allows for easy manipulation.</p>
<p>At some point that I can no longer remember, I thought it would be useful to have a similar thing, but using <a title="jQuery" href="http://www.jquery.com" target="_blank">jQuery</a> selectors instead. For instance, if you want to find out the jQuery selector representation of a particular element of a page, how would you do it? What if you wanted to test a selector on a particular website, and see what it would return?</p>
<h2>Enter jTools</h2>
<p>jTools is a plugin for Google Chrome that uses jQuery to provide various methods of page analysis on the site you&#8217;re currently on, in real time. It currently has two modes:</p>
<ol>
<li>Execute a jQuery selector and highlight the objects on the page that match</li>
<li>Hover over elements on a page and see the jQuery selector that represents it</li>
</ol>
<p>It&#8217;s still very much in development at the moment, so I haven&#8217;t packed it up as a proper Chrome extension. In fact, the only access to it at the moment is through <a title="jTools at Github" href="https://github.com/joonty/jtools" target="_blank">Github</a>. Here&#8217;s how to give it a go.</p>
<h3>1. Grab the Code</h3>
<p>You need git installed on your machine &#8211; <a title="Set up Git" href="http://help.github.com/set-up-git-redirect" target="_blank">follow these instructions</a> if you haven&#8217;t got it yet. Then run:</p>
<pre class="brush: bash; title: ; notranslate">
$ git clone git@github.com:joonty/jtools.git
</pre>
<p>That will grab all the source code.</p>
<h3>2. Load in Chrome</h3>
<p>Click the spanner (hand wrench for Americans) at the top right of Google Chrome, then go to Tools -&gt; Extensions. Hopefully you&#8217;ll see a button saying &#8220;Load Unpacked Extension&#8221; near the top of the page. If not, click a link saying &#8220;Developer Mode&#8221; at the right of the page and it should appear. In the file browser, navigate to the directory where you just cloned the source and accept.</p>
<p>This should load the extension into your browser.</p>
<h3>3. Try it Out</h3>
<p>Reload any of your open tabs to try the extension out. You will see a jQuery logo in the top right of the screen, for inserting jQuery selectors into the current page, and a logo at the bottom right of each page, for analysing the DOM jQuery style.</p>
<p>I might add more information here as the project grows, but I&#8217;m just going to leave you to mess around with it for the time being.</p>
<h3>4. Collaborate</h3>
<p>I&#8217;m all for social coding, so for the technical amongst you please feel free to add to the project and work with me on my Git repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://joncairns.com/2011/09/jtools-google-chrome-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

