Tag Archives: PHP

Belated Zendcon 2011 Recap

Image representing Zend Technologies as depict...

Image via CrunchBase

I started writing a Zendcon recap post as soon as I got back. And then mis-timed a couple projects. Note to freelancers, make sure your new gig starts AFTER your old gig is over. Or you’ll be working an insane schedule. Hence the lateness….

First the good part. What made Zendcon special.

I listen to many podcasts while on the LA freeways (aka parking lots), read a lot of blog posts by the pros, and connect with the #ZF community on twitter. It was great to finally meet so many “online friends” in person.

Even if you’re not a Zend Framework or PHP Developer, I recommend following these folks, as you will gain something from each of them.

First of all, many thanks to Kevin Schroeder (joind.in) for inviting me to be on

The PHP logo displaying the Handel Gothic font.

Image via Wikipedia

the advisory board for Zendcon 2011. It was a wonderful experience.

Juozas Kaziukėnas proves that even if you’re circa ~7’9″ tall, you can be a better programmer than basketball player. Back when he was still trying to get accepted to speak at conferences, I could tell that this fellow had chops. Glad the last couple of years he’s been getting the recognition he deserves. His talks included sessions on Doctrine and Azure.

I wish I’d have managed to catch some sessions by Daniel Cousineau. Besides having a really neat beard, this is one smart feller.

Wayne's World

Wayne's World (Image via RottenTomatoes.com)

If there’s one event I will remember about Zendcon2011, other than Kevin wearing a cape for two days, it will be 4 of us, David Zulke, Juozas, Paul Jones and yours truly in David’s car, singing Bohemian Rhapsody like in Wayne’s World. Anyone (ie David) who gets REST right and yells at those getting it wrong in conference talks has my props.

Paul Jones is one cool Marine. He’ll kick your ass physically and in code. I think anyone coming from OmniTI must be really smart, because between him and Elizabeth Marie Smith, you can spend a day chatting about hardcore programming and not get bored.

Image representing OmniTI as depicted in Crunc...

Image via CrunchBase

I missed Paul’s talks unfortunately and only caught one of Elizabeth’s, but it was great. If you don’t know SPL, you should try to catch her SPL talk. Actually, I want to hear all of them when they’re released.

Hanging out with Dr. Keith Casey is always a blast. One of the most unique individuals you’ll come across. He has so many great stories. And we’re very much of the same mind about connecting people who would mesh well together.

I hung out a lot with @jsundquist and @jcarouth. Promising young talent. When you meet good kids like them, you have hope for the future.

English: Zend Framework logo. Português: Logot...

Image via Wikipedia

I really enjoyed meeting one of our #LAMySQL speakers in person. @billkarwin. He’s so humble for someone who’s done so much. This guy headed up Zend Framework V1.0, wrote the groundbreaking SQL Antipatterns book and has had quite a career so far. Now at Percona. We chatted quite a bit and I’m honored to call him a friend.

Bill and I also hung out with another gentleman and scholar, Bradley Holt. These two guys forgot more about Databases than most of us have ever learned.

English: Zeev Suraski, significant contributor...

Image via Wikipedia

I’m kind of losing steam in telling these stories, because there’s one name I want to cover at the end, and life kind of sucks, it’s hard to focus on the good when I’ve got his story in the back of my mind…so if I miss anyone please accept my apologies.

Shout outs to @DragonBe & DASPRiD. Enjoyed IHOP with you guys!

Also nice to meet fellow Canuck @afilina who manages to get the PHP community out to the Frozen Tundras of Canada with her mega conference organizing skills.

Andi Gutmans

Image via Wikipedia

Too bad @skoop wasn’t there the whole conference as I was hoping to chat with him more. However I was very pleased to chat with the names behind Zend, Zeev Suraski and Andi Gutmans.

One of my favorite talks, which was in the uncon came from @iliaa. He always has so much to say about performance. If you’ve never had to worry about performance, it might be hard to tell who’s good. But trust me, he is.

And I’m going to wrap up with the dynamic duo behind Zend Framework. Ralph Schindler and MWOP. Ralph is hilarious! And I learned some interesting things chatting about the history of PHP with Matthew.

Running a project like Zend Framework has its positive sides to be sure, but also its thankless side. The public out there can be pretty mean and demanding at times. Especially with stuff they get for free. These guys really handle the community with class.

Sorry if this post is too positive, but it’s one of the few things I love. Meeting wicked smart people, often they are nice too, and just learning. If you focus on the good people, you can gain so much from conferences…

Now last, but not least I will mention Jeroen Keppens. We sat together during the opening Keynote, live tweeted the conference, and every time the live demo showed top tweets, one of us was on the screen. It was real fun. And I really enjoyed hangin’ with him and chatting. Having lived in Belgium for awhile, it’s easy to connect with people on that side of the world.

And this brings me to something that bothers me a lot about twitter. The world gets smaller. Because I run meetups, I follow a lot of people on Twitter and many are not just strangers. A good chunk are community members.

And almost every day something really sad happens to someone. I’m still kind of shocked from when Jeroen tweeted that his wife passed away from an accident. The original version of this post had more details, but now I see that his tweets are protected, so I will respect his privacy. I wish there was something we could do, but what can you do?

Jeroen is such a good guy. Why does he have to go through this? I don’t know. A week earlier, someone from the cloud community announced that his 2 year old daughter pulled a pot of boiling water onto herself and went to the hospital. Thank God she seems OK.

A couple of people have gotten cancer. Another Twitter friend had a relative commit suicide.

It’s hard to tweet as though it’s a normal day when someone you are fond of one way or another are going through such a hard time. Like a Doctor you have to get “used” to this because of the frequency. But it is hard and the downside of these tools that bring the world closer.

Enhanced by Zemanta

I wrote a new ACL module for Lithium: li3_simple_acl

Lithium hydride

This Image via Wikipedia, is a test of focus. Is it related? Perhaps not, but it looks better than a naked post.

I tried to work with the existing Lithium ACL module called li3_access, as several folks have done some really good work there. It has a lot of flexibility, including the ability to let you create your own Adapters. Which I tried at first.

The reason I chose to roll my own, at 4am no less, was that I was looking for something simple, that still lets you protect the resource.

A principle in making a good ACL layer is that you don’t want a bunch of if statements in your code. If($user == ‘admin’){ //do ‘A’ }. Because if you change your ACL rules, you have to find every instance of that and change it. Breaking your code.

If you thought you improved that situation by moving to an ACL but find yourself writing code like: if($acl->isAdmin($user, $request, $resource)){ // do something }, you’ll realize that you just abstracted away the same exact code, and did not solve your problem.

In my opinion, you should be calling your ACL with code that looks something like this: if($acl->isAllowed($user, $resource) { // do something }.

The $user array/object should contain the info needed to validate the $resource/$perms array. Then all you have to think about is how to make sure you can pass the needed info from your resource.

Image representing GitHub as depicted in Crunc...

Image via CrunchBase. Did you know that research scientists in Freedonia have proven that programmers improve their standard of living by opening up a github account?

For example, if you have a database row with a forum post and want to provide the author of that post permission, the user array has the userid and the row has the author’s userid. Simple to match.

Naturally, it still needs a bit more work. Like the ability to Deny users in the event that certain conditions match. Or allow some IP ADDRESSES access. Basically, it needs a little more flexibility. So please take a look at li3_simple_acl on my github account and give me feedback. Thanks.

Enhanced by Zemanta

Using ‘VC’ portion of ‘MVC’ in Zend Framework vs Lithium

Although I’ve been playing with Lithium for a few weeks, I haven’t needed to touch the view at all and barely looked at the Controller, so I didn’t really pay attention to this difference until just now…

In Zend, you set the vars in the controller for the view thusly.

Whereas in Lithium, you do it thusly.

Then in the view, to access those vars, here’s how Zend Framework does it.

With Lithium, if you use the same notation as the PHP short open tag, it will automatically escape the output. Which is great, although keep in mind that when you don’t want an htmlspecialchars escape, be sure to escape the var yourself as appropriate. Here’s the Lithium example.

I’m not sure how Zend Framework 2.0 will work yet. Been too busy to keep up to date.

I think the Lithium way is quite elegant. It’s a lot easier to explain to front end people who are mostly but not 100% in the view. There’s a variable, it’s the same in the controller and the view and you just access it.

It’s always good to check out different frameworks and learn what you can from each of them.

Aside: I love how people in the PHP community are for the most part really good about listening to the good and less good with an open mind and not making it personal. Matthew Weierophinney’s recent article about Filters and Nate Abele’s response is a classic example of the class acts in our community. I’ve learned much from all of you.

Setting up first Lithium project

I’m now starting my first Lithium project. So I thought I’d jot down notes while playing with it. Just random stuff. I’m not trying to accomplish anything in particular with this post. It might be interesting to you if you’re planning to try it out. Especially, if like me, you’re used to Zend Framework.

So far the docs are a little better than I expected, as we aren’t yet at release 1.0. But we should expect some speed bumps.

The first thing I noticed is the suggestion to install the whole framework under the docroot. To see the homepage you go to http://localhost/lithium . I’m sure that will change though so that your application code doesn’t live under your docroot. If you’re doing anything other than development, obviously you can and should fix this in production.

Something I like about the Zend Framework coding standards is the lack of an end php tag ?>. Having been bitten by that spacing after the endtag “bug” before, it now looks weird to me to see the end tag closing up a file. Hopefully #LI3 will follow Zend’s lead on that.

If you’d like to peruse the #LI3 coding standards, they are here. Overall I really like them.

Not sure what’s wrong with casting using shortcuts. Is it really ambiguous to say (int) $var rather than (integer) $var? (not a big deal in the scheme of things though)

Side note, I wish the PHP community would come to a consensus with the underscore thing before _protected methods.

I got used to it with Zend Framework. Now there’s a poll about removing it or not and last I looked it seemed that it was going away.

I just hope everyone keeps it…or removes it…but we make a community decision so that we don’t have another of these tabs vs spaces debates for the next 10 years. Which leads me to my next point. Kudos to #LI3 for choosing correctly with tabs ;) I thought I was in the minority about that. Certainly seems that way in the Open Source community.

One more thing. Just installed the Lithium Documentation Tool. This is so cool. Really excited to learn more.

I’m going to end this post with a little list of #LI3 RSS feeds that I started to follow. It may be updated periodically and do let me know if you know of any others:

http://nitschinger.at/

http://dev.lithify.me/lithium/wiki/blog.rss

How to integrate HtmlPurifier with Zend Framework

It looks like I’m inadvertently starting a series on how to integrate some external libraries with Zend Framework. So be it. Next on the list is HTMLPurifier.

Like with the Amazon AWS SDK, I’m just starting the process as I write this. I have no idea how hard it will be. So let’s start with a Unit Test again and try to make it pass.

The Unit test is located at /project/tests/application/models/PurifyTest.php and contains this.

Obviously, the test will fail at this point

phpunit –configuration phpunit_local.xml –group purify

So let’s do a couple things. First, download the latest version (my link might be stale by the time you read this. So just find the latest release).

Next, if you set it up the same way I do, you’ll have a share folder in your filesystem, where you’ll put the htmlpurifier-4.2.0 folder. Then in your project/library/ folder, you’ll symlink this folder: Htmlpurifier to the location of that directory. Now if you upgrade to a later version of the purifier, you just change the symlink. Then if there’s a problem, you downgrade in two seconds…

Now open the INSTALL file in purifier. What’s this?

These optional extensions can enhance the capabilities of HTML Purifier:

* iconv : Converts text to and from non-UTF-8 encodings
* bcmath : Used for unit conversion and imagecrash protection
* tidy : Used for pretty-printing HTML

OK, well that would be nice. As I recall, tidy these days comes with PHP so if you have a recent version, you should be OK. But just in case, I’m going to fire up phpinfo.

Whew. I’ve got all three, so I’m good to go. If you don’t, then I’d suggest you install it. If you don’t know how, no better time to learn than now. Don’t be scared. It’s a breeze after the first time and you will have learned a very valuable skill that you can use in project after project.

OK let’s continue.

Having read an article or two from Juozas and Paddy, I suspect we won’t need a model or anything, but let’s not get bogged down in details. We just want to see if we can talk to HTMLPurifier, not architect an application.

So without further ado, let’s create a model called Purify.php (in the /project/applications/models/Purify.php file). Here are the contents.

Running the Unit test shows an error…ah I pointed to the wrong location. This is the correct include file:

require_once APPLICATION_PATH . ‘/../library/Htmlpurifier/library/HTMLPurifier.auto.php';

OK, now there’s no error on the Unit Test, but we still need to make sure we can purify something.

In my case I’ve got APC opcode cache installed, so I’m going to add this include as well:

require_once APPLICATION_PATH . ‘/../library/Htmlpurifier/library/HTMLPurifier.includes.php';

Then I’m going to create some evil html, and try to purify it. Here’s the new code:

Now let’s run the unit test:

phpunit –configuration phpunit_local.xml –group purify
OK (1 test, 1 assertion)

So it looks like we’re good. However, I’m still not satisfied. There are small differences between running PHPUnit and running code on the webserver. What concerns me is that HTMLPurifier has an autoloader and I want to be sure it won’t clash with Zend Framework’s autoloader.

So this will be easy to test. It’s very similar to the PHP Unit test. We’ll just create a test controller…skip the view and just see if it errors out or not. Here’s the code.

And Bingo! Job done. Easier than I expected it. But I still hope someone finds it helpful.

P.S. From past experience, I want to add a comment. Before you nitpick about details like requiring a file in a constructor or other practices, don’t forget it’s just an attempt to prove we have no errors. I’m not trying to paint the Mona Lisa here.

How to integrate Amazon AWS PHP SDK with Zend Framework

I was really excited when I heard that Amazon released the AWS PHP SDK. They hired the developer behind CloudFusion, Ryan Parman.

Some things lend themselves well to a library, because they aren’t that fast-moving. Zend Framework’s services are extremely useful.

However, if you’ve ever attended a talk by one of the AWS devs, they inevitably reach a slide where they show you the list of products they’ve released and when. The pace is rather impressive.

Even Zend, with a huge list of contributors, will have a tough time keeping up. As Till says, this is a situation where you want Amazon to support the AWS service code.

Unfortunately, you can’t just drop the SDK into your library and integrate it with Zend Framework. I haven’t fixed all the details yet, but my goal as I write this is to get a PHP Unit Test to pass.

If you don’t plan to use this for awhile, just wait, as Ryan is planning to work on integrating with Zend.

This is an early release of the SDK and there are a couple of things that bothered me a bit, but I’m sure they’ll fix it up. To give you an idea, I’ll just quote Harold on the blog’s announcement page:

I have to be frank, there is lots of room for improvement in this SDK. Where are the unit tests? Also, what’s up with the file naming convention, and having multiple classes per file? This prevents us from using class autoloading. I’m not sure what format the docblock comments are in, but they sure aren’t standard PHPDoc, and therefore do not integrate with NetBeans/Zend Studio/etc. Also, the CFUtilities class could at least contain static methods, eliminating the need to instantiate it.

OK, let’s begin. Download the SDK here. Or use Pear. Assuming you’re using the recommended Zend Framework project directory structure, create a directory called Aws under your library folder. Drop the files from sdk-1.10 in there.

I haven’t totally figured out how I’m going to be using the SDK yet, so for now I created a Model called Model_Aws. Unfortunately, the SDK wants you to modify their sample configuration file and rename it config.inc.php.

As you know, in Zend Framework we have a config file that allows us to offer up different values depending on environment. So we aren’t too interested in using another, less flexible, config file.

Fortunately, you can pass the config data into the constructor. We want everything of value from the SDK’s config file to be in our application.ini file.

So what I did was, where the SDK has “define(‘AWS_KEY’, ”);”, in application.ini I put “aws.key = my_key_here”. Same concept for all the other constants, with the exception of “define(‘AWS_ENABLE_EXTENSIONS’, ‘false’);” which I’ve left in the SDK config file. Comment out or delete the constants from the SDK config.

If I just lost you, that’s because I’m assuming you’ll put some effort into figuring out how to find your keys. This is not a beginner’s tutorial or for people unfamiliar with Amazon AWS… OK, where was I?

Before I show you where my Model_Aws class stands for now, I want to mention that for convenience I put my config in the Registry. So this method is in my Bootstrap.php.

I haven’t been able to get beyond including the sdk file, because of errors, so my Model_Aws class (filename /path/to/models/Aws.php) has only this in it. We’ll add more as soon as I can run a Unit Test without error.

My unit test is in /path/to/tests/application/models/AwsTest.php. Here’s the contents. OK, on the command line I run:
phpunit –configuration phpunit_local.xml –group aws

and Woaw! I get an html file getting spit out onto the console. Not good.

So open your phpunit.xml file…you should have an exclude section. In there add this in order to exclude /path/to/library/Aws . This way PHPUnit won’t go through the files in there to generate the code coverage report.

Now try. And…Bingo! Test passes.

OK let’s figure out something useful to do now. How about we just take the S3 sample that the SDK comes with, modify it a touch and just create a unique bucket and upload some files…and count that we have as many files as we created.

Now this isn’t necessarily the most useful unit test ever, but my whole purpose was just to prove that I got the SDK playing nicely and my unit tests now pass. Here is the final version of the Model_Aws and AwsTest.php.

Got any questions? Post a comment below or ping me at http://twitter.com/joedevon.

Running PHPUnit + Zend Framework in different environments

One thing that I haven’t seen any of the tutorials cover is how to handle running PHPUnit in different environments. Most people are just setting a new APPLICATION_ENV called ‘testing’, put a section in application.ini called ‘testing’ as well, and leave it at that….

What I’ve found, is that I’m using various things that will change in different environments.

For example, on local I just create a shortcut domain in my hosts file so I can easily play. So if I’m building a site for someverylongexample.com, I just make x.com my localhost. Which works great for me. See, I’m lazy.

Then some clients like to have a different domain for staging so they can point to different databases, or whatever reason.

And then you may just want to run a few tests with an environment of ‘production’ or ‘development’. For example, I only log to firebug on local or dev.

Frustrated, I just modified the usual settings you see.

Before going any further, make sure you check out a basic tutorial on Zend Framework and PHPUnit. For example:
http://weierophinney.net/matthew/archives/190-Setting-up-your-Zend_Test-test-suites.html
or
http://www.zendcasts.com/unit-testing-with-the-zend-framework-with-zend_test-and-phpunit/2009/06/

Done?
Good.

So here’s how I modified the files to cover these requirements.

1. instead of running phpunit off of phpunit.xml, I renamed that file phpunit_local.xml.

So on the command line, you’ll run this:
phpunit –configuration phpunit_local.xml

The contents of that file are (I’ve had it w/ WordPress messing up my formatting. Why do they have a CODE tag if it doesn’t handle much of anything very well? I’m going to put this on pastie.org and pastebin.com & hopefully one of them will survive as long as this post stays relevant):

Pastie

or

Pastebin

2. You probably noticed that instead of pointing to the bootstrap.php file, I renamed it bootstrap_local.php.

Here are the contents:

Pastie

OR

Pastebin

3. I wanted to be able to override the APPLICATION_ENV to test production/dev/local from time to time. So I modified the ControllerTestCase.php as follows:

Pastie

OR

Pastebin

Now you can test whatever environment you want by just overriding _setAppEnv().

This way, when you switch to prod, you’ll have a robust test to run that things work in the new environment. This happens to be one of the most important aspects of Unit Tests that no one seems to speak about. You usually have a slightly different environment and it can break things. If you’re only running your unit tests on local, you’re really missing one of the key benefits of all this effort.

If you think I’m way off, or have ideas on improving this code, leave a comment or ping me on twitter.

P.S. I just took a look at Matthew’s post on Zend_Test, which I had just found through a Google search, and noticed he uses a TestHelper.php. You can certainly do something very similar with the TestHelper…which is analogous to the bootstrap.php file I was using.

2nd Update: Forgot to mention, obviously you can’t redefine constants, so you may just have a subset you want to test. I’m assuming that you’re organizing things with the wonderful @group functions of PHPUnit and that you run this intelligently.