Skip to content

Commit

Permalink
Merge pull request #387 from kaidesu/cached-config
Browse files Browse the repository at this point in the history
Properly load configuration files when cached
  • Loading branch information
kaidesu authored Dec 10, 2018
2 parents 23d6c59 + b72df4b commit 6ec838d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Cached config is now properly returned
- Properly reference default location when seeding modules

## [5.0.0] - 2018-11-30
### Added
Expand Down
52 changes: 46 additions & 6 deletions src/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Caffeinated\Modules\Support;

use SplFileInfo;
use Symfony\Component\Finder\Finder;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;

class ServiceProvider extends IlluminateServiceProvider
Expand Down Expand Up @@ -45,12 +47,11 @@ protected function addMiddleware($middleware)
*/
protected function loadConfigsFrom($path)
{
// skip config if cache
if (file_exists($this->app->getCachedConfigPath())) {
foreach (glob($path.'/*.php') as $file) {
$fileName = str_replace($path.'/', '', $file);
$key = substr($fileName, 0, -4);
$this->app['config']->set($key, array_merge_recursive(config($key, []), require $file));
if (! $this->app->configurationIsCached()) {
$files = $this->getConfigurationFiles($path);

foreach ($files as $key => $path) {
config()->set($key, require $path);
}
}
}
Expand All @@ -68,4 +69,43 @@ protected function loadFactoriesFrom($path)
require $file;
}
}

/**
* Get all of the configuration files for the application.
*
* @param string $path
* @return array
*/
private function getConfigurationFiles($path) {
$files = [];
$configPath = realpath($path);

foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) {
$directory = $this->getNestedDirectory($file, $configPath);

$files[$directory.basename($file->getRealPath(), '.php')] = $file->getRealPath();
}

ksort($files, SORT_NATURAL);

return $files;
}

/**
* Get the configuration file nesting path.
*
* @param \SplFileInfo $file
* @param string $configPath
* @return string
*/
protected function getNestedDirectory(SplFileInfo $file, $configPath)
{
$directory = $file->getPath();

if ($nested = trim(str_replace($configPath, '', $directory), DIRECTORY_SEPARATOR)) {
$nested = str_replace(DIRECTORY_SEPARATOR, '.', $nested).'.';
}

return $nested;
}
}

0 comments on commit 6ec838d

Please sign in to comment.