dereuromark / cakephp-queue

@@ -9,7 +9,7 @@
Loading
9 9
/**
10 10
 * @property \Queue\Model\Table\QueueProcessesTable $QueueProcesses
11 11
 *
12 -
 * @method \Queue\Model\Entity\QueueProcess[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
12 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess> paginate($object = null, array $settings = [])
13 13
 * @property \Queue\Model\Table\QueuedJobsTable $QueuedJobs
14 14
 */
15 15
class QueueProcessesController extends AppController {

@@ -6,6 +6,7 @@
Loading
6 6
use Cake\Console\Arguments;
7 7
use Cake\Console\ConsoleIo;
8 8
use Cake\Console\ConsoleOptionParser;
9 +
use Cake\Core\ContainerInterface;
9 10
use Cake\Log\Log;
10 11
use Psr\Log\LoggerInterface;
11 12
use Psr\Log\NullLogger;
@@ -17,6 +18,19 @@
Loading
17 18
 */
18 19
class RunCommand extends Command {
19 20
21 +
	/**
22 +
	 * @var \Cake\Core\ContainerInterface
23 +
	 */
24 +
	protected $container;
25 +
26 +
	/**
27 +
	 * @param \Cake\Core\ContainerInterface $container
28 +
	 */
29 +
	public function __construct(ContainerInterface $container) {
30 +
		parent::__construct();
31 +
		$this->container = $container;
32 +
	}
33 +
20 34
	/**
21 35
	 * @inheritDoc
22 36
	 */
@@ -91,7 +105,7 @@
Loading
91 105
	public function execute(Arguments $args, ConsoleIo $io): int {
92 106
		$logger = $this->getLogger($args);
93 107
		$io = new Io($io);
94 -
		$processor = new Processor($io, $logger);
108 +
		$processor = new Processor($io, $logger, $this->container);
95 109
96 110
		return $processor->run($args->getOptions());
97 111
	}

@@ -0,0 +1,32 @@
Loading
1 +
<?php
2 +
3 +
namespace Queue\Queue;
4 +
5 +
use Cake\Core\ContainerInterface;
6 +
7 +
trait ServicesTrait {
8 +
9 +
	/**
10 +
	 * @var \Cake\Core\ContainerInterface
11 +
	 */
12 +
	protected $container;
13 +
14 +
	/**
15 +
	 * @param string $id Classname or identifier of the service you want to retrieve
16 +
	 * @throws \Psr\Container\NotFoundExceptionInterface
17 +
	 * @throws \Psr\Container\ContainerExceptionInterface
18 +
	 * @return mixed
19 +
	 */
20 +
	protected function getService(string $id) {
21 +
		return $this->container->get($id);
22 +
	}
23 +
24 +
	/**
25 +
	 * @param \Cake\Core\ContainerInterface $container
26 +
	 * @return void
27 +
	 */
28 +
	public function setContainer(ContainerInterface $container) {
29 +
		$this->container = $container;
30 +
	}
31 +
32 +
}

@@ -14,7 +14,7 @@
Loading
14 14
/**
15 15
 * @property \Queue\Model\Table\QueuedJobsTable $QueuedJobs
16 16
 *
17 -
 * @method \Queue\Model\Entity\QueuedJob[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
17 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob> paginate($object = null, array $settings = [])
18 18
 * @property \Search\Controller\Component\SearchComponent $Search
19 19
 */
20 20
class QueuedJobsController extends AppController {

@@ -5,6 +5,7 @@
Loading
5 5
use Cake\Console\CommandCollection;
6 6
use Cake\Core\BasePlugin;
7 7
use Cake\Core\Configure;
8 +
use Cake\Core\ContainerInterface;
8 9
use Cake\Routing\RouteBuilder;
9 10
use Queue\Command\AddCommand;
10 11
use Queue\Command\BakeQueueTaskCommand;
@@ -64,4 +65,15 @@
Loading
64 65
		});
65 66
	}
66 67
68 +
	/**
69 +
	 * @param \Cake\Core\ContainerInterface $container The DI container instance
70 +
	 * @return void
71 +
	 */
72 +
	public function services(ContainerInterface $container): void {
73 +
		$container->add(ContainerInterface::class, $container);
74 +
		$container
75 +
			->add(RunCommand::class)
76 +
			->addArgument(ContainerInterface::class);
77 +
	}
78 +
67 79
}

@@ -24,10 +24,10 @@
Loading
24 24
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
25 25
 * @method \Queue\Model\Entity\QueueProcess saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
26 26
 * @method \Queue\Model\Entity\QueueProcess newEmptyEntity()
27 -
 * @method \Queue\Model\Entity\QueueProcess[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = [])
28 -
 * @method \Queue\Model\Entity\QueueProcess[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
29 -
 * @method \Queue\Model\Entity\QueueProcess[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
30 -
 * @method \Queue\Model\Entity\QueueProcess[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
27 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess>|false saveMany(iterable $entities, $options = [])
28 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess> saveManyOrFail(iterable $entities, $options = [])
29 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess>|false deleteMany(iterable $entities, $options = [])
30 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess> deleteManyOrFail(iterable $entities, $options = [])
31 31
 */
32 32
class QueueProcessesTable extends Table {
33 33

@@ -4,6 +4,7 @@
Loading
4 4
5 5
use Cake\Console\CommandInterface;
6 6
use Cake\Core\Configure;
7 +
use Cake\Core\ContainerInterface;
7 8
use Cake\Datasource\Exception\RecordNotFoundException;
8 9
use Cake\Datasource\ModelAwareTrait;
9 10
use Cake\ORM\Exception\PersistenceFailedException;
@@ -38,6 +39,11 @@
Loading
38 39
	 */
39 40
	protected $logger;
40 41
42 +
	/**
43 +
	 * @var \Cake\Core\ContainerInterface|null
44 +
	 */
45 +
	protected $container;
46 +
41 47
	/**
42 48
	 * @var array<string, array<string, mixed>>|null
43 49
	 */
@@ -61,10 +67,12 @@
Loading
61 67
	/**
62 68
	 * @param \Queue\Console\Io $io
63 69
	 * @param \Psr\Log\LoggerInterface $logger
70 +
	 * @param \Cake\Core\ContainerInterface|null $container
64 71
	 */
65 -
	public function __construct(Io $io, LoggerInterface $logger) {
72 +
	public function __construct(Io $io, LoggerInterface $logger, ?ContainerInterface $container = null) {
66 73
		$this->io = $io;
67 74
		$this->logger = $logger;
75 +
		$this->container = $container;
68 76
69 77
		$this->modelClass = 'Queue.QueuedJobs';
70 78
		$this->loadModel();
@@ -186,6 +194,11 @@
Loading
186 194
187 195
			$data = $queuedJob->data ? unserialize($queuedJob->data) : null;
188 196
			$task = $this->loadTask($taskName);
197 +
			$traits = class_uses($task);
198 +
			if ($this->container && $traits && in_array(ServicesTrait::class, $traits, true)) {
199 +
				/** @phpstan-ignore-next-line */
200 +
				$task->setContainer($this->container);
201 +
			}
189 202
			$task->run((array)$data, $queuedJob->id);
190 203
191 204
		} catch (Throwable $e) {

@@ -32,10 +32,10 @@
Loading
32 32
 * @mixin \Search\Model\Behavior\SearchBehavior
33 33
 * @property \Queue\Model\Table\QueueProcessesTable&\Cake\ORM\Association\BelongsTo $WorkerProcesses
34 34
 * @method \Queue\Model\Entity\QueuedJob newEmptyEntity()
35 -
 * @method \Queue\Model\Entity\QueuedJob[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = [])
36 -
 * @method \Queue\Model\Entity\QueuedJob[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
37 -
 * @method \Queue\Model\Entity\QueuedJob[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
38 -
 * @method \Queue\Model\Entity\QueuedJob[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
35 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob>|false saveMany(iterable $entities, $options = [])
36 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob> saveManyOrFail(iterable $entities, $options = [])
37 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob>|false deleteMany(iterable $entities, $options = [])
38 +
 * @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob> deleteManyOrFail(iterable $entities, $options = [])
39 39
 */
40 40
class QueuedJobsTable extends Table {
41 41
Files Complexity Coverage
src 665 57.58%
Project Totals (36 files) 665 57.58%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading