I am using minscript extension for Javascript and CSS minification on my site. I have the following code in my protected/config/main.php
'clientScript'=>array(
'class'=>'ext.minScript.components.ExtMinScript',
)
In one of the modules I need custom implementation of CClientScript class. So I made a new class and derived it from ExtMinScript, like this.
class BaseClientScript extends ExtMinScript
In the module initialization code, I use setComponent to "overwrite" the "clientScript" component from main configuration. Thus I get the minification implementation from ExtMinScript and my implementation in BaseClientScript.
If I want to disable minification in the module, I change the code to derive BaseClientScript from CClientScript.
class BaseClientScript extends CClientScript
Now I want minification to be configurable in the main configuration. (protected/config/main.php)
I can make two classes, one deriving from CClientScript and other from ExtMinScript. (BaseClientScript and BaseMinClientScript) I don't want to do that, as that would mean that I have duplicate my custom client-script implementation in two classes.
Is there a better way to handle this situation?
Use a technique like this:
define('MinClientScriptClass', 'CClientScript');
when you want to use minification in protected/config/main.php
and write this in BaseClientScript:
if (defined('MinClientScriptClass')) {
class MinClientScriptClass extends CClientScript {}
} else {
class MinClientScriptClass extends ExtMinScript {}
}
class BaseClientScript extends MinClientScriptClass
NOTE: When you want to disable minification just comment the define in main config. Or use config params in main.php, and config true/false value check in if above.