Skip to content

Commit

Permalink
refactor job resolver in case to get rid of container
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1Z committed Nov 29, 2019
1 parent f6b571e commit ab1502a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
14 changes: 11 additions & 3 deletions Base/JobResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 5 additions & 3 deletions DependencyInjection/Compiler/JobCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)]);
}
}
}
6 changes: 2 additions & 4 deletions DependencyInjection/QueueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 18 additions & 12 deletions Service/JobResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SfCod\QueueBundle\Base\JobInterface;
use SfCod\QueueBundle\Base\JobResolverInterface;
use SfCod\QueueBundle\Exception\FatalThrowableException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -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;
}
}

0 comments on commit ab1502a

Please sign in to comment.