From 00b2f7015c0d37e32838ca467207090a791e102f Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Sun, 14 Jul 2013 15:09:16 +0300 Subject: [PATCH 1/3] Fix code analyzer's warning --- ext/kernel/file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/kernel/file.c b/ext/kernel/file.c index e1944475846..2413902f190 100644 --- a/ext/kernel/file.c +++ b/ext/kernel/file.c @@ -370,6 +370,7 @@ void phalcon_file_put_contents(zval *return_value, zval *filename, zval *data TS if (use_copy) { data = © } + /* no break */ case IS_STRING: if (Z_STRLEN_P(data)) { From 7522784823523e49369a5174755557654244aeb5 Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Sun, 14 Jul 2013 15:12:15 +0300 Subject: [PATCH 2/3] Config\Adapter\Json --- ext/config.c | 2 - ext/config.m4 | 1 + ext/config.w32 | 2 +- ext/config/adapter/json.c | 96 +++++++++++++++++++++++++++++++++++++++ ext/config/adapter/json.h | 39 ++++++++++++++++ ext/pconfig.h | 4 ++ ext/phalcon.c | 2 + ext/phalcon.h | 1 + 8 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 ext/config/adapter/json.c create mode 100644 ext/config/adapter/json.h diff --git a/ext/config.c b/ext/config.c index 3d3274c229a..a88103d0450 100755 --- a/ext/config.c +++ b/ext/config.c @@ -88,8 +88,6 @@ static inline phalcon_config_object* fetchPhalconConfigObject(zval* zobj TSRMLS_ return (phalcon_config_object*)zend_objects_get_address(zobj TSRMLS_CC); } -static void phalcon_config_construct_internal(zval *this_ptr, zval *array_config TSRMLS_DC); - /** * @brief Counts the number of elements in the configuration; this is the part of Countable interface */ diff --git a/ext/config.m4 b/ext/config.m4 index 9a51870b1cf..df6ce00a651 100755 --- a/ext/config.m4 +++ b/ext/config.m4 @@ -196,6 +196,7 @@ mvc/model/relationinterface.c \ mvc/model/messageinterface.c \ mvc/model/transactioninterface.c \ config/adapter/ini.c \ +config/adapter/json.c \ config/exception.c \ filterinterface.c \ logger/multiple.c \ diff --git a/ext/config.w32 b/ext/config.w32 index 265983a2db5..fbd89c6c1ff 100755 --- a/ext/config.w32 +++ b/ext/config.w32 @@ -44,7 +44,7 @@ if (PHP_PHALCON != "no") { ADD_SOURCES("ext/phalcon/mvc/model/validator", "email.c presenceof.c inclusionin.c exclusionin.c uniqueness.c url.c regex.c numericality.c stringlength.c", "phalcon") ADD_SOURCES("ext/phalcon/mvc/model/resultset", "complex.c simple.c", "phalcon") ADD_SOURCES("ext/phalcon/mvc/model/behavior", "timestampable.c softdelete.c", "phalcon") - ADD_SOURCES("ext/phalcon/config/adapter", "ini.c", "phalcon") + ADD_SOURCES("ext/phalcon/config/adapter", "ini.c json.c", "phalcon") ADD_SOURCES("ext/phalcon/config", "exception.c", "phalcon") ADD_SOURCES("ext/phalcon/logger", "multiple.c formatter.c exception.c adapterinterface.c formatterinterface.c adapter.c item.c", "phalcon") ADD_SOURCES("ext/phalcon/logger/formatter", "json.c line.c syslog.c", "phalcon") diff --git a/ext/config/adapter/json.c b/ext/config/adapter/json.c new file mode 100644 index 00000000000..5d459fdba1a --- /dev/null +++ b/ext/config/adapter/json.c @@ -0,0 +1,96 @@ + +/* + +------------------------------------------------------------------------+ + | Phalcon Framework | + +------------------------------------------------------------------------+ + | Copyright (c) 2011-2013 Phalcon Team (http://www.phalconphp.com) | + +------------------------------------------------------------------------+ + | This source file is subject to the New BSD License that is bundled | + | with this package in the file docs/LICENSE.txt. | + | | + | If you did not receive a copy of the license and are unable to | + | obtain it through the world-wide-web, please send an email | + | to license@phalconphp.com so we can send you a copy immediately. | + +------------------------------------------------------------------------+ + | Authors: Andres Gutierrez | + | Eduar Carvajal | + | Vladimir Kolesnikov | + +------------------------------------------------------------------------+ +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "php_phalcon.h" +#include "phalcon.h" + +#include "kernel/main.h" +#include "kernel/memory.h" +#include "kernel/file.h" +#include "kernel/string.h" + +#include "config/adapter/json.h" + +/** + * Phalcon\Config\Adapter\Json + * + * Reads JSON files and converts them to Phalcon\Config objects. + * + * Given the following configuration file: + * + * + *{"phalcon":{"baseuri":"\/phalcon\/"},"models":{"metadata":"memory"}} + * + * + * You can read it as follows: + * + * + * $config = new Phalcon\Config\Adapter\Json("path/config.json"); + * echo $config->phalcon->baseuri; + * echo $config->models->metadata; + * + * + */ + + +/** + * Phalcon\Config\Adapter\Json initializer + */ +PHALCON_INIT_CLASS(Phalcon_Config_Adapter_Json){ + + PHALCON_REGISTER_CLASS_EX(Phalcon\\Config\\Adapter, Json, config_adapter_json, "phalcon\\config", phalcon_config_adapter_json_method_entry, 0); + + return SUCCESS; +} + +/** + * Phalcon\Config\Adapter\Json constructor + * + * @param string $filePath + */ +PHP_METHOD(Phalcon_Config_Adapter_Json, __construct){ + + zval *file_path, *contents, *array; + + phalcon_fetch_params(0, 1, 0, &file_path); + + ALLOC_INIT_ZVAL(contents); + ALLOC_INIT_ZVAL(array); + phalcon_file_get_contents(contents, file_path TSRMLS_CC); + + if (Z_TYPE_P(contents) == IS_STRING) { + phalcon_json_decode(array, contents, 1 TSRMLS_CC); + } + + zval_ptr_dtor(&contents); + + if (Z_TYPE_P(array) != IS_ARRAY) { + zval_dtor(array); + array_init_size(array, 0); + } + + phalcon_config_construct_internal(getThis(), array TSRMLS_CC); + zval_ptr_dtor(&array); +} diff --git a/ext/config/adapter/json.h b/ext/config/adapter/json.h new file mode 100644 index 00000000000..549133367a4 --- /dev/null +++ b/ext/config/adapter/json.h @@ -0,0 +1,39 @@ + +/* + +------------------------------------------------------------------------+ + | Phalcon Framework | + +------------------------------------------------------------------------+ + | Copyright (c) 2011-2013 Phalcon Team (http://www.phalconphp.com) | + +------------------------------------------------------------------------+ + | This source file is subject to the New BSD License that is bundled | + | with this package in the file docs/LICENSE.txt. | + | | + | If you did not receive a copy of the license and are unable to | + | obtain it through the world-wide-web, please send an email | + | to license@phalconphp.com so we can send you a copy immediately. | + +------------------------------------------------------------------------+ + | Authors: Andres Gutierrez | + | Eduar Carvajal | + | Vladimir Kolesnikov | + +------------------------------------------------------------------------+ +*/ + +#ifndef PHALCON_CONFIG_ADAPTER_JSON_H +#define PHALCON_CONFIG_ADAPTER_JSON_H + +extern zend_class_entry *phalcon_config_adapter_json_ce; + +PHALCON_INIT_CLASS(Phalcon_Config_Adapter_Json); + +PHP_METHOD(Phalcon_Config_Adapter_Json, __construct); + +ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_config_adapter_json___construct, 0, 0, 1) + ZEND_ARG_INFO(0, filePath) +ZEND_END_ARG_INFO() + +PHALCON_INIT_FUNCS(phalcon_config_adapter_json_method_entry){ + PHP_ME(Phalcon_Config_Adapter_Json, __construct, arginfo_phalcon_config_adapter_json___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_FE_END +}; + +#endif /* PHALCON_CONFIG_ADAPTER_JSON_H */ diff --git a/ext/pconfig.h b/ext/pconfig.h index d2be9569896..a47f5efcd74 100644 --- a/ext/pconfig.h +++ b/ext/pconfig.h @@ -89,3 +89,7 @@ PHALCON_INIT_FUNCS(phalcon_config_method_entry){ PHP_FE_END }; +/** + * @internal + */ +void phalcon_config_construct_internal(zval *this_ptr, zval *array_config TSRMLS_DC); diff --git a/ext/phalcon.c b/ext/phalcon.c index 8e27d2466ad..0ae2c5d68b3 100755 --- a/ext/phalcon.c +++ b/ext/phalcon.c @@ -181,6 +181,7 @@ zend_class_entry *phalcon_logger_formatter_syslog_ce; zend_class_entry *phalcon_logger_formatterinterface_ce; zend_class_entry *phalcon_config_exception_ce; zend_class_entry *phalcon_config_adapter_ini_ce; +zend_class_entry *phalcon_config_adapter_json_ce; zend_class_entry *phalcon_forms_form_ce; zend_class_entry *phalcon_forms_element_ce; zend_class_entry *phalcon_forms_exception_ce; @@ -558,6 +559,7 @@ PHP_MINIT_FUNCTION(phalcon){ PHALCON_INIT(Phalcon_Logger_Adapter_File); PHALCON_INIT(Phalcon_Logger_Formatter_Syslog); PHALCON_INIT(Phalcon_Config_Adapter_Ini); + PHALCON_INIT(Phalcon_Config_Adapter_Json); PHALCON_INIT(Phalcon_Config_Exception); PHALCON_INIT(Phalcon_Forms_Form); PHALCON_INIT(Phalcon_Forms_Manager); diff --git a/ext/phalcon.h b/ext/phalcon.h index 34792c2ac29..7fe099c7566 100755 --- a/ext/phalcon.h +++ b/ext/phalcon.h @@ -209,6 +209,7 @@ #include "logger/adapter/file.h" #include "logger/formatter/syslog.h" #include "config/adapter/ini.h" +#include "config/adapter/json.h" #include "config/exception.h" #include "forms/form.h" #include "forms/manager.h" From f7c59118e83e9ba92d9cf076bc8057eebffc4c77 Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Sun, 14 Jul 2013 15:12:48 +0300 Subject: [PATCH 3/3] Unit test --- unit-tests/ConfigTest.php | 8 +++++++- unit-tests/config/config.ini | 0 unit-tests/config/config.json | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) mode change 100755 => 100644 unit-tests/config/config.ini create mode 100644 unit-tests/config/config.json diff --git a/unit-tests/ConfigTest.php b/unit-tests/ConfigTest.php index 117700b9d39..5329b4630f6 100644 --- a/unit-tests/ConfigTest.php +++ b/unit-tests/ConfigTest.php @@ -78,7 +78,13 @@ public function testIniConfig() $this->assertTrue($this->_compareConfig($this->_config, $config)); } - public function testStandarConfig() + public function testJSONConfig() + { + $config = new Phalcon\Config\Adapter\Json('unit-tests/config/config.json'); + $this->assertTrue($this->_compareConfig($this->_config, $config)); + } + + public function testStandardConfig() { $config = new Phalcon\Config($this->_config); $this->_compareConfig($this->_config, $config); diff --git a/unit-tests/config/config.ini b/unit-tests/config/config.ini old mode 100755 new mode 100644 diff --git a/unit-tests/config/config.json b/unit-tests/config/config.json new file mode 100644 index 00000000000..0787d4021dc --- /dev/null +++ b/unit-tests/config/config.json @@ -0,0 +1 @@ +{"phalcon":{"baseuri":"\/phalcon\/"},"models":{"metadata":"memory"},"database":{"adapter":"mysql","host":"localhost","username":"user","password":"passwd","name":"demo"},"test":{"parent":{"property2":"yeah"}}}