mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Adding PhpReader to implement php file reading as per #1334
This commit is contained in:
parent
aa0bad9247
commit
fdb5955d6c
3 changed files with 128 additions and 0 deletions
66
cake/libs/config/php_reader.php
Normal file
66
cake/libs/config/php_reader.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
/**
|
||||
* PhpReader file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* PHP Reader allows Configure to load configuration values from
|
||||
* files containing simple PHP arrays.
|
||||
*
|
||||
* @package cake.libs.config
|
||||
*/
|
||||
class PhpReader {
|
||||
/**
|
||||
* The path this reader finds files on.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_path = null;
|
||||
|
||||
/**
|
||||
* Constructor for PHP Config file reading.
|
||||
*
|
||||
* @param string $path The path to read config files from. Defaults to CONFIGS
|
||||
*/
|
||||
public function __construct($path = CONFIGS) {
|
||||
$this->_path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a config file and return its contents.
|
||||
*
|
||||
* Keys with `.` will be treated as values in plugins. Instead of reading from
|
||||
* the initialized path, plugin keys will be located using App::pluginPath().
|
||||
*
|
||||
*
|
||||
* @param string $key The identifier to read from. If the key has a . it will be treated
|
||||
* as a plugin prefix.
|
||||
* @return array Parsed configuration values.
|
||||
*/
|
||||
public function read($key) {
|
||||
$file = $this->_path . $key . '.php';
|
||||
if (!file_exists($file)) {
|
||||
throw new RuntimeException(__('Could not load configuration file: ') . $file);
|
||||
}
|
||||
include $file;
|
||||
if (!isset($config)) {
|
||||
return array();
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
}
|
53
cake/tests/cases/libs/config/php_reader.test.php
Normal file
53
cake/tests/cases/libs/config/php_reader.test.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* PhpConfigReaderTest
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::import('Core', 'config/PhpReader');
|
||||
|
||||
class PhpReaderTest extends CakeTestCase {
|
||||
/**
|
||||
* setup
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->path = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config'. DS;
|
||||
}
|
||||
/**
|
||||
* test reading files
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testRead() {
|
||||
$reader = new PhpReader($this->path);
|
||||
$values = $reader->read('var_test');
|
||||
$this->assertEquals('value', $values['Read']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test an exception is thrown by reading files that don't exist.
|
||||
*
|
||||
* @expectedException RuntimeException
|
||||
* @return void
|
||||
*/
|
||||
function testReadWithNonExistantFile() {
|
||||
$reader = new PhpReader($this->path);
|
||||
$reader->read('fake_values');
|
||||
}
|
||||
}
|
9
cake/tests/test_app/config/var_test.php
Normal file
9
cake/tests/test_app/config/var_test.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
$config = array(
|
||||
'Read' => 'value',
|
||||
'Deep' => array(
|
||||
'Deeper' => array(
|
||||
'Deepest' => 'buried'
|
||||
)
|
||||
)
|
||||
);
|
Loading…
Reference in a new issue