Archive for the ‘code’ Category

Validating URLs with Zend Framework

with one comment

It’s pretty common to want to validate a URL when processing a form. The Zend Framework has a lot of validators that can be used out of the box with Zend_Form. Unfortunately, there is no Zend_Validate_Uri. Upon closer inspection, you will find Zend_Uri, which indeed can be used to check for a valid URL; however, it does not implement the Zend_Validate_Interface and cannot be used as a drop-in form element validator. Fortunately, it is pretty easy to come up with something that can be dropped in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Url_Validator extends Zend_Validate_Abstract
{
    const INVALID_URL = 'invalidUrl';
 
    protected $_messageTemplates = array(
        self::INVALID_URL   => "'%value%' is not a valid URL.",
    );
 
    public function isValid($value)
    {
        $valueString = (string) $value;
        $this->_setValue($valueString);
 
        if (!Zend_Uri::check($value)) {
            $this->_error(self::INVALID_URL);
            return false;
        }
        return true;
    }
}
 
// When creating the form:
$website = $form->createElement('text', 'website');
$website->addValidator(new Url_Validator);

This reminds me of a senior coder I used to work under who constantly yelled at us to “Code to the interface!”. ;)

Note: I originally posted this as a response to a thread here.

Written by steve

February 9th, 2009 at 9:10 pm

Posted in PHP, code, zend framework

Monitoring SVN commits with Twitter

without comments

I originally wrote this on my employer’s blog, shift+control. I’m cross posting here because I don’t blog enough ;)


Like a lot of developers, we use SVN on a day to day basis. I can’t imagine working without it. We’ve been using it for over a year now. Svnlook on revision 1 give me this:

[steve@76design ~]$ svnlook info /svn -r 1
steve
2007-08-23 18:23:03 -0400 (Thu, 23 Aug 2007)
23
Created folder remotely

And svnlook youngest gives me this:

[steve@76design ~]$ svnlook youngest /svn
6567

In a year we have over 6500 commits and many projects contained in that (now not so little) repository. Given that there are so many commits going on, we thought it would be useful to have a feed of commits happening in real time. If we had that, we could get a good feel for the activity in the office over the day. One obvious choice for a feed is RSS, and we have that too, but we thought it could be fun to have a twitter stream of our commits.

I did some quick googling and came across a Google code project called twitvn. Unfortunately, due to hosting restrictions, I was unable to get it installed on our svn server. So, like any developer would do, I wrote my own ;)

SVN provides you with some interesting ways to interact with it pre and post commit. I wanted to hook in on post-commit and fire off a twitter message with some details about the commit. Turns out it’s fairly simple to do.

After every commit, if a script is available at [svn path]/hooks/post-commit, SVN will run that script. Two arguments are provided, the svn path and the revision number of the commit. Using these two pieces of info, you can then pull out whatever details you want about that particular commit and choose to do what you want with it. Knowing that, here is the approach I took:

  1. Get the particulars about the commit using ’svnlook info’
  2. Get the modified files using ’svnlook changed’
  3. Determine the author and the project that was being committed
  4. Create a twitter message and send it using Curl

I decided to write it using PHP, mostly because it’s what I know and I wanted to get it done quickly. In order to determine the project, I assumed that the top level folder of the first modified file is the project (this will depend on your repository layout so your mileage may vary here).

The final script is called tweeter.php, grab it here.

In order to run it, drop your twitter account details into CMD_CURL_TWITTER and add this line to your post-commit file:

[path-to-php5-cli] [path-to-tweeter.php] “${REPOS}” “${REV}”

Let me know if you get some use out of it!

Written by steve

December 11th, 2008 at 7:18 pm