SkiPHP Conference 2014

It’s always fun to be a part of a conference’s first year. This year, I was lucky enough to speak at the inaugural SkiPHP Conference in Salt Lake City, UT. Since the conference was very micro-framework friendly, I was able to present my own Bullet PHP Micro-Framework. The talk went well, was very well recieved, and got very highly rated on Joind.in. The talk (and Bullet itself) presents some interesting ideas that I was very happy to be able to share in a conference talk format.

Overall, the conference was great. It was very well put together, and very well run. The orgainzers sent lots of emails ahead of time letting us know what was going on, and took care of speakers very well – even having breakfast ready for us each morning in the speaker lounge (something that was very much appreciated by myself and many other speakers as well). I thought the talk selection was good as well – there were more sessions at this conference that I was personally interested in atending than I have in while at many other conferences over the years.

Huge thanks and kudos go to the SkiPHP organizers – I hope this is a sign of good things from the Salt Lake City PHP community, and I hope there are many more sucessful years of this conference ahead.

Bullet: The Functional PHP Micro-Framework

My presentation slides are avaialble on Slideshare, though I am not sure how enlightening they will be without the audio. For this particular talk of mine, the audio context is pretty important. Luckily, my entire talk was recorded on video, so I will post that here when available.

Post-Conference Activities

Half the fun of a conference is all the activities surrounding it, and all the people you meet and talk to who are also there. At SkiPHP, the main event was obvious – skiing! A small group of us stayed an extra day or two after the conference to hit the slopes at Snowbird.

skiphp-group

Pictured (Front): Adam Culp, Ben Edmonds, Chris Boden, Josh Adell (Back) Paul Jones, Vance Lucas, John Coggeshall

Paul Jones and I started with the beginner course and stayed on the “Chickadee” hill since it was our first time snow skiing, but the other guys hit the real slopes, and have much better, more majestic mountanside pictures and views to share. I did manage to take a good selfie on the ski lift though!

skiphp-selfie

Selfie on the ski lift at Snowbird in Salt Lake City, UT

Wrapping Up

All in all, my time at SkiPHP was a blast. The Salt Lake City PHP community really came together to put on a great conference and show their support for events like this in their backyard. I am already looking forward to submitting again next year! Maybe I can try some green slopes this time :).

New Year, New Blog

It’s a whole new year, and I’ve got a whole new blog. This time around, I knew I wanted a static blog generator instead of a WordPress site (they are a little more developer friendly, and there are no security vulnerabilities with static HTML), and I’ve been window shopping a bit. A self-hosted blog was important to me since I want to make sure I will always own and control all my own content.

The Options

Though popular, Octopress was out, because of my experience using it on OKC.js. It is difficult to customize, and the Octopress code is mixed in with your blog and website content, making upgrading difficult as well.

Using Jekyll was very tempting – and I almost used it, but I decided to take a look at another option first – and I’m very glad I did.

Enter Middleman

I ended up going with Middleman for this blog. We just re-launched the Brightbit website with it, and Joshua (my design co-founder) was raving about it, so I had to give it a shot. Both Jekyll and Octopress use Liquid templates, which are a learning curve if you’ve never used them. I personally don’t like the syntax, so I wasn’t too keen on doing a lot of layout customization with it.

Middleman, however, is different. Middleman offers so much more flexibility – pure Ruby code, Sass, the option to use Slim or Haml for templates, blog posts, and pages, and the same asset pipeline that Rails has for combining and compressing your CSS and JavaScript into a single file for production deployment. Layout customization is also easier and better feeling in general. The last, and perhaps biggest reason Middleman is a winner is that Middleman exists entirely inside it’s own self-contained gem. Your site’s project folder has only what it should – your site’s content. There is no Middleman cruft in there that you have to bring along, and upgrades are clean and brainless since Middleman is a gem.

Wrapping Up

Importing all my old posts from WordPress and converting them all to markdown was a bit tricky and time-consuming (though wp2middleman did most of the heavy lifting), but I’m glad I did it. Here’s to a great 2014 on a great new (and much better looking) blog.

Valitron: The Simple Validation Library That Doesn’t Suck

Valitron is a simple, minimal and elegant stand-alone PHP validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax.

Why Another Validation Library?

Valitron was created out of frustration with other validation libraries that have dependencies on large components from other frameworks unrelated to validation like Illuminate Validation (laravel 4) requiring  Symfony HttpFoundation, pulling in a ton of extra files that aren’t needed for basic validation. It also has purposefully simple syntax used to run all validations in one call instead of individually validating each value by instantiating new classes and validating values one at a time like Respect Validation requires you to do. Valitron also has a focus on being concise – validation rules are just a single line per rule, and can include multiple fields in an array. This is handy, because in most use cases, a single validation rule – like “required” will be applied to many fields, so it doesn’t make sense to start with the field first like Fuel Validation does.

In short, Valitron is everything you’ve been looking for in a validation library but haven’t been able to find until now: simple pragmatic syntax, lightweight code that makes sense, extensibility for custom callbacks and validations, good test coverage, and no dependencies.

Usage Example

Valitron is made to setup all your validation rules on the fields you need, and then run all the validations in one call. This is better than validating the fields one-by-one, because that approach causes a lot of “if” statements and branching logic that doesn’t make the resulting code any better than doing the validations by hand (which obviously sucks).

<?php
$v = new Valitron\Validator($_POST);
// Input array
$v->rule('required', ['name', 'email', 'date_start']);
$v->rule('email', 'email'); // Email uses filter_var
$v->rule('dateAfter', 'date_start', new \DateTime()); // After today
if($v->validate()) {
     echo "Yay! We're all good!";
     print_r($v->data());
} else {
     // Errors
     print_r($v->errors());
}

More usage examples and documentation can be found on the Valitron GitHub Page. Valitron is on Packagist, and can be installed via Composer.

Introducing Bullet: The Functional PHP Micro-Framework

Bullet is a new PHP micro-framework with a unique functional approach to URL routing that allows for more flexibility and requires less verbosity than the more typical full route+callback approach found in other micro-frameworks.

The Problem with Independent Scope

The main problem with most micro-frameworks and even full-stack MVC frameworks that leads to code duplication is that the callback or method executed to perform the action and respond to the URL route lives fully within its own scope. This means that you are forced to repeat a lot of setup code across URL route handlers that load the same resource, authorize it, etc.

Some typical micro-framework code might look like this:

<?php
// View single post
$app->get('/posts/:id', function($id) {
     $post = Post::find($id);
     check_user_acl_for($post);
     // ...
});

// Delete post
$app->delete('/posts/:id', function($id) {
     $post = Post::find($id);
     check_user_acl_for($post);
     $post->delete();
     // ...
});

// Edit post
$app->get('/posts/:id/edit', function($id) {
     $post = Post::find($id);
     check_user_acl_for($post);
     // ...
});

You may be able to move the ACL check to a middleware layer or “before” hook if the framework supports it, but there is always a certain amount of duplicate code you will either never be able to get rid of, or have to jump through hoops to get rid of (like adding more abstraction or re-checking the current URL, etc).

The Benefits of Shared Scope

Bullet uses a unique nested callback style that splits the URL by directory separator and only handles a single part of the URL at a time with it’s own callback. At first blush, this approach might seem like more work, but the key to how Bullet works is that nested closures – by definition – can use variables defined in the scope of their parent. This leads to some pretty powerful and profund capabilities that can only be done using the same nested closure style that Bullet uses.

Continue reading

Handling Exceptions in Gearman Tasks (Even Background Ones)

I recently had some issues with Gearman tasks throwing exceptions and killing the whole Gearman daemon. This made it nearly impossible to trace errors back to their origin, because the logged exception stack trace didn’t provide much useful information, because it just logged where it failed in Gearman – not the actual file and line of code that was doing the work. I dug into the code and started trying things like GearmanClient::setExceptionCallback and running the tasks, but since the tasks were being run with addTaskBackground instead of just addTask, the callbacks were never getting executed, and I still was not able to do anything to handle exceptions for the jobs that were being run (and they were still killing the Gearman daemon). Clearly, I was going to have to get a little more creative.

The only other place to add code that will catch exceptions for all jobs run is in the GearmanWorker::addFunction method. So I looked at the following one-liner for adding named job callbacks:

<?php
$worker->addFunction($name, $task);

And replaced it with a closure that uses a try/catch and then logs any exceptions to Exceptional so we can see the full stack trace and exact point of failure for any job – even background jobs:

<?php
$worker->addFunction($name, function() use($task) {
     try {
         $result = call_user_func_array($task, func_get_args());
     } catch(\Exception $e) {
         $result = GEARMAN_WORK_EXCEPTION;
         echo "Gearman: CAUGHT EXCEPTION: " . $e->getMessage();
         // Send exception to Exceptional so it can be logged with details
         Exceptional::handle_exception($e, FALSE);
     }

     return $result;
});

And it works beautifully. Now all the jobs are run, the Gearman daemon is never killed by a PHP process, and all the exceptions are logged with full granular details that makes it easy to troubleshoot and fix any errors.

Confoo 2012 Montreal

My second year speaking at Confoo was more fun than my first. This year I met a lot of new people and had a lot of interesting discussions, particularly in the CMS room and at lunch after my Stackbox CMS presentation. The whole philosophy and approach of Stackbox seemed to have stuck a chord with other people passionate about CMSes and a lot of discussion emerged about different CMS concepts and how to integrate them into the various CMSes around. The whole edit-on-page approach of Stackbox isn’t new, but it’s striking how much simpler it is for the end user than most existing CMSes that are used today – Drupal, Joomla, and WordPress included. I think  all CMSes should strive to enable on-page and in-place editing wherever possible – it really makes a huge difference in usability.

The second talk I gave at Confoo was  Hierarchical MVC (HMVC) – What, Why, and How – an architectural talk I have been wanting to give at several conferences for a little while now (but had until now not been accepted). The talk was very well received, and hopefully helped at least a few people though some of the tougher architectural decisions they might be facing in their own projects. The gist of the talk is that HMVC can help break up code into "widget" type blocks, and can go further down the path of fully separating concerns than more strict traditional MVC can. HMVC is all about code re-use across multiple places, like a comment module that is dispatched to anywhere you display comments across multiple types of content (blog posts, articles, pages, events, etc.). Traditional MVC forces you to use view partials, different layouts, duplicate code, or some other separate widget system to achieve the same level of flexibility you get from HMVC.

One of the best things I like about Confoo is the sheer diversity of the schedule. There were 10 tracks this year, with talks spanning across all types of technologies, markets, and languages, like PHP, Ruby, Python, .NET, Java, and JavaScript to Scaling, Startups, CMSes and Agile. Confoo is the most technologically diverse conference I have had the privilege of speaking at, and I benefit from that diversity every year by expanding my horizons a little bit. While I was there, I attended a talk on a Ruby framework called Renee by Joshua Hull. Even though most of my background is in PHP and I was presenting with PHP examples and projects for my talks, I use Ruby on Rails quite a bit for client work at my company  Brightbit and was working on a Rails API at the time, so I thought I’d check it out. I had been thinking about better REST frameworks beyond MVC for a little while, and the talk inspired me to try the same nested callback style with closures in PHP, and I started hacking together the very first (crude) implementation of Bullet on the plane ride back home the next day, just to see if it was possible. I had the basic concept working well in under an hour thanks to PHP 5.3’s awesome closures. That’s why I like and value the diversity at Confoo so much. You get to see things that are going on in other languages and expand your horizon a bit, then you can bring those benefits back to another language you work in, and benefit even more people. I’ve fleshed out Bullet a little more now and made it a proper project, but the details of that are for another blog post. For now, I leave you with the slide decks I presented at Confoo.

Slide Decks

Stackbox CMS Slides

Hierarchical MVC (HMVC) Slides

Nginx + PHP-FPM Blank Pages with PHAR Packages

Ran into this issue when setting up a new VPS for AutoRidge. This happens when using Nginx and PHP-FPM with PHP 5.3+ and the Suhosin patch when trying to run a PHP script using a PHAR package. From what I can gather, the Suhosin patch basically blocks PHP include/require functions from executing files ending with .phar, which results in a PHP segfault that leaves no trace of any error at all. This is what makes this error so frustratingly difficult to track down – there is no trace left in any logs about what is happening or that any PHP error even occurred at all.

The solution is to open your "suhosin.ini" file to ensure the Suhosin patch is allowing PHP to open and execute PHAR files.

Mine was at:

/etc/php5/conf.d/suhosin.ini

If your suhosin config file is not there and you don’t know where it is, run this:

locate suhosin.ini

Find the config key suhosin.executor.include.whitelist and add "phar" to it.

suhosin.executor.include.whitelist ="phar"

Then restart PHP-FPM and Nginx and you should be good to go!

/etc/init.d/php5-fpm restart /etc/init.d/nginx restart

Excessive Data Usage with iPhone 4S / iOS5

Got a new iPhone 4S or recently upgraded your iPhone 4 or 3GS to IOS5 and noticing unusually high cellular data usage? Are you close to exceeding your data usage limit when you never have before? Have you already exceeded it? You are not alone.

My wife recently traded in her old iPhone 3G for a shiny new iPhone 4S, and within 10 days had exceeded her 200MB AT&T data plan limit when she has never exceeded it before. So what gives? It’s a new phone, and Siri does transmit voice data back to Apple’s servers, but could that really have caused the usage? Continue reading