Categories
Mac PHP

Debugging with XDebug and PhpStorm on MacOS X

In an earlier post I demonstrated how to install PHP5.4 using brew. Today, I required xdebug for debugging purposes and found it quite easy to install via homebrew.

Install XDebug for PHP
It’s as simple as issuing the command:

Enabling XDebug
I had to add the extension to my php.ini file:

Follow the output of the brew info output of the package you installed (either php53 or php54):

To enable remote debugging, I also added these lines:

Debugging Web Applications
In the run configuration, add “Web Application” type. Configure the server and path as usual. When setup, you shall notice the debug button is active. Setup some break points and hit the debug button.

You can also use the “Listen PHP Debug Connection” option along with some xdebug bookmarklet for quick debugging.

Debugging Command Line Scripts
To debug command line scripts, first issue this command on terminal:

PS: I added the line to my ~/.bash_profile so that I don’t have to type it every time I login to bash shell.

Turn on incoming connections by clicking the “Start Listen PHP Debug Connection” button. Set some break points and then run the script from command line.

Categories
PHP

Composer & Packagist: Painless Dependency Manager for PHP

URLs:
Packagist: http://packagist.org/
Composer: http://getcomposer.org/

Composer is a painless dependency manager for PHP. If you have used the conventional PEAR packaging system, you have probably felt the pain already. Composer came to our rescue.

Getting Started with Composer:
Follow this link to get yourself introduced with Composer: http://getcomposer.org/doc/00-intro.md

In short, you declare the dependency of your project in a file named “composer.json”. Then run “composer install” on the terminal. Composer will install the dependencies and provide you a single autoloader file which you can require in your project and start using the libraries/tools etc.

I recommend installing composer into your own “~/bin” so that you can quickly access it from any place. Use these commands to achieve that:

Of course, I assumed that you already have a “~/bin” directory created and the path is in your bash profile or bash rc.

Packagist: Creating packages for Composer
(1) You must have a public repository of codes to prepare a package for packagist. The code repo should be in Subversion, Git or Mercurial.

(2) Create a composer.json in your package’s root directory. It is a good idea to use:

The prompt will help you get started with a basic skeleton.

(3) Add more metadata to the package by adding different fields to the composer.json file. You can read about all the available fields here: http://getcomposer.org/doc/04-schema.md

If your package depends on other packages, do mention them on the file. Also if your package needs certain PHP extension, use “ext-*” format. Like – “ext-apc” for apc extension, “ext-curl” for curl etc.

Do not forget to use the autoload feature. Define your PSR-0 or conventional autoloading pattern so that your package can be easily used with the Composer autoloader. Look into the “autoload” part of the schema for more details.

(4) After you have finished editing the composer.json file, validate it by running:

This is step is very important. If you have any error in the json document, packagist will not be able to parse your package. For now, there is no notification system for such failures. So you better validate before you publish the codes.

(5) Create an account, login to packagist.org and submit your package. Packagist allows convenient hooks for auto updating to your recent branches.

(6) Package versioning is dependent on the tags of CVS. Name your tags in “X.Y.Z” or “vX.Y.Z” format. Eg. “1.0.3” or “v1.0.3”.

Demo:
You might want to examine these works for digging deeper into composer and packagist –

PhpTube Package: http://packagist.org/packages/masnun/phptube
Github Repository: https://github.com/masnun/phptube

Categories
Mac MySQL PHP

Mac OS X Lion: Homebrew, PHP54, MySQL and PEAR

In one of my old posts, I noted short documentation on how to get started with the AMP stack on Mac OS X Lion. In that setup, I was using the default PHP and downloaded MySQL from the mysql website. In this post, I am going to describe how I used homebrew to setup my AMP environment with ease!

The Homebrew Magic
Homebrew is a cool tool for Mac OS users. Written in Ruby, this beautiful tool works as a package manager for Mac OS. Unlike Mac Ports or Fink, homebrew takes less space, uses the default os x tools to compile everything to a custom location. This makes life suck less and clutter free!

To install PHP 5.4 branch using homebrew, just issue this command:

PS: If you want to customize the installation (like installing the mysql module by default) add necessary configuration options. Using the “bash-completion” formula from brew, you can hit tabs to see available installation options.

To view information on your PHP 5.4 installation via brew, type in:

Seperate MySQL Installation: Where is the ctl?
I have installed MySQL separately. I added the mysql and mysqldmin to my bash_profile like this:

And to start/stop/restart mysql server, I added:

That allows me to do:

Using PEAR
Homebrew installs all the PEAR packages to “/usr/local/Cellar/php54/5.4.3/lib/php”. So, I added it to my php include_path.

For PEAR to work correctly, we need sudo. If there are tools involved in the pear setup which can be run from command line (eg. phpunit), the binary files are stored in: “/usr/local/Cellar/php54/5.4.3/bin”.

I added the above path to my .bash_profile for convenience 🙂