Skip to content

Commit b0ef1b1

Browse files
committed
Fix "metadata/config" in service provider
Added: - Class `MockModule` to test container entry "module/classes" Changed: - Service "metadata/config" to improve logic processing "module/classes"
1 parent c77fcfa commit b0ef1b1

File tree

4 files changed

+74
-32
lines changed

4 files changed

+74
-32
lines changed

src/Charcoal/Model/ServiceProvider/ModelServiceProvider.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,26 +229,31 @@ protected function registerMetadataDependencies(Container $container)
229229
$container['metadata/config'] = function (Container $container) {
230230
$appConfig = isset($container['config']) ? $container['config'] : [];
231231
$metaConfig = isset($appConfig['metadata']) ? $appConfig['metadata'] : null;
232+
$metaConfig = new MetadataConfig($metaConfig);
232233

233-
$config = new MetadataConfig($metaConfig);
234+
if (isset($container['module/classes'])) {
235+
$extraPaths = [];
236+
$basePath = rtrim($appConfig['base_path'], '/');
237+
$modules = $container['module/classes'];
238+
foreach ($modules as $module) {
239+
if (defined(sprintf('%s::APP_CONFIG', $module))) {
240+
$configPath = ltrim($module::APP_CONFIG, '/');
241+
$configPath = $basePath.'/'.$configPath;
234242

235-
$extraMetadataPaths = [];
236-
$basePath = $appConfig['base_path'];
237-
foreach ($container['module/classes'] as $module) {
238-
if (defined(sprintf('%s::APP_CONFIG', $module))) {
239-
$moduleConfig = $module::APP_CONFIG;
240-
$extraMetadataPaths = array_merge(
241-
$extraMetadataPaths,
242-
$config->loadFile($basePath.$moduleConfig)['metadata']['paths']
243-
);
244-
};
245-
}
243+
$configData = $metaConfig->loadFile($configPath);
244+
$extraPaths = array_merge(
245+
$extraPaths,
246+
$configData['metadata']['paths']
247+
);
248+
};
249+
}
246250

247-
if (!empty($extraMetadataPaths)) {
248-
$config->addPaths($extraMetadataPaths);
251+
if (!empty($extraPaths)) {
252+
$metaConfig->addPaths($extraPaths);
253+
}
249254
}
250255

251-
return $config;
256+
return $metaConfig;
252257
};
253258
}
254259

tests/Charcoal/Mock/MockModule.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Charcoal\Tests\Mock;
4+
5+
/**
6+
*
7+
*/
8+
class MockModule
9+
{
10+
const APP_CONFIG = 'tests/Charcoal/Mock/config.json';
11+
}

tests/Charcoal/Mock/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"metadata": {
3+
"paths": [
4+
"tests/Charcoal/Model/metadata"
5+
]
6+
}
7+
}

tests/Charcoal/Model/ServiceProvider/ModelServiceProviderTest.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,43 +76,41 @@ private function container()
7676
{
7777
$container = new Container();
7878

79-
$container['cache'] = new Pool(new Ephemeral());
80-
$container['metadata/cache'] = new Pool(new Ephemeral());
79+
$container['logger'] = new NullLogger();
80+
$container['cache'] = new Pool(new Ephemeral());
81+
$container['database'] = new PDO('sqlite::memory:');
82+
8183
$container['config'] = new AppConfig([
8284
'metadata' => [
83-
'paths' => []
84-
]
85+
'paths' => [],
86+
],
8587
]);
86-
$container['database'] = new PDO('sqlite::memory:');
87-
$container['logger'] = new NullLogger();
8888

8989
$container['view/loader'] = new PhpLoader([
9090
'logger' => $container['logger'],
9191
'base_path' => dirname(__DIR__),
92-
'paths' => [ 'views' ]
92+
'paths' => [ 'views' ],
9393
]);
9494

9595
$container['view/engine'] = new PhpEngine([
9696
'logger' => $container['logger'],
97-
'loader' => $container['view/loader']
97+
'loader' => $container['view/loader'],
9898
]);
9999

100100
$container['view'] = new GenericView([
101101
'logger' => $container['logger'],
102-
'engine' => $container['view/engine']
102+
'engine' => $container['view/engine'],
103103
]);
104104

105-
$container['language/manager'] = new LocalesManager([
105+
$container['locales/manager'] = new LocalesManager([
106106
'locales' => [
107-
'en' => [ 'locale' => 'en-US' ]
108-
]
107+
'en' => [
108+
'locale' => 'en-US',
109+
],
110+
],
109111
]);
110112
$container['translator'] = new Translator([
111-
'manager' => $container['language/manager']
112-
]);
113-
114-
$container['metadata/config'] = new MetadataConfig([
115-
'paths' => []
113+
'manager' => $container['locales/manager'],
116114
]);
117115

118116
return $container;
@@ -172,4 +170,25 @@ public function testRegisterSetsMetadataLoader()
172170
$this->assertTrue(isset($container['metadata/loader']));
173171
$this->assertInstanceOf(MetadataLoader::class, $container['metadata/loader']);
174172
}
173+
174+
/**
175+
* @return void
176+
*/
177+
public function testExtraMetadataPaths()
178+
{
179+
$container = new Container([
180+
'config' => [
181+
'base_path' => dirname(dirname(dirname(dirname(__DIR__)))),
182+
],
183+
'module/classes' => [
184+
'Charcoal\\Tests\\Mock\\MockModule',
185+
],
186+
]);
187+
188+
$provider = new ModelServiceProvider();
189+
$provider->register($container);
190+
191+
$metadataConfig = $container['metadata/config'];
192+
$this->assertContains('tests/Charcoal/Model/metadata', $metadataConfig->paths());
193+
}
175194
}

0 commit comments

Comments
 (0)