MichelPQueuePackage.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Michel\PQueue\Michel\Package;
  3. use Michel\Package\PackageInterface;
  4. use Michel\PQueue\Command\PQueueWorkerRunCommand;
  5. use Michel\PQueue\HandlerResolver\ContainerHandlerResolver;
  6. use Michel\PQueue\PQueueConsumer;
  7. use Michel\PQueue\PQueueConsumerFactory;
  8. use Michel\PQueue\PQueueHandlerFinder;
  9. use Michel\PQueue\Transport\FilesystemTransport;
  10. use Michel\PQueue\Transport\TransportInterface;
  11. use Psr\Container\ContainerInterface;
  12. class MichelPQueuePackage implements PackageInterface
  13. {
  14. public function getDefinitions(): array
  15. {
  16. return [
  17. TransportInterface::class => static function (ContainerInterface $container) {
  18. $class = $container->get('pqueue.transport.class');
  19. if (!is_subclass_of($class, TransportInterface::class)) {
  20. throw new \InvalidArgumentException("Invalid transport class provided");
  21. }
  22. $options = $container->get('pqueue.transport.options');
  23. return $class::create($options);
  24. },
  25. PQueueHandlerFinder::class => static function (ContainerInterface $container) {
  26. $sources = $container->has('pqueue.handlers.sources') ? $container->get('pqueue.handlers.sources') : [];
  27. $classes = $container->has('pqueue.handlers.classes') ? $container->get('pqueue.handlers.classes') : [];
  28. return new PQueueHandlerFinder(
  29. array_merge($classes, $sources),
  30. $container->get('michel.environment') === 'dev' ? null : $container->get('pqueue.handlers.cache_dir')
  31. );
  32. },
  33. PQueueConsumerFactory::class => static function (ContainerInterface $container) {
  34. /**
  35. * @var PQueueHandlerFinder $finder
  36. */
  37. $finder = $container->get(PQueueHandlerFinder::class);
  38. return new PQueueConsumerFactory(new ContainerHandlerResolver($container),$finder->find());
  39. },
  40. PQueueConsumer::class => static function (ContainerInterface $container) {
  41. /**
  42. * @var PQueueConsumerFactory $factory
  43. */
  44. $factory = $container->get(PQueueConsumerFactory::class);
  45. return $factory->createConsumer();
  46. }
  47. ];
  48. }
  49. public function getParameters(): array
  50. {
  51. return [
  52. 'pqueue.transport.class' => FilesystemTransport::class,
  53. 'pqueue.transport.options' => function (ContainerInterface $container) {
  54. return [
  55. 'directory' => $container->get('michel.project_dir').DIRECTORY_SEPARATOR.'var/pqueue'
  56. ];
  57. },
  58. 'pqueue.handlers.sources' => function (ContainerInterface $container) {
  59. return [
  60. $container->get('michel.project_dir').DIRECTORY_SEPARATOR.'src/MessageHandler'
  61. ];
  62. },
  63. 'pqueue.handlers.classes' => [],
  64. 'pqueue.handlers.cache_dir' => function (ContainerInterface $container) {
  65. return $container->get('michel.cache_dir');
  66. },
  67. ];
  68. }
  69. public function getRoutes(): array
  70. {
  71. return [];
  72. }
  73. public function getControllerSources(): array
  74. {
  75. return [];
  76. }
  77. public function getListeners(): array
  78. {
  79. return [];
  80. }
  81. public function getCommandSources(): array
  82. {
  83. return [
  84. PQueueWorkerRunCommand::class
  85. ];
  86. }
  87. }