Skip to content

Commit 8b1fab0

Browse files
committed
init
0 parents  commit 8b1fab0

12 files changed

+831
-0
lines changed

Connector.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Queue\Connectors\ConnectorInterface;
6+
use Interop\Queue\PsrConnectionFactory;
7+
8+
class Connector implements ConnectorInterface
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function connect(array $config)
14+
{
15+
$config = array_replace([
16+
'connection_factory_class' => null,
17+
'queue' => 'default',
18+
'time_to_run' => 0,
19+
], $config);
20+
21+
if (empty($config['connection_factory_class'])) {
22+
throw new \LogicException('The "connection_factory_class" option is required');
23+
}
24+
25+
$factoryClass = $config['connection_factory_class'];
26+
if (false == class_exists($factoryClass)) {
27+
throw new \LogicException(sprintf('The "connection_factory_class" option "%s" is not a class', $factoryClass));
28+
}
29+
30+
$rc = new \ReflectionClass($factoryClass);
31+
if (false == $rc->implementsInterface(PsrConnectionFactory::class)) {
32+
throw new \LogicException(sprintf('The "connection_factory_class" option must contain a class that implements "%s" but it is not', PsrConnectionFactory::class));
33+
}
34+
35+
/** @var PsrConnectionFactory $factory */
36+
$factory = new $factoryClass($config);
37+
38+
return new Queue($factory->createContext(), $config['queue'], $config['time_to_run']);
39+
}
40+
}

EnqueueServiceProvider.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Queue\QueueManager;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class EnqueueServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function boot()
14+
{
15+
/** @var QueueManager $manager */
16+
$manager = $this->app['queue'];
17+
18+
$manager->addConnector('interop', function () {
19+
return new Connector();
20+
});
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function register()
27+
{
28+
}
29+
}

Job.php

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Contracts\Queue\Job as JobContract;
7+
use Illuminate\Queue\Jobs\Job as BaseJob;
8+
use Interop\Queue\PsrConsumer;
9+
use Interop\Queue\PsrContext;
10+
use Interop\Queue\PsrMessage;
11+
12+
class Job extends BaseJob implements JobContract
13+
{
14+
/**
15+
* @var PsrContext
16+
*/
17+
private $psrContext;
18+
19+
/**
20+
* @var PsrConsumer
21+
*/
22+
private $psrConsumer;
23+
24+
/**
25+
* @var PsrMessage
26+
*/
27+
private $psrMessage;
28+
29+
/**
30+
* @param Container $container
31+
* @param PsrContext $psrContext
32+
* @param PsrConsumer $psrConsumer
33+
* @param PsrMessage $psrMessage
34+
* @param string $connectionName
35+
*/
36+
public function __construct(Container $container, PsrContext $psrContext, PsrConsumer $psrConsumer, PsrMessage $psrMessage, $connectionName)
37+
{
38+
$this->container = $container;
39+
$this->psrContext = $psrContext;
40+
$this->psrConsumer = $psrConsumer;
41+
$this->psrMessage = $psrMessage;
42+
$this->connectionName = $connectionName;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function delete()
49+
{
50+
parent::delete();
51+
52+
$this->psrConsumer->acknowledge($this->psrMessage);
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function release($delay = 0)
59+
{
60+
if ($delay) {
61+
throw new \LogicException('To be implemented');
62+
}
63+
64+
$requeueMessage = clone $this->psrMessage;
65+
$requeueMessage->setProperty('x-attempts', $this->attempts() + 1);
66+
67+
$this->psrContext->createProducer()->send($this->psrConsumer->getQueue(), $requeueMessage);
68+
69+
$this->psrConsumer->acknowledge($this->psrMessage);
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getQueue()
76+
{
77+
return $this->psrConsumer->getQueue()->getQueueName();
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function attempts()
84+
{
85+
return $this->psrMessage->getProperty('x-attempts', 1);
86+
}
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
91+
public function getRawBody()
92+
{
93+
return $this->psrMessage->getBody();
94+
}
95+
}

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017 Kotliar Maksym
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Queue.php

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Contracts\Queue\Queue as QueueContract;
6+
use Illuminate\Queue\Queue as BaseQueue;
7+
use Interop\Queue\PsrContext;
8+
9+
class Queue extends BaseQueue implements QueueContract
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $queueName;
15+
16+
/**
17+
* @var int
18+
*/
19+
protected $timeToRun;
20+
/**
21+
* @var PsrContext
22+
*/
23+
private $psrContext;
24+
25+
/**
26+
* @param PsrContext $psrContext
27+
* @param string $queueName
28+
* @param int $timeToRun
29+
*/
30+
public function __construct(PsrContext $psrContext, $queueName, $timeToRun)
31+
{
32+
$this->psrContext = $psrContext;
33+
$this->queueName = $queueName;
34+
$this->timeToRun = $timeToRun;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function size($queue = null)
41+
{
42+
return 0;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function push($job, $data = '', $queue = null)
49+
{
50+
return $this->pushRaw($this->createPayload($job, $data), $queue);
51+
}
52+
53+
/**
54+
* Push a new job onto the queue.
55+
*
56+
* @param string $queue
57+
* @param string $job
58+
* @param mixed $data
59+
*
60+
* @return mixed
61+
*/
62+
public function pushOn($queue, $job, $data = '')
63+
{
64+
new \LogicException('to be implemented');
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function pushRaw($payload, $queue = null, array $options = [])
71+
{
72+
return $this->psrContext->createProducer()->send(
73+
$this->getQueue($queue),
74+
$this->psrContext->createMessage($payload)
75+
);
76+
}
77+
78+
/**
79+
* {@inheritdoc}
80+
*/
81+
public function later($delay, $job, $data = '', $queue = null)
82+
{
83+
new \LogicException('to be implemented');
84+
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
public function pop($queue = null)
90+
{
91+
$queue = $this->getQueue($queue);
92+
93+
$psrConsumer = $this->psrContext->createConsumer($queue);
94+
if ($psrMessage = $psrConsumer->receive(1000)) { // 1 sec
95+
return new Job(
96+
$this->container,
97+
$this->psrContext,
98+
$psrConsumer,
99+
$psrMessage,
100+
$this->connectionName
101+
);
102+
}
103+
}
104+
105+
/**
106+
* Get the queue or return the default.
107+
*
108+
* @param string|null $queue
109+
*
110+
* @return \Interop\Queue\PsrQueue
111+
*/
112+
public function getQueue($queue = null)
113+
{
114+
return $this->psrContext->createQueue($queue ?: $this->queueName);
115+
}
116+
117+
/**
118+
* @return PsrContext
119+
*/
120+
public function getPsrContext()
121+
{
122+
return $this->psrContext;
123+
}
124+
125+
/**
126+
* @return int
127+
*/
128+
public function getTimeToRun()
129+
{
130+
return $this->timeToRun;
131+
}
132+
}

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Enqueue Laravel Queue Extension
2+
3+
[![Gitter](https://badges.gitter.im/php-enqueue/Lobby.svg)](https://gitter.im/php-enqueue/Lobby)
4+
5+
You can use all transports built on top of [queue-interop](https://github.com/queue-interop/queue-interop) including [all supported](https://github.com/php-enqueue/enqueue-dev/tree/master/docs/transport) by Enqueue.
6+
7+
## Resources
8+
9+
* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md)
10+
* [Questions](https://gitter.im/php-enqueue/Lobby)
11+
* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)
12+
13+
## Developed by Forma-Pro
14+
15+
Forma-Pro is a full stack development company which interests also spread to open source development.
16+
Being a team of strong professionals we have an aim an ability to help community by developing cutting edge solutions in the areas of e-commerce, docker & microservice oriented architecture where we have accumulated a huge many-years experience.
17+
Our main specialization is Symfony framework based solution, but we are always looking to the technologies that allow us to do our job the best way. We are committed to creating solutions that revolutionize the way how things are developed in aspects of architecture & scalability.
18+
19+
If you have any questions and inquires about our open source development, this product particularly or any other matter feel free to contact at opensource@forma-pro.com
20+
21+
## License
22+
23+
It is released under the [MIT License](LICENSE).

0 commit comments

Comments
 (0)