Category Archives: Software

Z-Ray for Zend Server 7

I see that Zend Server 7 has now been released.

I’ve been running the beta for all my development work for a while now and the main reason is the new Z-Ray feature. Z-Ray is a bar that is injected into the bottom of your page showing lots of useful information.

This is what it looks like in its closed state when run on my development version of joind.in:

Z ray1

At a glance, I can see that this page’s performance is acceptable, there’s a notice that I need to look at and that 11 database queries were executed.

Continue reading

Perl syntax highlighting in Sublime Text 3

I’m currently writing a project in Perl for a client and have discovered that the default Perl syntax highlighting in Sublime Text is terrible.

Fortunately, the community has stepped up and Blaise Roth has created the ModerlPerl package. Install via Package Control.

To get all Perl files to open with the new syntax highlighter, use View > Syntax > Open all with current extension as… and select ModernPerl from the sub-menu.

Setting up Zend Server 6 on OS X for PHP development

I recently decided to upgrade my Mac’s PHP to 5.4. One of the available options is Zend Server 6.1, Free edition. These are my notes how to set it up so that it works the way I develop.

Installation

Mount the disk image and follow the installation wizard. On OS X, Zend Server installs PHP, Apache and MySQL inside /usr/local/zend.

The Zend Server admin interface is at at http://localhost:10081. On first run you have to go through a wizard. I picked development for my Launch Option. You have to set an admin password – as it’s my dev machine, I picked something simple :)

More information is available from Joe Stagner’s guide on devzone. Ignore the bit about editing php.ini though as you can do that via Zend Server’s admin system.

Apache

Like Joe, I also want the Apache running on port 80. Edit /usr/local/zend/apache2/conf/httpd.conf and replace Listen 10888 with Listen 80.

Note that the default localhost directory is /usr/local/zend/apache2/htdocs and the Apache processes run as user daemon with group daemon.

Zend Server admin

Once logged in, you can configure Zend Server:

Configuration -> PHP:
Set your PHP settings are required. Then press Save. To restart the server, click the button with two arrows in a circle on the toolbar.

You must set the date.timezone to stop the PHP warning. I also set sendmail_path as I like to redirect email on my development box.

Configuration -> Components:
Look down the list and turn off what you don’t need. I turned off Zend Data Cache, Zend Job Queue and Zend Page Cache in addition to what is already off.

Command line control

Add the relevant Zend Server paths to your environment. Edit .bash_profile and add

export PATH=/usr/local/zend/bin:/usr/local/zend/mysql/bin:$PATH 

to the bottom. This will give you access to the php, mysql and other Zend Server related CLI tools.

zendctl.sh

The main CLI tool to control Zend Server is zendctl.sh. The most interesting commands are:

  • sudo zendctl.sh restart-apache to restart Apache
  • sudo zendctl.sh restart-mysql to restart MySQL

MySQL

Zend Server supplies MySQL version 5.5.27 rather than 5.6 for some reason. If you want 5.6, then grab it from MySQL’s website or homebrew.

If you use your own MySQL, then stop Zend Server from starting its one by editing /etc/zcd.rc and changing MYSQL_EN=true to MYSQL_EN=false.

If you’re using Zend Server’s MySQL, then set a root password:

$ mysqladmin -u root password {new-password}
$ mysqladmin -u root -p{new-password} -h localhost password {new-password}
$ mysqladmin -u root -p{new-password} reload
$ history -c

I also had to link mysql.sock to /tmp so that some mysql CLI applications would work:

  • sudo ln -s /usr/local/zend/mysql/tmp/mysql.sock /tmp/mysql.sock

Apache vhosts

I couldn’t work out how to configure Apache virtual hosts in the Zend Server admin unless you had an “application package”, so I did them manually.

Apache is configured to look in /usr/local/zend/etc/sites.d/ for files names globals-*.conf and vhost_*.conf.

I created globals-vhost.conf:

NameVirtualHost *:80

LoadModule vhost_alias_module modules/mod_vhost_alias.so

And then I have vhost_dev.conf to configure my virtual hosts:

<virtualhost *:80>
    ServerAdmin webmaster@akrabat.com
    DocumentRoot "/www"
    ServerName localhost
    <directory "/www">
	    Options Indexes FollowSymLinks MultiViews
    	AllowOverride All
        Order allow,deny
	    Allow from all
    </directory>
</virtualhost>

<virtualhost *:80>
    VirtualDocumentRoot "/www/dev/%-2+/public"
    ServerName subdomains.dev
    ServerAlias *.dev
    UseCanonicalName Off
    LogFormat "%V %h %l %u %t "%r" %s %b" vcommon
    ErrorLog "/www/dev/vhosts-error_log"
    DirectoryIndex index.php index.html
    <directory "/www/dev/*">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
        
        SetEnv APPLICATION_ENV development
                
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]        
    </directory>    
</virtualhost>

This allows me to put my development files in /www and sets up automatic virtual hosts for http://*.dev.

Fix sending email

Sending email doesn’t work out of the box. To fix:

$ sudo mv /usr/local/zend/lib/libsasl2.2.dylib /usr/local/zend/lib/libsasl2.2.dylib.old
$ sudo ln -s /usr/lib/libsasl2.2.dylib /usr/local/zend/lib/libsasl2.2.dylib

See here for details.

Logs

The Zend Server admin displays your logs via Overview -> Logs. There is a list of log files down the left hand side. The interesting ones for me are:

  • error: Apache error log (/usr/local/zend/var/log/error.log)
  • access: Apache access log (/usr/local/zend/var/log/access.log)
  • php: PHP log (/usr/local/zend/var/log/php.log)

That’s it. Zend Server now works as a development stack and you get the nice diagnostics features like code tracing and the events list.

Password less command line scripts with MySQL 5.6

I have a number of command line scripts that copy MySQL databases down from staging servers and store them locally. These scripts set the password on the command line using the -p option.

With MySQL 5.6, a new warning is displayed every time my scripts run:

Warning: Using a password on the command line interface can be insecure.

This is annoying!

The way to solve this is to use the new command line tool mysql_config_editor to set up a “login path” which the mysql command line client can then use.

$ mysql_config_editor set --login-path=restore --host=localhost --user=restore --password

You will then be asked to enter the password for the user that you’ve set, ‘restore’ in this case.

Now, you can log in to mysql on the command line using:

$ mysql --login-path=restore

and note that you didn’t need to enter a password!

In my case, I then changed my scripts so that the restore line looks like this:

cat ${FILENAME} | mysql --login-path=restore ${DBNAME}

Adding Diff syntax highlighting to Sublime Text

My chosen colour scheme for Sublime Text doesn’t include support for diff/patch files, so I added my own.

To the bottom of my .tmTheme file, I added this just above the closing </array>:

        <dict>
            <key>name</key>
            <string>diff.header</string>
            <key>scope</key>
            <string>meta.diff, meta.diff.header</string>
            <key>settings</key>
            <dict>
                <key>foreground</key>
                <string>#009933</string>
            </dict>
        </dict>
        <dict>
            <key>name</key>
            <string>diff.deleted</string>
            <key>scope</key>
            <string>markup.deleted</string>
            <key>settings</key>
            <dict>
                <key>foreground</key>
                <string>#DD5555</string>
            </dict>
        </dict>
        <dict>
            <key>name</key>
            <string>diff.inserted</string>
            <key>scope</key>
            <string>markup.inserted</string>
            <key>settings</key>
            <dict>
                <key>foreground</key>
                <string>#3333FF</string>
            </dict>
        </dict>
        <dict>
            <key>name</key>
            <string>diff.changed</string>
            <key>scope</key>
            <string>markup.changed</string>
            <key>settings</key>
            <dict>
                <key>foreground</key>
                <string>#E6DB74</string>
            </dict>
        </dict>

This sets up a green colour for the meta information, blue for added lines, red for deleted lines and a yellowish colour for changed.

Notes on embedding fonts in rst2pdf

I wanted to use Helvetica Light in a PDF that I’m creating using rst2pdf and this proved a little tricker than I expected.

In this case, I discovered that setting stdFont: Helvetica-Light or stdFont: HelveticaLight in the style file resulted in Verdana being used in the PDF file!

Fortunately, the -v switch to rst2pdf is your friend when you want to know what’s happening:

[INFO] findfonts.py:270 Trying to embed Helvetica-Light
[INFO] findfonts.py:307 fname for findTTFont: Helvetica-Light
[INFO] findfonts.py:308 Variants via findTTFont: ['/Library/Fonts/Microsoft/Verdana.ttf', '/Library/Fonts/Microsoft/Verdana Bold.ttf', '/Library/Fonts/Microsoft/Verdana Italic.ttf', '/Library/Fonts/Microsoft/Verdana Bold Italic.ttf']
[INFO] findfonts.py:317 Registering font: Verdana from /Library/Fonts/Microsoft/Verdana.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold from /Library/Fonts/Microsoft/Verdana Bold.ttf
[INFO] findfonts.py:317 Registering font: Verdana Italic from /Library/Fonts/Microsoft/Verdana Italic.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold Italic from /Library/Fonts/Microsoft/Verdana Bold Italic.ttf
[INFO] findfonts.py:329 Embedding via findTTFont as ['Verdana', 'Verdana Bold', 'Verdana Italic', 'Verdana Bold Italic']

As can be seen by the output, rst2pdf can’t find Helvetica-Light and so has substituted Verdana instead. This would seem to be because it couldn’t find a TTF file for the font on my Mac as Helvetica Light is stored in a dfont file.

To solve this, I used DFontSplitter to extract Helvetica Light as a separate TTF file and added this as an embedded font to the style file:

embeddedFonts: []
    [fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf]

and re-ran with -v which gave me this information:

[INFO] styles.py:276 Registering font: fonts/HelveticaLight from ./fonts/HelveticaLight.ttf

I now know that the name to use when referencing this font is “fonts/HelveticaLight” and so I can use it within the style file like this:

stdFont: fonts/HelveticaLight

and the font is embedded as I expected.

Improved Sublime Text 2 PHP getter and setter generation

As I’ve been using Sublime Text 2 for all coding for the last year, I’ve noticed a significant problem with my simple ‘getset‘ snippet that I created last year: it doesn’t handle underscores in property names correctly.

I’ve finally got around to fix it!

As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. This snippet creates a getXxx() and setXxx() method from a property name.

To create a snippet, go to Tools->New Snippet... and replace the code example provided with this:

<snippet>
    <content><![CDATA[/**
 * Getter for ${1:$SELECTION}
 *
 * @return mixed
 */
public function get${1/(?:^|_)(w)/U$1$2/g$1}()
{
    return $this->$1;
}

/**
 * Setter for $1
 *
 * @param mixed $${1/_(w)/U$1/g$1} Value to set

 * @return self
 */
public function set${1/(?:^|_)(w)/U$1$2/g$1}($${1/_(w)/U$1/g$1})
{
    $this->$1 = $${1/_(w)/U$1/g$1};
    return $this;
}

]]></content>
    <!-- Optional: Tab trigger to activate the snippet -->
    <tabTrigger>gs</tabTrigger>
    <!-- Optional: Scope the tab trigger will be active in -->
    <scope>source.php</scope>
    <!-- Optional: Description to show in the menu -->
    <description>Create getter and setter methods</description>
</snippet>

Save the file as getset.sublime-snippet and you’re done.

To use, simply type gs followed by tab and it will automatically expand and you can then type your underscore_separated property name and the correct method names will be created.

Alternatively, select some text and press shift+cmd+p and then type gs to automatically replace the selected text with the get and set methods completed for the text that was selected.

As an example, typing gs {tab} date_last_updated will produce:

    /**
     * Getter for date_last_updated
     *
     * @return mixed
     */
    public function getDateLastUpdated()
    {
        return $this->date_last_updated;
    }
    
    /**
     * Setter for date_last_updated
     *
     * @param mixed $dateLastUpdated Value to set
    
     * @return self
     */
    public function setDateLastUpdated($dateLastUpdated)
    {
        $this->date_last_updated = $dateLastUpdated;
        return $this;
    }

I’ve also updated the snippet in my sublime-akrabat package on github.

Gittyup – Easily keep master in sync with upstream

If, like me, you use git and have an upstream remote which is not your origin, then I highly recommend that you use Evan Coury‘s gittyup script.

This is a very simple script that does the following:

1. Verify that you are in a valid Git repo.
2. Remember which branch you are on.
3. Stash any uncommitted changes you have.
4. Checkout master.
5. Fetch all remotes. (nice to track other remotes)
6. Merge upstream/master into local master (FF ONLY!)
7. Push your updated local master branch to origin.
8. Check out the branch you were previously on.
9. Applies any stashed changes and index status.

This is a very quick an easy way to update your local master with upstream and push to origin without losing any changes you may currently be working on.

You should set it up now!

Sublime Text 2 Plugin: Function Name Display

As I’m using Sublime Text 2 more and more, I thought it would be useful to display the current method name in the status bar. I poked around on the forums and created a plugin from ideas I found in a couple of different threads.

This plugin is imaginatively titled Sublime Function Name Display!

As I’ve hooked into the on_selection_modified event handler, I was keen to avoid slowing down the editor too much when you move your carat quickly. As a result, I have used a simple waiting system from forum member ‘facelessuser’, to ensure that the plugin only updates the status bar 100ms after you’ve stopped moving the carat.

As I don’t know Python, I copied the code from other people but I’m happy with the way my first plugin has turned out.