Installing Sonar Source on Mac OSX – Part 2

I had some notes a while ago for installing Sonar on a Mac. Recently I upgraded to a new Mac, OS X Yosemite (10.10.3), and found I needed to add a few steps to the install process.

  • You need the JDK to run Sonar
  • You need xdebug

xdebug

To install xdebug run:

1
sudo pecl install xdebug

Then add to your php.ini the following (I just added it to the bottom), replace the version in the path with whichever you installed:

1
2
3
4
5
zend_extension=/usr/local/Cellar/php56/5.6.5/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
[xdebug]
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000

To find which php.ini you’re using (useful if you’re using multiple versions of PHP via brew as I do) run:

1
php -i | grep php.ini

Then restart apache:

1
sudo apachectl restart

To check that the xdebug module is loaded:

1
php -m | grep xdebug

Then you should be set :)

Disabling the Flash Plugin for Testing

Sometimes you want to test things without the Flash plugin enabled, using Chrome this is nice and easy!

Just type chrome://plugins/ in the address bar to open the Chrome plugin settings page. Should look something like:

Flash plugins settings page in Chrome

Note the Adobe Flash Player on the last line, just disable that and you’re good to go. Don’t even need a browser restart, just a new tab.

You can test this using Adobe’s Flash Player Help page

Installing VirtualBox Guest Additions

Currently I have VirtualBox 4.3.18 installed on Mac OS X Mavericks. I recently downloaded several Windows 7/8/8.1 images from either modern.ie or MSDN, and I wanted to install guest additions as well so I could do things like:

  • change the size of the guest window
  • turn on bidirectional shared clipboard
  • add shared folder(s)
  • etc

There is a handy menu option to do this from the VirtualBox VM menu:

1
Devices / Insert Guest Additions CD image... (Host+D)

My problem was that every time I clicked that (for every Windows image I tried) nothing happened. For some reason that menu option was not finding or not opening the guest additions installer.

Luckily it’s pretty straight forward to do this manually from within the guest virtual machine. Follow the screenshots below:

Open Computer

Installing guest additions - 1

Double click the VirtualBox Guest Additions

Installing guest additions - 2

Double click VBoxWindowsAdditions

Installing guest additions - 3

Click Yes

Installing guest additions - 4

Click Next

Installing guest additions - 5

Click Next

Installing guest additions - 6

Click Install (I didn’t add Direct3D Support)

Installing guest additions - 7

Click Install

Installing guest additions - 8

Installing guest additions - 9

Click Finish

Installing guest additions - 10

Opening .eml Files on a Mac

More and more I’m getting emails from clients with .eml attachments. These are generally email messages created by Microsoft Outlook and may contain their own attachments (files or images etc).

The issue for me is that I don’t use Outlook…but I still need to find a way to open them, ideally a way that includes accessing the original attachments.

Luckily there are a several options, including (first download the eml file):

  • If you have Apple Mail setup, just double click the .eml to open the original email. This also gives you access to the attachments
  • Open in Preview (select the file and hit the space bar). This is ok but only for the text part of the email, no attachments
  • Download and rename the eml file it as .mht (or .mhtml) then open it in any modern browser. This is because it’s just a MIME HTML file. This is ok but only for the text part of the email, no attachments
  • Install dedicated software like this Chrome extension

Clearly using Apple Mail is the winner, for me this mean setting it up with an account I rarely use…just so I could open it.

Simple JSON Prettify

I went looking for a quick and easy way to style JSON content for display in the browser and quickly came across this answer at stack overflow with a corresponding jsfiddle entry.

However the problem is that this is kinda broken, the output isn’t valid JSON because the keys aren’t quoted. It’s just outputting a JavaScript object intead. A quick fix (and slight refactor) to the JavaScript gave me what I needed, I couldn’t post on SO as I don’t have the reputation (yeah that’s annoying) so I’m posting here for reference and I forked the original JSFiddle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var prettyPrint = (function () {
    'use strict';

   function replacer (match, pIndent, pKey, pVal, pEnd) {
        var key = '<span class=json-key>';
        var val = '<span class=json-value>';
        var str = '<span class=json-string>';
        var r = pIndent || '';
        if (pKey) {
            r = r + key + pKey.replace(/[: ]/g, '') + '</span>: ';
        }
        if (pVal) {
            r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>';
        }
        return r + (pEnd || '');
    }

    function render (obj) {
        var jsonLine = /^( *)("[\w]+": )?("[^"]*"|[\w.+-]*)?([,[{])?$/mg;
        return JSON.stringify(obj, null, 3)
            .replace(/&/g, '&amp;').replace(/\\"/g, '\\&quot;')
            .replace(/</g, '&lt;').replace(/>/g, '&gt;')
            .replace(jsonLine, replacer);
    }

    return {
        render: render
    };
}());

And the CSS that goes with it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pre {
   background-color: ghostwhite;
   border: 1px solid silver;
   padding: 10px 20px;
   margin: 20px;
}

.json-key {
   color: brown;
}

.json-value {
   color: navy;
}

.json-string {
   color: olive;
}

The end result

The original:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
   {
      name: "Earth",
      order: 3,
      stats: {
         life: true,
         mass: 5.973600000000001e+24
      }
   },
   {
      name: "Saturn",
      order: 6,
      stats: {
         life: null,
         mass: 5.6846e+26
      }
   }
]

The result after a minor change:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
   {
      "name": "Earth",
      "order": 3,
      "stats": {
         "life": true,
         "mass": 5.973600000000001e+24
      }
   },
   {
      "name": "Saturn",
      "order": 6,
      "stats": {
         "life": null,
         "mass": 5.6846e+26
      }
   }
]

Careful Using PHP Array_merge

Using array_merge bit me today when I merged an array (which happened to be empty) with a numerically indexed array.

Because the array I was merging was empty, I expected my other (numerically indexed) array to remain untouched. However that wasn’t the case, from the docs:

Values in the input array with numeric keys will be renumbered with incrementing
keys starting from zero in the result array.

This meant that the following array keys went from being their constant numeric values, to 0, 1, 2, 3:

1
2
3
4
5
6
$pdoOptions = [
    PDO::ATTR_PERSISTENT         => false,
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_WARNING,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
    PDO::ATTR_EMULATE_PREPARES   => false
];

I really dislike this behaviour, but it is what it is so be warned. Instead I just looped over my merging array and set the keys manually:

1
2
3
4
5
if (count($myArr)) {
    foreach ($myArr as $key => $value) {
        $otherArr[$key] = $value;
    }
}
php

PHP [‘SCRIPT_NAME’] Returning Empty String

I was working with Slim (v2.2.x) the other day and saw that a PHP warning was being thrown in one of the interal Slim files:

1
Warning: strpos(): Empty needle in /vendor/slim/slim/Slim/Environment.php on line 143

The code throwing the warning was the following if statement:

1
if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {

The actual issue was that $_SERVER['SCRIPT_NAME'] was an empty string, so strpos was complaining as that is invalid syntax. Strangely, this was all working fine when we had the apache rewrite rules in an .htaccess file:

1
2
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

However once we moved the rules to the VirtualHost we got the warning. Some googling told me that this is a 6 year old problem with either apache, php, mod_php or the boogey man…depending on who you believe. It seems that various rewrite settings working differently if you have them in .htaccess vs the VirtualHost block.

I considered sending a pull request to Slim to handle the scenario where SCRIPT_NAME is empty:

1
if (strlen($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {

But instead we added the PT (passthrough) rewrite flag. This works fine for what we’re doing and $_SERVER['SCRIPT_NAME'] now returns /index.php as it should.

1
2
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^(.*)$ /index.php/$1 [QSA,PT,L]

Problem Dumping MySQL Database - Unknown System Variable ‘GTID_MODE’

I was trying to dump and restore a MySQL database today, but received the following error when I tried to dump:

1
mysqldump: Couldn't execute 'SELECT @@GTID_MODE': Unknown system variable 'GTID_MODE' (1193)

The MySQL version of the database I was trying to dump was 5.5.28 and the server I was trying to restore into was 5.6.10.

A quick google told me that the error was likely due to this mismatch in versions. From the teamextension blog:

This error is in part due to the introduction of Global Transaction Identifiers (GTIDs) in MySQL 5.6.
GTIDs make it simple to track and compare replication across a master-slave topology.

The solution is adding the following option to your dump statement:

1
--set-gtid-purged=OFF

In all, my statement was:

1
mysqldump -h[host] -u[user] --single-transaction --routines --triggers --set-gtid-purged=OFF --compatible=ansi [database] | mysql --h[host] -u[user]

INI Syntax Highlighting in Sublime Text

I went searching for a package to provide syntax highlighting for INI files in Sublime Text. The reason? INI files are ugly ;(

INI no syntax highlighting

I found one that works well called sublime-text-2-ini.

Installation isn’t done via the package manager, but it’s still simple. Open a terminal and navigate to your Sublime Packages folder, then clone in the package from github.

1
2
cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/
git clone git@github.com:clintberry/sublime-text-2-ini.git ./INI

That’s it! Here’s what it looks like after installation:

INI with syntax highlighting

Error Writing Git Commit Messages With Vi

I typically write my git commit messages in the terminal, but today I wanted to write a multi-line comment so I left out the -m argument to git commit so it would open up my default editor, which is vi:

1
git commit

I wrote my comment, saved and exit but got an error:

1
2
3
$ git commit
error: There was a problem with the editor 'vi'.
Please supply the message using either -m or -F option.

A quick google and I found the solution, I thought I’d repost it here for myself as well as others.

There are a couple of solutions offered in that article, for me I just set the full path to vi with a -f flag in my .gitconfig:

1
2
[core]
  editor = /usr/bin/vi -f

As an FYI…if you want to see the values in git config, you can run this command in your project and it will list your global and project configuration:

1
git config -l
git