Skip to content

Commit

Permalink
Merge pull request #71 from php-enqueue/fix-issue-67
Browse files Browse the repository at this point in the history
JobQueue/Job shouldn't be required when Doctrine schema update
  • Loading branch information
makasim authored May 4, 2017
2 parents bce8a7a + 2071b5f commit 50ca22d
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 143 deletions.
42 changes: 41 additions & 1 deletion pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class EnqueueExtension extends Extension
class EnqueueExtension extends Extension implements PrependExtensionInterface
{
/**
* @var TransportFactoryInterface[]
Expand Down Expand Up @@ -132,4 +133,43 @@ public function getConfiguration(array $config, ContainerBuilder $container)

return new Configuration($this->factories);
}

public function prepend(ContainerBuilder $container)
{
$this->registerJobQueueDoctrineEntityMapping($container);
}

private function registerJobQueueDoctrineEntityMapping(ContainerBuilder $container)
{
if (false == class_exists(Job::class)) {
return;
}

$bundles = $container->getParameter('kernel.bundles');

if (false == isset($bundles['DoctrineBundle'])) {
return;
}

foreach ($container->getExtensionConfig('doctrine') as $config) {
// do not register mappings if dbal not configured.
if (false == empty($config['dbal'])) {
$rc = new \ReflectionClass(Job::class);
$jobQueueRootDir = dirname($rc->getFileName());
$container->prependExtensionConfig('doctrine', [
'orm' => [
'mappings' => [
'enqueue_job_queue' => [
'is_bundle' => false,
'type' => 'xml',
'dir' => $jobQueueRootDir.'/Doctrine/mapping',
'prefix' => 'Enqueue\JobQueue\Doctrine\Entity',
],
],
],
]);
break;
}
}
}
}
108 changes: 0 additions & 108 deletions pkg/enqueue-bundle/Entity/Job.php

This file was deleted.

18 changes: 0 additions & 18 deletions pkg/enqueue-bundle/Entity/JobUnique.php

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/enqueue-bundle/Resources/config/job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ parameters:

services:
enqueue.job.storage:
class: 'Enqueue\JobQueue\JobStorage'
class: 'Enqueue\JobQueue\Doctrine\JobStorage'
arguments:
- '@doctrine'
- 'Enqueue\Bundle\Entity\Job'
- 'Enqueue\JobQueue\Doctrine\Entity\Job'
- '%enqueue.job.unique_job_table_name%'

enqueue.job.processor:
Expand Down
2 changes: 1 addition & 1 deletion pkg/enqueue-bundle/Tests/Functional/Job/JobStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Enqueue\Bundle\Tests\Functional\Job;

use Enqueue\Bundle\Tests\Functional\WebTestCase;
use Enqueue\JobQueue\JobStorage;
use Enqueue\JobQueue\Doctrine\JobStorage;

/**
* @group functional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,32 @@ public function testShouldNotLoadSignalExtensionServiceIfDisabled()

self::assertFalse($container->hasDefinition('enqueue.consumption.signal_extension'));
}

public function testShouldAddJobQueueEntityMapping()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.bundles', ['DoctrineBundle' => true]);
$container->prependExtensionConfig('doctrine', ['dbal' => true]);

$extension = new EnqueueExtension();

$extension->prepend($container);

$config = $container->getExtensionConfig('doctrine');

$this->assertSame(['dbal' => true], $config[1]);
$this->assertNotEmpty($config[0]['orm']['mappings']['enqueue_job_queue']);
}

public function testShouldNotAddJobQueueEntityMappingIfDoctrineBundleIsNotRegistered()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.bundles', []);

$extension = new EnqueueExtension();

$extension->prepend($container);

$this->assertSame([], $container->getExtensionConfig('doctrine'));
}
}
1 change: 1 addition & 0 deletions pkg/job-queue/CalculateRootJobStatusProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Enqueue\Client\ProducerInterface;
use Enqueue\Client\TopicSubscriberInterface;
use Enqueue\Consumption\Result;
use Enqueue\JobQueue\Doctrine\JobStorage;
use Enqueue\Psr\PsrContext;
use Enqueue\Psr\PsrMessage;
use Enqueue\Psr\PsrProcessor;
Expand Down
1 change: 1 addition & 0 deletions pkg/job-queue/CalculateRootJobStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enqueue\JobQueue;

use Doctrine\Common\Collections\Collection;
use Enqueue\JobQueue\Doctrine\JobStorage;

class CalculateRootJobStatusService
{
Expand Down
1 change: 1 addition & 0 deletions pkg/job-queue/DependentJobProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enqueue\Client\ProducerInterface;
use Enqueue\Client\TopicSubscriberInterface;
use Enqueue\Consumption\Result;
use Enqueue\JobQueue\Doctrine\JobStorage;
use Enqueue\Psr\PsrContext;
use Enqueue\Psr\PsrMessage;
use Enqueue\Psr\PsrProcessor;
Expand Down
2 changes: 2 additions & 0 deletions pkg/job-queue/DependentJobService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Enqueue\JobQueue;

use Enqueue\JobQueue\Doctrine\JobStorage;

class DependentJobService
{
/**
Expand Down
15 changes: 15 additions & 0 deletions pkg/job-queue/Doctrine/Entity/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Enqueue\JobQueue\Doctrine\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Enqueue\JobQueue\Job as BaseJob;

class Job extends BaseJob
{
public function __construct()
{
parent::__construct();

$this->childJobs = new ArrayCollection();
}
}
7 changes: 7 additions & 0 deletions pkg/job-queue/Doctrine/Entity/JobUnique.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Enqueue\JobQueue\Doctrine\Entity;

class JobUnique
{
protected $name;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Enqueue\JobQueue;
namespace Enqueue\JobQueue\Doctrine;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Enqueue\JobQueue\DuplicateJobException;
use Enqueue\JobQueue\Job;

class JobStorage
{
Expand Down
21 changes: 21 additions & 0 deletions pkg/job-queue/Doctrine/mapping/Job.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<entity name="Enqueue\JobQueue\Doctrine\Entity\Job" table="enqueue_job">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<field name="ownerId" column="owner_id" type="string" nullable="true" />
<field name="name" column="name" type="string" nullable="false" />
<field name="status" column="status" type="string" nullable="false" />
<field name="interrupted" column="interrupted" type="boolean" />
<field name="unique" column="`unique`" type="boolean" />
<field name="createdAt" column="created_at" type="datetime" nullable="false" />
<field name="startedAt" column="started_at" type="datetime" nullable="true" />
<field name="stoppedAt" column="stopped_at" type="datetime" nullable="true" />
<field name="data" column="data" type="json_array" nullable="true" />
<many-to-one field="rootJob" target-entity="Enqueue\JobQueue\Doctrine\Entity\Job" inversed-by="childJobs">
<join-column name="root_job_id" referenced-column-name="id" on-delete="CASCADE" />
</many-to-one>
<one-to-many field="childJobs" target-entity="Enqueue\JobQueue\Doctrine\Entity\Job" mapped-by="rootJob" />
</entity>
</doctrine-mapping>
8 changes: 8 additions & 0 deletions pkg/job-queue/Doctrine/mapping/JobUnique.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<entity name="Enqueue\JobQueue\Doctrine\Entity\JobUnique" table="enqueue_job_unique">
<id name="name" type="string">
<generator strategy="AUTO" />
</id>
</entity>
</doctrine-mapping>
1 change: 1 addition & 0 deletions pkg/job-queue/JobProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enqueue\JobQueue;

use Enqueue\Client\ProducerInterface;
use Enqueue\JobQueue\Doctrine\JobStorage;

class JobProcessor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Enqueue\JobQueue\CalculateRootJobStatusProcessor;
use Enqueue\JobQueue\CalculateRootJobStatusService;
use Enqueue\JobQueue\Job;
use Enqueue\JobQueue\JobStorage;
use Enqueue\JobQueue\Doctrine\JobStorage;
use Enqueue\JobQueue\Topics;
use Enqueue\Psr\PsrContext;
use Enqueue\Null\NullMessage;
Expand Down
4 changes: 2 additions & 2 deletions pkg/job-queue/Tests/CalculateRootJobStatusServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Enqueue\JobQueue\CalculateRootJobStatusService;
use Enqueue\JobQueue\Job;
use Enqueue\JobQueue\JobStorage;
use Enqueue\JobQueue\Doctrine\JobStorage;

class CalculateRootJobStatusServiceTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -356,7 +356,7 @@ public function testShouldSetStatusSuccessIfAllAreSuccess()
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|JobStorage
* @return \PHPUnit_Framework_MockObject_MockObject|\Enqueue\JobQueue\Doctrine\JobStorage
*/
private function createJobStorageMock()
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/job-queue/Tests/DependentJobProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Enqueue\Consumption\Result;
use Enqueue\JobQueue\DependentJobProcessor;
use Enqueue\JobQueue\Job;
use Enqueue\JobQueue\JobStorage;
use Enqueue\JobQueue\Doctrine\JobStorage;
use Enqueue\JobQueue\Topics;
use Enqueue\Psr\PsrContext;
use Enqueue\Null\NullMessage;
Expand Down Expand Up @@ -325,7 +325,7 @@ private function createContextMock()
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|JobStorage
* @return \PHPUnit_Framework_MockObject_MockObject|\Enqueue\JobQueue\Doctrine\JobStorage
*/
private function createJobStorageMock()
{
Expand Down
Loading

0 comments on commit 50ca22d

Please sign in to comment.