
catalyst at mikeraynham
Jun 11, 2011, 11:34 AM
Views: 1527
Permalink
|
On 11/06/11 18:38, David E. Wheeler wrote: > On Jun 10, 2011, at 8:40 PM, Mike Raynham wrote: > >> Would you be happy for me to submit a patch for Bric::Config so that >> configuration directives can be obtained via an AUTOLOAD getter method? >> >> Configuration directives can then be migrated as and when required. > > No autoload, please. > > We could maybe set something up, though, so that they get created as functions rather than constants. Then you could replace them at runtime using Test::MockModule. My plan is to create an AUTOLOAD function in Bric::Config which will replace each of the constants with a getter function. I've attached a diff of the Bric::Config code that I've been experimenting with so far. if (MEDIA_UNIQUE_FILENAME) { ... } would be replaced with: if (Bric::Config::get_media_unique_filename) { ... } The functions could be exported, but I thought it might be clearer (if a little longer) to use the fully qualified name. As there are lots of constants throughout the code, they can be switched out as and when required, rather than all at once. The AUTOLOAD function throws an exception if the configuration directive is defined as a constant, so it shouldn't be possible to use both at the same time. Many of the constants can simply be removed or commented out, and they will automatically be available via a getter function. In some cases, default values are set at the point at which the constant is created. In these cases, the $config hash value will need setting or updating as appropriate. For example: use constant MEDIA_URI_ROOT => '/data/media'; would have to be changed to: $config->{MEDIA_URI_ROOT} = '/data/media'; The functions created by AUTOLOAD can then be overridden with Test::MockModule. Is this what you were thinking? I've started a DevTest module for Bric::Config too. At the moment it just checks that AUTOLOAD throws the correct exceptions. I'm sure it could be enhanced later. Regards, Mike
|