container.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /**
  3. * Initial a dependency injection container that implemented PSR-11 and return the container.
  4. */
  5. declare(strict_types=1);
  6. /**
  7. * This file is part of Hyperf.
  8. *
  9. * @link https://www.hyperf.io
  10. * @document https://doc.hyperf.io
  11. * @contact [email protected]
  12. * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
  13. */
  14. use Hyperf\Config\ProviderConfig;
  15. use Hyperf\Di\Annotation\Scanner;
  16. use Hyperf\Di\Container;
  17. use Hyperf\Di\Definition\DefinitionSource;
  18. use Hyperf\Utils\ApplicationContext;
  19. $configFromProviders = ProviderConfig::load();
  20. $definitions = include __DIR__ . '/dependencies.php';
  21. $serverDependencies = array_replace($configFromProviders['dependencies'] ?? [], $definitions['dependencies'] ?? []);
  22. $annotations = include __DIR__ . '/autoload/annotations.php';
  23. $scanDirs = $configFromProviders['scan']['paths'];
  24. $scanDirs = array_merge($scanDirs, $annotations['scan']['paths'] ?? []);
  25. $ignoreAnnotations = $annotations['scan']['ignore_annotations'] ?? ['mixin'];
  26. $container = new Container(new DefinitionSource($serverDependencies, $scanDirs, new Scanner($ignoreAnnotations)));
  27. if (! $container instanceof \Psr\Container\ContainerInterface) {
  28. throw new RuntimeException('The dependency injection container is invalid.');
  29. }
  30. return ApplicationContext::setContainer($container);