beberlei / yadif

Yet Another (PHP) Dependency Injection Framework
Other
37 stars 1 forks source link

Allow for casting / calling dependency's methods when injecting #15

Closed dkarlovi closed 8 years ago

dkarlovi commented 13 years ago

This isn't a bug, more like a RFC. :)

I'm trying to setup HTMLPurifier like so:

; HTMLPurifier config
htmlPurifier.class                          = "HTMLPurifier"
htmlPurifier.arguments.0                    = "htmlPurifierConfig"
htmlPurifierConfig.class                    = "Zend_Config"
htmlPurifierConfig.arguments.0              = ":htmlPurifierConfig"
htmlPurifierConfig.arguments.1              = ":htmlPurifierPreset"
htmlPurifierConfig.arguments.2              = ":htmlPurifierOptions"
htmlPurifierConfig.params.:htmlPurifierConfig = APPLICATION_ROOT "/config/htmlpurifier.ini"
htmlPurifierConfig.params.:htmlPurifierPreset = "default"
htmlPurifierConfig.params.:htmlPurifierOptions.nestSeparator = "@"

But as HTMLPurifier does not accept Zend_Config instances but arrays, I would need to call Zend_Config::toArray() instead of injecting. So, instead of injecting the object itself, it would inject the resulting array.

I've for now worked around it with:

class My_HTMLPurifier extends HTMLPurifier
{
    public function __construct($config = null)
    {
        if (null !== $config) {
            if ($config instanceof Zend_Config) {
                // HTMLPurifier will not accept a Zend_Config-type object
                // cast to array
                $config = $config->toArray();
            }
        }

        parent::__construct($config);
    }
}

and setting htmlPurifier.class to My_HTMLPurifier which also seems like an OK solution for now.

Thoughts?