Is it possible to save the config for HTMLPurifier in a separate file so that I don't have to enter the config lines every time I create an instance?
I am using the standalone version as follows:
library/HTMLPurifier.php
library/standalone
You can store the config into codeigniter config file,
create a wrapper class that wrap around HTMLPurifier,
and putting all your config setting into constructor, like this :-
class Purifier extends HTMLPurifier
{
public function __construct()
{
// call parent::__construct
// load and set your configuration
// seq might affect the logic, you might want to reverse it
}
Related
I have a class which reads a config file (array).
Here's a basic example:
class ConfigReader
{
function __construct($configFileName)
{
$this->config = $this->load($configFileName);
}
private function load()
{
//load config file
}
public function hasValue($value)
{
//parse config for value return true
}
public function getValue($key)
{
//etc....
}
}
How should I should I unit test this? Should I mock the config file? I assume not because if I change something in the real config file the test would not pick it up...If I should mock it how should I test the config file?
Dealing with the filesystem is tricky. There are a couple of ways to test this.
One have a test config file that you would use in your test that contains example values. I am not sure what exactly you are going to do with the values. That may require mocking other system values. The down side to this is that you are maintaining a file for testing and have to make sure that paths/permissions of the file are such that the test can run. You can end up with the test failing because the file wasn't readable rather than an actual problem.
The preferred way would be to mock the filesystem. PHPUnit's documentation recommends using vfsStream. This would allow you to create a config file in your test on the fly and try to load it. The advantage this has is that you don't have to maintain the separate file system/paths for loading the file.
I am not able to provide more detail without having a better idea of what your class methods are trying to accomplish. But this should get you on the right start.
Is is possible to move config folder on same level of application folder?
I would like to have a structure like this:
application
controllers
models
views
config
error
system
No, You can't do it with CodeIgniter's native functions. But You can load them manually if You really want.
At the end of /application/config/config.php add "folder scanner" function with require_once(). Something like this:
foreach(glob(FCPATH.'config/*.php') as $cfg){
require_once($cfg);
}
upd Also You can add FCPATH.'config/' to CodeIgniter's core (/system/core/Config.php at var $_config_paths = array(APPPATH); // => var $_config_paths = array(APPPATH,FCPATH.'config/');) but this is bad practice because folder /system/ updates when CI updates.
upd2 as #jhuet wrote at the comments below: Instead of hacking right into CI's core classes, you should rather create at class called MY_Config in application/core directory. Make this class extend CI_Config and in the constructor change the value of $_config_path
Is there any dynamic way to put sqlite database connection instead of using sqlite:f:\\wamp\\www\\qdr\\protected\\data\\testdrive.db in main.php ?
'db'=>array(
'connectionString'=>'sqlite:f:\\wamp\\www\\qdr\\protected\\data\\testdrive.db',
),
Sql lite is about a single file so you can keep it like this. No dynamic needed. I am sure you are using wamp. Just keep it like this. This is the best solution for you. When you move the project to live server then change accordingly. Sqlite is a single file so do not bother that much.
It depends on what you understand under the vague term dynamic. But you could for example create your custom DBConnection class and override init() there:
class DbConnection extends CDbConnection
{
public function init()
{
// Set $this->connectionString to whatever you want, maybe
// $this->connectionString = 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db';
parent::init();
}
}
You can use this component as db component if you add
'class' => 'DbConnection',
to your main.php.
Note, though, that the init() method is only called the first time when the db component is accessed. So whatever you set as connectionString there will be used for the current request.
I've got some files using raw php (including config files) that's used for the automatic mailing stuff in my server.
Suppose inside this file I define a couple of constants (define(...)) and an array filled with database connection info, like user, host and so).
The website is done using Yii, so it also uses a config file.
These raw files can be placed anywhere (inside protected, outside, at the same level of index.php, whatever).
Now the problem comes that I've got a different configuration file (and different users/password for databases, and so) outside Yii, but I need to use it in some places inside Yii, too.
Is there a clear way to import these files to a controller? I've done it placing them inside extensions, but the raw functions didn't work from there.
The best approach would be to see if you can put your custom code into class files and put those in the components directory or similar and convert your files to classes (if they aren't already). That way you can get at your data without a lot of mixing of code which is going to be hard to maintain.
Simple approach will be to place the files in extensions and add the path of the extensions to your yii configuration. Then make a controller and call methods from its actions. Lets consider an example of swiftmailer. Here is a custom controller you can use.
class mailerController extends Controller{
public function actions()
{
return array(
//can add other actions here
);
}
public function actionIndex(){
echo "use mailer?e=<email>&m=<message>&sub=<subject> to send mail from the site to the email address";
}
public static function actionSendMail($e,$m,$sub){
$content = $m ; // can use a template and then assign to content
$SM = new SwiftMailer(); //the external method, should be present in include path
// Get config
$mailHost = Yii::app()->params['mailhost'];
$mailPort = 25; // Optional
$Transport = $SM->smtpTransport($mailHost, $mailPort);
$Mailer = $SM->mailer($Transport);
$Message = $SM
->newMessage($sub)
->setFrom(Yii::app()->params['sitemail'])
->setTo($e)
->addPart($content, 'text/html');
return ( $Mailer->send($Message));
} }
once your controller is ready, it can be called like
yoursite.com/mailer?e=<email>&m=<message>&sub=<subject>
Just need get some vals located in application.ini(main ini) in the Controller plugin
I know I can create an instance of Zend_config_Ini but would like to use the existing resource (application.ini is already used in Front Controller)
Use Zend Registry
// when loading the config
Zend_Registry::set('config', $config);
// later, somewhere
$config = Zend_Registry::get('config');
Try:
Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
If you now that you will need your configuration values from application.ini in few places around your whole application, you could read it up in bootstrap and store it in registry:
$appConfig = new Zend_Config_Ini('path/to/application.ini');
Zend_Registry::set('applicationConfig', $appConfig);
then later you can always access it:
Zend_Registry::get('applicationConfig')->someValue;
For reasons of testability and decoupling I would not use the Zend_Registry approach. In my opinion the plugin should not know anything about Zend_Config, but instead all options that are needed in the plugin should be injected from outside.
For example, if you want to use the config option "MyOption" in the Plugin Controller_Plugin_MyPlugin you could inject the needed parameters in the Bootstrap class as follows:
public function run()
{
$sopPlugin = $this->getResource('frontcontroller')->getPlugin('Controller_Plugin_MyPlugin');
$sopPlugin->setMyOption($this->getOption('MyOption'));
parent::run();
}
The advantage is that with this solution you avoid dependencies to Zend_Registry (which in fact is just an euphemism for a global variable) in your plugin code.