Reading configuration using reader classes. You can pass the key configFile in Html settings to load in constructor.

This commit is contained in:
Juan Basso 2011-01-23 19:27:50 -02:00
parent 1a90bf7292
commit 175e008308
4 changed files with 120 additions and 10 deletions

View file

@ -152,6 +152,19 @@ class HtmlHelper extends AppHelper {
'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
);
/**
* Default Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
if (!empty($settings['configFile'])) {
$this->loadConfig($settings['configFile']);
}
}
/**
* Adds a link to the breadcrumbs array.
*
@ -906,19 +919,54 @@ class HtmlHelper extends AppHelper {
}
/**
* Parses tag templates into $this->tags.
* Load Html configs
*
* @param $name file name inside app/config to load.
* @return array merged tags from config/$name.php
* @param mixed $configFile String with the config file (load using PhpReader) or an array with file and reader name
* @param string $path Path with config file
* @return mixed False to error or loaded configs
*/
public function loadConfig($name = 'tags') {
if (file_exists(CONFIGS . $name .'.php')) {
require(CONFIGS . $name .'.php');
if (isset($tags)) {
$this->_tags = array_merge($this->_tags, $tags);
public function loadConfig($configFile, $path = CONFIGS) {
$file = null;
$reader = 'php';
if (!is_array($configFile)) {
$file = $configFile;
} elseif (isset($configFile[0])) {
$file = $configFile[0];
if (isset($configFile[1])) {
$reader = $configFile[1];
}
} else {
return trigger_error(__('Cannot load the configuration file. Wrong "configFile" configuration.'), E_USER_NOTICE);
}
return $this->_tags;
$readerClass = Inflector::camelize($reader) . 'Reader';
if (!App::import('Lib', 'config/' . $readerClass)) {
return trigger_error(__('Cannot load the configuration file. Unknown reader.'), E_USER_NOTICE);
}
try {
$readerObj = new $readerClass($path);
$configs = $readerObj->read($file);
if (isset($configs['tags']) && is_array($configs['tags'])) {
$this->_tags = array_merge($this->_tags, $configs['tags']);
}
if (isset($configs['minimizedAttributes']) && is_array($configs['minimizedAttributes'])) {
$this->_minimizedAttributes = array_merge($this->_minimizedAttributes, $configs['minimizedAttributes']);
}
if (isset($configs['docTypes']) && is_array($configs['docTypes'])) {
$this->__docTypes = array_merge($this->__docTypes, $configs['docTypes']);
}
if (isset($configs['attributeFormat'])) {
$this->_attributeFormat = $configs['attributeFormat'];
}
if (isset($configs['minimizedAttributeFormat'])) {
$this->_minimizedAttributeFormat = $configs['minimizedAttributeFormat'];
}
} catch (Exception $e) {
return trigger_error(__('Cannot load the configuration file. Failed to load the file.'), E_USER_NOTICE);
}
return $configs;
}
/**

View file

@ -60,6 +60,20 @@ class TestHtmlHelper extends HtmlHelper {
function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
}
/**
* Get a protected attribute value
*
* @param string $attribute
* @return mixed
*/
public function getAttribute($attribute) {
if (!isset($this->{$attribute})) {
return null;
}
return $this->{$attribute};
}
}
/**
@ -128,7 +142,7 @@ class HtmlHelperTest extends CakeTestCase {
function setUp() {
parent::setUp();
$this->View = $this->getMock('View', array('addScript'), array(new TheHtmlTestController()));
$this->Html = new HtmlHelper($this->View);
$this->Html = new TestHtmlHelper($this->View);
$this->Html->request = new CakeRequest(null, false);
$this->Html->request->webroot = '';
@ -1357,6 +1371,44 @@ class HtmlHelperTest extends CakeTestCase {
);
}
/**
* testLoadConfig method
*
* @return void
*/
public function testLoadConfig() {
$path = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config'. DS;
$result = $this->Html->loadConfig('htmlhelper_tags', $path);
$expected = array(
'tags' => array(
'form' => 'start form',
'formend' => 'finish form'
)
);
$this->assertEqual($result, $expected);
$tags = $this->Html->getAttribute('_tags');
$this->assertEqual($tags['form'], 'start form');
$this->assertEqual($tags['formend'], 'finish form');
$this->assertEqual($tags['selectend'], '</select>');
$result = $this->Html->loadConfig(array('htmlhelper_minimized.ini', 'ini'), $path);
$expected = array(
'minimizedAttributeFormat' => 'format'
);
$this->assertEqual($result, $expected);
$this->assertEqual($this->Html->getAttribute('_minimizedAttributeFormat'), 'format');
$this->expectError();
$result = $this->Html->loadConfig('wrong_file');
$this->assertFalse($result);
$this->expectError();
$result = $this->Html->loadConfig(array('htmlhelper_tags', 'wrong_reader'), $path);
$this->assertFalse($result);
}
/**
* test parsing attributes.
*

View file

@ -0,0 +1,2 @@
minimizedAttributeFormat = "format"

View file

@ -0,0 +1,8 @@
<?php
$config = array(
'tags' => array(
'form' => 'start form',
'formend' => 'finish form'
)
);