Saturday, October 16, 2010

Using nginx with PHP 5.3.3 on Windows

This post is mainly a reminder to my future self in case I need to do something like this again.


Using bleeding edge technologies on Windows has always been a painful process. Mainly because not many LAMP developers use windows (its just not in the acronym), which leads to poor support of the OS and lack of learning material.


After playing with NodeJS and watching Ryan’s presentation, I realized all the drawbacks of Apache - my default web server for many years - and decided to give ngninx a shot.


  1. Download and install a copy of the most recent php version for windows (PHP 5.3.3). Please note, that since we’re not gonna be using Apache, you can download the non tread safe version compiled with VC9.

  2. The second step is to get nginx executable in the download section of nginx website. On Windows its as simple as unzipping the file into c:\nginx directory.

  3. After that is done, we need to configure it to work with PHP. To do that let’s open c:\nginx\conf\nginx.conf and create the following server config:

    The above config tells nginx that application on http://your_app.lcl/ is located at c:\www\path\to\your\website on your filesystem. Furthermore, it tells nginx that all *.php files need to be served through a fast_cgi server on port 9000.

  4. Now that we configured nginx to use fast_cgi for *.php files, we need to start a fast_cgi server daemon. From your windows console run C:\php\php-cgi.exe -b 127.0.0.1:9000, where C:\php is the php installation path.

  5. You can access your application at http://your_app.lcl/ after starting the nginx process in c:\nginx\nginx.exe

Happy Coding!

Friday, October 15, 2010

Symfony Console Commands and DIC

I personally feel that conventions should be best practices and not inevitable parts of frameworks. Conventions are good, but they kill testability. So while they can save you some time you would have had to spend on configuration otherwise, they also limit the granularity of your interfaces and break testability.


My recent example of not testable controllers and how it could have been fixed was very well received amongst fellow Symfony2 developers, so that gives me enough confidence to propose something else.


There is another major part of the framework that can hardly be tested as it relies on Symfony’s internals and cannot use DIC for own configuration. Console Commands. They are registered by manual scan of bundles’ Console directory. They therefore cannot be configured through DIC with all dependencies moved to their interface definition and just get the generic Container instance instead.


Or can they? The answer is: “Yes, they can”.


And it wouldn’t be a lot of work to switch that. All we need to do is register each command in DIC as a service, and use tags to specify that this service is a command:


Let’s look at how we could then test one of the least testable Symfony2 commands - the Symfony\Bundle\FrameworkBundle\Command\AssetsInstallCommand. This command copies public assets like JavaScript and CSS files from the bundles’ Resources/public directories into a publicly accessible web directory, that is passed to it as the only parameter.


Since I’m gonna be testing the already existing class, the test will not be as elegant as it could have been:


While writing this test, I found out the command wasn’t testable because of a hard-coded mkdir function call that I couldn’t mock out. In order to fix it, I found the already existent Symfony\Bundle\FrameworkBundle\Util\Filesystem::mkdirs() function that wraps it, and makes it mockable, which I then proceeded to use. The only other changes I had to introduce were - get rid of Container dependency, and add Symfony\Bundle\FrameworkBundle\Command\AssetsInstallCommand::setKernel and Symfony\Bundle\FrameworkBundle\Command\AssetsInstallCommand::setFilesystem methods for direct injection of primary dependencies.


So here it is - the modified AssetsInstallCommand, that is fully unit-tested:


And here is the result of running it in PHPUnit:


phpunit screenshot

As always, feedback is much appreciated, and I have nice DISQUS Comments for that very purpose now!


Happy Coding!


P.S. While I was posting this, and embedding my thoughts in public gists, Kris Wallsmith suggested to use the tags to specify command names as well, which is a very interesting suggestion.


P.P.S. Henrik Bjørnskov was very happy when I shared this idea with him and contributed most of the initial implementation of this feature here.


P.P.P.S Code that I provided in the post is available on my GitHub repository, and is built on top of Henrik’s efforts.

Saturday, October 2, 2010

Symfony2 DIC Component Overview

As some of you might know, the Symfony2 framework consists of two main ingredients:


  • Components

  • Bundles

The logical separation should be the following:



The Symfony Components are standalone and reusable PHP classes. With no pre-requisite, except for PHP, you can install them today, and start using them right away. (Symfony Components Web Site)




A Bundle is a structured set of files (PHP files, stylesheets, JavaScripts, images, …) that implements a single feature (a blog, a forum, …) and which can be easily shared with other developers. (Symfony2 Documentation)



Of course, there are different vendor libraries that Symfony2 uses, that are not Components or Bundles. Its important to remember, that in order to expose that functionality in your Symfony2 application and make it accessible, you have to create a Bundle. Its a good practice and an unwritten convention.


I think that the main reason for doing so is to avoid setting up third party libraries yourself and delegate that to Symfony2’s DIC component, which was built for that very purpose. This lets other developers overload some of your configuration, class names and parameters without modifying your core classes and breaking backwards compatibility.



DIC stands for Dependency Injection Container.



The main idea behind Dependency Injection Containers is to extract all the instantiation and wiring logic from your application into a well-tested dedicated component, avoiding the code duplication that inevitably happens if you’re practicing Dependency Injection and Testability without DIC. By removing all of the setup code, Symfony2 removes another possibility of error and lets you concentrate on your domain problems instead of object instantiation.


Each object in Symfony2 DIC is called a Service. Service is an instance of some Class, that is created either by direct instantiation using the ‘new’ construct or using some other Service’s factory method, that gets certain dependencies injected into it as part of the instantiation process.


It is much easier to understand how services are configured by looking at an example configuration:


I personally find it very readable.


During the container instantiation, the XmlFileLoader takes the above-mentioned services.xml file and transforms it into PHP code, which looks similar to the following pseudo-code:


Now you have sort of a bird-eye view of how your objects are built and interact all in one place. No need to open some bootstrap file to see how everything gets wired together, and most importantly, no need to touch your code in order to change how things get wired together. Ideally, we want application to be able to perform completely different tasks, just by re-arranging some dependencies.


NOTE: All of your DI xml (or yaml or php) configurations need to live under <bundle name>/Resources/config directory of your application, in our example, I would store the configuration in MyCompany/PaymentBundle/Resources/config/services.xml.


The next step is to let your Symfony2 application know that you have this service configuration and want it to be included in the main application container. The way you do it is very conventional, although I know at least one way to make it configurable, but that’s a different topic and deserves its own blog post.


In order to include your custom configuration, you usually need to create something called Dependency Injection Extension. A DI Extension is a class, that lives under <bundle name>/DependencyInjection directory, that implements Symfony\Component\DependencyInjection\Extension\ExtensionInterface and which name is suffixed with ‘Extension’.


Inside that class, you need to implement four methods:


  • public function load($tag, array $config, ContainerBuilder $configuration);

  • public function getNamespace();

  • public function getXsdValidationBasePath();

  • public function getAlias();

Or you could choose to extend Symfony\Component\DependencyInjection\Extension\Extension and have to worry only about the last three.


Let’s look at an example extension, that would register our services.xml configuration file with Symfony2’s DIC:


This extension does several things:


  • It will include the services.xml into DIC only if ‘payment_gateway’ service is not yet defined - this is to avoid conflicts and lazy-load the configuration

  • It will override some of default parameters, if you specify your own when enabling the extension

  • It also provides the XSD schema location and base path for validation of XML configuration

After you created the extension, all you need to do is add PaymentBundle to the application Kernel::registerBundles() method’s returned array. Then in the application configuration file specif something like ‘payment.config: ~’ (assuming you’re using yaml configs). That should do it, you should now be able to call $container->getService(‘payment_gateway’) and get the fully set up instance of Gateway.


Happy Coding!

Friday, October 1, 2010

Interface Injection and Symfony2 DIC

During the preparation for my recent talk, I had to refresh my knowledge of the theoretical side of Dependency Injection, since I was using it for a while, but found myself not knowing some of the terminology. I found “Dependency Injection” article on Wikipedia.org to very fulfilling. One thing that caught my attention specifically was the types of Dependency Injection, and Wikipedia defines three of them:


  1. Interface Injection (type 1)

  2. Setter Injection (type 2)

  3. Constructor Injection (type 3)

While the last two types are what I use in day-to-day coding, the first type was rather unfamiliar. So I looked it up, and Martin Fowler had a good definition.



[Interface Injection] - [is a technique] to define and use interfaces for the injection (Martin Fowler)



Examples on Martin Fowler’s blog are quite complicated and would be more confusing to PHP developers than beneficial. But the general idea, which resonates with my point on Composition over Inheritance, is very simple - allow certain service to be injected into other services, that implement a common interface.


Example use case:


I have an interface ContainerAware, and I want all services that implement that interface to receive instance of Container through its ->setContainer() method. This would be very useful in Symfony2, if you were defining controllers as services and needed container. Right now there is a hard-coded ‘instanceof’ check deep inside Symfony2’s controller instantiation process, that sets the container on ContainterAware controllers. If Symfony2 DIC component supported the first type of Dependency Injection (Interface Injection), this would not be necessary, and many more interfaces could be defined, removing the verbosity from configuration.


Enough talking, show us some code:


NOTE: Actually any controller that extends Symfony\Bundle\FrameworkBundle\Controller\Controller implements ContainerAwareInterface, as it inherits it from parent, but for the purposes of this example, I chose code that shows everything explicitly.


NOTE: There is no way to define Interface Injectors in Symfony2 yet, as it doesn’t support the first type of Dependency Injection. This is my proposed way of configuring interface injectors in xml.


All we would need to do now is register our controller within the Symfony2’s DIC component without defining the method call explicitly, since the DIC would know to apply interface injectors to all services, requiring them:


The described approach makes DIC configurations take less space to achieve the same result. Because if you’re implementing a certain interface that defines rules for injection of other services, you usually need to re-use the same services across all instances of the interface, but have to explicitly define the injections every time.


I can see some of the Symfony2’s built-in services benefiting from Interface Injection:


I definitely have a lot more classes, that can benefit from it in my problem domain.


So with all that in mind I spent last evening putting together my own implementation of Interface Injection in Symfony2’s DIC component, and you can find it in my Symfony2 for on github.


Please comment with suggestions and/or feedback, very interested in what you think.


Happy Coding!

Thursday, September 30, 2010

Symfony2 Controller Testing

Having a rich background in different MVC frameworks, one thing that was always unclear was how to test controllers. I think the main reason it wasn’t obvious is because controllers had always been sort of a black magic element in frameworks. There were too many conventions on where on the file system controller should be located, what dependencies it should have knowledge of and those dependencies were always wired with the controller the hard way (view layer).


Environment like that assumes no easy way for controller testing, since you can’t instantiate a controller and some of its primary dependencies to test the interaction - you have to boot the whole framework and write functional tests.


Because of how complicated that process is, people usually don’t unit-test controllers, functional test is the maximum you can get, but usually you don’t get anything at all.


Symfony2 changes that completely.


Initially Symfony2 framework had only conventional controller loading approach. Controller instance still was very lightweight and was not required to extend some parent class in order to work. If your controllers implemented ContainerAware interface, you would have the DIC (dependency injection container) inserted using ContainerAware::setContainer() method, which you could then use to access any service that you registered in DIC.


The proposed method of testing controllers at the time was a black box testing approach, where you test full requests to your application and assert their output like so:


Although this method is easy to read and understand, there are a couple of drawbacks:


  • To execute the test, we need to bootstrap the Kernel

  • This test can only assert response body, which makes it fragile to design changes

  • As a result of all of the above, it runs much slower and does much more than it needs to

In ideal world, I would want to test controller interaction with other services in my application, like so:


NOTE: Controller is just a POPO (plain old PHP object) without any base class that it needs to extend. Symfony2 doesn’t need anything but the class itself for controller to work.


NOTE: read more about mock object framework in PHPUnit


Well, the good news is that Symfony2 allows that. Now all your controllers can be services. The old, conventional approach is still supported and is irreplaceable for small application controllers, with no need for unit-testing.


To make the above example controller correctly interact with Symfony2 and work as expected, we need the following.


Create controller class:


Create DIC configuration, using the following xml:


Create routing configuration:


NOTE: in the above example, we used service_id:action instead of the regular BundleBundle:Controller:action (without the ‘Action’ suffix)


After all of the above is done, we need to inform Symfony2 of our services. To avoid creating a new Dependency Injection extension and creating configuration file entry, we can register our services directly:


NOTE: the above technique was originally invented by Kris Wallsmith in a project we were working on at OpenSky.


Now you’re ready to go. You need to include your bundle level routing file in the application level routing configuration, create the Index directory. The final file structure should look something like this:


After all the above steps are completed, you can try it from the browser, by going to:


    http://your_application/your_front_controller.php/index


Happy Coding!

Wednesday, September 29, 2010

Clean code talk on Friday

Clean code talk on Friday

Last Friday I gave a talk on unit-testing and programming best practices at OpenSky. This is the talk summary and some insight on how that turned out.

Tuesday, September 28, 2010

My review of Symfony NYC Meetup on September 23rd, 2010

My review of Symfony NYC Meetup on September 23rd, 2010

Some of you might, others might not know that our company, OpenSky, became the official sponsor of Symfony NYC Meetups, which means, that we’ll be seeing you the last Thursday of every month, here, at OpenSky HQ in NYC for the foreseeable future…

Saturday, July 24, 2010

NodeJS + MongoDB = TinyURL

Last week I started playing with NodeJS.


NodeJS is an evented IO for V8 JavaScript. It uses asynchronous processing for all operations. If you want to open file, you pass in the callback function as argument to file open function, which then gets executed when the file is opened and available.


The aforementioned approach leads a much higher number of requests with a much less execution and, most importantly, wait times. And since its all JavaScript, we’re not surprised, because that is how JavaScript essentially works in the browser window. Think about it, the browser needs to re-draw its ui every time you a change occurs, staying responsive to user interactions at the same time, the only way to do this is by executing event/callback loops.


So where would you use NodeJS. In real-time data processing. The perfect example of such usage is HummingBird, which uses NodeJS and MongoDB for producing real-time usage analytics and statistics for Gilt Groupe website, you can fork it on github.


Now I was installing NodeJS under Cygwin which was a bit tricky at first (the python version that comes with Cygwin needed to be ‘rebased’).


To run a rebase command on Windows, you need to close all Cygwin processes, open C:\cygwin\bin\ash.exe, and run:


/usr/bin/rebaseall

In order to compile NodeJS, you would need the following:


After all the above dependencies were installed, cd to /path/to/your/nodejs, and run:


./configure
make
make install

To run tests, execute:


make test

Now that you have NodeJS installed, the first step is to create a ‘Hello World!’ application, to do this, create example.js with the following content:


When the file is created, go to the directory its located and run:


node example.js

If everything is correct, you should see something like this:



I used NodeJS and MongoDB to create a simple TinyURL application - you give it the url, it shortens it. The shortening algorithm is simple, each new url is assigned an auto-incrementing numeric index, which is then base 36 encoded to produce url key. Whenever url key is received, its base 36 decoded to get the numeric index, which is used to get the target location and send a 301 redirect header.


The application is very simple and lacks any kind of validation or error handling. The source code is open and available on github. Please feel free to fork it and use it. Feedback is much appreciated.

Friday, July 23, 2010