Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect config during initial creation #467

Merged
merged 5 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions jigsaw-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@

$container->bind('config', function ($c) use ($cachePath) {
$config = (new ConfigFile($c['cwd'] . '/config.php', $c['cwd'] . '/helpers.php'))->config;
$config['view.compiled'] = $cachePath;

$config->put('view.compiled', $cachePath);
return $config;
});

Expand Down
2 changes: 1 addition & 1 deletion src/Console/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function includeEnvironmentConfig($env)
$environmentConfigPath = $this->getAbsolutePath("config.{$env}.php");
$environmentConfig = (new ConfigFile($environmentConfigPath))->config;

$this->app->config = collect($this->app->config)
$this->app->config = $this->app->config
->merge(collect($environmentConfig))
->filter(function ($item) {
return $item !== null;
Expand Down
12 changes: 5 additions & 7 deletions src/File/ConfigFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

namespace TightenCo\Jigsaw\File;

use Illuminate\Support\Arr;

class ConfigFile
{
public $config;

public function __construct($config_path, $helpers_path = '')
public function __construct(string $config_path, string $helpers_path = '')
{
$config = file_exists($config_path) ? include $config_path : [];
$helpers = file_exists($helpers_path) ? include $helpers_path : [];

$this->config = array_merge($config, $helpers);
$this->config = collect($config)->merge($helpers);
$this->convertStringCollectionsToArray();
}

protected function convertStringCollectionsToArray()
{
$collections = Arr::get($this->config, 'collections');
$collections = $this->config->get('collections');

if ($collections) {
$this->config['collections'] = collect($collections)->flatMap(function ($value, $key) {
$this->config->put('collections', collect($collections)->flatMap(function ($value, $key) {
return is_array($value) ? [$key => $value] : [$value => []];
});
}));
}
}
}
2 changes: 1 addition & 1 deletion src/Handlers/CollectionItemHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CollectionItemHandler
private $config;
private $handlers;

public function __construct($config, $handlers)
public function __construct(Collection $config, $handlers)
{
$this->config = $config;
$this->handlers = collect($handlers);
Expand Down
3 changes: 2 additions & 1 deletion src/Loaders/DataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TightenCo\Jigsaw\Loaders;

use Illuminate\Support\Collection;
use TightenCo\Jigsaw\SiteData;

class DataLoader
Expand All @@ -13,7 +14,7 @@ public function __construct(CollectionDataLoader $collectionDataLoader)
$this->collectionDataLoader = $collectionDataLoader;
}

public function loadSiteData($config)
public function loadSiteData(Collection $config)
{
return SiteData::build($config);
}
Expand Down
5 changes: 3 additions & 2 deletions src/View/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TightenCo\Jigsaw\View;

use Illuminate\Support\Collection;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Factory;

Expand All @@ -21,9 +22,9 @@ class ViewRenderer
'js', 'json', 'xml', 'rss', 'atom', 'txt', 'text', 'html',
];

public function __construct(Factory $viewFactory, BladeCompiler $bladeCompiler, $config = [])
public function __construct(Factory $viewFactory, BladeCompiler $bladeCompiler, Collection $config = null)
{
$this->config = collect($config);
$this->config = $config ?? collect();
Comment on lines +25 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what the most idiomatic way to convert this from an array => collection was with the default value. As far as I can tell the only place its ever called without passing a config is in the ViewRendererTest.php with it_does_not_register_view_hint_paths_if_not_specified_in_config I just didn't want to remove it incase I missed something.

$this->viewFactory = $viewFactory;
$this->bladeCompiler = $bladeCompiler;
$this->finder = $this->viewFactory->getFinder();
Expand Down
8 changes: 4 additions & 4 deletions tests/ViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function it_registers_view_hint_paths()
$mock->shouldReceive('getFinder');
$mock->shouldReceive('addNamespace')->with('view::hint', 'path');
$mock->shouldReceive('addExtension');
new ViewRenderer($mock, Mockery::mock(BladeCompiler::class), [
new ViewRenderer($mock, Mockery::mock(BladeCompiler::class), collect([
'viewHintPaths' => [
'view::hint' => 'path'
]
]);
]));

$this->addToAssertionCount(
Mockery::getContainer()->mockery_getExpectationCount()
Expand Down Expand Up @@ -54,9 +54,9 @@ public function it_does_not_register_view_hint_paths_if_empty_in_config()
$mock->shouldReceive('getFinder');
$mock->shouldNotReceive('addNamespace')->with('view::hint', 'path');
$mock->shouldReceive('addExtension');
new ViewRenderer($mock, Mockery::mock(BladeCompiler::class), [
new ViewRenderer($mock, Mockery::mock(BladeCompiler::class), collect([
'viewHintPaths' => []
]);
]));

$this->addToAssertionCount(
Mockery::getContainer()->mockery_getExpectationCount()
Expand Down