symfony helpers / sfSymfony-tmbundle
Complete symfony bundle
Requirements
The only requirement for sfSymfony is sfPHP bundle. You must install sfPHP bundle, because without it many snippets in sfSymfony simply will not work.
Features
- symfony 1.4 ready;
- almost full symfony coverage (>350 snippets);
- class expander;
- symfony help getter for classes/configs;
- contextual snippets;
- contextual singleton getters;
- complete pack of getters/setters for just everything;
- full pack of snippets for every helper;
- full pack of form snippets;
- full snippets coverage of Unit & Functional tests;
- symfony CLI GUI with autocompletion.
Class expander
sfSymfony has many magical snippets & commands, that do most of work for you. Class expander (cla) is one of them. What it do is create proper class snippet by trying to guess it with filename. For example:
cla in freshy actions.class.php, called under [?php $0 will create:
/*
* This file is part of the .
* (c) 2010 TM_ORGANIZATION_NAME
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* mainActions does things.
*
* @package
* @subpackage actions
* @author TM_ORGANIZATION_NAME
* @version 1.0.0
*/
class mainActions extends sfActions
{
}
In mySuperAction.class.php calling cla under [?php will insert:
/*
* This file is part of the .
* (c) 2010 TM_ORGANIZATION_NAME
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* myAction does things.
*
* @package
* @subpackage actions
* @author TM_ORGANIZATION_NAME
* @version 1.0.0
*/
class myAction extends sfAction
{
}
In myForm.class.php:
/*
* This file is part of the .
* (c) 2010 TM_ORGANIZATION_NAME
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* myForm does things.
*
* @package
* @subpackage forms
* @author TM_ORGANIZATION_NAME
* @version 1.0.0
*/
class myForm extends sfForm
{
}
It will also work for all:
- components/component files;
- form filter files;
- filter files;
- configuration files;
- widget files.
Contextual help
sfSymfony comes bundled with 2 great commands, that runs by ⌃⌥H.
- in *.yml config files, generated with symfony 1.4, this command will opens associated documentation web page;
- in *.class.php files this command will opens symfony API documentation for parent class of current.
Contextual snippets
Many of sfSymfony snippets is contextual. This means, that bundle guesses context in which you call snippet & same snippet in different contexts will inserts logically identical, but semantically different code. For example:
exein actions class will insertpublic function execute${1/(.)(.*)/\u$1\E$2/}(sfWebRequest \$request)...snippet;- same
exein single action class will insertpublic function execute(sfWebRequest \$request)... exein components class:public function execute${1/(.)(.*)/\u$1\E$2/}(\$request)...;- and
exein single component:public function execute(\$request)....
You don't need to remember thousands of different snippet names such as exe, cexe or else. Bundle will guess the right syntax for you with only one simple exe!
Singleton getters
The main aim of any bundle is to simplify routine tasks. Most routine task in symfony is work with singleton object & its childs. I've introduced in sfSymfony contextual snippets for near every symfony core object.
ctx- current sfContext instance;ctrl- current sfController instance;rq- current sfRequest instance;rp- current sfResponse instance;mail- current sfMailer instance;con- current database connection;lgr- current sfLogger instance;usr- current sfUser instance;i18n- current I18N instance.
Contexts of sfPHP make them context dependent. It means, that in different contexts this snippets will produce different code to get necessary object. For example:
usrin actions or components class will produce\$this->getUser()$0;- in filter
usrwill insert\$this->getContext()->getUser()$0; - in other symfony class
usrwill producesfContext::getInstance()->getUser(); - and in template
usrwill insert\$sf_user$0.
Again, you don't need to remember thousands of different snippets. You need to just remember 9, sfSymfony will do the rest for you.
Getters/Setters pack
We now have snippets to get needed objects. Most of the time, we don't need objects itself, but want to run an action on it. For this needs sfSymfony has full stack of Getters/Setters snippets. Getter/Setter snippets is simple snippets, that follows 6 simple rules:
- starts with
->; - just 3 symbols in size (5 with
->); - if original command is one word length - snippet name will be first 3 letters of it (
find()→->fin); - if original command is two words length - snippet name will be first letter of first word + first 2 letter of second word (
isAuthenticated()→->iau); - If original command is 3 or more words in length - snippet name will consist of first letter from each word, until it's 3 symbols (
isXmlHttpRequest()→->ixh) - If there's other singular snippet with same name (
getFileName()→->gfn), last letter of plural snippet will bes(getFileNames()→->gfs).
Expand Getter To Class modifier
We already knows how to get objects & run actions against them. But it's huge amount of actions for routine task:
- enter
usr; - press
⇥; - enter
->; - enter
iau; - press
⇥again.
And we can simplify this workflow by removing steps 2-4. . modifier will help us with this. You already must to know that modifiers is simple macroses, that:
- is one symbol in length;
- ends current snippet.
What . is do is just moves 3 letters back, expand snippet, appends -> & moves 3 letters forward, after which expands snippet again. In practice, our previos $this->getUser()->isAuthenticated() 5 steps snippet turns into 2 steps:
- enter
usriau.; - press
⇥.
usriau. expand into $this->getUser()->isAuthenticated() in action context.
And in template same usriau. will expand into $sf_user->isAuthenticated(). That simple!
Want to use a helper? There's snippet for that!
Yep. Every helper has associated snippet in sfSymfony. All snippets is divided in groups, named by helper package name.
Helpers follows simple rules:
- every helper starts with underscore (
_); - after underscore follows first letters of all words in helper name (
distance_of_time_in_words()→_dotiw)
Helpers works in source.php context. It means, that trying to expand _dotiw in template will do nothing. I've done that because sometimes we need to call helper from php code (writing other helpers for example) & helper is firstly the php command.
But don't worry, we have $ & ? modifiers from sfPHP bundle, that will help us in template context. _dotiw$ will generate [?php echo distance_of_time_in_words(${1:\$from_time}${2:, ${3:null}${4:, true}})$0 ?] as we expecting & _uh? will be [?php use_helper(${1:'helper'}) ?].
CLI GUI
sfSymfony has splendid GUI for symfony CLI tasks. To open it, simply press ⌃⇧S.
You will see simple & clear GUI with text field in which you can input symfony commands & it will:
- autocompletes them;
- shows short descriptions of commands when you type;
- shows full help when you choose needed command.
Just try & you'll love it.