From 175e00830830328e104a7669dffba36c5b6d89d8 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sun, 23 Jan 2011 19:27:50 -0200 Subject: [PATCH] Reading configuration using reader classes. You can pass the key configFile in Html settings to load in constructor. --- cake/libs/view/helpers/html.php | 66 ++++++++++++++++--- .../cases/libs/view/helpers/html.test.php | 54 ++++++++++++++- .../test_app/config/htmlhelper_minimized.ini | 2 + .../tests/test_app/config/htmlhelper_tags.php | 8 +++ 4 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 cake/tests/test_app/config/htmlhelper_minimized.ini create mode 100644 cake/tests/test_app/config/htmlhelper_tags.php diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index d784c6f66e8..21543f3111d 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -152,6 +152,19 @@ class HtmlHelper extends AppHelper { 'xhtml11' => '' ); +/** + * 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 @@ 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; } /** diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index c3e73790e48..a7a4cfbecb6 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -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 @@ 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'], ''); + + $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. * diff --git a/cake/tests/test_app/config/htmlhelper_minimized.ini b/cake/tests/test_app/config/htmlhelper_minimized.ini new file mode 100644 index 00000000000..e6a722d67f7 --- /dev/null +++ b/cake/tests/test_app/config/htmlhelper_minimized.ini @@ -0,0 +1,2 @@ +minimizedAttributeFormat = "format" + diff --git a/cake/tests/test_app/config/htmlhelper_tags.php b/cake/tests/test_app/config/htmlhelper_tags.php new file mode 100644 index 00000000000..abff9cdf5f4 --- /dev/null +++ b/cake/tests/test_app/config/htmlhelper_tags.php @@ -0,0 +1,8 @@ + array( + 'form' => 'start form', + 'formend' => 'finish form' + ) +); \ No newline at end of file