Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.1.4] - 2024-11-23
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

My gut tells me it's 2025 ;-)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh noes 🤦 🙂


- fix - do not trigger an error when no library is defined

## [0.1.3] - 2024-11-23

- fix - `Request::get()` deprecation
Expand Down Expand Up @@ -47,3 +51,4 @@ This is the initial release of the bundle.
[0.1.1]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.1
[0.1.2]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.2
[0.1.3]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.3
[0.1.4]: https://github.com/jolicode/mediabundle/releases/tag/v0.1.4
5 changes: 4 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@
// library
->set('joli_media.library_container', LibraryContainer::class)
->public()
->arg('$libraries', tagged_locator('joli_media.library', indexAttribute: 'name'))
->args([
'$libraries' => tagged_locator('joli_media.library', indexAttribute: 'name'),
'$logger' => service('logger')->ignoreOnInvalid(),
])
->alias(LibraryContainer::class, 'joli_media.library_container')

->set('.joli_media.library.abstract', Library::class)
Expand Down
2 changes: 1 addition & 1 deletion src/JoliMediaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function loadExtension(array $config, ContainerConfigurator $container, C

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

// pre-processors
Expand Down
25 changes: 22 additions & 3 deletions src/Library/LibraryContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@

namespace JoliCode\MediaBundle\Library;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;

class LibraryContainer
{
public function __construct(
private readonly ServiceLocator $libraries,
private string $defaultLibraryName,
private ?string $defaultLibraryName = null,
private readonly ?LoggerInterface $logger = null,
) {
if (!$this->has($defaultLibraryName)) {
throw new \InvalidArgumentException(\sprintf('Library "%s" not found.', $defaultLibraryName));
if (0 === $libraries->count()) {
$this->logger?->warning('No library has been defined in the MediaBundle configuration. Please add one to be able to use the bundle features.');
}

if (null === $this->defaultLibraryName && $libraries->count() > 0) {
$names = array_keys($libraries->getProvidedServices());
$this->defaultLibraryName = $names[0];
}

if (null !== $this->defaultLibraryName && !$this->has($this->defaultLibraryName)) {
throw new \InvalidArgumentException(\sprintf('Library "%s" not found.', $this->defaultLibraryName));
}
}

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

public function getDefault(): Library
{
if (null === $this->defaultLibraryName) {
throw new \InvalidArgumentException('The default library is not defined');
}

return $this->get($this->defaultLibraryName);
}

public function getDefaultName(): string
{
if (null === $this->defaultLibraryName) {
throw new \InvalidArgumentException('The default library is not defined');
}

return $this->defaultLibraryName;
}

Expand Down