Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b324c3

Browse files
committed
Simpified Configuration
1 parent 9524bc4 commit 2b324c3

14 files changed

Lines changed: 284 additions & 70 deletions

DependencyInjection/Configuration.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ public function getConfigTreeBuilder()
2222

2323
$rootNode
2424
->children()
25-
->scalarNode('driver')
26-
->cannotBeOverwritten()
27-
->isRequired()
28-
->cannotBeEmpty()
25+
->arrayNode('adapter')
26+
->children()
27+
->scalarNode('class')
28+
->end()
29+
->arrayNode('options')
30+
->end()
31+
->end()
2932
->end()
3033
->end();
3134

DependencyInjection/XaavQueueExtension.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Xaav\QueueBundle\DependencyInjection;
44

5+
use Symfony\Component\DependencyInjection\Definition;
6+
use Symfony\Component\DependencyInjection\Reference;
57
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
68
use Symfony\Component\DependencyInjection\ContainerBuilder;
79
use Symfony\Component\Config\FileLocator;
@@ -11,8 +13,14 @@ class XaavQueueExtension extends Extension
1113
{
1214
public function load(array $configs, ContainerBuilder $container)
1315
{
14-
//$configuration = new Configuration();
15-
//$config = $this->processConfiguration($configuration, $configs);
16+
$configuration = new Configuration();
17+
$config = $this->processConfiguration($configuration, $configs);
18+
$config['adapter']['options']['service_container'] = new Reference('service_container');
19+
20+
$adapter = new Definition($config['adapter']['class']);
21+
$adapter->setArguments(array($config['adapter']['options']));
22+
23+
$container->setDefinition('xaav.queue.adapter', $adapter);
1624

1725
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
1826
$loader->load('services.yml');

Entity/JobQueue.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

Entity/Queue.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Xaav\QueueBundle\Entity;
4+
5+
use Doctrine\ORM\EntityManager;
6+
use Xaav\QueueBundle\Queue\Job\JobInterface;
7+
use Xaav\QueueBundle\Queue\QueueInterface;
8+
use Doctrine\ORM\Mapping as ORM;
9+
10+
/**
11+
* @ORM\Entity()
12+
*/
13+
class Queue implements QueueInterface
14+
{
15+
/**
16+
* @ORM\Id
17+
* @ORM\Column(type="integer")
18+
* @ORM\GeneratedValue(strategy="AUTO")
19+
*/
20+
protected $id;
21+
22+
/**
23+
* @ORM\Column(type="string", unique=true)
24+
*/
25+
protected $name;
26+
27+
/**
28+
* @ORM\OneToMany(targetEntity="SerializedJob", mappedBy="queue",cascade={"persist", "all"})
29+
*/
30+
protected $serializedJobs;
31+
32+
/**
33+
* @var EntityManager
34+
*/
35+
protected $entityManager;
36+
37+
/**
38+
* Constructor
39+
*/
40+
public function __construct()
41+
{
42+
$this->serializedJobs = new \Doctrine\Common\Collections\ArrayCollection();
43+
}
44+
45+
/**
46+
* Get the name of the Queue
47+
*/
48+
public function getName()
49+
{
50+
return $this->name;
51+
}
52+
53+
/**
54+
* Set the name of the Queue
55+
*/
56+
public function setName($name)
57+
{
58+
$this->name = $name;
59+
}
60+
61+
/**
62+
* Get id
63+
*
64+
* @return integer $id
65+
*/
66+
public function getId()
67+
{
68+
return $this->id;
69+
}
70+
71+
/**
72+
* Set Entity Manager
73+
*/
74+
public function setEntityManager(EntityManager $entityManager)
75+
{
76+
$this->entityManager = $entityManager;
77+
}
78+
79+
/**
80+
* {@InheritDoc}
81+
*/
82+
public function get()
83+
{
84+
$serializedJob = $this->serializedJobs->last();
85+
if ($serializedJob) {
86+
$this->entityManager->remove($serializedJob);
87+
88+
return unserialize($serializedJob->getData());
89+
}
90+
}
91+
92+
/**
93+
* {@InheritDoc}
94+
*/
95+
public function add(JobInterface $job)
96+
{
97+
$serializedJob = new SerializedJob();
98+
$serializedJob->setData(serialize($job));
99+
$serializedJob->setJobQueue($this);
100+
101+
$this->serializedJobs->add($serializedJob);
102+
}
103+
}

Entity/SerializedJob.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,37 @@ class SerializedJob
2222
protected $data;
2323

2424
/**
25-
* @ORM\ManyToOne(targetEntity="JobQueue")
25+
* @ORM\ManyToOne(targetEntity="Queue")
2626
*/
2727
protected $jobQueue;
2828

29+
/**
30+
* Get Job Queue
31+
*/
2932
public function getJobQueue()
3033
{
3134
return $this->jobQueue;
3235
}
3336

37+
/**
38+
* Set Job Queue
39+
*/
3440
public function setJobQueue(JobQueue $jobQueue)
3541
{
3642
$this->jobQueue = $jobQueue;
3743
}
3844

45+
/**
46+
* Get Data
47+
*/
3948
public function setData($data)
4049
{
4150
$this->data = $data;
4251
}
4352

53+
/**
54+
* Set Data
55+
*/
4456
public function getData()
4557
{
4658
return $this->data;

JobQueue/JobQueue/DoctrineJobQueue.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class DoctrineJobQueue implements JobQueueInterface
1919
*/
2020
protected $entityManager;
2121

22-
public function setEntityManager(EntityManager $entityManager)
22+
public function __construct(array $options)
2323
{
24-
$this->entityManager = $entityManager;
24+
$container = $options['service_container'];
25+
$this->entityManager = $container->get('doctrine')->getEntityManager();
2526
}
2627

2728
public function getJobFromQueue()

JobQueue/Provider/DoctrineProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class DoctrineProvider implements JobQueueProviderInterface
99
{
1010
protected $entityManager;
1111

12-
public function __construct(EntityManager $entityManager)
13-
{
14-
$this->entityManager = $entityManager;
15-
}
12+
public function __construct(array $options)
13+
{
14+
$container = $options['service_container'];
15+
$this->entityManager = $container->get('doctrine')->getEntityManager();
16+
}
1617

1718
public function getJobQueueByName($name)
1819
{

Queue/Adapter/DoctrineAdapter.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Xaav\QueueBundle\Queue\Adapter;
4+
5+
use Xaav\QueueBundle\Entity\JobQueue;
6+
7+
class DoctrineAdapter implements QueueAdapterInterface
8+
{
9+
protected $entityManager;
10+
11+
public function __construct(array $options)
12+
{
13+
$container = $options['service_container'];
14+
$this->entityManager = $container->get('doctrine')->getEntityManager();
15+
}
16+
17+
public function get($name)
18+
{
19+
$queue = $this->entityManager
20+
->getRepository('XaavQueueBundle:Queue')
21+
->findOneByName($name);
22+
23+
if(!$queue) {
24+
25+
$queue = new JobQueue();
26+
$queue->setName($name);
27+
28+
$this->entityManager->persist($queue);
29+
}
30+
31+
$queue->setEntityManager($this->entityManager);
32+
33+
return $queue;
34+
}
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Xaav\QueueBundle\Queue\Adapter;
4+
5+
interface QueueAdapterInterface
6+
{
7+
public function __construct(array $options);
8+
9+
/**
10+
* Gets the queue with the specified name from this provider.
11+
*
12+
* @param string $name A unique identifier of the queue
13+
*/
14+
public function get($name);
15+
}

Queue/Job/JobInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Xaav\QueueBundle\Queue\Job;
4+
5+
interface JobInterface
6+
{
7+
/**
8+
* Process the job
9+
*
10+
* @return boolean True if the job is done
11+
*/
12+
public function process();
13+
}

0 commit comments

Comments
 (0)