Categories
PHP

Using annotations in PHP with Doctrine Annotation Reader

Ever since I have seen the use of Annotations in Symfony2 framework, I have been looking for a way to use them in my own projects as well. After looking into the Symfony2’s source code, I noticed that they are indirectly using Doctrine’s Annotation Reader. Reading annotation from your own class is pretty easy. PHP includes the docblocks in the different reflection objects. You just have to read the docblock and parse it. Doctrine does it pretty nicely. Let’s see an example!

To get started, you first need to install doctrine-common package. I recommend you install using composer. Here’s my composer.json for the project:

Now, we need to define the annotation classes. Every annotation you use should have a class with the same name. Doctrine Annotation Reader will map those annotations with these classes and pass the values to the public properties of these classes.

In our example, we shall use one annotation class named – “AnnotatedDescription”. It can be directly passed a value, like:

or in an associative fashion –

We shall allow two keys – “desc” and “type” in these example. But feel free to add your own. Again, in real life, I would declare separate classes for capturing single value and key-value pairs.

Lets declare the AnnotatedDescription class in annotations.php:

Note that, the $value property is used for annotations which accept just a value. For key value pairs, we must declare properties with the key names. You *must* annotate the classes with @Annotation to let Doctrine’s Annotation Reader know that this class should be used for annotations mapping.

Now, lets see the parser file (parse_annotations.php):

The code is pretty much self explanatory. We instantiate “Doctrine\Common\Annotations\AnnotationReader” and use it’s different methods to get the annotations. You might want to study the source code of “Doctrine\Common\Annotations” namespace for even detailed usages.

Note that we must pass reflection objects for the class, properties and methods (eg. ReflectionClass, ReflectionObject, ReflectionProperty, ReflectionMethod). For the properties , instead of directly constructing ReflectionProperty object, we can use getProperty() on a ReflectionObject object in runtime.

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
Mac Python

Setting up Python, MySQL Module and Django on Mac OS X Lion

I setup Python and Django using homebrew the easy way. I am going to detail the process now.

Install Python

The command should first install any dependencies for Python and then install the latest version from source code.

Adding Python to PATH

We need to edit our ~/.bash_profile to add “/usr/local/share/python” to the path variable. I added the following line to my bash profile:

I reloaded the profile by issuing:

This makes the python tools and toys accessible from my terminal.

Install MySQL Module

It’s recommended that you already have “mysql” installed from brew to build the mysql module. I already had it installed so it was a painless installation for me. I just issued the command:

Install Django
I am going to keep using the PIP awesomeness! Let’s hit:

It will install django with any dependencies. You can now invoke the “django-admin.py” command from your terminal. However, I prefer the command be “django-admin”, instead of “django-admin.py”. So I created the following alias in my ~/.bash_profile:

Isn’t it simple? 😀