Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding accessor patterns to `Libraries::get()`.

  • Loading branch information...
commit b27096cc6f136b658208639cef3098e00022e8d8 1 parent c8545ff
@nateabele nateabele authored
Showing with 64 additions and 42 deletions.
  1. +38 −15 core/Libraries.php
  2. +26 −27 tests/cases/core/LibrariesTest.php
View
53 core/Libraries.php
@@ -337,37 +337,60 @@ protected static function _configure($config) {
}
/**
- * Returns configuration for given name.
+ * Allows library information to be retrieved in various ways, including:
*
- * @param string $name Registered library to retrieve configuration for.
- * @param string $key Optional key name. If `$name` is set and is the name of a valid library,
- * returns the given named configuration key, i.e. `'path'`, `'webroot'` or
- * `'resources'`.
+ * By name:
+ * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(1-1) }}}
+ *
+ * With no parameters, to return all configuration for all libraries:
+ * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(22-22) }}}
+ *
+ * By list of names with a key to extract:
+ * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(34-34) }}}
+ *
+ * With no name, and a key to extract, to return a key/value array, where the library name is
+ * the key, and the `$key` value is the value:
+ * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(37-37) }}}
+ *
+ * By containing class name:
+ * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(45-45) }}}
+ *
+ * @param mixed $name Either the name of a library added in `Libraries::add()`, an array of
+ * library names, or a fully-namespaced class name (see usage examples above).
+ * @param string $key Optional key name. If `$name` is set and is the name of a valid library
+ * (or an array of valid libraries), returns the given named configuration key,
+ * i.e. `'path'`, `'webroot'` or `'resources'`.
* @return mixed A configuation array for one or more libraries, or a string value if `$key` is
- * specified.
+ * specified and `$name` is a string, or a library name (string) if `$name` is a
+ * fully-namespaced class name.
*/
public static function get($name = null, $key = null) {
$configs = static::$_configurations;
- if (!$name) {
+ if (!$name && !$key) {
return $configs;
}
if ($name === true) {
$name = static::$_default;
}
- if (is_array($name)) {
- foreach ($name as $i => $key) {
- unset($name[$i]);
- $name[$key] = isset($configs[$key]) ? $configs[$key] : null;
- }
- return $name;
+ if (is_array($name) || (!$name && $key)) {
+ $name = $name ?: array_keys(static::$_configurations);
+ $call = array(get_called_class(), 'get');
+ return array_combine($name, array_map($call, $name, array_fill(0, count($name), $key)));
}
$config = isset($configs[$name]) ? $configs[$name] : null;
- if (!$key) {
+ if ($key) {
+ return isset($config[$key]) ? $config[$key] : null;
+ }
+ if (strpos($name, '\\') === false) {
return $config;
}
- return isset($config[$key]) ? $config[$key] : null;
+ foreach (static::$_configurations as $library => $config) {
+ if ($config['prefix'] && strpos($name, $config['prefix']) === 0) {
+ return $library;
+ }
+ }
}
/**
View
53 tests/cases/core/LibrariesTest.php
@@ -101,11 +101,9 @@ public function testPathFiltering() {
/**
* Tests accessing library configurations.
- *
- * @return void
*/
public function testLibraryConfigAccess() {
- $result = Libraries::get('lithium');
+ $config = Libraries::get('lithium'); // => ['path' => '/path/to/lithium', ...]
$expected = array(
'path' => str_replace('\\', '/', realpath(realpath(LITHIUM_LIBRARY_PATH) . '/lithium')),
'prefix' => 'lithium\\',
@@ -123,22 +121,39 @@ public function testLibraryConfigAccess() {
$expected['default'] = true;
}
- $this->assertEqual($expected, $result);
+ $this->assertEqual($expected, $config);
$this->assertNull(Libraries::get('foo'));
- $result = Libraries::get();
- $this->assertTrue(isset($result['lithium']));
- $this->assertEqual($expected, $result['lithium']);
+ $configs = Libraries::get(); // => ['lithium' => ['path' => ...], 'myapp' => [...], ...]
+ $this->assertTrue(isset($configs['lithium']));
+ $this->assertEqual($expected, $configs['lithium']);
if ($this->hasApp) {
- $this->assertTrue(isset($result['app']));
+ $this->assertTrue(isset($configs['app']));
}
+
+ $configs = Libraries::get(array('lithium')); // => ['lithium' => ['path' => '...', ...]]
+ $this->assertEqual(array('lithium'), array_keys($configs));
+ $this->assertEqual($expected, $configs['lithium']);
+
+ $prefixes = Libraries::get(array('lithium'), 'prefix'); // => ['lithium' => 'lithium\\']
+ $this->assertEqual(array('lithium' => 'lithium\\'), $prefixes);
+
+ $allPre = Libraries::get(null, 'prefix'); // => ['my' => 'my\\', 'lithium' => 'lithium\\']
+ $this->assertTrue($allPre);
+ $this->assertEqual(array_keys(Libraries::get()), array_keys($allPre));
+
+ foreach ($allPre as $prefix) {
+ $this->assertTrue(is_string($prefix) || is_bool($prefix));
+ }
+
+ $library = Libraries::get('lithium\core\Libraries'); // 'lithium'
+ $this->assertEqual('lithium', $library);
+ $this->assertNull(Libraries::get('foo\bar\baz'));
}
/**
* Tests the addition and removal of default libraries.
- *
- * @return void
*/
public function testLibraryAddRemove() {
$lithium = Libraries::get('lithium');
@@ -164,8 +179,6 @@ public function testLibraryAddRemove() {
/**
* Tests that an exception is thrown when a library is added which could not be found.
- *
- * @return void
*/
public function testAddInvalidLibrary() {
$this->expectException("Library `invalid_foo` not found.");
@@ -174,8 +187,6 @@ public function testAddInvalidLibrary() {
/**
* Tests that non-prefixed (poorly named or structured) libraries can still be added.
- *
- * @return void
*/
public function testAddNonPrefixedLibrary() {
$tmpDir = realpath(Libraries::get(true, 'resources') . '/tmp');
@@ -212,8 +223,6 @@ public function testAddNonPrefixedLibrary() {
/**
* Tests that non-class files are always filtered out of `find()` results unless an alternate
* filter is specified.
- *
- * @return void
*/
public function testExcludeNonClassFiles() {
$result = Libraries::find('lithium');
@@ -241,8 +250,6 @@ public function testExcludeNonClassFiles() {
/**
* Tests the loading of libraries
- *
- * @return void
*/
public function testLibraryLoad() {
$this->expectException('Failed to load class `SomeInvalidLibrary` from path ``.');
@@ -251,8 +258,6 @@ public function testLibraryLoad() {
/**
* Tests path caching by calling `path()` twice.
- *
- * @return void
*/
public function testPathCaching() {
$this->assertFalse(Libraries::cache(false));
@@ -275,8 +280,6 @@ public function testCacheControl() {
/**
* Tests recursive and non-recursive searching through libraries with paths.
- *
- * @return void
*/
public function testFindingClasses() {
$result = Libraries::find('lithium', array(
@@ -372,8 +375,6 @@ public function testServiceLocateAllCommands() {
/**
* Tests locating service objects. These tests may fail if not run on a stock install, as other
* objects may preceed the core objects in load order.
- *
- * @return void
*/
public function testServiceLocation() {
$this->assertNull(Libraries::locate('adapter', 'File'));
@@ -586,8 +587,6 @@ class UserTest extends \\lithium\\test\\Unit { public function testMe() {
/**
* Tests that `Libraries::realPath()` correctly resolves paths to files inside Phar archives.
- *
- * @return void
*/
public function testPathsInPharArchives() {
$base = Libraries::get('lithium', 'path');
@@ -695,4 +694,4 @@ class LibTest{ public function testMe() {
}
}
-?>
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.