forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkflowStateFactory.php
72 lines (55 loc) · 1.94 KB
/
WorkflowStateFactory.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
<?php
namespace Intern;
class WorkflowStateFactory {
private static $dir = 'WorkflowState';
public static function getState($className)
{
if(!isset($className) || empty($className)){
throw new \InvalidArgumentException('Missing state name.');
}
$className = '\Intern\WorkflowState\\' . $className;
return new $className;
}
public static function getAllStates()
{
$dir = PHPWS_SOURCE_DIR . 'mod/intern/class/' . self::$dir;
// Get the directory listing and filter out anything that doesn't look right
$files = scandir("{$dir}/");
$states = array();
foreach($files as $f){
// Look for directories that don't start with '.'
if(!is_dir($dir . '/' . $f) && substr($f, 0, 1) != '.'){
// Include each one
\PHPWS_Core::initModClass('intern', self::$dir . '/' . $f);
$className = preg_replace('/\.php/', '', $f);
$className = 'Intern\WorkflowState\\' . $className;
// Instantiate each one
$states[] = new $className;
}
}
return $states;
}
public static function getStatesAssoc()
{
$states = self::getAllStates();
// Sort the states into a reasonable order
uasort($states, array('self', 'sortStates'));
$assoc = array();
foreach($states as $s){
$assoc[$s->getName()] = $s->getFriendlyname();
}
return $assoc;
}
/**
* Call-back function for sorting states by their priority. Lower sort index => lower priority.
* @param WorkflowState $a
* @param WorkflowState $b
*/
private static function sortStates(WorkflowState $a, WorkflowState $b)
{
if($a->getSortIndex() == $b->getSortIndex()){
return 0;
}
return ($a->getSortIndex() < $b->getSortIndex()) ? -1 : 1;
}
}