| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Config; |
| 4 |
|
| 5 |
use RecursiveIteratorIterator; |
| 6 |
use RecursiveDirectoryIterator; |
| 7 |
use FluentForm\Framework\Foundation\Provider; |
| 8 |
|
| 9 |
class ConfigProvider extends Provider |
| 10 |
{ |
| 11 |
/** |
| 12 |
* The provider booting method to boot this provider |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public function booting() |
| 16 |
{ |
| 17 |
$config = new Config(array('app' => $this->app->getAppConfig())); |
| 18 |
|
| 19 |
$this->app->bindInstance( |
| 20 |
'config', $config, 'Config', 'FluentForm\Framework\Config\Config' |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* The provider booted method to be called after booting |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function booted() |
| 29 |
{ |
| 30 |
$this->loadConfig(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Loads all configuration files from config directory |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function loadConfig() |
| 38 |
{ |
| 39 |
$files = []; |
| 40 |
$configPath = $this->app->configPath(); |
| 41 |
$itr = new RecursiveIteratorIterator(new RecursiveDirectoryIterator( |
| 42 |
$configPath, RecursiveDirectoryIterator::SKIP_DOTS |
| 43 |
)); |
| 44 |
|
| 45 |
foreach($itr as $file) { |
| 46 |
if (pathinfo($file, PATHINFO_EXTENSION) == "php" && $file->getFileName() != 'app.php') { |
| 47 |
$fileRealPath = $file->getRealPath(); |
| 48 |
$directory = $this->getDirectory($file, $configPath); |
| 49 |
$this->app->config->set($directory.basename($fileRealPath, '.php'), include $fileRealPath); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get nested directory names joined by a "." |
| 56 |
* @param string $file [A config file] |
| 57 |
* @param string $configPath |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
protected function getDirectory($file, $configPath) |
| 61 |
{ |
| 62 |
$ds = DIRECTORY_SEPARATOR; |
| 63 |
|
| 64 |
if ($directory = trim(str_replace(trim($configPath, '/'), '', $file->getPath()), $ds)) { |
| 65 |
$directory = str_replace($ds, '.', $directory).'.'; |
| 66 |
} |
| 67 |
|
| 68 |
return $directory; |
| 69 |
} |
| 70 |
} |