Skip to content

Commit 50ca22d

Browse files
authored
Merge pull request #71 from php-enqueue/fix-issue-67
JobQueue/Job shouldn't be required when Doctrine schema update
2 parents bce8a7a + 2071b5f commit 50ca22d

23 files changed

+144
-143
lines changed

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

+41-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
use Symfony\Component\Config\FileLocator;
99
use Symfony\Component\Config\Resource\FileResource;
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1112
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1213
use Symfony\Component\DependencyInjection\Reference;
1314
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1415

15-
class EnqueueExtension extends Extension
16+
class EnqueueExtension extends Extension implements PrependExtensionInterface
1617
{
1718
/**
1819
* @var TransportFactoryInterface[]
@@ -132,4 +133,43 @@ public function getConfiguration(array $config, ContainerBuilder $container)
132133

133134
return new Configuration($this->factories);
134135
}
136+
137+
public function prepend(ContainerBuilder $container)
138+
{
139+
$this->registerJobQueueDoctrineEntityMapping($container);
140+
}
141+
142+
private function registerJobQueueDoctrineEntityMapping(ContainerBuilder $container)
143+
{
144+
if (false == class_exists(Job::class)) {
145+
return;
146+
}
147+
148+
$bundles = $container->getParameter('kernel.bundles');
149+
150+
if (false == isset($bundles['DoctrineBundle'])) {
151+
return;
152+
}
153+
154+
foreach ($container->getExtensionConfig('doctrine') as $config) {
155+
// do not register mappings if dbal not configured.
156+
if (false == empty($config['dbal'])) {
157+
$rc = new \ReflectionClass(Job::class);
158+
$jobQueueRootDir = dirname($rc->getFileName());
159+
$container->prependExtensionConfig('doctrine', [
160+
'orm' => [
161+
'mappings' => [
162+
'enqueue_job_queue' => [
163+
'is_bundle' => false,
164+
'type' => 'xml',
165+
'dir' => $jobQueueRootDir.'/Doctrine/mapping',
166+
'prefix' => 'Enqueue\JobQueue\Doctrine\Entity',
167+
],
168+
],
169+
],
170+
]);
171+
break;
172+
}
173+
}
174+
}
135175
}

pkg/enqueue-bundle/Entity/Job.php

-108
This file was deleted.

pkg/enqueue-bundle/Entity/JobUnique.php

-18
This file was deleted.

pkg/enqueue-bundle/Resources/config/job.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ parameters:
33

44
services:
55
enqueue.job.storage:
6-
class: 'Enqueue\JobQueue\JobStorage'
6+
class: 'Enqueue\JobQueue\Doctrine\JobStorage'
77
arguments:
88
- '@doctrine'
9-
- 'Enqueue\Bundle\Entity\Job'
9+
- 'Enqueue\JobQueue\Doctrine\Entity\Job'
1010
- '%enqueue.job.unique_job_table_name%'
1111

1212
enqueue.job.processor:

pkg/enqueue-bundle/Tests/Functional/Job/JobStorageTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Enqueue\Bundle\Tests\Functional\Job;
44

55
use Enqueue\Bundle\Tests\Functional\WebTestCase;
6-
use Enqueue\JobQueue\JobStorage;
6+
use Enqueue\JobQueue\Doctrine\JobStorage;
77

88
/**
99
* @group functional

pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,32 @@ public function testShouldNotLoadSignalExtensionServiceIfDisabled()
434434

435435
self::assertFalse($container->hasDefinition('enqueue.consumption.signal_extension'));
436436
}
437+
438+
public function testShouldAddJobQueueEntityMapping()
439+
{
440+
$container = new ContainerBuilder();
441+
$container->setParameter('kernel.bundles', ['DoctrineBundle' => true]);
442+
$container->prependExtensionConfig('doctrine', ['dbal' => true]);
443+
444+
$extension = new EnqueueExtension();
445+
446+
$extension->prepend($container);
447+
448+
$config = $container->getExtensionConfig('doctrine');
449+
450+
$this->assertSame(['dbal' => true], $config[1]);
451+
$this->assertNotEmpty($config[0]['orm']['mappings']['enqueue_job_queue']);
452+
}
453+
454+
public function testShouldNotAddJobQueueEntityMappingIfDoctrineBundleIsNotRegistered()
455+
{
456+
$container = new ContainerBuilder();
457+
$container->setParameter('kernel.bundles', []);
458+
459+
$extension = new EnqueueExtension();
460+
461+
$extension->prepend($container);
462+
463+
$this->assertSame([], $container->getExtensionConfig('doctrine'));
464+
}
437465
}

pkg/job-queue/CalculateRootJobStatusProcessor.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Enqueue\Client\ProducerInterface;
66
use Enqueue\Client\TopicSubscriberInterface;
77
use Enqueue\Consumption\Result;
8+
use Enqueue\JobQueue\Doctrine\JobStorage;
89
use Enqueue\Psr\PsrContext;
910
use Enqueue\Psr\PsrMessage;
1011
use Enqueue\Psr\PsrProcessor;

pkg/job-queue/CalculateRootJobStatusService.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\JobQueue;
44

55
use Doctrine\Common\Collections\Collection;
6+
use Enqueue\JobQueue\Doctrine\JobStorage;
67

78
class CalculateRootJobStatusService
89
{

pkg/job-queue/DependentJobProcessor.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Enqueue\Client\ProducerInterface;
77
use Enqueue\Client\TopicSubscriberInterface;
88
use Enqueue\Consumption\Result;
9+
use Enqueue\JobQueue\Doctrine\JobStorage;
910
use Enqueue\Psr\PsrContext;
1011
use Enqueue\Psr\PsrMessage;
1112
use Enqueue\Psr\PsrProcessor;

pkg/job-queue/DependentJobService.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Enqueue\JobQueue;
44

5+
use Enqueue\JobQueue\Doctrine\JobStorage;
6+
57
class DependentJobService
68
{
79
/**

pkg/job-queue/Doctrine/Entity/Job.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Enqueue\JobQueue\Doctrine\Entity;
3+
4+
use Doctrine\Common\Collections\ArrayCollection;
5+
use Enqueue\JobQueue\Job as BaseJob;
6+
7+
class Job extends BaseJob
8+
{
9+
public function __construct()
10+
{
11+
parent::__construct();
12+
13+
$this->childJobs = new ArrayCollection();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Enqueue\JobQueue\Doctrine\Entity;
3+
4+
class JobUnique
5+
{
6+
protected $name;
7+
}

pkg/job-queue/JobStorage.php renamed to pkg/job-queue/Doctrine/JobStorage.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
namespace Enqueue\JobQueue;
3+
namespace Enqueue\JobQueue\Doctrine;
44

55
use Doctrine\Common\Persistence\ManagerRegistry;
66
use Doctrine\DBAL\Connection;
77
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
88
use Doctrine\DBAL\LockMode;
99
use Doctrine\ORM\EntityManager;
1010
use Doctrine\ORM\EntityRepository;
11+
use Enqueue\JobQueue\DuplicateJobException;
12+
use Enqueue\JobQueue\Job;
1113

1214
class JobStorage
1315
{
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
3+
<entity name="Enqueue\JobQueue\Doctrine\Entity\Job" table="enqueue_job">
4+
<id name="id" type="integer">
5+
<generator strategy="AUTO" />
6+
</id>
7+
<field name="ownerId" column="owner_id" type="string" nullable="true" />
8+
<field name="name" column="name" type="string" nullable="false" />
9+
<field name="status" column="status" type="string" nullable="false" />
10+
<field name="interrupted" column="interrupted" type="boolean" />
11+
<field name="unique" column="`unique`" type="boolean" />
12+
<field name="createdAt" column="created_at" type="datetime" nullable="false" />
13+
<field name="startedAt" column="started_at" type="datetime" nullable="true" />
14+
<field name="stoppedAt" column="stopped_at" type="datetime" nullable="true" />
15+
<field name="data" column="data" type="json_array" nullable="true" />
16+
<many-to-one field="rootJob" target-entity="Enqueue\JobQueue\Doctrine\Entity\Job" inversed-by="childJobs">
17+
<join-column name="root_job_id" referenced-column-name="id" on-delete="CASCADE" />
18+
</many-to-one>
19+
<one-to-many field="childJobs" target-entity="Enqueue\JobQueue\Doctrine\Entity\Job" mapped-by="rootJob" />
20+
</entity>
21+
</doctrine-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
3+
<entity name="Enqueue\JobQueue\Doctrine\Entity\JobUnique" table="enqueue_job_unique">
4+
<id name="name" type="string">
5+
<generator strategy="AUTO" />
6+
</id>
7+
</entity>
8+
</doctrine-mapping>

pkg/job-queue/JobProcessor.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\JobQueue;
44

55
use Enqueue\Client\ProducerInterface;
6+
use Enqueue\JobQueue\Doctrine\JobStorage;
67

78
class JobProcessor
89
{

pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Enqueue\JobQueue\CalculateRootJobStatusProcessor;
88
use Enqueue\JobQueue\CalculateRootJobStatusService;
99
use Enqueue\JobQueue\Job;
10-
use Enqueue\JobQueue\JobStorage;
10+
use Enqueue\JobQueue\Doctrine\JobStorage;
1111
use Enqueue\JobQueue\Topics;
1212
use Enqueue\Psr\PsrContext;
1313
use Enqueue\Null\NullMessage;

pkg/job-queue/Tests/CalculateRootJobStatusServiceTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Enqueue\JobQueue\CalculateRootJobStatusService;
66
use Enqueue\JobQueue\Job;
7-
use Enqueue\JobQueue\JobStorage;
7+
use Enqueue\JobQueue\Doctrine\JobStorage;
88

99
class CalculateRootJobStatusServiceTest extends \PHPUnit\Framework\TestCase
1010
{
@@ -356,7 +356,7 @@ public function testShouldSetStatusSuccessIfAllAreSuccess()
356356
}
357357

358358
/**
359-
* @return \PHPUnit_Framework_MockObject_MockObject|JobStorage
359+
* @return \PHPUnit_Framework_MockObject_MockObject|\Enqueue\JobQueue\Doctrine\JobStorage
360360
*/
361361
private function createJobStorageMock()
362362
{

pkg/job-queue/Tests/DependentJobProcessorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Enqueue\Consumption\Result;
88
use Enqueue\JobQueue\DependentJobProcessor;
99
use Enqueue\JobQueue\Job;
10-
use Enqueue\JobQueue\JobStorage;
10+
use Enqueue\JobQueue\Doctrine\JobStorage;
1111
use Enqueue\JobQueue\Topics;
1212
use Enqueue\Psr\PsrContext;
1313
use Enqueue\Null\NullMessage;
@@ -325,7 +325,7 @@ private function createContextMock()
325325
}
326326

327327
/**
328-
* @return \PHPUnit_Framework_MockObject_MockObject|JobStorage
328+
* @return \PHPUnit_Framework_MockObject_MockObject|\Enqueue\JobQueue\Doctrine\JobStorage
329329
*/
330330
private function createJobStorageMock()
331331
{

0 commit comments

Comments
 (0)