diff --git a/Base/JobResolverInterface.php b/Base/JobResolverInterface.php index 3c04c56..0ad9e7b 100644 --- a/Base/JobResolverInterface.php +++ b/Base/JobResolverInterface.php @@ -10,11 +10,19 @@ interface JobResolverInterface { /** - * Resolve job by class + * Resolve job by id * - * @param string $class + * @param string $id * * @return JobInterface */ - public function resolve(string $class): JobInterface; + public function resolve(string $id): JobInterface; + + /** + * Add new job + * + * @param string $id + * @param JobInterface $job + */ + public function addJob(string $id, JobInterface $job); } diff --git a/DependencyInjection/Compiler/JobCompilerPass.php b/DependencyInjection/Compiler/JobCompilerPass.php index 93f8e4f..34fbd84 100644 --- a/DependencyInjection/Compiler/JobCompilerPass.php +++ b/DependencyInjection/Compiler/JobCompilerPass.php @@ -2,9 +2,11 @@ namespace SfCod\QueueBundle\DependencyInjection\Compiler; +use SfCod\QueueBundle\Base\JobResolverInterface; use SfCod\QueueBundle\Service\JobQueue; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; /** * Class JobCompilerPass @@ -20,15 +22,15 @@ class JobCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - if (!$container->has(JobQueue::class)) { + if (!$container->hasDefinition(JobResolverInterface::class)) { return; } + $jobResolver = $container->getDefinition(JobResolverInterface::class); $taggedServices = $container->findTaggedServiceIds('sfcod.jobqueue.job'); foreach ($taggedServices as $id => $tags) { - $definition = $container->findDefinition($id); - $definition->setPublic(true); + $jobResolver->addMethodCall('addJob', [$id, new Reference($id)]); } } } \ No newline at end of file diff --git a/DependencyInjection/QueueExtension.php b/DependencyInjection/QueueExtension.php index 3bc0955..c3099f1 100644 --- a/DependencyInjection/QueueExtension.php +++ b/DependencyInjection/QueueExtension.php @@ -57,7 +57,8 @@ public function load(array $config, ContainerBuilder $container) $definition ->setAutowired(true) ->setAutoconfigured(true) - ->setPublic(true); + ->setPublic(true) + ->addTag('sfcod.jobqueue.job'); $container->setDefinition($job, $definition); } @@ -147,9 +148,6 @@ private function createManager(array $config, ContainerBuilder $container) { $resolver = new Definition(JobResolverInterface::class); $resolver->setClass(JobResolver::class); - $resolver->setArguments([ - new Reference(ContainerInterface::class), - ]); $connector = new Definition(ConnectorInterface::class); $connector->setClass(MongoConnector::class); diff --git a/README.md b/README.md index 821605b..a0605c6 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ OR instead of namespaces you can use instanceof or direct service autowiring services: # _instanceof: # SfCod\QueueBundle\Base\JobInterface: -# tags: ['sfcod.jobqueue.job_handler'] +# tags: ['sfcod.jobqueue.job'] App\Job\: resource: '../src/Job/*' - tags: ['sfcod.jobqueue.job_handler'] + tags: ['sfcod.jobqueue.job'] ``` -namespaces - is not required. You can set here all namespace where your job classes are, otherwise all services with tag 'sfcod.jobqueue.job_handler' will be fetched from symfony container. +namespaces - is not required. You can set here all namespace where your job classes are, otherwise all services with tag 'sfcod.jobqueue.job' will be fetched from symfony container. connection - is not required. If it is not set, bundle will use this service SfCod\QueueBundle\Base\MongoDriverInterface::class as default. #### Adding jobs to queue: diff --git a/Service/JobResolver.php b/Service/JobResolver.php index 97aab87..63f387e 100644 --- a/Service/JobResolver.php +++ b/Service/JobResolver.php @@ -4,6 +4,7 @@ use SfCod\QueueBundle\Base\JobInterface; use SfCod\QueueBundle\Base\JobResolverInterface; +use SfCod\QueueBundle\Exception\FatalThrowableException; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -16,29 +17,34 @@ class JobResolver implements JobResolverInterface { /** - * @var ContainerInterface + * @var JobInterface[] */ - protected $container; + private $jobs = []; /** - * JobResolver constructor. + * Resolve the given class. + * + * @param string $id * - * @param ContainerInterface $container + * @return JobInterface */ - public function __construct(ContainerInterface $container) + public function resolve(string $id): JobInterface { - $this->container = $container; + if (isset($this->jobs[$id])) { + return $this->jobs[$id]; + } + + throw new FatalThrowableException("Job handler '$id' not found."); } /** - * Resolve the given class. - * - * @param string $class + * @inheritDoc * - * @return JobInterface + * @param string $id + * @param JobInterface $job */ - public function resolve(string $class): JobInterface + public function addJob(string $id, JobInterface $job) { - return $this->container->get($class); + $this->jobs[$id] = $job; } }