Convert all helpers into static classes [message #75234] |
Thu, 19 March 2009 19:13  |
heliocorreia Messages: 12 Registered: March 2006 |
Junior Member |
|
|
Hi,
There is a conflict with Symfony and WordPress with "__" function at I18N Helper. I think that converting all symfony helpers to static classes may solve the problem.
Example:
sfHelperI18N::__("Hello");
instead of:
This may solve future problems with embedding 3rd-party app and Symfony Helpers.
|
|
|
|
|
|
Re: Convert all helpers into static classes [message #75639 is a reply to message #75625 ] |
Thu, 26 March 2009 08:07   |
naholyr Messages: 223 Registered: June 2007 |
Faithful Member |
|
|
This system is not satisfying, we have to copy-paste all the contents of the helper. When an upgrade of the framework or the plugin defining this plugin adds or modifies a few helpers, we'll have to do the same operation again...
If I want all my external links being decorated differently (adding an "external" class automatically), I have no other option than doing it manually OR copy-paste the whole UrlHelper.php.
I would like to be able to do something like this :
/lib/helper/MyUrlHelper.php
<?php class MyUrlHelper extends sfUrlHelper
{
protected function isExternal($url)
{
// regexp to detect external links, not the point here
}
public function linkTo($name, $url, $options = array())
{
if ($this->isExternal($url)) {
if (isset($options['class'])) {
$options['class'] .= ' external';
} else {
$options['class'] = 'external';
}
}
}
} ?>
But today the class "sfUrlHelper" does not exist 
Then, as a workaround it would be nice at least to be able to generate it manually from the functions-group :
/lib/helper/MyUrlHelper.php
<?php sntHelperBase::classifyHelper('Url', 'sfUrlHelper');
... ?>
Finally I'd like my "MyUrlHelper" being loaded instead of "UrlHelper" when I run "use_helper('Url')" :/
/apps/SF_APP/config/app.yml
What's missing ?
- Implement those things (helper classifying, helper overclassing).
- Load classes "***Helper" instead of simply including file "***Helper.php" (let the autoloader work).
- Generate shortcut underscored functions from the camelized methods of the loaded class.
In fact everything presented here, I realized it quite simply, this is why I think this should definitely considered for a core feature as it's so simple to implement 
For the loading thing, just overwrite your appConfiguration :
/apps/SF_APP/config/SF_APPConfiguration.php
<?php ...
public function loadHelpers($helpers, $moduleName = '', $oldWayOnly = false)
{
if ($oldWayOnly)
{
parent::loadHelpers($helpers, $moduleName);
}
else
{
sntLoader::loadHelpers($helpers, $moduleName, $this);
}
}
... ?>
And get attached files to find the missing features not directly pasted here :
- sntHelperBase.php : generates a class from a functions group. The important thing is that the file is read but not loaded, which meanse there won't be conflict with shortcut names.
- sntLoader.php : implements the overclassing feature (only way to extend core or plugin helpers in a safe way), and generation of the underscored shortcuts.
Oh by the way, I didn't talk about the implications in your template. There is none, this is all the point of all this stuff : being able to extend helpers without manual copy-paste (role of classifier), and without any change into the templates (typically the role of shortcut-generator and overclassing).[/edit]
Attachment: helper.zip
(Size: 2.97KB, Downloaded 104 time(s))
[Updated on: Thu, 26 March 2009 08:24]
|
|
|
|
|
|
|
|