Mike Naberezny » PHP http://mikenaberezny.com Fri, 27 Aug 2010 02:15:17 +0000 en hourly 1 http://wordpress.org/?v=3.0.1 Horde/Routes 0.4 Released http://mikenaberezny.com/2009/03/04/horderoutes-04-released/ http://mikenaberezny.com/2009/03/04/horderoutes-04-released/#comments Wed, 04 Mar 2009 18:00:40 +0000 Mike Naberezny http://mikenaberezny.com/?p=888 Horde/Routes 0.4 has been released. Horde/Routes is a URL mapping system for PHP 5 that provides classes for mapping URLs to the controllers and actions of an MVC system. Learn more at the project website or this presentation.

This version includes a new utility class, Horde_Routes_Printer, a debugging aid that can print out a summary of all routes that are connected to a mapper. It is available through a convenience method from Horde_Routes_Utils:

$map = new Horde_Routes_Mapper();
$map->connect('post', ':title',
              array('controller'=>'posts','action'=>'show'));
$map->connect('feeds', 'feeds/:format',
              array('controller'=>'feeds'));              
$map->connect(':controller/:action/:id');
 
$map->utils->printRoutes();

This will cause the following information to be printed to php://output:

 post  /:title                  {:controller=>"posts", :action=>"show"}
feeds  /feeds/:format           {:controller=>"feeds", :action=>"index"}
       /:controller/:action/:id

You’ll see above that the name of the route (if any) is shown, the route path, and the default values for the route. If a route only matches for a certain HTTP method, this will be shown as well. It’s handy when you have a large number of routes, or resource routes.

If you’d like the debug output to go somewhere else, pass a stream resource as the first argument to printRoutes(). The output is formatted similarly to the rake routes command from Rails. Although the notation is a little unusual for PHP, it’s compact and readable. See Horde_Routes_Printer if you’d like to modify the format.

Since it’s release, we’re received a good amount of positive feedback on Horde/Routes. It is now being shared by the upcoming Horde 4 (Rampage), the Seagull framework, and a few others. Check out the integration guide for how to add it to your project.

]]>
http://mikenaberezny.com/2009/03/04/horderoutes-04-released/feed/ 2
PyWorks 2008 Slides http://mikenaberezny.com/2008/11/15/pyworks-2008-slides/ http://mikenaberezny.com/2008/11/15/pyworks-2008-slides/#comments Sat, 15 Nov 2008 22:45:51 +0000 Mike Naberezny http://mikenaberezny.com/?p=721 Slides from my PyWorks 2008 talks are now available. This conference was shared with php|works and I enjoyed this format. It was great to see many of my Python and PHP friends at the same event. Thanks to everyone who attended my talks.

Py65: Microcontroller Simulation with Python

Download Slides (PDF)

This talk introduced the venerable 6502 microprocessor family, building small computer systems with these parts, and then simulating those systems with Py65.

The audience participation was great. We had fun stepping through some small assembly language programs on the simulator. One attendee wrote:

This was fascinating and the speaker was awesomely enthusiastic. The overview of microcontrollers and their significance was enlightening and entertaining. The simulator design presented was fantastically simple and very Pythonic. I can’t wait to see where this project goes.

Thanks and I’m glad you enjoyed it. For updates on the Py65 simulator, please watch my blog and the Py65 project page on Ohloh.

URL Mapping with Routes

Download Slides (PDF)

We explored the Routes library from the ground up, setting it up and then exploring its options and matching. We worked through many of the examples with live demos on the Python interactive interpreter.

The talk was attended by several Pylons users, who gained a better understanding of how Routes works by seeing it outside the context of any particular web framework.

URL Mapping with Horde/Routes

Download Slides (PDF)

Bonus Slides! Horde/Routes is a PHP 5 library that is a direct port of Routes. Since there were so many PHP folks at this conference as well, I ported all of the examples in my Routes talk to work with Horde/Routes.

These slides will help you get acquainted with the PHP version. Since the two presentations are otherwise identical, you might also find it an interesting comparison between Python and PHP 5.

]]>
http://mikenaberezny.com/2008/11/15/pyworks-2008-slides/feed/ 0
PHP Temporary Streams http://mikenaberezny.com/2008/10/17/php-temporary-streams/ http://mikenaberezny.com/2008/10/17/php-temporary-streams/#comments Sat, 18 Oct 2008 01:17:13 +0000 Mike Naberezny http://mikenaberezny.com/?p=156 It’s been a while since David Sklar called out to let a thousand string concatenations bloom. That discussion produced some entertaining suggestions for putting strings together such as using preg_replace and calling out to MySQL with SELECT CONCAT.

Here’s an approach that uses filesystem functions. When combined with some lesser-known PHP streams functionality, it has several practical applications.

Opening Files for Reading and Writing

There are several modes for opening a file that will allow you to seek to arbitrary positions in the file and read or write at those positions. One of the most frequently used of these modes is w+, which the tmpfile function uses automatically.

We can repeatedly write, then rewind the pointer and read:

$f = tmpfile();
fwrite($f, 'foo');
fwrite($f, 'bar');
 
rewind($f);
$contents = stream_get_contents($f);  //=> "foobar"
fclose($f);

When writing to the filesystem, the above provides yet another inefficient solution to David’s exercise. Now let’s take it a bit further to see how this can be useful.

In-Memory Streams

PHP 5.1 introduced two new in-memory streams: php://memory and php://temp. The php://memory stream operates entirely in memory. The php://temp stream operates in memory until it reaches a given size, then transparently switches to the filesystem.

We can modify the above example to use the php://memory stream instead of hitting the filesystem:

$f = fopen('php://memory', 'w+');
fwrite($f, 'foo');
fwrite($f, 'bar');
 
rewind($f);
$contents = stream_get_contents($f);  //=> "foobar"
fclose($f);

Putting a string inside a fast temporary stream can be very useful. For example, we can then attach filters to that stream.

Testing

Temporary streams are also handy for testing. There are some rather elaborate virtual file system libraries out there but many times a stream is all you need.

Zend_Log has a log handler for streams that accepts either a filename or a stream resource. We can configure it with a php://memory stream for testing:

$f = fopen('php://memory', 'w+');
$writer = new Zend_Log_Writer_Stream($f);
$logger = new Zend_Log($writer);

Assuming your well-designed application has a convenient injection point for the logger instance, your test can pass it in before your test activates some action which should result in logging:

$logger->crit('critical message worth testing');

When the action has completed, the test can rewind $f and use it as a test spy.

rewind($f);
$contents = stream_get_contents($f);
$this->assertRegExp('/message worth testing/i', $contents);  // PHPUnit

Not surprisingly, my unit tests for Zend_Log use this same technique.

Application Usage

Not long ago, we built a custom document storage system for a client. One of its more interesting features is that it integrates with an internet fax service so users can select documents in the system and then fax them. For each fax, the software automatically generates a cover page.

To make the cover page, I first made a nice template using a drawing tool and then saved it in PDF format. I then used Zend_Pdf to write over the template with dynamic content. I first demonstrated this technique in this article.

Since the cover page is only used once (during transmission) and easy to regenerate, I don’t save the output to the filesystem. Instead, I create a php://temp stream. The instance method Zend_Pdf->render() writes the PDF output only to that stream, which is then rewound. Functions like stream_copy_to_stream or fpassthru can then be used to send the final output where it needs to go, and the whole process normally never needs to use the disk.

]]>
http://mikenaberezny.com/2008/10/17/php-temporary-streams/feed/ 6
PHP Developer Best Practices http://mikenaberezny.com/2008/09/16/php-developer-best-practices/ http://mikenaberezny.com/2008/09/16/php-developer-best-practices/#comments Tue, 16 Sep 2008 18:01:24 +0000 Mike Naberezny http://mikenaberezny.com/?p=426 Matthew Weier O’Phinney and I gave a tutorial session at ZendCon 2008 this year titled “PHP Developer Best Practices”. The tutorial touched on source control, coding standards, testing, documentation, and more.

The slides are now available in PDF format.

We were located in Hall B of the Santa Clara Convention Center, which is a very large room that’s also used for the keynotes. Andi told us the room was selected based on the number of people who registered for our session. We initially had doubts but the attendance was greater than any previous year and the room worked out quite well. We were thankful that unlike last year, everyone was able to get a seat.

Thank you to all who attended. We enjoyed meeting many of you during the breaks and hope that you found our session helpful.

]]>
http://mikenaberezny.com/2008/09/16/php-developer-best-practices/feed/ 5
Speaking at PyWorks 2008 http://mikenaberezny.com/2008/08/29/speaking-at-pyworks-2008/ http://mikenaberezny.com/2008/08/29/speaking-at-pyworks-2008/#comments Fri, 29 Aug 2008 20:54:47 +0000 Mike Naberezny http://mikenaberezny.com/?p=342 PyWorks is a new conference from the folks at Python Magazine. It is being co-hosted with a PHP conference, php|works, and attendees of either conference have access to talks on both the Python and PHP tracks. Being a user of both languages, I think this a great idea and I’m looking forward to this format. I’ll be giving two talks at PyWorks 2008:

Microcontroller Simulation in Python

This talk will present Py65, my open-source simulation of a small microcomputer system based on the MOS 6502. The 6502 is a very famous microprocessor that was used in early microcomputers like the Commodore 64, but its design has stood up for over 30 years. Cores based on the 6502 are now widely used in embedded devices, with WDC estimating annual volumes in the hundreds of millions of units.

Using Python and software tools such as Py65, low-level software for embedded systems can be developed and tested much faster. This talk will discuss the design and implementation of Py65, how it and tools like it can help, and will also touch on other Python libraries that can assist with embedded development. (PyWorks Abstract)

URL Mapping with Routes

Routes is a Python package that provides a solution to the problem, “how do I map URLs to my code?” Its solution is an interesting one, and is actually a re-implementation of a feature from Ruby on Rails. Routes itself has also now been ported to PHP 5 as part of the Horde project. Routes is used by the Pylons web framework and other frameworks in Python, and is relatively easy to use as a standalone package.

The Routes method of URL dispatch is based around pattern matching rather than object publishing. For those new to Routes, we’ll have an introduction to the basic Routes concepts and how it works. We’ll also dive into the Routes internals and follow some URLs through their recognition phase, learning about how Routes does it job along the way. Web developers and framework implementers alike will gain a better understanding of Routes and how to use it effectively. (PyWorks Abstract)

]]>
http://mikenaberezny.com/2008/08/29/speaking-at-pyworks-2008/feed/ 2
Speaking at ZendCon 2008 http://mikenaberezny.com/2008/08/22/speaking-at-zendcon-2008/ http://mikenaberezny.com/2008/08/22/speaking-at-zendcon-2008/#comments Sat, 23 Aug 2008 04:13:21 +0000 Mike Naberezny http://mikenaberezny.com/?p=323 I will be speaking again this year at the Zend/PHP Conference. I have teamed up with Matthew Weier-O’Phinney to present PHP Developer Best Practices:

During this tutorial session, we will cover a number of best practices you can institute in your organization or in your personal coding toolbox, including:

  • Testing Strategies
  • Coding Standards
  • Documentation
  • Version Control
  • Deployment Methodologies

We will discuss the hows and whys of each topic, and strive to cover concrete examples that you can start implementing immediately, so that you and your developers can start working more efficiently.

It has become a tradition for Matthew and I to present a tutorial session together at ZendCon. We’ve done a new one every year since the first ZendCon. I always enjoy this conference and I’m looking forward to it again this year.

Update: Slides from the presentation are now available.

]]>
http://mikenaberezny.com/2008/08/22/speaking-at-zendcon-2008/feed/ 0
OSCON 2008 Slides http://mikenaberezny.com/2008/07/25/oscon-2008-slides/ http://mikenaberezny.com/2008/07/25/oscon-2008-slides/#comments Fri, 25 Jul 2008 18:09:47 +0000 Mike Naberezny http://mikenaberezny.com/?p=214 Slides from my two talks at OSCON 2008 are now available:

Both talks were well attended had great audience participation. Thanks to everyone that attended and I hope you enjoyed them.

About seven people came up after the Integration Testing talk with good questions and feedback. I think that 45 minutes was enough to provide good starting points for the topics covered but it was clear that these users were engaged and ready to dig into it more. If I give this talk again, it is going to be in a tutorial format so we can get much deeper into the material and have some exercises.

After my Supervisor talk at the end of the day, I had the pleasure of going out to dinner with Roger Hoover and some other Supervisor users. It’s been very exciting to see Supervisor picking up traction over the past year. Besides the work that Chris McDonough and I are doing, Supervisor is being used in several large architectures of companies whose names you know. If you’re using Supervisor and wouldn’t mind us telling others about it, please contact me.

]]>
http://mikenaberezny.com/2008/07/25/oscon-2008-slides/feed/ 0
Horde/Routes 0.3 Released http://mikenaberezny.com/2008/06/15/horderoutes-03-released/ http://mikenaberezny.com/2008/06/15/horderoutes-03-released/#comments Sun, 15 Jun 2008 21:23:44 +0000 Mike Naberezny http://mikenaberezny.com/?p=160 Horde/Routes is a URL mapping system for PHP 5. It is a direct port of the Routes, a Python library that is part of the Pylons project. Horde/Routes is a standalone library designed to be integrated into any MVC framework.

This release brings in some changes from Routes 1.8. The most noticeable change is that custom actions on RESTful routes are now delimited with the forward slash (/) instead of the semicolon (;). This was done for parity with Ruby on Rails.

I have also fixed some bugs, notably that the resource route generator failed to generate routes that recognized PUT and DELETE requests on “formatted” resources (/messages/1.xml). This fix will be merged upstream to the Python version.

I am also using the Python version and I met with Ben Bangert, the author of the Python version, at PyCon 2008. Each release of Horde/Routes has resulted in patches to the Python version. It’s nice how this small ecosystem has developed around the routes concept between these projects (Ruby on Rails, Pylons, and our work in Horde).

Since Horde/Routes 0.3, the default RESTful routes generated by Horde/Routes are fully compatible with the latest version of ActiveResource. We have a new project at work that is using Horde/Routes and ActiveResource together and it works well.

]]>
http://mikenaberezny.com/2008/06/15/horderoutes-03-released/feed/ 4
Speaking at OSCON 2008 http://mikenaberezny.com/2008/04/02/speaking-at-oscon-2008/ http://mikenaberezny.com/2008/04/02/speaking-at-oscon-2008/#comments Wed, 02 Apr 2008 15:18:26 +0000 Mike Naberezny http://mikenaberezny.com/?p=103 The OSCON 2008 website has published its talk schedule. I’ll be giving two talks at OSCON this year; one on the Python track and one on the PHP track.

Supervisor as a Platform

I will quickly introduce you to Supervisor and the immediate benefits of running your server processes under it. We will then dive into how applications written specifically for Supervisor can take advantage of it as a platform — writing your own event listeners to observe Supervisor and the process lifecycle, controlling with XML-RPC, and extending the Supervisor core with your own Python extensions.

This will be an expanded version of the talk I gave with Chris McDonough at PyCon 2008. Since PyCon, there’s been quite a few interesting developments in Supervisor like the ability to extend supervisorctl and progress made on configuration reloading. We’ll touch on these as well, so if you attended the PyCon talk there will still be new and interesting material in this talk for you.

Integration Testing PHP Applications

While more PHP developers are recognizing the importance and benefits of unit testing, the uptake of PHP developers using automated integration or acceptance testing is relatively slow. This testing is equally crucial to maintaining the integrity of applications.

I’m going to help get you started testing at the application level with practical tips and source code. We’ll look at how to structure your HTML markup so it’s more easily testable, making tests easier to write and maintain with CSS selectors, organizing your tests, and testing with or without a browser.

We write a lot of PHP applications with this kind of testing at Maintainable. Before the conference, we plan to release some open source PHP code to help you test that we’ll cover in the talk as well. I’d also suggest you check out Sebastian Bergmann’s tutorial session on PHPUnit’s integration with Selenium RC.

]]>
http://mikenaberezny.com/2008/04/02/speaking-at-oscon-2008/feed/ 0
New Rails for PHP Developers Website http://mikenaberezny.com/2008/02/18/new-rails-for-php-developers-website/ http://mikenaberezny.com/2008/02/18/new-rails-for-php-developers-website/#comments Mon, 18 Feb 2008 20:05:34 +0000 Mike Naberezny http://mikenaberezny.com/2008/02/18/new-rails-for-php-developers-website/

Rails for PHP Developers is a new website that’s a companion to the new book by the same name. Like the book, it’s aimed towards PHP developers who have an interest in Rails and Ruby. The website features articles that alternate between Ruby and PHP focus, so PHP developers that aren’t interested in Ruby should still find it useful.

]]>
http://mikenaberezny.com/2008/02/18/new-rails-for-php-developers-website/feed/ 0