Category Archives: Tricks

How To Install Composer programatically

If you want to automate the download and installation of Composer, chances are you are trying to figure out how to install Composer programatically. For example, I have a setup at work where I have an install script to create local developer environments in vagrant, which includes Composer. The fatal mistake is copying the Composer download script directly from the website. The reason for this is that the signature regularly changes as the software updates, and Composer downloads with a hard coded signature will fail. Fortunately, Composer offers a very easy way to verify signatures programatically.

To use it in Linux, you can substitute the signature string with:

$(curl -Ss https://composer.github.io/installer.sig)

The full syntax:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$(curl -Ss https://composer.github.io/installer.sig)') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

The end result of this is that you can install composer on a new machine without having to constantly check the signature on the web.

This is useful if you need a script, or just want a simple executable that downloads and installs Composer without having to visit the web page every time.

Can’t Log In using HyperDB? Here’s how to fix it

I recently had an issue where a high availability server I was running with HyberDB and a master/slave relationship wasn’t letting me log in. I spent a lot of time digging around, and couldn’t see any issues. SHOW SLAVE STATUS on my MySQL replication server showed no errors either.

It turns out, the big issue was latency. WordPress seeks to verify your session after you’ve logged in. If you are verifying on a different server than the one from which you’re inserting, there is a chance that you may not actually get the data on time.

The solution? Add this to your db-config.php file:

/**
 * Add a sleep setting, to ensure that setting
 * cookies and all verification happens after
 * replication has already taken place.
 * If you replication is taking more than
 * 1 second, you may have a server latency issue.
 */
add_action('set_logged_in_cookie', function() {
    sleep(1);
}, 10, 0);

That should fix your issues.

Getting the MySQL command on MacOS (v 10.11 and up)

If you are familiar with using the terminal on Linux, the absence of the mysql command can at times feel painful for a Mac user. While launching MySQLWorkbench is easy enough, sometimes those of us familiar with *nix shells just want to use the terminal. I spent an hour or so searching for a solution, with only MySQL server or outdated homebrew formulae to be found. Then, I realized something that should have been obvious all along.

If you have MySQLWorkbench installed on your computer, it must have some way to communicate with a MySQL server, and my money was on—wait for it—the mysql command.

Here’s how I got it back (Note: This method requires MySQLWorkBench)

First, I did a search for the path.

sudo find / -name mysql -print -quit

Then, I copied that path (in my case it was /Applications/MySQLWorkbench.app/Contents/MacOS/mysql).

Finally, I put that path into my .bashrc file.

printf "alias mysql='Applications/MySQLWorkbench.app/Contents/MacOS/mysql'" >> ~/.bashrc

I repeated the same for mysqldump.

Altogether Now

If you’re looking for an easy few lines to copy it, here goes:

printf "\nalias mysql='$(sudo find / -name mysql -print -quit)'\nalias mysqldump='$(sudo find / -name mysqldump -print -quit)'" >> ~/.bashrc

If you’re running VVV, you can easily test your connection like so:

mysql -h vvv.dev -u external -p

 # (password is external)

If you are not running VVV, that’s fine – just substitute the hostname, username, and password as needed.

Regular Expression to Change Text Domains

I found myself in a situation where I had to change my text domain on WordPress. I changed it from a single word to hyphen separated (example: foo to foo-bar). Like a dummy, my first call was to just do a Replace All. That didn’t work, because convention in WordPress is to put a namespace in front of functions, and my namespace was foo. As you know, hyphens do not belong in PHP functions.

If you ever find yourself in a similar situation, I wanted to share a RegEx trick that may help save you time, just as I hope it will save me some time when I find myself in this situation again.

(__\((?:[^'"]*)(?:\'|\")(?:[^']*)(?:\'|\"),(?:[^'"]*))(?:[^'"]*)(?:\'|\")<OLD NAMESPACE>((?:\'|\")(?:[^'"]*)\))

(Replace [YOUR OLD NAMESPACE] with the namespace you want to replace).

From there, it’s as simple as replacing your match. This regular expression makes it easy to do this by wrapping your replace with your first match and your second match.

In vim, you could simply type this:

:%s/\(__(\(?:[^'"]*\)\(?:\'|\"\)\(?:[^']*\)\(?:\'|\"\),\(?:[^'"]*\)\)\(?:[^'"]*\)\(?:\'|\"\)<YOUR OLD NAMESPACE>\(\(?:\'|\"\)\(?:[^'"]*\))\)/\<NEW NAMESPACE>\2

(Note that you want to replace the text in brackets with the text you want to find and replace).