Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Reading configuration using reader classes. You can pass the key conf…
…igFile in Html settings to load in constructor.
  • Loading branch information
jrbasso committed Jan 23, 2011
1 parent 1a90bf7 commit 175e008
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 10 deletions.
66 changes: 57 additions & 9 deletions cake/libs/view/helpers/html.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -906,19 +919,54 @@ function __nestedListItem($items, $options, $itemOptions, $tag) {
}

/**
* 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);
}

$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 $this->_tags;
return $configs;
}

/**
Expand Down
54 changes: 53 additions & 1 deletion cake/tests/cases/libs/view/helpers/html.test.php
Expand Up @@ -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};
}

}

/**
Expand Down Expand Up @@ -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 = '';

Expand Down Expand Up @@ -1357,6 +1371,44 @@ function testCrumbList() {
);
}

/**
* 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.
*
Expand Down
2 changes: 2 additions & 0 deletions cake/tests/test_app/config/htmlhelper_minimized.ini
@@ -0,0 +1,2 @@
minimizedAttributeFormat = "format"

8 changes: 8 additions & 0 deletions cake/tests/test_app/config/htmlhelper_tags.php
@@ -0,0 +1,8 @@
<?php

$config = array(
'tags' => array(
'form' => 'start form',
'formend' => 'finish form'
)
);

0 comments on commit 175e008

Please sign in to comment.