Skip to content

Commit

Permalink
Fix "metadata/config" in service provider
Browse files Browse the repository at this point in the history
Added:
- Class `MockModule` to test container entry "module/classes"

Changed:
- Service "metadata/config" to improve logic processing "module/classes"
  • Loading branch information
mcaskill committed Feb 10, 2020
1 parent c77fcfa commit b0ef1b1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
35 changes: 20 additions & 15 deletions src/Charcoal/Model/ServiceProvider/ModelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,31 @@ protected function registerMetadataDependencies(Container $container)
$container['metadata/config'] = function (Container $container) {
$appConfig = isset($container['config']) ? $container['config'] : [];
$metaConfig = isset($appConfig['metadata']) ? $appConfig['metadata'] : null;
$metaConfig = new MetadataConfig($metaConfig);

$config = new MetadataConfig($metaConfig);
if (isset($container['module/classes'])) {
$extraPaths = [];
$basePath = rtrim($appConfig['base_path'], '/');
$modules = $container['module/classes'];
foreach ($modules as $module) {
if (defined(sprintf('%s::APP_CONFIG', $module))) {
$configPath = ltrim($module::APP_CONFIG, '/');
$configPath = $basePath.'/'.$configPath;

$extraMetadataPaths = [];
$basePath = $appConfig['base_path'];
foreach ($container['module/classes'] as $module) {
if (defined(sprintf('%s::APP_CONFIG', $module))) {
$moduleConfig = $module::APP_CONFIG;
$extraMetadataPaths = array_merge(
$extraMetadataPaths,
$config->loadFile($basePath.$moduleConfig)['metadata']['paths']
);
};
}
$configData = $metaConfig->loadFile($configPath);
$extraPaths = array_merge(
$extraPaths,
$configData['metadata']['paths']
);
};
}

if (!empty($extraMetadataPaths)) {
$config->addPaths($extraMetadataPaths);
if (!empty($extraPaths)) {
$metaConfig->addPaths($extraPaths);
}
}

return $config;
return $metaConfig;
};
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Charcoal/Mock/MockModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Charcoal\Tests\Mock;

/**
*
*/
class MockModule
{
const APP_CONFIG = 'tests/Charcoal/Mock/config.json';
}
7 changes: 7 additions & 0 deletions tests/Charcoal/Mock/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"metadata": {
"paths": [
"tests/Charcoal/Model/metadata"
]
}
}
53 changes: 36 additions & 17 deletions tests/Charcoal/Model/ServiceProvider/ModelServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,43 +76,41 @@ private function container()
{
$container = new Container();

$container['cache'] = new Pool(new Ephemeral());
$container['metadata/cache'] = new Pool(new Ephemeral());
$container['logger'] = new NullLogger();
$container['cache'] = new Pool(new Ephemeral());
$container['database'] = new PDO('sqlite::memory:');

$container['config'] = new AppConfig([
'metadata' => [
'paths' => []
]
'paths' => [],
],
]);
$container['database'] = new PDO('sqlite::memory:');
$container['logger'] = new NullLogger();

$container['view/loader'] = new PhpLoader([
'logger' => $container['logger'],
'base_path' => dirname(__DIR__),
'paths' => [ 'views' ]
'paths' => [ 'views' ],
]);

$container['view/engine'] = new PhpEngine([
'logger' => $container['logger'],
'loader' => $container['view/loader']
'loader' => $container['view/loader'],
]);

$container['view'] = new GenericView([
'logger' => $container['logger'],
'engine' => $container['view/engine']
'engine' => $container['view/engine'],
]);

$container['language/manager'] = new LocalesManager([
$container['locales/manager'] = new LocalesManager([
'locales' => [
'en' => [ 'locale' => 'en-US' ]
]
'en' => [
'locale' => 'en-US',
],
],
]);
$container['translator'] = new Translator([
'manager' => $container['language/manager']
]);

$container['metadata/config'] = new MetadataConfig([
'paths' => []
'manager' => $container['locales/manager'],
]);

return $container;
Expand Down Expand Up @@ -172,4 +170,25 @@ public function testRegisterSetsMetadataLoader()
$this->assertTrue(isset($container['metadata/loader']));
$this->assertInstanceOf(MetadataLoader::class, $container['metadata/loader']);
}

/**
* @return void
*/
public function testExtraMetadataPaths()
{
$container = new Container([
'config' => [
'base_path' => dirname(dirname(dirname(dirname(__DIR__)))),
],
'module/classes' => [
'Charcoal\\Tests\\Mock\\MockModule',
],
]);

$provider = new ModelServiceProvider();
$provider->register($container);

$metadataConfig = $container['metadata/config'];
$this->assertContains('tests/Charcoal/Model/metadata', $metadataConfig->paths());
}
}

0 comments on commit b0ef1b1

Please sign in to comment.