Skip to content

Commit

Permalink
adding yaml and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
duythien committed Oct 25, 2014
1 parent c2d86a7 commit 212d477
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
132 changes: 132 additions & 0 deletions ext/phalcon/config/adapter/yaml.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ext/phalcon/config/adapter/yaml.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions phalcon/config/adapter/yaml.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 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> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Config\Adapter;

use Phalcon\Config;
use Phalcon\Config\Exception;

class Yaml extends Config
{

/**
* Phalcon\Config\Adapter\Yaml constructor
*
* @param string $filePath
* @param array $callbacks
* @throws \Phalcon\Config\Exception
*/
public function __construct(string! filePath, array! callbacks = null)
{
if (!extension_loaded("yaml")) {
throw new Exception("Yaml extension not loaded");
}
var yamlConfig;
int ndocs = 0;
if callbacks != null {
let yamlConfig = yaml_parse_file(filePath, 0, ndocs, callbacks);
} else {
let yamlConfig = yaml_parse_file(filePath);
}
if yamlConfig === false {
throw new Exception("Configuration file " . basename(filePath) . " can't be loaded");
}

parent::__construct(yamlConfig);
}

}
22 changes: 22 additions & 0 deletions unit-tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
+------------------------------------------------------------------------+
*/

define('APPROOT', dirname(__DIR__));
define('CONFKEY', 'secret');

class ConfigTest extends PHPUnit_Framework_TestCase
{

Expand Down Expand Up @@ -227,4 +230,23 @@ public function testJsonConfig()
$config = new Phalcon\Config\Adapter\Json('unit-tests/config/config.json');
$this->assertTrue($this->_compareConfig($this->_config, $config));
}

public function testYamlConfig()
{
$config = new Phalcon\Config\Adapter\Yaml('unit-tests/config/config.yml');
$this->assertTrue($this->_compareConfig($this->_config, $config));
}

public function testYamlConfigCallback()
{
$config = new Phalcon\Config\Adapter\Yaml('unit-tests/config/config.yml', array(
'!decrypt' => function($value) {
return (new Phalcon\Crypt)->setCipher('blowfish')->decryptBase64($value, CONFKEY);
},
'!approot' => function($value) {
return APPROOT . $value;
}
));
$this->assertTrue($this->_compareConfig($this->_config, $config));
}
}

0 comments on commit 212d477

Please sign in to comment.