Saturday, 3 May 2014

Eclipse Kepler and PDT

The other day I found myself reinstalling stuff on my laptop and one of the things I wanted to put on there was my favourite programming IDE, Eclipse. It had a been a year since I'd used it frequently, and I had to follow a number of steps to get it going properly with PDT (PHP Development Tools), the PHP development plugin. I was surprised how little information I could find quickly online to help with doing this; so I thought I'd share the steps I'd followed.

I carried this out in Ubuntu Linux 14.04, but I think some of the things should work similarly in another OS like Windows if you tried. I'm assuming you've installed things for a LAMP environment already, i.e. Apache web server and the PHP language. I am also going to stick with the default XDebug debugger for PHP as I find it fine, even though the Zend Debugger is trendy at the moment.

Step 1: Give Me Full-Strength Java

Eclipse is built in Java, so that's what you need first. I am not a fan of the open source Java versions (they're kind like a decaffeinated cup of coffee), so I downloaded and installed the official Oracle Java Development Kit.

In your browser, go to the Oracle site:
http://www.oracle.com/technetwork/java/javase/downloads

Choose Java Platform, Standard Edition > Download >JDK
Choose Accept >Linux x64 >jdk-8u5-linux-x64.tar.gz
(the JDK you choose may vary based on version number and your processor. It's about 150 mb in size).

In a shell, make yourself a directory for the Java Virtual Machine. Copy your downloaded JDK there and unzip it. (The version placeholder below will hold your version number, e.g. "1.8.0_05"):
cd [my download directory];
mkdir /usr/lib/jvm;
cp jdk[version].tar.gz /usr/lib/jvm;
cd /usr/lib/jvm;
tar xvf jdk[version].tar.gz;
rm jdk[version].tar.gz;


Find your Java compiler and update your system. Set the permissions how you like.
ls /usr/lib/jvm/jdk[version]/bin/;
-You will see the Java compiler there, i.e. see "javac". Note this down.

chmod -R 770 /usr/lib/jvm/jdk[version];
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk[version]/bin/javac 1;
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk[version]/bin/java 1;
update-alternatives --config javac;
update-alternatives --config java;


Check all is OK:
java -version
The output should be something like:
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)


Step 2: Install Eclipse

Go the Eclipse site and download Eclipse Standard 4.3.x:
http://www.eclipse.org/downloads/

Copy your download over to handy directory, like /opt. Then unzip it and set permissions:
cd [my download directory];
cp eclipse-standard-kepler-[version].tar.gz /opt
cd /opt;
tar xvf eclipse-standard-kepler-[version].tar.gz
rm eclipse-standard-kepler-[version].tar.gz
sudo chown -R [my user]:root eclipse;
sudo chmod -R +r eclipse;


Put Eclipse in your user PATH, e.g:
sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse

Start Eclipse on the command line with:
eclipse -clean &

Or make item in Ubuntu's launcher menu (with the Alacarte application):
sudo apt-get install alacarte
alacarte &

Choose >Programming >Add Item
name: eclipse4.3.2
path: /opt/eclipse/eclipse

Step 3: Install PDT (the PHP development plugin)

In Eclipse, go to:
Help >Install new software >Add
For name, type: Kepler updates
for Location, type: http://download.eclipse.org/releases/kepler

You will now see an install plugin wizard
-Choose >Programming Languages >PHP Development Tools (PDT) > Next
-You will see a Review >Next
-Choose "I Accept" >Finish
-Then you will see install progress bar

You can check PDT is installed by going to:
-Window >Open Perspective >Other >PHP
-This switches to the PHP Development perspective

Set up some editor preferences how you like:
-Go to Window >Preferences >PHP >Code Style >Formatter
-Set Profile to PHP Conventions, choose >Show
-Go to >Indentation tab
    -Tab policy: spaces
    -Indentation size: 2
-go to >Line wrapping: max line width = 80

Set your PHP executable. If you don't know this, in a shell type "which php".
-Go to Window >Preferences >PHP Executables >Add
-Name: php559
-Exec: /usr/bin/php
-php ini: /etc/php5/apache2/php.ini
-php debugger: XDebug

CLI My Output

Now you come to a point where you decide whether: (a) you want to run things from Eclipse and direct them to a web server/browser, or (b) you want to view command line output. I like option (b), especially for testing.

Set up console output with the debugger:
-Go to Window >Preferences >PHP >Debug - choose XDebug
-Choose >Enable CLI debugging

Note: PDT is not set up well for console output - it opens a separate debug tab, which can be annoying. So to turn this off:
Go to Window >Preferences >Run/Debug >Perspectives
For the 2 lines under "PHP CLI application":
- Choose Debug at right
 - For "Open the associated perspective when launching", choose Never (turns off auto switch to Debug tab)
- For "Open the associated perspective when an application suspends", choose "Prompt"

To actually run a PHP script to get command line output, what I had to do was:
-select my opened PHP file in Eclipse
-right-click to see the options for "Run as PHP CLI Application" / "Debug as CLI Application"
(the menu at top doesn't seem to populate with these options)
or push Alt-Shift-X H.

Step 4: Configure some extras in Eclipse

PDT'S Javascript Editor

The PDT seems to come with a helpful Javascript Editor. You can configure this by going to:
Window >Preferences >JavaScript >Code Style >Formatter
-Choose >New profile, and name a new one, e.g: Eclipse_custom_by_me
-I changed the tab settings for this editor:
-Choose > Edit > Indentation
-Choose Spaces Only, 2, 2 >OK
>OK

The Eclipse Color Theme plugin

One thing I don't like is a glaring white screen, and this plugin lets you configure a number of different themes for Eclipse that are much friendlier to your eyes. To install it:
-Go to: Help >Install new software >Add
-For Name, type: Eclipse Color Theme
-For Location, type: http://eclipse-color-theme.github.com/update
-Choose Select >Next, and you will see a progress bar
-Choose >Next >I Accept >Finish

Eclipse will then Restart. Choose your theme by going to:
Window >Preferences >General >Appearance >Color Theme
(My favourite theme is Oblivion. It's nice and dark.)
Now you're ready to write the next world-beating PHP project in Eclipse using the PDT plugin!