22
33namespace JoliCode \MediaBundle \Library ;
44
5+ use Psr \Log \LoggerInterface ;
56use Symfony \Component \DependencyInjection \ServiceLocator ;
67
78class 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