-
Notifications
You must be signed in to change notification settings - Fork 452
/
tasks.php
118 lines (98 loc) · 3.82 KB
/
tasks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Concrete\Controller\Backend;
use Concrete\Core\Command\Scheduler\Scheduler;
use Concrete\Core\Command\Task\Input\InputFactory;
use Concrete\Core\Command\Task\Output\OutputFactory;
use Concrete\Core\Command\Task\Response\HttpResponseFactory;
use Concrete\Core\Command\Scheduler\Response\HttpResponseFactory as ScheduleHttpResponseFactory;
use Concrete\Core\Command\Task\Runner\Context\ContextFactory;
use Concrete\Core\Controller\AbstractController;
use Concrete\Core\Entity\Automation\Task;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Validation\CSRF\Token;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\JsonResponse;
class Tasks extends AbstractController
{
/**
* @var ErrorList
*/
protected $errorList;
/**
* @var Token
*/
protected $token;
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var HttpResponseFactory
*/
protected $httpResponseFactory;
/**
* @var ScheduleHttpResponseFactory
*/
protected $scheduleHttpResponseFactory;
public function __construct(
ErrorList $errorList,
Token $token,
EntityManager $entityManager,
HttpResponseFactory $httpResponseFactory,
ScheduleHttpResponseFactory $scheduleHttpResponseFactory
) {
parent::__construct();
$this->errorList = $errorList;
$this->token = $token;
$this->entityManager = $entityManager;
$this->httpResponseFactory = $httpResponseFactory;
$this->scheduleHttpResponseFactory = $scheduleHttpResponseFactory;
}
public function execute()
{
if (!$this->token->validate('execute', $this->request->request->get('ccm_token'))) {
$this->errorList->add($this->token->getErrorMessage());
}
$page = \Concrete\Core\Page\Page::getByPath('/dashboard/system/automation/tasks');
$checker = new Checker($page);
if (!$checker->canViewPage()) {
$this->errorList->add(t('You do not have permissions to run tasks.'));
}
$task = null;
if ($this->request->request->has('id')) {
/**
* @var $task Task
*/
$task = $this->entityManager->find(Task::class, $this->request->request->get('id'));
}
if (!$task) {
$this->errorList->add(t('Invalid task object.'));
}
if ($this->errorList->has()) {
return new JsonResponse($this->errorList);
} else {
/**
* @var $inputFactory InputFactory
* @var $contextFactory ContextFactory
*/
$inputFactory = $this->app->make(InputFactory::class);
$contextFactory = $this->app->make(ContextFactory::class);
$input = $inputFactory->createFromRequest($this->request, $task->getController()->getInputDefinition());
if ($this->request->request->get('scheduleTask')) {
$scheduler = $this->app->make(Scheduler::class);
$scheduledTask = $scheduler->createScheduledTask($task, $input, $this->request->request->get('cronExpression'));
return $this->scheduleHttpResponseFactory->createResponse($scheduledTask);
} else {
$runner = $task->getController()->getTaskRunner($task, $input);
$handler = $this->app->make($runner->getTaskRunnerHandler());
$handler->boot($runner);
$context = $contextFactory->createDashboardContext($runner);
$handler->start($runner, $context);
$handler->run($runner, $context);
$response = $handler->complete($runner, $context);
return $this->httpResponseFactory->createResponse($response);
}
}
}
}