Navigation Menu

Skip to content

Commit

Permalink
Adding accessor patterns to Libraries::get().
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Sep 13, 2012
1 parent c8545ff commit b27096c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
53 changes: 38 additions & 15 deletions core/Libraries.php
Expand Up @@ -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;
}
}
}

/**
Expand Down
53 changes: 26 additions & 27 deletions tests/cases/core/LibrariesTest.php
Expand Up @@ -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\\',
Expand All @@ -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');
Expand All @@ -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.");
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 ``.');
Expand All @@ -251,8 +258,6 @@ public function testLibraryLoad() {

/**
* Tests path caching by calling `path()` twice.
*
* @return void
*/
public function testPathCaching() {
$this->assertFalse(Libraries::cache(false));
Expand All @@ -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(
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -695,4 +694,4 @@ class LibTest{ public function testMe() {
}
}

?>
?>

0 comments on commit b27096c

Please sign in to comment.