Source of file WorkflowDefinitionImporter.php
Size: 3,571 Bytes - Last Modified: 2021-12-23T10:27:27+00:00
/var/www/docs.ssmods.com/process/src/src/Admin/WorkflowDefinitionImporter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 | <?php namespace Symbiote\AdvancedWorkflow\Admin; use Exception; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\ValidationException; use Symbiote\AdvancedWorkflow\DataObjects\ImportedWorkflowTemplate; use Symbiote\AdvancedWorkflow\Templates\WorkflowTemplate; use Symfony\Component\Yaml\Yaml; /** * Workflow definition import-specific logic. @see {@link WorkflowDefinitionExporter}. * * @author russell@silverstripe.com * @license BSD License (http://silverstripe.org/bsd-license/) * @package advancedworkflow */ class WorkflowDefinitionImporter { /** * Generates an array of WorkflowTemplate Objects of all uploaded workflows. * * @param string $name. If set, a single-value array comprising a WorkflowTemplate object who's first * constructor param matches $name is returned. * @return WorkflowTemplate|WorkflowTemplate[] */ public function getImportedWorkflows($name = null) { $imports = DataObject::get(ImportedWorkflowTemplate::class); $importedDefs = array(); foreach ($imports as $import) { if (!$import->Content) { continue; } $structure = unserialize($import->Content); $struct = $structure[Injector::class]['ExportedWorkflow']; $template = Injector::inst()->createWithArgs(WorkflowTemplate::class, $struct['constructor']); $template->setStructure($struct['properties']['structure']); if ($name) { if ($struct['constructor'][0] == trim($name)) { return $template; } continue; } $importedDefs[] = $template; } return $importedDefs; } /** * Handles finding and parsing YAML input as a string or from the contents of a file. * * @see addYAMLConfigFile() on {@link SS_ConfigManifest} from where this logic was taken and adapted. * @param string $source YAML as a string or a filename * @return array */ public function parseYAMLImport($source) { if (is_file($source)) { $source = file_get_contents($source); } // Make sure the linefeeds are all converted to \n, PCRE '$' will not match anything else. $convertLF = str_replace(array("\r\n", "\r"), "\n", $source); /* * Remove illegal colons from Transition/Action titles, otherwise sfYamlParser will barf on them * Note: The regex relies on there being single quotes wrapped around these in the export .ss template */ $converted = preg_replace("#('[^:\n][^']+)(:)([^']+')#", "$1;$3", $convertLF); $parts = preg_split('#^---$#m', $converted, -1, PREG_SPLIT_NO_EMPTY); // If we got an odd number of parts the config, file doesn't have a header. // We know in advance the number of blocks imported content will have so we settle for a count()==2 check. if (count($parts) != 2) { $msg = _t('WorkflowDefinitionImporter.INVALID_YML_FORMAT_NO_HEADER', 'Invalid YAML format.'); throw new ValidationException($msg); } try { $parsed = Yaml::parse($parts[1]); return $parsed; } catch (Exception $e) { $msg = _t( 'WorkflowDefinitionImporter.INVALID_YML_FORMAT_NO_PARSE', 'Invalid YAML format. Unable to parse.' ); throw new ValidationException($msg); } } } |