Skip to content

Commit

Permalink
Merge pull request #844 from sjinks/config-adapter-json
Browse files Browse the repository at this point in the history
[NFR] Phalcon\Config\Adapter\Json
  • Loading branch information
Phalcon committed Jul 15, 2013
2 parents 624cd16 + f7c5911 commit cd01796
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 4 deletions.
2 changes: 0 additions & 2 deletions ext/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions ext/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion ext/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
96 changes: 96 additions & 0 deletions ext/config/adapter/json.c
Original file line number Diff line number Diff line change
@@ -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 <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
| Vladimir Kolesnikov <vladimir@extrememember.com> |
+------------------------------------------------------------------------+
*/

#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:
*
*<code>
*{"phalcon":{"baseuri":"\/phalcon\/"},"models":{"metadata":"memory"}}
*</code>
*
* You can read it as follows:
*
*<code>
* $config = new Phalcon\Config\Adapter\Json("path/config.json");
* echo $config->phalcon->baseuri;
* echo $config->models->metadata;
*</code>
*
*/


/**
* 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);
}
39 changes: 39 additions & 0 deletions ext/config/adapter/json.h
Original file line number Diff line number Diff line change
@@ -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 <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
| Vladimir Kolesnikov <vladimir@extrememember.com> |
+------------------------------------------------------------------------+
*/

#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 */
1 change: 1 addition & 0 deletions ext/kernel/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ void phalcon_file_put_contents(zval *return_value, zval *filename, zval *data TS
if (use_copy) {
data = &copy;
}
/* no break */

case IS_STRING:
if (Z_STRLEN_P(data)) {
Expand Down
4 changes: 4 additions & 0 deletions ext/pconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 2 additions & 0 deletions ext/phalcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ext/phalcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion unit-tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Empty file modified unit-tests/config/config.ini
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions unit-tests/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"phalcon":{"baseuri":"\/phalcon\/"},"models":{"metadata":"memory"},"database":{"adapter":"mysql","host":"localhost","username":"user","password":"passwd","name":"demo"},"test":{"parent":{"property2":"yeah"}}}

0 comments on commit cd01796

Please sign in to comment.