Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a get() method to titon\libs\bundles\Bundle handle the responsi…

…bility of getting data

Rewrote hasFile(), loadFile() and parseFile() in titon\libs\bundles\Bundle to require a filename (with extension) as an argument, instead of a key
  • Loading branch information...
commit 782eafcf9c885b20eb7627a7025560b1ecaf845b 1 parent 7650392
@milesj milesj authored
View
4 tests/titon/libs/bundles/messages/core/IniMessageBundleTest.php
@@ -48,7 +48,7 @@ public function testBundleDetection() {
* Test that loading messages from a catalog work.
*/
public function testMessageLoading() {
- $messages = $this->object->loadFile('default');
+ $messages = $this->object->get('default');
$this->assertTrue(is_array($messages));
$this->assertEquals(array('titon', 'test', 'type'), array_keys($messages));
@@ -58,7 +58,7 @@ public function testMessageLoading() {
'type' => 'ini'
), $messages);
- $messages = $this->object->loadFile('doesntExist');
+ $messages = $this->object->get('doesntExist');
$this->assertTrue(is_array($messages));
$this->assertEmpty($messages);
View
4 tests/titon/libs/bundles/messages/core/JsonMessageBundleTest.php
@@ -48,7 +48,7 @@ public function testBundleDetection() {
* Test that loading messages from a catalog work.
*/
public function testMessageLoading() {
- $messages = $this->object->loadFile('default');
+ $messages = $this->object->get('default');
$this->assertTrue(is_array($messages));
$this->assertEquals(array('titon', 'test', 'type'), array_keys($messages));
@@ -58,7 +58,7 @@ public function testMessageLoading() {
'type' => 'json'
), $messages);
- $messages = $this->object->loadFile('doesntExist');
+ $messages = $this->object->get('doesntExist');
$this->assertTrue(is_array($messages));
$this->assertEmpty($messages);
View
4 tests/titon/libs/bundles/messages/core/PhpMessageBundleTest.php
@@ -48,7 +48,7 @@ public function testBundleDetection() {
* Test that loading messages from a catalog work.
*/
public function testMessageLoading() {
- $messages = $this->object->loadFile('default');
+ $messages = $this->object->get('default');
$this->assertTrue(is_array($messages));
$this->assertEquals(array('titon', 'test', 'type'), array_keys($messages));
@@ -58,7 +58,7 @@ public function testMessageLoading() {
'type' => 'php'
), $messages);
- $messages = $this->object->loadFile('doesntExist');
+ $messages = $this->object->get('doesntExist');
$this->assertTrue(is_array($messages));
$this->assertEmpty($messages);
View
4 tests/titon/libs/bundles/messages/core/XmlMessageBundleTest.php
@@ -48,7 +48,7 @@ public function testBundleDetection() {
* Test that loading messages from a catalog work.
*/
public function testMessageLoading() {
- $messages = $this->object->loadFile('default');
+ $messages = $this->object->get('default');
$this->assertTrue(is_array($messages));
$this->assertEquals(array('titon', 'test', 'type'), array_keys($messages));
@@ -58,7 +58,7 @@ public function testMessageLoading() {
'type' => 'xml'
), $messages);
- $messages = $this->object->loadFile('doesntExist');
+ $messages = $this->object->get('doesntExist');
$this->assertTrue(is_array($messages));
$this->assertEmpty($messages);
View
4 tests/titon/libs/bundles/messages/gettext/PoMessageBundleTest.php
@@ -48,7 +48,7 @@ public function testBundleDetection() {
* Test that loading messages from a catalog work.
*/
public function testMessageLoading() {
- $messages = $this->object->loadFile('default');
+ $messages = $this->object->get('default');
$this->assertTrue(is_array($messages));
$this->assertEquals(array('basic', 'multiline', 'plurals', 'context'), array_keys($messages));
@@ -59,7 +59,7 @@ public function testMessageLoading() {
'context' => 'Context message'
), $messages);
- $messages = $this->object->loadFile('doesntExist');
+ $messages = $this->object->get('doesntExist');
$this->assertTrue(is_array($messages));
$this->assertEmpty($messages);
View
21 vendors/titon/libs/bundles/Bundle.php
@@ -26,6 +26,15 @@
public function findBundle(array $locations);
/**
+ * Return data based on a key. If data does not exist, load it from the file that matches the key.
+ *
+ * @access public
+ * @param string $key
+ * @return array
+ */
+ public function get($key);
+
+ /**
* List of all filenames within the resource bundle.
*
* @access public
@@ -53,28 +62,28 @@ public function getPath();
* Check if the file exists within the bundle.
*
* @access public
- * @param $key
+ * @param string $filename
* @return boolean
*/
- public function hasFile($key);
+ public function hasFile($filename);
/**
* Load the file from the resource bundle and parse its contents.
* If file does not exist, return an empty array.
*
* @access public
- * @param string $key
+ * @param string $filename
* @return array
*/
- public function loadFile($key);
+ public function loadFile($filename);
/**
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
*/
- public function parseFile($path);
+ public function parseFile($filename);
}
View
43 vendors/titon/libs/bundles/BundleAbstract.php
@@ -73,6 +73,25 @@ public function findBundle(array $locations) {
}
/**
+ * Return data based on a key. If data does not exist, load it from the file that matches the key.
+ *
+ * @access public
+ * @param string $key
+ * @return array
+ */
+ public function get($key) {
+ if (isset($this->_config[$key])) {
+ return $this->_config[$key];
+ }
+
+ $filename = Inflector::filename($key, static::EXT, false);
+
+ $this->_config[$key] = $this->loadFile($filename);
+
+ return $this->_config[$key];
+ }
+
+ /**
* List of all filenames within the resource bundle.
*
* @access public
@@ -110,11 +129,11 @@ public function getPath() {
* Check if the file exists within the bundle.
*
* @access public
- * @param $key
+ * @param string $filename
* @return boolean
*/
- public function hasFile($key) {
- return in_array(Inflector::filename($key, static::EXT, false), $this->getFiles());
+ public function hasFile($filename) {
+ return in_array($filename, $this->getFiles());
}
/**
@@ -122,22 +141,16 @@ public function hasFile($key) {
* If file does not exist, return an empty array.
*
* @access public
- * @param string $key
+ * @param string $filename
* @return array
*/
- public function loadFile($key) {
- if (isset($this->_config[$key])) {
- return $this->_config[$key];
- }
-
- if ($this->hasFile($key)) {
- $data = $this->parseFile($this->getPath() . Inflector::filename($key, static::EXT, false));
+ public function loadFile($filename) {
+ if ($this->hasFile($filename)) {
+ $data = $this->parseFile($filename);
} else {
$data = array();
}
- $this->_config[$key] = $data;
-
return $data;
}
@@ -145,11 +158,11 @@ public function loadFile($key) {
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
* @throws titon\libs\bundles\BundleException
*/
- public function parseFile($path) {
+ public function parseFile($filename) {
throw new BundleException(sprintf('You must define the parseFile() method within your %s.', get_class($this)));
}
View
18 vendors/titon/libs/bundles/locales/LocaleBundle.php
@@ -50,7 +50,7 @@ class LocaleBundle extends BundleAbstract {
* @return array
*/
public function getInflections() {
- return $this->loadFile('inflections');
+ return $this->get('inflections');
}
/**
@@ -60,7 +60,7 @@ public function getInflections() {
* @return array
*/
public function getLocale() {
- return $this->loadFile('locale');
+ return $this->get('locale');
}
/**
@@ -80,7 +80,7 @@ public function getParent() {
* @return array
*/
public function getValidations() {
- return $this->loadFile('validations');
+ return $this->get('validations');
}
/**
@@ -120,15 +120,15 @@ public function initialize() {
* @param string $key
* @return array
*/
- public function loadFile($key) {
+ public function get($key) {
if (isset($this->_config[$key])) {
return $this->_config[$key];
}
- $data = parent::loadFile($key);
+ $data = parent::get($key);
if ($parent = $this->getParent()) {
- $data = $data + $parent->loadFile($key);
+ $data = $data + $parent->get($key);
}
$this->_config[$key] = $data;
@@ -140,11 +140,11 @@ public function loadFile($key) {
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
*/
- public function parseFile($path) {
- return include_once $path;
+ public function parseFile($filename) {
+ return include_once $this->getPath() . $filename;
}
}
View
6 vendors/titon/libs/bundles/messages/core/IniMessageBundle.php
@@ -29,11 +29,11 @@ class IniMessageBundle extends MessageBundleAbstract {
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
*/
- public function parseFile($path) {
- return parse_ini_file($path, false, INI_SCANNER_NORMAL);
+ public function parseFile($filename) {
+ return parse_ini_file($this->getPath() . $filename, false, INI_SCANNER_NORMAL);
}
}
View
6 vendors/titon/libs/bundles/messages/core/JsonMessageBundle.php
@@ -29,11 +29,11 @@ class JsonMessageBundle extends MessageBundleAbstract {
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
*/
- public function parseFile($path) {
- return json_decode(file_get_contents($path), true);
+ public function parseFile($filename) {
+ return json_decode(file_get_contents($this->getPath() . $filename), true);
}
}
View
6 vendors/titon/libs/bundles/messages/core/PhpMessageBundle.php
@@ -29,11 +29,11 @@ class PhpMessageBundle extends MessageBundleAbstract {
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
*/
- public function parseFile($path) {
- return include_once $path;
+ public function parseFile($filename) {
+ return include_once $this->getPath() . $filename;
}
}
View
6 vendors/titon/libs/bundles/messages/core/XmlMessageBundle.php
@@ -29,11 +29,11 @@ class XmlMessageBundle extends MessageBundleAbstract {
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
*/
- public function parseFile($path) {
- $xml = simplexml_load_file($path);
+ public function parseFile($filename) {
+ $xml = simplexml_load_file($this->getPath() . $filename);
$array = array();
foreach ($xml->children() as $key => $value) {
View
8 vendors/titon/libs/bundles/messages/gettext/PoMessageBundle.php
@@ -40,11 +40,11 @@ public function initialize() {
* Parse the file at the given path and return the result.
*
* @access public
- * @param $path
+ * @param string $filename
* @return array
*/
- public function parseFile($path) {
- $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ public function parseFile($filename) {
+ $lines = file($this->getPath() . $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$data = array();
$key = '';
$value = '';
@@ -101,7 +101,7 @@ public function parseFile($path) {
* Remove the quotes from a message string.
*
* @access public
- * @param $string
+ * @param string $string
* @return string
*/
protected function _dequote($string) {
Please sign in to comment.
Something went wrong with that request. Please try again.