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; ?>
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
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).
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.
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.
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.
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!