This repository was archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagent.php
105 lines (95 loc) · 2.57 KB
/
agent.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
<?php
/**
* @link https://github.com/notamedia/bitrix-agent-manager
* @copyright Copyright © 2015 — 2016 Notamedia Ltd.
* @license MIT
*/
namespace Notamedia\AgentManager;
/**
* Abstract class Agent for development simple workers.
*
* Algorithm of Agent execution:
* 1. Bitrix launches static method `Agent::agent()`. Your Agents should be registered in the same format:
* `\Vendor\Packeage\ClassName::agent();`. All arguments from this method will be duplicated to the object constructor:
* `agent($arg1, …, $arg2)` → `__construct($arg1, …, $arg2)`.
* 2. Create an object of Agent class.
* 3. Call `init()` method. It is needed for some initial operations, for example: loading required modules.
* 4. Call `execute()` method. This will execute main agent's logic.
*/
abstract class Agent
{
/**
* @var bool Agent is recurring.
*/
protected $recurring = true;
/**
* Agent constructor.
*
* All arguments from `agent()` method should be duplicated in the constructor, for example:
* ```
* agent($arg1, …, $arg2)` → `__construct($arg1, …, $arg2)
* ```
*/
public function __construct()
{
}
/**
* Running Agent by Bitrix.
*
* Bitrix calls this method to run Agent. Your Agents should be registered in the same format:
* `\Vendor\Packeage\ClassName::agent();`. All arguments from this method should be duplicated in the object
* constructor:
*
* `agent($arg1, …, $arg2)` → `__construct($arg1, …, $arg2)`.
*
* @return string
*/
public static function agent()
{
$reflection = new \ReflectionClass(get_called_class());
/**
* @var static $worker
*/
$worker = $reflection->newInstanceArgs(func_get_args());
$worker->run();
if ($worker->isRecurring())
{
return '\\' . get_called_class() . '::agent();';
}
}
/**
* Runs the Agent.
*
* Notice, that overriding agent's initialisation and body, should be done though `init` and `execute` methods, not
* here.
*
* @see Agent::init()
* @see Agent::execute()
*/
public function run()
{
$this->init();
$this->execute();
}
/**
* Initializes the Agent.
*/
protected function init()
{
}
/**
* Executes the Agent.
*/
protected function execute()
{
}
/**
* Checks if Agent is the a recurring.
*
* @return bool
*/
public function isRecurring()
{
return $this->recurring;
}
}