Discussion:
Custom namespaces
Robert Goldsmith
2012-04-16 11:09:39 UTC
Permalink
Hi all,

I'm looking to add a custom namespace to allow me to do some additional processing on a template. As the processing is something I want to do globally and at template compile time the custom namespace seems like a good option and there seems to be some support for this.

I've looked at the way the namespace code works and I believe I need to:

* Create a new class that extends the PHPTAL_Namespace_Builtin class
* In the constructor of this class register my attributes and the namespace prefix etc.
* Replace the builtin method for createAttributeHandler() so I can load my implementation classes from elsewhere.

This all looks pretty reasonable but now I'm looking at how these namespace classes are initiated and I'm looking at class PHPTAL_Dom_Defs. The comments in this class suggest I can create a subclass and use it to register additional namespaces but then the class is setup as a singleton and I can't see how I'd be able to replace it.

Assuming that all I need to do is call PHPTAL_Dom_Defs's registerNamespace() method to register my new namespace then could someone possibly suggest a good time to do this? Normally, the namespaces are registered in the constructor which is called the first time PHPTAL_Dom_Defs is used. I guess I'd need to find a sensible point guaranteed to be before the first use of this class and call it myself to register the new namespace.

Robert
----
Robert Goldsmith
rgoldsmith-ekKPKLigxzH10XsdtD+***@public.gmane.org
Kornel Lesiński
2012-04-16 20:32:18 UTC
Permalink
On Mon, 16 Apr 2012 12:09:39 +0100, Robert Goldsmith
Post by Robert Goldsmith
This all looks pretty reasonable but now I'm looking at how these
namespace classes are initiated and I'm looking at class
PHPTAL_Dom_Defs. The comments in this class suggest I can create a
subclass and use it to register additional namespaces but then the class
is setup as a singleton and I can't see how I'd be able to replace it.
Assuming that all I need to do is call PHPTAL_Dom_Defs's
registerNamespace() method to register my new namespace then could
someone possibly suggest a good time to do this? Normally, the
namespaces are registered in the constructor which is called the first
time PHPTAL_Dom_Defs is used. I guess I'd need to find a sensible point
guaranteed to be before the first use of this class and call it myself
to register the new namespace.
The singleton isn't most elegant indeed.

I think you can safely execute
PHPTAL_Dom_Defs::getInstance()->registerNamespace() at any time before
PHPTAL class compiles the template.
--
regards, Kornel Lesiński
Loading...