Chris Sedlmayr Real World Development

30Jun/100

Textmate tip: Automatic PHP spl_autoload class name from file path

Create a new textmate command, and set the output to 'Insert as snippet'
Paste in the following.

#!/usr/bin/php
<?php
$path = $_ENV['TM_FILEPATH'];
$path = trim($path, '/');
$path = trim($path, '.php');
$parts = explode('/', $path);
$lastPart = end($parts);
echo 'class ';
foreach ($parts as $id => $part) {
    // textmate placeholders start at 1
    $id = $id+1;
    if ($lastPart == $part) {
        echo '${'.$id.':'.$part.'}';
    } else {
        echo '${'.$id.':'.$part.'_}';
    }
}
?>

And bind to a tab trigger.
When you execute from within a file it performs the following.

Retrieves the full path of the file you are in.
Removes any forward slashes from the beginning and end of the path.
Removes the .php extension from the end of the file.
Explodes each part of the path, seperated by a slash (/) into an array.
Gets the pointer for the last item in the array.
echo's 'class'
Then iterates through the array, creating a textmate snippet placeholder for each one, all of which have the required underscore (_) apart from the last one.

So for a file that is in the path /The/Full/Path/Filename.php
We end up with the resulting snippet of
${1:The_}${2:Full_}${3:Path_}${4:Filename}

Which then enables you to tab through them and remove those from the start that you don't need.
You could go a step further and only spit out the last 3 parts as that is generally how many are needed for a class name. But it really depends on your app structure so I've left it as is for now.

Check out the image for the options.

Update:
I've made a version that gives you just the last 3 parts of the path as mentioned above. I have both running on the 'class' tab command so can choose between them as required.

#!/usr/bin/php
<?php
$path = $_ENV['TM_FILEPATH'];
$path = trim($path, '/');
$path = trim($path, '.php');
$parts = explode('/', $path);
echo 'class ';
// do this 3 times
for($i = 1; $i <= 3; $i++) {
  $varName = 'part_'.$i;
  $part = array_pop($parts);
  if ($i == 1) {
    $$varName = '${'.$i.':'.$part.'}';
  } else {
    $$varName = '${'.$i.':'.$part.'_}';
  }
}
// pass the snippet to the file
echo $part_3.$part_2.$part_1;
?>
24Jun/100

Slides from Symfony Live 2010

Just going to post links to all the slides I can find from #sflive2010 yesterday.

Fabien Potencier - News of the Symfony2 World

Fabien Potencier - Unit and Functional Testing with Symfony2

Fabien Potencier - Caching on the Edge with Symfony2

Francois Zaninotto - Symfony2 meets propel 1.5

Jonathan Wage - Symfony2 and Doctrine2 Integration

Bernhard Schussek - The new form framework

They are also all embedded on the new blog post on the official symfony site

15Apr/100

Git pull and git merge between developers

I use git locally, but the main upstream repo is an SVN one, there are also a couple of other devs that also use git locally.
We are using git svn as the bridge between these 2.

If we want to share code or update each other it was generally the case that we would have to commit everything back to svn (git svn dcommit), then the others would have to rebase. It felt like I was abusing git and not making the most of it.

Enter git pull

Git pull allows you to grab code from another git repo and store it in your local repo.

Git pull

I'll presume you already have a git repo locally, and that there is another git repo somewhere you have access to.

First of all we add the remote location.

git remote add mp git.user@mpdevbox:/www/cm/

Let me break that command down

git remote add = The git command
mp = A friendly name you use for this remote location
git.user@mpdevbox = the remote user and the remote location
:/www/cm = The path to the remote git repo, (note the colon)

Having completed that command you now have a reference to a remote git repo, If you want to pull the code down from it, it's simple (you may want to create and checkout a branch before you do so, depending on your needs. I won't so will just pull)

git pull mp master
Where master is the name of your local branch you want to pull into.

This will pull down the code from the remote branch and merge it into your branch, if that all goes OK it will then perform a commit, if you don't wan the commit then just add --no-commit to the end of the pull command

If there are conflicts that git fails to resolve automatically then you will need to resolve them manually.
This can be easily done using mergetool
just run

git mergetool

and follow the instructions, on Mac this will open a merge/diff client which lets you easily choose from your local and the pulled down repo. Once done you can commit if you want, there is even a pre-filled commit message for you about the merge.

It's as simple as that really, this approach also makes it much easier to develop on multiple machines, without having to get rid of SVN as your main repo (which depending on circumstances, requirements, or the people that make those decisions, may not be an easy sell).

6Apr/100

Git manuals, docs and handy links

As i am a recent git convert I find myself reading a lot of guides etc on the best way to use git and how my workflow can change now i'm using a DVCS.

This is an attempt to collate my more useful findings, partly for myself, but it may prove useful for you too.

Git Books

http://book.git-scm.com/index.html

http://progit.org/book/

Git-SVN Resources

http://orestis.gr/blog/2008/08/23/git-svn-tutorial/

Full Git online manual

http://www.kernel.org/pub/software/scm/git/docs/v1.7.0.4/git.html

Combining multiple git commits into 1 SVN commit

In some cases I use git as a local repo, but the main repo upstream is SVN. This sample enables me to combine multiple local git commits into a single commit back to SVN.
This was taken from http://stackoverflow.com/questions/1408381/combine-local-git-commits-into-one-commit-with-git-svn.

git tag local # create a temporary tag
git reset --hard trunk
git merge --squash local
git commit # write your single commit message here
git svn dcommit
git tag -d local # delete the temporary tag named local</code>

Delicious

I also tend to boo0kmark a lot of stuff over at Delicious so you can check out my git stuff there too. Items tagged 'Git' at my Delicious

16Mar/101

Renaming multiple files by rule

I recently had the need to rename a bunch of files according to a simple rule.
In the directory, any files that had a prefix of "Gee_" should have the prefix changed to "Kif_"
After a little digging I found a simple but quite tasty solution.
Simply run this at your cli.

 
for f in Gee_*; do mv "$f" "Kif_${f#Gee_}"; done

Job done. It finds files matching the pattern supplied, then loops through renaming them appropriately.

It's worth noting this isn't recursive so will only complete for files in your current directory.

Filed under: sysad 1 Comment
16Mar/100

The use of 301 rewrite

If you are hosting a domain (or multiple domains) using VirtualHost entries, it is bad practice to use individual entries for the same domain just to cover the inclusion or exclusion of the www in the address, amongst other things it is bad for SEO, but of course you still want visitors to be able to go to your site via the 2 starting addresses http://sedlmayr.co.uk and http://www.sedlmayr.co.uk

For example you should not have an entry for www.sedlmayr.co.uk and another for sedlmayr.co.uk
There are a few ways you can do this but have found this particular one to be effective.
Whatever url you go to; http://sedlmayr.co.uk or http://www.sedlmayr.co.uk a redirect happens on the http://sedlmayr.co.uk that sends you to http://www.sedlmayr.co.uk.

But what about people that go to specific pages such as http://sedlmayr.co.uk/services

We use a RewriteRule so that whatever is after the domain itself (in this case services)(When using http://sedlmayr.co.uk) http://sedlmayr.co.uk/services is shown as http://www.sedlmayr.co.uk/services
It is very simple to achieve this.

Within you VirtualHost entry you need to specify a ServerAlias. So I would have
ServerName www.sedlmayr.co.uk
ServerAlias sedlmayr.co.uk
And then within your projects .htaccess file you would put

RewriteCond %{HTTP_HOST} ^sedlmayr\.co.uk
RewriteRule ^(.*)$ http://www.sedlmayr.co.uk/$1 [R=301,L]

Which needs to go at the top.
That's it!
Hope this is helpful to you at some point.

16Mar/100

Imagemagick sfThumbnail Plugin Fix for Scaling under Symfony

I've been using the sfThumbnail Plugin in symfony for image resizing when users upload. When I asked the plugin to resize an image with scaling there was a problem and the image came out skewed and distorted. After a little research I found the following fix.

Once you have installed the plugin, edit the sfImageMagickAdapter.class.php in the plugin dir.

Change lines 223-237:

    $width  = $this->sourceWidth;
    $height = $this->sourceHeight;
    $x = $y = 0;
    switch (@$this->options['method']) {
      case "shave_all":
        if ($width > $height)
        {
          $x = ceil(($width - $height) / 2 );
          $width = $height;
        }
        elseif ($height > $width)
        {
          $y = ceil(($height - $width) / 2);
          $height = $width;
        }

to:

    $width  = $this->sourceWidth;
    $height = $this->sourceHeight;
    $mWidth = $this->maxWidth;
    $mHeight = $this->maxHeight;
    $x = $y = 0;
    switch (@$this->options['method']) {
      case "shave_all":
        if ($width > $height)
        {
          $x = ceil(($width - ($height*$mWidth)/$mHeight) / 2 );
          $width = $height;
        }
        elseif ($height > $width)
        {
          $y = ceil(($height - $width*($mHeight/$mWidth)) / 2);
          $height = $width;
        }

It's a straight forward edit so you should be able to see why this simple change has the desired effect, and your images will now behave nicely when scaling.

16Mar/100

HOWTO: collapse the web debug toolbar in symfony by default

To automatically collapse, hide, shrink (or whatever you want to call it) the web debug toolbar to just the SF and close buttons; add this line
window.onload=sfWebDebugToggleMenu;

to the end of this file ...\symfony\web\sf\sf_web_debug\js\main.js

This handy little tip courtesy of IsRobot.

Tagged as: , , No Comments
16Mar/102

Using helpers within an action in symfony

If you ever need to use a helper outside a template, you can still load a helper group from anywhere by calling

sfLoader::loadHelpers($helpers)

where $helpers is a helper group name or an array of helper group names.
For instance, if you want to use simple_format_text() in an action, you need to call

sfLoader::loadHelpers('Text');

first.

I know this is covered in the symfony docs but it's a fairly handy thing to know so thought I would make it easier to find.

Tagged as: , , 2 Comments
16Mar/101

Zebra Striping with PHP The Easy Way

Using one of the built in Math functions of PHP we can easily create zebra striped lists.
Let's say we have an array of entries stored within the $comments variable.
All we have to do with this is to use the fmod function and assign a css class that alternates per entry.

<ul>
  <?php foreach ($comments as $int => $comment): ?>
    <li class="<?php echo fmod($int, 2) ? 'even' : 'odd' ?>">
  <?php endforeach; ?>
</ul>

Then just give the even and odd classes different colours to suit your layout.

li.even {
  background-color:#FFFFFF;
  color:#000000;
}
 
li.odd {
  background-color:#000000;
  color:#FFFFFF;
}

And you're done!