1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZWorkflowEngineBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException; |
10
|
|
|
|
11
|
|
|
class GenerateCommand extends AbstractCommand |
12
|
|
|
{ |
13
|
|
|
const DIR_CREATE_PERMISSIONS = 0755; |
14
|
|
|
|
15
|
|
|
private $availableWorkflowFormats = array('yml', 'json'); |
16
|
|
|
private $thisBundle = 'EzWorkflowEngineBundle'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Configure the console command |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this->setName('kaliop:workflows:generate') |
24
|
|
|
->setDescription('Generate a blank workflow definition file.') |
25
|
|
|
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The format of workflow file to generate (' . implode(', ', $this->availableWorkflowFormats) . ')', 'yml') |
26
|
|
|
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to generate the workflow definition file in. eg.: AcmeWorkflowsBundle') |
27
|
|
|
->addArgument('name', InputArgument::OPTIONAL, 'The workflow name (will be prefixed with current date)', null) |
28
|
|
|
->setHelp(<<<EOT |
29
|
|
|
The <info>kaliop:workflows:generate</info> command generates a skeleton workflows definition file: |
30
|
|
|
|
31
|
|
|
<info>php ezpublish/console kaliop:workflows:generate bundlename</info> |
32
|
|
|
EOT |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Run the command and display the results. |
38
|
|
|
* |
39
|
|
|
* @param InputInterface $input |
40
|
|
|
* @param OutputInterface $output |
41
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
42
|
|
|
* @throws \InvalidArgumentException When an unsupported file type is selected |
43
|
|
|
*/ |
44
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$bundleName = $input->getArgument('bundle'); |
47
|
|
|
$name = $input->getArgument('name'); |
48
|
|
|
$fileType = $input->getOption('format'); |
49
|
|
|
|
50
|
|
|
if ($bundleName == $this->thisBundle) { |
51
|
|
|
throw new \InvalidArgumentException("It is not allowed to create workflows in bundle '$bundleName'"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!in_array($fileType, $this->availableWorkflowFormats)) { |
55
|
|
|
throw new \InvalidArgumentException('Unsupported workflow file format ' . $fileType); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$workflowDirectory = $this->getWorkflowDirectory($bundleName); |
|
|
|
|
59
|
|
|
|
60
|
|
|
if (!is_dir($workflowDirectory)) { |
61
|
|
|
$output->writeln(sprintf( |
62
|
|
|
"Workflows directory <info>%s</info> does not exist. I will create it now....", |
63
|
|
|
$workflowDirectory |
64
|
|
|
)); |
65
|
|
|
|
66
|
|
|
if (mkdir($workflowDirectory, self::DIR_CREATE_PERMISSIONS, true)) { |
67
|
|
|
$output->writeln(sprintf( |
68
|
|
|
"Workflows directory <info>%s</info> has been created", |
69
|
|
|
$workflowDirectory |
70
|
|
|
)); |
71
|
|
|
} else { |
72
|
|
|
throw new FileException(sprintf( |
73
|
|
|
"Failed to create workflows directory %s.", |
74
|
|
|
$workflowDirectory |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$date = date('YmdHis'); |
80
|
|
|
|
81
|
|
|
if ($name == '') { |
82
|
|
|
$name = 'placeholder'; |
83
|
|
|
} |
84
|
|
|
$fileName = $date . '_' . $name . '.' . $fileType; |
|
|
|
|
85
|
|
|
|
86
|
|
|
$path = $workflowDirectory . '/' . $fileName; |
87
|
|
|
|
88
|
|
|
$warning = $this->generateWorkflowFile($path, $fileType); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$output->writeln(sprintf("Generated new workflow file: <info>%s</info>", $path)); |
91
|
|
|
|
92
|
|
|
if ($warning != '') { |
93
|
|
|
$output->writeln("<comment>$warning</comment>"); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Generates a workflow definition file. |
99
|
|
|
* |
100
|
|
|
* @param string $path filename to file to generate (full path) |
101
|
|
|
* @param string $fileType The type of workflow file to generate |
102
|
|
|
* @param array $parameters |
103
|
|
|
* @return string A warning message in case file generation was OK but there was something weird |
104
|
|
|
* @throws \Exception |
105
|
|
|
*/ |
106
|
|
|
protected function generateWorkflowFile($path, $fileType, $parameters = array()) |
107
|
|
|
{ |
108
|
|
|
$warning = ''; |
109
|
|
|
|
110
|
|
|
// Generate workflow file by template |
111
|
|
|
$template = 'Workflow.' . $fileType . '.twig'; |
112
|
|
|
$templatePath = $this->getApplication()->getKernel()->getBundle($this->thisBundle)->getPath() . '/Resources/views/WorkflowTemplate/'; |
|
|
|
|
113
|
|
|
if (!is_file($templatePath . $template)) { |
114
|
|
|
throw new \Exception("Generation of a workflow example is not supported with format '$fileType'"); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$code = $this->getContainer()->get('twig')->render($this->thisBundle . ':WorkflowTemplate:' . $template, $parameters); |
118
|
|
|
|
119
|
|
|
file_put_contents($path, $code); |
120
|
|
|
|
121
|
|
|
return $warning; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $bundleName a bundle name or filesystem path to a directory |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function getWorkflowDirectory($bundleName) |
129
|
|
|
{ |
130
|
|
|
// Allow direct usage of a directory path instead of a bundle name |
131
|
|
|
if (strpos($bundleName, '/') !== false && is_dir($bundleName)) { |
132
|
|
|
return rtrim($bundleName, '/'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$activeBundles = array(); |
136
|
|
|
foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { |
137
|
|
|
$activeBundles[] = $bundle->getName(); |
138
|
|
|
} |
139
|
|
|
asort($activeBundles); |
140
|
|
|
if (!in_array($bundleName, $activeBundles)) { |
141
|
|
|
throw new \InvalidArgumentException("Bundle '$bundleName' does not exist or it is not enabled. Try with one of:\n" . implode(', ', $activeBundles)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$bundle = $this->getApplication()->getKernel()->getBundle($bundleName); |
145
|
|
|
$workflowDirectory = $bundle->getPath() . '/' . $this->getContainer()->get('ez_migration_bundle.helper.config.resolver')->getParameter('ez_workflowengine_bundle.workflow_directory'); |
146
|
|
|
|
147
|
|
|
return $workflowDirectory; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|