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+ }
0 commit comments