PHPStorm File Templates Includes Custom Variables
by chance on Feb.10, 2012, under development, php, phpstorm, tools
PHPStorm's File template settings are very versatile. They allow you to not only make a file template, but also allow you to parse out redundant elements (such as licensing) in the template by allowing an includes section. In your File Template, any undefined variable automatically elicits a prompt from PHPStorm.
PHPStorm allows you access to the includes section via the #parse directive. If you're want To have Custom variables to be filled in correctly via prompt, you will need to have the variable declared in the template.
Example
"chance license.php"
/**
* @package ${Package}
* @author Chance Garcia
* @copyright (C)Copyright ${YEAR} chancegarcia.com
*/
In the above includes example, I'm wanting to have a custom variable named Package. I can only cause PHPStorm to prompt for this value if I include the variable in my template. If I'm already using the variable in the template, then it will fill in when the includes file is parsed.
Example:
<?php #parse("chance license.php") class ${Package}_#if(${ExtraClassInfo} != "")${ExtraClassInfo}_#end${NAME} { }
In the above template, the ${Package} variable will be given a prompt since it is used in the template and an unknown variable and the parsed "chance license.php" include will be able to use that prompt value.
I am also using another variable to Prompt for extra class name information. Since PHPStorm uses Velocity Template Language (VTL), I am able to use the VTL conditional syntax to insert that information if it is entered and ignore it if it is not. This technique is useful in a situation where you want your include file to have a custom variable value but do not need to display this value in your template.
Example:
<?xml version="1.0"?> <!-- #if(${Package})#end #parse("chance license.php") -->
In the above example, we make PHPStorm prompt for the custom value needed for out parsed include file. This gives us our expected include file without printing our custom variable anywhere else in our template.
MAMP PRO, PECL, SSH2, and OSX CLI (AKA acronym madness)
by chance on Nov.08, 2011, under development, php, tutorial
Or madness in general. So as you may know, I've started using PHPStorm to work locally. I decided to dust off an old SSH2 wrapper I wrote and play with how PHPStorm uses PHPUnit and does refactoring. I knew that I would have to install the extension via PECL and was like, "Oh it can't be that bad to do it through MAMP PRO" and as you may have guessed, I sealed my own fate for a couple hours. One thing I can say is that, even though I use a convenient app like MAMP PRO to set up my local development environment, I'm glad my sysadmin-fu is up to snuff enough to fly without the conveniences because after this ordeal, I feel like I might as well have made my MAMP stack from scratch with all the hoops I jumped tonight. (I'm pretty sure that last sentence is also one of the worst run-on sentences ever).
The first thing you need to know is that the PECL command is located at the directory
/Applications/MAMP/bin/php/php5.3.6/bin
when I tried to run
sudo ./pecl install -a ssh2 channel://pecl.php.net/ssh2-0.11.3
from that directory, I got the error:
Notice: unserialize(): Error at offset 267 of 1133 bytes in Config.php on line 1050 PHP Notice: unserialize(): Error at offset 267 of 1133 bytes in /Applications/MAMP/bin/php/php5.3.6/lib/php/PEAR/Config.php on line 1050 ERROR: The default config file is not a valid config file or is corrupted.
I found out later that the error is most likely caused by the file
/Applications/MAMP/bin/php/php5.3.6/conf/pear.conf
So if you get an error, try doing
sudo mv /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf.bkp
If you're still getting the error, you can also the following:
- Install Xcode Tools from your Mac OS X DVD or download it from Apple Developer Connection
- Download the complete PHP source code from php.net into /Applications/MAMP/bin/php/php5.3.6/
- Create an include directory in the the php5.3.6 directory.
- Unzip/tar the php source archive and move it to/Applications/MAMP/bin/php/php5.3.6/include/
- Rename the php source directory to just php (so then it will look like /Applications/MAMP/bin/php/php5.3.6/include/php)
- In Terminal use the following commands to compile the extension:
cd /Applications/MAMP/bin/php/php5.3.6/include/php
./configure
That should allow you to run the PECL command however, you will probably get a complaint about not having libssh installed. So you will have to
- go download libssh2 (http://www.libssh2.org/). As much as I like git, I recommend downloading the latest release tarball instead of cloning the repo. For some reason, I got a version incompatibility error when trying to run the PECL install with the repo version and had to do all these steps over with the tarball release version.
- unzip the directory and cd to the upacked directory in the terminal.
- run: ./configure; make all install;
libssh should compile and place the output file in src/*.o - copy all headers from the include folder within the libssh2 directory to
/usr/local/include/ - copy all compiled files (*.o) from the src folder within the libssh2
directory to /usr/local/lib/
After you've successfully ran the the PECL install, it should tell you to Add the line
extension=ssh2.so
to the Dynamic Extensions area of your php.ini file. I believe both MAMP and MAMP PRO allow you to edit the necessary ini through File>Edit Template.
Lastly, restart apache. You should be able to see the SSH2 extension when you load a phpinfo() page.
Bonus round:
I'm not sure how I have PHP CLI working in OSX but if you've followed the steps above and try to verify the extension install with through the cli, it won't show up.
$ php -a
php> phpinfo();
This is because (if you're also using MAMP PRO) the binaries are different. The good news is that you can make things happy in 3 easy steps
- run `sudo ln -s /Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so /usr/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so` in the terminal.
- Edit the /etc/php.ini (copy /etc/php.ini.default if /etc/php.ini doesn't exist) like you did for the MAMP php.ini(s)
- profit.
Note: I still have to install PHPUnit into my current setup so that may or may not elicit another blog post.
Sources:
- http://smbjorklund.no/how-enable-pecl-uploadprogress-extention-mamp
- http://php.net/manual/en/ref.ssh2.php
- http://forum.mamp.info/viewtopic.php?t=13815
Related articles
- Using MySQL Workbench with MAMP (phpprotip.com)
Magento Extension GitIgnore Stub
by chance on Oct.24, 2011, under development, Magento, php
As you may or may not know, I've been working with Magento lately. In the past few months, one thing I've found that I needed is a stock .gitignore file for when making extensions. Haven't found one so I made one on github and will also post here for anyone else in need. I'm still relatively new at working with Magento and in someways git/github so please feel free to critique/streamline.
# replace {company} and {extension} placeholders with your information
# ignore phpstorm files
/.idea
# Ignore all not in app & skin
/*
!/app/
!/js/
# ignore all in js, except module files for this module
/js/*
!/js/{company}/
# Ignore all in app, except code & etc
# Ignore all in app/code except /local/{company}
/app/*
!/app/code/
!/app/etc/
!/app/design/
# Ignore all in app/code/local except {company}
/app/code/core
/app/code/community
/app/code/local/*
!app/code/local/{company}/
# use lines below if you have multiple company extensions
# and want to just commit a specific one
#/app/code/local/{company}/*
#!app/code/local/{company}/{extension}
# Ignore all of app/etc except our specific module files
/app/etc/*
!/app/etc/modules/
/app/etc/modules/*
!/app/etc/modules/{company}_{package}.xml
#Ignore all of app/design except for our files
/app/design/*
!app/design/frontend/
!app/design/adminhtml/
/app/design/frontend/*
!/app/design/frontend/base/
/app/design/frontend/base/default/etc/
/app/design/frontend/base/default/template/*
!/app/design/frontend/base/default/template/{extension}/
/app/design/frontend/base/default/layout/*
!/app/design/frontend/base/default/layout/{extension}/*
/app/design/adminhtml/*
!/app/design/adminhtml/default/
/app/design/adminhtml/default/find/
/app/design/adminhtml/default/default/etc/
/app/design/adminhtml/default/default/locale/
/app/design/adminhtml/default/default/template/*
!/app/design/adminhtml/default/default/template/{extension}/
/app/design/adminhtml/default/default/layout/*
!/app/design/adminhtml/default/default/layout/{extension}/*
Using MySQL Workbench with MAMP
by chance on Oct.07, 2011, under development, mysql, php, regex
Recently I've started working locally due to the PHPStorm IDE. In order to work locally, I invested in MAMP Pro to make use of their GUI interface for configuring vhosts. I also like to use MySQL Workbench for my database work, unfortunately these 2 items never seemed to work together for me before. After a brief google search I was able to find out how to connect to MAMP's MySQL install to do SQL Development, which is by done by choosing the local socket connection method and using the value /Applications/MAMP/tmp/mysql/mysql.sock for the connection.
After setting that up, I got to thinking, "I'm already using an IDE for code convenience and a GUI for vhost convenience, I should set up Server Administration with MySQL Workbench too!" So after some googling, I found an article to help me set up Server Administration with MAMP. Unfortunately the article only got things partially working and has no way to comment on it to have it corrected. So I'm going to outline the steps. Also I just noticed that the images with the article are updated correctly but it's hard to cut and paste an image of something someone has done so I'll just give you the value pairs
- Configuration File: /Applications/MAMP/tmp/mysql/my.cnf
- Start: /Applications/MAMP/bin/startMysql.sh -
- Stop: /Applications/MAMP/bin/stopMysql.sh -
- Check MySQL Status: ps -xa | grep "/Applications/MAMP/Library/bin/[m]ysqld"
expected exceptions annotations, mocked object calls, oh my.
by chance on Dec.24, 2010, under development, php, phpunit, unittesting
Note: I have tested this in PHPUnit 3.4.1 and haven't tried it out in 3.5.
Anyone who has worked with PHPUnit has most likely worked with expected exceptions and mock objects. The nice thing about working with expected exceptions is that we have access to a handy @expectedException annotation. I've gotten into the habit of using this for exceptions my fixtures should throw but also for when I'm using a mock object to verify a method call. So my tests usually expect foo_exception for fixture throws and when i'm testing method calls via a mock, they expect Exception. Therein lies my problem. Because all my custom class exceptions obviously extend the Exception class, I can get some false positives in testing.
equire_once 'Zend/Loader/Autoloader.php'; $loader = Zend_Loader_Autoloader::getInstance(); require_once('foo.php'); class tmpTest extends PHPUnit_Framework_Testcase { /** * @expectedException Exception */ public function testFooBar() { $foo=new foo(); $foo->bar(); } /** * @expectedException Exception */ public function testBarBaz() { $mock=$this->getMock('foo',array('baz')); $mock->expects($this->any()) ->method('baz') ->will($this->throwException(new Exception('baz'))); $mock->barbaz(); } }
class foo_exception extends Exception{} class foo { public function bar() { throw new foo_exception('bar'); } public function baz() { echo "bwah\n"; } public function barbaz() { $this->bar(); $this->baz(); } }
So here we have an expectation for Exception but if we look at the code, we see that the bar method throws a foo_exception and the testBarBaz test is trying to test for the baz call via a mock that throws an Exception. if we change the annotation to expect foo_exception, the test still passes. This leads me to believe the best way to isolate the behavior we wish to test is to not use annotation for these sorts of tests. Or if you want to use annotation, be sure to use a unique exception for the mock. This means, unfortunately for me, that I'll have to go back through all my tests and ensure there's no false positives.
Lesson learned: be careful using shortcuts (and don't stand in the fire).
On a side note, this part of PHPUnit is why those tests will behave that way. The behavior is completely my fault but I wanted to confirm it was behaving because of how it was verifying the expected exception.
fun with arrays and requirement chains
by chance on Dec.01, 2010, under development, php, web dev
Recently, I had to figure out if a given set of features contained all of their necessary requirements.
For better or worse, the table was modeled so that the feature table referenced itself so that it could create a parent/child requirement chain. For example:
| feature_id | requirement_id |
|---|---|
| 2 | null |
| 5 | 2 |
| 7 | null |
| 11 | 5 |
So my problem is to find out if a given set of requested features, make sure that the requirements are also present. This includes any requirements the requirement feature may have. In this example, 11 requires 5 which requires 2.
Let's take $featureRequest1=array(2,5,7,11) and $featureRequest2=array(7,5,11). If I were to run a look up of requirements, I would find that we have
$requirements=array(2,5).
I tried to use the php in_array function but it didn't work as I expected it to.
in_array($requirements,$featureRequest1); // i expect true var_dump(in_array($requirements,$featureRequest1)); // false in_array($requirements,$featureRequest2); // i expect false var_dump(in_array($requirements,$featureRequest2)); // false
I then realized what I need is for there to be a clear intersection between the requirements and the request.
$requirements==array_intersect($requirements,$featureRequest1); // expect true var_dump($requirements==array_intersect($requirements,$featureRequest1)); // true $requirements==array_intersect($requirements,$featureRequest2); // expect false var_dump($requirements==array_intersect($requirements,$featureRequest2)); // false
I should probably note that array_interest will preserve the array keys. To fix it, you can either flip the 2 arguments or pass the return array to array_values
Meet the PHP Dev Derby Team
by chance on Sep.10, 2010, under dev derby, development, php, the combine
I liked how the Dev Derby website did team leader profiles and decided to try to do something similar. No one provided any pictures but most did answer some questions I asked so without much further ado, meet the PHP Dev Derby Team.
The team consists of
- Dennis Rogers (@_drogers)
- Max Beatty (@maxbeatty)
- Matthew Haralovich (aka zon)
- Grant Simpson (@grantls)
- Ryan Dagey (@dageytech)
Update: added in Ryan Dagey's answers. And photo.
Call to Arms
by chance on Aug.20, 2010, under dev derby, php, the combine

- Image via Wikipedia
How much code can you generate in a day?
We are actively recruiting participants to take place in a developer event ("Dev Derby") that pits one language against others. It is a day-long programming challenge where teams of developers work to create an application serving a real-world need. Five teams will represent different programming languages—PHP, C#, Ruby, ColdFusion, and Java—to produce a demo application that will be released as open source software.
http://devderby.com/application/
Team Leaders for each language will review applications and select competitively balanced squads. There is no cost to enter, but spots on teams are limited. Winning teams can win prizes and all participants are eligible for discounts to other tech events taking place that week.
The Dev Derby will start and end on Saturday, September 11, 2010, in Bloomington, Indiana. It is part of The Combine (http://thecombine.org), the area’s first major technology conference, and at the start of the BFusion/BFlex conference (http://bflex.info). Dev Derby involves an intense six-hour coding session.
Each challenge submission will be judged by knowledge leaders and representatives of the non-profit organizations benefiting from this work. The criteria spans Design (features and UI choices made), Technical Efficiency (code and performance), Communication (documentation and presentation), and Practical Value (use, adoption, and maintenance).
Prizes will be awarded at the end of the day, following a panel discussion about the development process and the future of application programming.
Dev Derby is situated in The Combine along with other technology-related events, such as Tech Cocktail, Ignite Bloomington, and a variety of of other gatherings. It is hosted by the BFusion/BFlex conference, a two-day hands-on training event from the experts of Adobe Flex and ColdFusion. Dev Derby is inspired by our experience with Startup Weekend in 2008, but with a short day of coding and focused on a specific challenge.
Apply now: http://devderby.com/application/
Valuable Professional Reading
by chance on Aug.11, 2010, under dev derby, development, php, the combine, zend

- Image via Wikipedia
The team leaders for Dev Derby have been asked to list what we consider valuable professional reading. Our book selections are not limited to our respective Languages. I thought I would share my list with everyone.
The first 2 (GoF design patterns and Patterns of enterprise application architecture) really just need to be in every developer's library. The rest are a collections of books I've read and liked as well as recommendations from developers I like and respect. Keith Casey pointed me in the general direction of a lot of these books.I suspect that some of the Dev Derby people will end up contacting various publishers to solicit swag sometime soon.
In other news about Dev Derby, I think that an application for teams should be available to announce sometime soon. I do know we have a deadline for selection approaching and it's kinda hard to select without people to select from.
GoF design patterns:
* ISBN-10: 0201633612
* ISBN-13: 978-020163361
Patterns of enterprise application architecture:
* ISBN-10: 0321127420
* ISBN-13: 978-0321127426
(continue reading...)
Auth/ACL implementation strategies
by chance on Aug.09, 2010, under development, php, security, theory crafting, zend
I'm going to talk more about ACLs than Auth. Auth is simple, it's the ACL that will trip you up. Since both concepts are coupled together when you're making a login system, I feel it's appropriate to at least touch on Auth. What I want to cover is the ways we can create the ACL object to suit needs based on the scale of the project. I'm going to assume that readers have a passing familiarity with using the Auth and Acl objects and may have even implemented them into projects.
(continue reading...)
Looking for something?
Use the form below to search the site:
Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!
Archives
All entries, chronologically...










