|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
7
|
|
|
use SilverStripe\ORM\DataObject; |
|
8
|
|
|
use SilverStripe\ORM\ValidationException; |
|
9
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\ImportedWorkflowTemplate; |
|
10
|
|
|
use Symbiote\AdvancedWorkflow\Templates\WorkflowTemplate; |
|
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Workflow definition import-specific logic. @see {@link WorkflowDefinitionExporter}. |
|
15
|
|
|
* |
|
16
|
|
|
* @author [email protected] |
|
17
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
|
18
|
|
|
* @package advancedworkflow |
|
19
|
|
|
*/ |
|
20
|
|
|
class WorkflowDefinitionImporter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Generates an array of WorkflowTemplate Objects of all uploaded workflows. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $name. If set, a single-value array comprising a WorkflowTemplate object who's first |
|
|
|
|
|
|
26
|
|
|
* constructor param matches $name is returned. |
|
27
|
|
|
* @return WorkflowTemplate|WorkflowTemplate[] |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getImportedWorkflows($name = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$imports = DataObject::get(ImportedWorkflowTemplate::class); |
|
32
|
|
|
$importedDefs = array(); |
|
33
|
|
|
foreach ($imports as $import) { |
|
34
|
|
|
if (!$import->Content) { |
|
35
|
|
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
$structure = unserialize($import->Content); |
|
38
|
|
|
$struct = $structure[Injector::class]['ExportedWorkflow']; |
|
39
|
|
|
$template = Injector::inst()->createWithArgs(WorkflowTemplate::class, $struct['constructor']); |
|
40
|
|
|
$template->setStructure($struct['properties']['structure']); |
|
41
|
|
|
if ($name) { |
|
42
|
|
|
if ($struct['constructor'][0] == trim($name)) { |
|
43
|
|
|
return $template; |
|
44
|
|
|
} |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
$importedDefs[] = $template; |
|
48
|
|
|
} |
|
49
|
|
|
return $importedDefs; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Handles finding and parsing YAML input as a string or from the contents of a file. |
|
54
|
|
|
* |
|
55
|
|
|
* @see addYAMLConfigFile() on {@link SS_ConfigManifest} from where this logic was taken and adapted. |
|
56
|
|
|
* @param string $source YAML as a string or a filename |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function parseYAMLImport($source) |
|
60
|
|
|
{ |
|
61
|
|
|
if (is_file($source)) { |
|
62
|
|
|
$source = file_get_contents($source); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Make sure the linefeeds are all converted to \n, PCRE '$' will not match anything else. |
|
66
|
|
|
$convertLF = str_replace(array("\r\n", "\r"), "\n", $source); |
|
67
|
|
|
/* |
|
68
|
|
|
* Remove illegal colons from Transition/Action titles, otherwise sfYamlParser will barf on them |
|
69
|
|
|
* Note: The regex relies on there being single quotes wrapped around these in the export .ss template |
|
70
|
|
|
*/ |
|
71
|
|
|
$converted = preg_replace("#('[^:\n][^']+)(:)([^']+')#", "$1;$3", $convertLF); |
|
72
|
|
|
$parts = preg_split('#^---$#m', $converted, -1, PREG_SPLIT_NO_EMPTY); |
|
73
|
|
|
|
|
74
|
|
|
// If we got an odd number of parts the config, file doesn't have a header. |
|
75
|
|
|
// We know in advance the number of blocks imported content will have so we settle for a count()==2 check. |
|
76
|
|
|
if (count($parts) != 2) { |
|
77
|
|
|
$msg = _t('WorkflowDefinitionImporter.INVALID_YML_FORMAT_NO_HEADER', 'Invalid YAML format.'); |
|
78
|
|
|
throw new ValidationException($msg); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
|
|
$parsed = Yaml::parse($parts[1]); |
|
83
|
|
|
return $parsed; |
|
84
|
|
|
} catch (Exception $e) { |
|
85
|
|
|
$msg = _t( |
|
86
|
|
|
'WorkflowDefinitionImporter.INVALID_YML_FORMAT_NO_PARSE', |
|
87
|
|
|
'Invalid YAML format. Unable to parse.' |
|
88
|
|
|
); |
|
89
|
|
|
throw new ValidationException($msg); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.