FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Workflow.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Workflow extends \Rsi\Fred\Component{
6 
7  protected $_lockItem = 'workflow';
8 
9  protected $_workflows = null;
10 
11  /**
12  * Get workflow data.
13  * @param string $id Workflow ID.
14  * @return array
15  */
16  protected function workflow($id){
17  if(!array_key_exists($id,$this->workflows)) throw new \Exception("Unknown workflow '$id'");
18  return $this->workflows[$id];
19  }
20  /**
21  * Create a new workflow item.
22  * @param string $workflow Workflow for this item.
23  * @param string $id Unique ID for this item.
24  * @param array $data Data for this item.
25  * @param string $status Initial status for this item.
26  * @return Rsi\\Ginger\\Workflow\\Item Item or false when not authorized.
27  */
28  public function createItem($workflow,$id,$data = null,$status = 'start'){
29  throw new \Exception('Not implemented');
30  }
31  /**
32  * Load an existing workflow item.
33  * @param string $id Item ID.
34  * @return Rsi\\Ginger\\Workflow\\Item Item or false when not found or not authorized.
35  */
36  public function loadItem($id){
37  return false;
38  }
39  /**
40  * Request a lock on a workflow item.
41  * @param string $id Item ID.
42  * @return bool True on success.
43  */
44  public function lockItem($id){
45  return $id && $this->component('lock')->request($this->_lockItem,$id);
46  }
47  /**
48  * Release a lock on a workflow item.
49  * @param string $id Item ID.
50  * @return bool True on success.
51  */
52  public function unlockItem($id){
53  return $id && $this->component('lock')->release($this->_lockItem,$id);
54  }
55  /**
56  * Get all data for an item.
57  * @param string $id Item ID.
58  * @return array
59  */
60  public function getItemData($id){
61  return [];
62  }
63  /**
64  * Set (add) data for an item.
65  * @param string $id Item ID.
66  * @param array $data
67  */
68  public function setItemData($id,$data){
69  }
70  /**
71  * Get current status for an item.
72  * @param string $id Item ID.
73  * @return string
74  */
75  public function getItemStatus($id){
76  return null;
77  }
78  /**
79  * Set the status for an item.
80  * @param string $id Item ID.
81  * @param string $status New status.
82  * @param string $remarks
83  * @return bool True when successful, false when not possible or not authorized.
84  */
85  public function setItemStatus($id,$status,$remarks = null){
86  return false;
87  }
88  /**
89  * Get all possible (authorized) change statuses from the current status.
90  * @param string $id Item ID.
91  * @return array Key = status ID, value = status data (array).
92  */
93  public function getItemStatuses($id){
94  return [];
95  }
96  /**
97  * Push an item into the queue (reschedule to execute as soon as possible).
98  * @param string $id Item ID.
99  * @param array $data Data to add to the item.
100  */
101  public function push($id,$data = null){
102  }
103  /**
104  * Pause an item (remove from queue).
105  * @param string $id Item ID.
106  */
107  protected function pause($id){
108  }
109  /**
110  * Shift the first item from the queue.
111  * @param bool $lock Lock the item (or skip if not lockable).
112  * @return string ID or empty when the queue is empty.
113  */
114  protected function shift($lock = false){
115  return null;
116  }
117  /**
118  * Process the queue.
119  * @param int $timeout Maximum execution time (seconds).
120  */
121  public function execute($timeout = null){
122  if($timeout) $timout = time() + $timeout;
123  $log = $this->component('log');
124  while((!$timeout || ($timeout < time())) && ($id = $this->shift(true))) try{
125  $log->debug("Executing workflow item '$id'",__FILE__,__LINE__);
126  if(($result = $this->loadItem($id)->execute()) !== false) $log->debug("Workflow item '$id' result = '$result'",__FILE__,__LINE__);
127  else{
128  $status = $this->getItemStatus($id);
129  $log->error("Timeout expired for workflow item '$id', but no action for status '$status'",__FILE__,__LINE__);
130  $this->pause($id);
131  }
132  }
133  catch(\Exception $e){
134  $log->emergency("Error while executing workflow item '$id': " . $e->getMessage(),$e->getFile(),$e->getLine());
135  $this->pause($id);
136  }
137  finally{
138  $this->unlockItem($id);
139  }
140  }
141 
142  protected function getWorkflows(){
143  return [];
144  }
145 
146 }
loadItem($id)
Load an existing workflow item.
Definition: Workflow.php:36
setItemStatus($id, $status, $remarks=null)
Set the status for an item.
Definition: Workflow.php:85
push($id, $data=null)
Push an item into the queue (reschedule to execute as soon as possible).
Definition: Workflow.php:101
createItem($workflow, $id, $data=null, $status='start')
Create a new workflow item.
Definition: Workflow.php:28
lockItem($id)
Request a lock on a workflow item.
Definition: Workflow.php:44
getItemStatus($id)
Get current status for an item.
Definition: Workflow.php:75
unlockItem($id)
Release a lock on a workflow item.
Definition: Workflow.php:52
execute($timeout=null)
Process the queue.
Definition: Workflow.php:121
workflow($id)
Get workflow data.
Definition: Workflow.php:16
pause($id)
Pause an item (remove from queue).
Definition: Workflow.php:107
getItemData($id)
Get all data for an item.
Definition: Workflow.php:60
getItemStatuses($id)
Get all possible (authorized) change statuses from the current status.
Definition: Workflow.php:93
component($name)
Get a component (local or default).
Definition: Component.php:80
setItemData($id, $data)
Set (add) data for an item.
Definition: Workflow.php:68
shift($lock=false)
Shift the first item from the queue.
Definition: Workflow.php:114