Skip to content

Commit 959f7d3

Browse files
committed
Initial commit of config component and config tests.
Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
1 parent 74bd7c7 commit 959f7d3

2 files changed

Lines changed: 193 additions & 0 deletions

File tree

components/config/repository.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php namespace Feather\Components\Config;
2+
3+
use Str;
4+
use Cache;
5+
use Event;
6+
use Config;
7+
8+
class Repository {
9+
10+
/**
11+
* Bootstrap the config repository, override the Laravel event for configuration
12+
* file loading so that Gear and Theme items can be picked up.
13+
*
14+
* @return void
15+
*/
16+
public function __construct()
17+
{
18+
Event::override(Config::loader, function($bundle, $file)
19+
{
20+
if(str_contains($file, ':'))
21+
{
22+
list($item, $file) = explode(':', $file);
23+
24+
// Configuration items that are colon separated are for Gears and Themes.
25+
// A requirement for these items is that they be prefixed with the name of
26+
// the Gear or Theme, followed by a space, followed by the configuration
27+
// item to fetch.
28+
list($name, $file) = explode(' ', $file);
29+
30+
$path = path(Str::plural($item)) . $name . DS . 'config' . DS . $file . EXT;
31+
32+
if(file_exists($path))
33+
{
34+
return require $path;
35+
}
36+
}
37+
38+
return Config::file($bundle, $file);
39+
});
40+
}
41+
42+
/**
43+
* Load and cache the database configuration items.
44+
*
45+
* @return void
46+
*/
47+
public function db()
48+
{
49+
Cache::forget('config');
50+
51+
$items = Cache::sear('config', function()
52+
{
53+
$config = array();
54+
55+
foreach(Models\Config::all() as $item)
56+
{
57+
array_set($config, $item->key, str_replace('(:feather)', '(:bundle)', $item->value));
58+
}
59+
60+
return $config;
61+
});
62+
}
63+
64+
/**
65+
* Determine if a key exists.
66+
*
67+
* @param string $key
68+
* @return bool
69+
*/
70+
public function has($key)
71+
{
72+
return !is_null($this->get($key));
73+
}
74+
75+
/**
76+
* Set a config key to a given value.
77+
*
78+
* @param string $key
79+
* @param mixed $value
80+
* @return void
81+
*/
82+
public function set($key, $value)
83+
{
84+
return Config::set($this->prefix($key), $value);
85+
}
86+
87+
/**
88+
* Get a key from the configuration.
89+
*
90+
* @param string $key
91+
* @param mixed $default
92+
* @return mixed
93+
*/
94+
public function get($key, $default = null)
95+
{
96+
return Config::get($this->prefix($key), $default);
97+
}
98+
99+
/**
100+
* Determine the prefix for a given key.
101+
*
102+
* @param string $key
103+
* @return string
104+
*/
105+
private function prefix($key)
106+
{
107+
if(!str_contains($key, ': '))
108+
{
109+
return $key;
110+
}
111+
112+
list($prefix, $key) = explode(': ', $key);
113+
114+
switch($prefix)
115+
{
116+
// The feather prefix is applied to both database and file-based
117+
// configuration items located within the bundle.
118+
case 'feather':
119+
return "feather::{$key}";
120+
break;
121+
122+
// The gear prefix is applied to Feather gears, the gear
123+
// name should appear before the file and key to be used.
124+
case 'gear':
125+
list($gear, $key) = explode(' ', $key);
126+
127+
return "feather::gear:{$gear} {$key}";
128+
break;
129+
default:
130+
return $key;
131+
}
132+
}
133+
134+
}

tests/config.test.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
class ConfigTest extends PHPUnit_Framework_TestCase {
4+
5+
public function setUp()
6+
{
7+
Bundle::start('feather');
8+
}
9+
10+
public function testItemsCanBeSet()
11+
{
12+
$config = $this->getRepository();
13+
14+
$config->set('name', 'jason');
15+
$this->assertEquals('jason', $config->get('name'));
16+
17+
$config->set('person.name', 'jason');
18+
$this->assertEquals('jason', $config->get('person.name'));
19+
20+
$config->set('namespace::person.name', 'jason');
21+
$this->assertEquals('jason', $config->get('namespace::person.name'));
22+
23+
$config->set('gear: mock person.name', 'jason');
24+
$this->assertEquals('jason', $config->get('gear: mock person.name'));
25+
26+
$config->set('theme: mock person.name', 'jason');
27+
$this->assertEquals('jason', $config->get('theme: mock person.name'));
28+
}
29+
30+
public function testGetBasicItems()
31+
{
32+
$config = $this->getRepository();
33+
34+
$this->assertEquals('bar', $config->get('test.foo'));
35+
$this->assertEquals('orange', $config->get('test.apple'));
36+
}
37+
38+
public function testEntireArrayCanBeReturned()
39+
{
40+
$config = $this->getRepository();
41+
42+
$this->assertEquals($this->getItems(), $config->get('test'));
43+
}
44+
45+
private function getRepository()
46+
{
47+
$config = new Feather\Components\Config\Repository;
48+
49+
$config->set('test', $this->getItems());
50+
51+
return $config;
52+
}
53+
54+
private function getItems()
55+
{
56+
return array('foo' => 'bar', 'apple' => 'orange');
57+
}
58+
59+
}

0 commit comments

Comments
 (0)