Skip to content

Commit 8789d9d

Browse files
authored
Merge pull request #68 from jolicode/log-when-no-library-defined
log a warning when no library is defined
2 parents e2b6a8c + f55e0aa commit 8789d9d

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [0.1.4] - 2024-11-23
4+
5+
- fix - do not trigger an error when no library is defined
6+
37
## [0.1.3] - 2024-11-23
48

59
- fix - `Request::get()` deprecation
@@ -47,3 +51,4 @@ This is the initial release of the bundle.
4751
[0.1.1]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.1
4852
[0.1.2]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.2
4953
[0.1.3]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.3
54+
[0.1.4]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.4

config/services.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@
206206
// library
207207
->set('joli_media.library_container', LibraryContainer::class)
208208
->public()
209-
->arg('$libraries', tagged_locator('joli_media.library', indexAttribute: 'name'))
209+
->args([
210+
'$libraries' => tagged_locator('joli_media.library', indexAttribute: 'name'),
211+
'$logger' => service('logger')->ignoreOnInvalid(),
212+
])
210213
->alias(LibraryContainer::class, 'joli_media.library_container')
211214

212215
->set('.joli_media.library.abstract', Library::class)

src/JoliMediaBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
9797

9898
// define the default library name
9999
$builder->getDefinition('joli_media.library_container')
100-
->setArgument('$defaultLibraryName', $config['default_library'] ?? array_key_first($config['libraries']))
100+
->setArgument('$defaultLibraryName', $config['default_library'])
101101
;
102102

103103
// pre-processors

src/Library/LibraryContainer.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22

33
namespace JoliCode\MediaBundle\Library;
44

5+
use Psr\Log\LoggerInterface;
56
use Symfony\Component\DependencyInjection\ServiceLocator;
67

78
class LibraryContainer
89
{
910
public function __construct(
1011
private readonly ServiceLocator $libraries,
11-
private string $defaultLibraryName,
12+
private ?string $defaultLibraryName = null,
13+
private readonly ?LoggerInterface $logger = null,
1214
) {
13-
if (!$this->has($defaultLibraryName)) {
14-
throw new \InvalidArgumentException(\sprintf('Library "%s" not found.', $defaultLibraryName));
15+
if (0 === $libraries->count()) {
16+
$this->logger?->warning('No library has been defined in the MediaBundle configuration. Please add one to be able to use the bundle features.');
17+
}
18+
19+
if (null === $this->defaultLibraryName && $libraries->count() > 0) {
20+
$names = array_keys($libraries->getProvidedServices());
21+
$this->defaultLibraryName = $names[0];
22+
}
23+
24+
if (null !== $this->defaultLibraryName && !$this->has($this->defaultLibraryName)) {
25+
throw new \InvalidArgumentException(\sprintf('Library "%s" not found.', $this->defaultLibraryName));
1526
}
1627
}
1728

@@ -30,11 +41,19 @@ public function get(?string $name = null): Library
3041

3142
public function getDefault(): Library
3243
{
44+
if (null === $this->defaultLibraryName) {
45+
throw new \InvalidArgumentException('The default library is not defined');
46+
}
47+
3348
return $this->get($this->defaultLibraryName);
3449
}
3550

3651
public function getDefaultName(): string
3752
{
53+
if (null === $this->defaultLibraryName) {
54+
throw new \InvalidArgumentException('The default library is not defined');
55+
}
56+
3857
return $this->defaultLibraryName;
3958
}
4059

0 commit comments

Comments
 (0)