This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » development » Feature requests » Convert all helpers into static classes
Convert all helpers into static classes [message #75234] Thu, 19 March 2009 19:13 Go to next message
heliocorreia  is currently offline 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:
__("Hello");


This may solve future problems with embedding 3rd-party app and Symfony Helpers.
Re: Convert all helpers into static classes [message #75258 is a reply to message #75234 ] Fri, 20 March 2009 08:31 Go to previous messageGo to next message
naholyr  is currently offline naholyr
Messages: 223
Registered: June 2007
Faithful Member
+1, this is an important feature.

I worked on this direction, with 4 features :
- A new way of loading helpers which includes a class
- A generation of shortcuts (class to functions) in the cache for backward compatibility.
- A call to a function transforms a functions file (helper) into a class, so you can for example extend old helpers without risking functions conflict.
- A "helper overclassing" ability in the settings you can tell "when i want to load the helper named 'X', load 'Y' instead".

This is honestly not so hard, and helpers become fully extendable.


To solve your problem you could then :
- classify I18N helper
- create a new helper which does not extend but wraps this helper
- make "sf__" an alias of "__" in this class
- use sf__() instead of __() when you need
Even if I'm not sure it's ideal as generated code will use __() and not your new function, but it's a first step.
Re: Convert all helpers into static classes [message #75541 is a reply to message #75258 ] Tue, 24 March 2009 21:18 Go to previous messageGo to next message
heliocorreia  is currently offline heliocorreia
Messages: 12
Registered: March 2006
Junior Member
A workaround when working with generator is copy all generated code from cache dir to app's template dir. Then, change __() calls for sf__()

Please, classify helpers! Smile
Re: Convert all helpers into static classes [message #75625 is a reply to message #75258 ] Wed, 25 March 2009 22:57 Go to previous messageGo to next message
heliocorreia  is currently offline heliocorreia
Messages: 12
Registered: March 2006
Junior Member
I found a better workaround at "Inside View Layer":

Quote:

This system even allows you to override the existing symfony helpers. For instance, to redefine all the helpers of the Text helper group, just create a TextHelper.php file in your apps/frontend/lib/helper/ directory. Whenever you call use_helper('Text'), symfony will use your helper group rather than its own. But be careful: as the original file is not even loaded, you must redefine all the functions of a helper group to override it; otherwise, some of the original helpers will not be available at all.



So... classify helpers isn't necessary any more.

This post may be closed
Re: Convert all helpers into static classes [message #75639 is a reply to message #75625 ] Thu, 26 March 2009 08:07 Go to previous messageGo to next message
naholyr  is currently offline 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 Sad

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
all: 
  helpers: 
    Url: MyUrl


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 Sad

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]

Re: Convert all helpers into static classes [message #75641 is a reply to message #75234 ] Thu, 26 March 2009 09:08 Go to previous messageGo to next message
michael.piecko  is currently offline michael.piecko
Messages: 624
Registered: June 2006
Location: Germany
Faithful Member
heliocorreia wrote on Thu, 19 March 2009 19:13

There is a conflict with Symfony and WordPress ...



Why try to change symfony? Isn't it easier to adapt WordPress?

Michael
Re: Convert all helpers into static classes [message #75655 is a reply to message #75234 ] Thu, 26 March 2009 10:37 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
@naholyr - for your suggestion, put a synopsis on the devs mailing list, and link to here. I like the idea Razz


Remember Palestine
Re: Convert all helpers into static classes [message #75668 is a reply to message #75655 ] Thu, 26 March 2009 14:13 Go to previous messageGo to next message
naholyr  is currently offline naholyr
Messages: 223
Registered: June 2007
Faithful Member
halfer wrote on Thu, 26 March 2009 10:37

@naholyr - for your suggestion, put a synopsis on the devs mailing list, and link to here. I like the idea Razz

Right, plus I'd like to have feedbacks on the proposed implementation.
Re: Convert all helpers into static classes [message #75670 is a reply to message #75641 ] Thu, 26 March 2009 15:02 Go to previous messageGo to next message
heliocorreia  is currently offline heliocorreia
Messages: 12
Registered: March 2006
Junior Member
@michael.piecko,

Because I'll have to adapt every 3rd-party application when it conflits with Symfony.
Re: Convert all helpers into static classes [message #76843 is a reply to message #75234 ] Wed, 15 April 2009 09:12 Go to previous message
naholyr  is currently offline naholyr
Messages: 223
Registered: June 2007
Faithful Member
For your information, I published this work as a plugin here :

http://www.symfony-project.org/plugins/nahoClassHelpersPlugi n

There is only SVN for the moment, README and package are on their way Wink

How to use :
- in your file "/apps/myApp/config/MyAppConfiguration.php" change "extends sfApplicationConfiguration" to "extends nahoClassHelpersApplicationConfiguration". If you can't, just copy-paste the "loadHelpers()" method from " /plugins/nahoClassHelpersPlugin/lib/nahoClassHelpersApplicat ionConfiguration.php ".
- Usage : look at an example in "/plugins/nahoClassHelpersPlugin/data/Example.php"
Previous Topic:Stylesheet and javascript inclusion function
Next Topic:Form embedded
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software